Skip to content

Commit

Permalink
feat: our own scalacheck implicit removed
Browse files Browse the repository at this point in the history
  • Loading branch information
jachro committed Feb 27, 2024
1 parent c2b2086 commit c757dc9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@
* limitations under the License.
*/

package io.renku.scalacheck
package io.renku.search.model

import cats.{Functor, Semigroupal}
import io.renku.search.model.projects.*
import org.scalacheck.Gen

object all extends all
import java.time.Instant
import java.time.temporal.ChronoUnit

trait all:
object ModelGenerators {

extension [V](gen: Gen[V])
def generateOne: V = gen.sample.getOrElse(generateOne)
def generateAs[D](f: V => D): D = f(generateOne)
val visibilityGen: Gen[Visibility] = Gen.oneOf(Visibility.values.toList)
val creationDateGen: Gen[CreationDate] = instantGen().map(CreationDate.apply)

given Functor[Gen] = new Functor[Gen]:
override def map[A, B](fa: Gen[A])(f: A => B): Gen[B] = fa.map(f)
private def instantGen(
min: Instant = Instant.EPOCH,
max: Instant = Instant.now()
): Gen[Instant] =
Gen
.chooseNum(min.toEpochMilli, max.toEpochMilli)
.map(Instant.ofEpochMilli(_).truncatedTo(ChronoUnit.MILLIS))

given Semigroupal[Gen] = new Semigroupal[Gen] {
override def product[A, B](fa: Gen[A], fb: Gen[B]): Gen[(A, B)] =
fa.flatMap(a => fb.map(a -> _))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,38 @@
package io.renku.search.solr.client

import cats.syntax.all.*
import io.renku.scalacheck
import io.renku.search.model.*
import io.renku.search.model.ModelGenerators.*
import io.renku.search.solr.documents.*
import org.scalacheck.Gen
import org.scalacheck.cats.implicits.*

import java.time.Instant
import java.time.temporal.ChronoUnit

object SearchSolrClientGenerators extends scalacheck.all:
object SearchSolrClientGenerators:

private def projectIdGen: Gen[projects.Id] =
Gen.uuid.map(uuid => projects.Id(uuid.toString))

def projectDocumentGen(name: String, desc: String): Gen[Project] =
(projectIdGen, userIdGen).mapN((projectId, creatorId) =>
Project(
projectId,
projects.Name(name),
projects.Slug(name),
Seq(projects.Repository(s"http://github.com/$name")),
Gen.oneOf(projects.Visibility.values.toList).generateOne,
Option(projects.Description(desc)),
creatorId,
instantGen().generateAs(projects.CreationDate.apply),
Seq(creatorId)
(projectIdGen, userIdGen, visibilityGen, creationDateGen)
.mapN((projectId, creatorId, visibility, creationDate) =>
Project(
projectId,
projects.Name(name),
projects.Slug(name),
Seq(projects.Repository(s"http://github.com/$name")),
visibility,
Option(projects.Description(desc)),
creatorId,
creationDate,
Seq(creatorId)
)
)
)

def userDocumentGen: Gen[User] =
userIdGen.map(id => User(id))

private def userIdGen: Gen[users.Id] = Gen.uuid.map(uuid => users.Id(uuid.toString))

private def instantGen(
min: Instant = Instant.EPOCH,
max: Instant = Instant.now()
): Gen[Instant] =
Gen
.chooseNum(min.toEpochMilli, max.toEpochMilli)
.map(Instant.ofEpochMilli(_).truncatedTo(ChronoUnit.MILLIS))
extension [V](gen: Gen[V])
def generateOne: V = gen.sample.getOrElse(generateOne)
def generateAs[D](f: V => D): D = f(generateOne)

0 comments on commit c757dc9

Please sign in to comment.