From 257a029490ccaec466d6c8cc2569954ffd0082ff Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Tue, 27 Feb 2024 15:41:07 +0100 Subject: [PATCH] Move common code to commons module --- .../commons/generators/CommonGenerators.scala | 33 +++++++++++++++++++ .../io/renku/search/query/EntityType.scala | 1 - .../scala/io/renku/search/query/Order.scala | 1 + .../search/query/parse/QueryParser.scala | 2 +- .../renku/search/query/QueryGenerators.scala | 23 +++++-------- .../solr/client/SearchSolrClientSpec.scala | 2 +- 6 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 modules/commons/src/test/scala/io/renku/commons/generators/CommonGenerators.scala diff --git a/modules/commons/src/test/scala/io/renku/commons/generators/CommonGenerators.scala b/modules/commons/src/test/scala/io/renku/commons/generators/CommonGenerators.scala new file mode 100644 index 00000000..8eafb24a --- /dev/null +++ b/modules/commons/src/test/scala/io/renku/commons/generators/CommonGenerators.scala @@ -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) diff --git a/modules/search-query/src/main/scala/io/renku/search/query/EntityType.scala b/modules/search-query/src/main/scala/io/renku/search/query/EntityType.scala index c68d874d..924f8219 100644 --- a/modules/search-query/src/main/scala/io/renku/search/query/EntityType.scala +++ b/modules/search-query/src/main/scala/io/renku/search/query/EntityType.scala @@ -15,4 +15,3 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - diff --git a/modules/search-query/src/main/scala/io/renku/search/query/Order.scala b/modules/search-query/src/main/scala/io/renku/search/query/Order.scala index de80624e..18aa8418 100644 --- a/modules/search-query/src/main/scala/io/renku/search/query/Order.scala +++ b/modules/search-query/src/main/scala/io/renku/search/query/Order.scala @@ -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) diff --git a/modules/search-query/src/main/scala/io/renku/search/query/parse/QueryParser.scala b/modules/search-query/src/main/scala/io/renku/search/query/parse/QueryParser.scala index 8b320a1b..0fbe5438 100644 --- a/modules/search-query/src/main/scala/io/renku/search/query/parse/QueryParser.scala +++ b/modules/search-query/src/main/scala/io/renku/search/query/parse/QueryParser.scala @@ -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) diff --git a/modules/search-query/src/test/scala/io/renku/search/query/QueryGenerators.scala b/modules/search-query/src/test/scala/io/renku/search/query/QueryGenerators.scala index 4bb09b2f..93077aa7 100644 --- a/modules/search-query/src/test/scala/io/renku/search/query/QueryGenerators.scala +++ b/modules/search-query/src/test/scala/io/renku/search/query/QueryGenerators.scala @@ -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 @@ -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) @@ -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(_)) @@ -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] = @@ -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] = @@ -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] = diff --git a/modules/search-solr-client/src/test/scala/io/renku/search/solr/client/SearchSolrClientSpec.scala b/modules/search-solr-client/src/test/scala/io/renku/search/solr/client/SearchSolrClientSpec.scala index 30407bab..62f691fb 100644 --- a/modules/search-solr-client/src/test/scala/io/renku/search/solr/client/SearchSolrClientSpec.scala +++ b/modules/search-solr-client/src/test/scala/io/renku/search/solr/client/SearchSolrClientSpec.scala @@ -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 ()