Skip to content

Commit

Permalink
Move common code to commons module
Browse files Browse the repository at this point in the history
  • Loading branch information
eikek committed Feb 27, 2024
1 parent fa1bc5a commit 2fcd760
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 Swiss Data Science Center (SDSC)
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
* Eidgenössische Technische Hochschule Zürich (ETHZ).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.renku.commons.generators

import cats.data.NonEmptyList
import org.scalacheck.Gen
import io.renku.search.model.projects.Visibility

object CommonGenerators:
val visibility: Gen[Visibility] =
Gen.oneOf(Visibility.values.toSeq)

def nelOfN[A](n: Int, gen: Gen[A]): Gen[NonEmptyList[A]] =
for {
e0 <- gen
en <- Gen.listOfN(n - 1, gen)
} yield NonEmptyList(e0, en)
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ object Order:
given Encoder[OrderedBy] = Encoder.forString.contramap(_.render)
given Decoder[OrderedBy] =
Decoder.forString.mapEither(s => QueryParser.orderedBy.parseAll(s).leftMap(_.show))
given cats.Order[OrderedBy] = cats.Order.by(_.render)

def fromString(s: String): Either[String, Order] =
QueryParser.sortTerm.parseAll(s).leftMap(_.show)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private[query] object QueryParser {
}

val orderedByNel: P[NonEmptyList[Order.OrderedBy]] =
nelOf(orderedBy, commaSep)
nelOf(orderedBy, commaSep).map(_.distinct)

val comparison: P[Comparison] =
P.stringIn(Comparison.values.map(_.asString)).map(Comparison.unsafeFromString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package io.renku.search.query

import cats.data.NonEmptyList
import cats.syntax.all.*
import io.renku.commons.generators.CommonGenerators
import io.renku.search.model.projects.Visibility
import io.renku.search.query.parse.QueryUtil
import org.scalacheck.Gen
Expand Down Expand Up @@ -89,17 +90,6 @@ object QueryGenerators:
dir <- sortDirection
} yield Order.OrderedBy(field, dir)

// TODO move to commons
val visibility: Gen[Visibility] =
Gen.oneOf(Visibility.values.toSeq)

// TODO move to commons
def nelOfN[A](n: Int, gen: Gen[A]): Gen[NonEmptyList[A]] =
for {
e0 <- gen
en <- Gen.listOfN(n - 1, gen)
} yield NonEmptyList(e0, en)

private val alphaNumChars = ('a' to 'z') ++ ('A' to 'Z') ++ ('0' to '9')
private val simpleWord: Gen[String] = {
val len = Gen.choose(2, 12)
Expand All @@ -121,7 +111,7 @@ object QueryGenerators:
}

private val stringValues: Gen[NonEmptyList[String]] =
Gen.choose(1, 4).flatMap(n => nelOfN(n, phrase))
Gen.choose(1, 4).flatMap(n => CommonGenerators.nelOfN(n, phrase))

val projectIdTerm: Gen[FieldTerm] =
stringValues.map(FieldTerm.ProjectIdIs(_))
Expand All @@ -137,7 +127,10 @@ object QueryGenerators:

val visibilityTerm: Gen[FieldTerm] =
Gen
.frequency(10 -> visibility.map(NonEmptyList.one), 1 -> nelOfN(2, visibility))
.frequency(
10 -> CommonGenerators.visibility.map(NonEmptyList.one),
1 -> CommonGenerators.nelOfN(2, CommonGenerators.visibility)
)
.map(vs => FieldTerm.VisibilityIs(vs.distinct))

private val comparison: Gen[Comparison] =
Expand All @@ -147,7 +140,7 @@ object QueryGenerators:
for {
cmp <- comparison
len <- Gen.frequency(5 -> Gen.const(1), 1 -> Gen.choose(1, 3))
pd <- nelOfN(len, dateTimeRef)
pd <- CommonGenerators.nelOfN(len, dateTimeRef)
} yield FieldTerm.Created(cmp, pd)

val fieldTerm: Gen[FieldTerm] =
Expand All @@ -167,7 +160,7 @@ object QueryGenerators:

val sortTerm: Gen[Order] =
Gen.choose(1, 5).flatMap { len =>
nelOfN(len, orderedBy).map(Order.apply)
CommonGenerators.nelOfN(len, orderedBy).map(_.distinct).map(Order.apply)
}

val segment: Gen[Query.Segment] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SearchSolrClientSpec extends CatsEffectSuite with SearchSolrSpec:
projectDocumentGen("solr-project", "solr project description").generateOne
for {
_ <- client.insertProjects(Seq(project))
r <- client.queryProjects(Query.parse("solr").toOption.get, 10,0)
r <- client.queryProjects(Query.parse("solr").toOption.get, 10, 0)
_ <- IO.println(r.responseBody.docs)
_ = assert(r.responseBody.docs.map(_.copy(score = None)) contains project)
} yield ()
Expand Down

0 comments on commit 2fcd760

Please sign in to comment.