aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/common.scala
blob: 7318ff60cb0e5f917a0d7e01523c4e9495029f48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package xyz.driver.pdsuidomain.fakes.entities

import java.time.{LocalDate, LocalDateTime, LocalTime}

import xyz.driver.core.generators.{nextDouble, nextOption}
import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId, StringId, UuidId}
import xyz.driver.pdsuidomain.entities.{Trial, TrialHistory}

import scala.util.Random

object common {
  import xyz.driver.core.generators

  def nextUuidId[T]: UuidId[T] = UuidId[T](generators.nextUuid())

  def nextLongId[T]: LongId[T] = LongId[T](generators.nextInt(Int.MaxValue).toLong)

  def nextStringId[T]: StringId[T] = StringId[T](generators.nextString(maxLength = 20))

  def nextTrialStatus: Trial.Status = generators.oneOf[Trial.Status](Trial.Status.All)

  def nextPreviousTrialStatus: Trial.Status = generators.oneOf[Trial.Status](Trial.Status.AllPrevious)

  def nextLocalDateTime: LocalDateTime = LocalDateTime.of(nextLocalDate, LocalTime.MIDNIGHT)

  def nextLocalDate: LocalDate = LocalDate.of(
    1970 + Random.nextInt(68),
    1 + Random.nextInt(12),
    1 + Random.nextInt(28) // all months have at least 28 days
  )

  def nextCondition = generators.oneOf[Trial.Condition](Trial.Condition.All)

  def nextTrialAction = generators.oneOf[TrialHistory.Action](TrialHistory.Action.All)

  def nextTrialState = generators.oneOf[TrialHistory.State](TrialHistory.State.All)

  def genBoundedRange[T](from: T, to: T)(implicit ord: Ordering[T]): (T, T) = {
    if (ord.compare(from, to) > 0) {
      to -> from
    } else {
      from -> to
    }
  }

  def genBoundedRangeOption[T](from: T, to: T)(implicit ord: Ordering[T]): (Option[T], Option[T]) = {
    val ranges = nextOption(from).map { left =>
      val range = genBoundedRange(left, to)
      range._1 -> nextOption(range._2)
    }

    ranges.map(_._1) -> ranges.flatMap(_._2)
  }

  def nextFuzzyValue(): FuzzyValue =
    generators.oneOf[FuzzyValue](FuzzyValue.All)

  def nextStartAndEndPages: (Option[Double], Option[Double]) =
    genBoundedRangeOption[Double](nextDouble(), nextDouble())

}