Skip to content

Commit

Permalink
RandomService method renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mboisnard authored and serpro69 committed Mar 9, 2024
1 parent b672f7c commit 6219997
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 38 deletions.
24 changes: 7 additions & 17 deletions core/src/main/kotlin/io/github/serpro69/kfaker/IRandom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,19 @@ interface IRandom {
* and the specified [bound] value (exclusive),
* drawn from this [random] number generator's sequence.
*
* @param bound the exclusive maximum value
* @throws IllegalArgumentException if `bound < 0`
*/
fun nextLong(bound: Long): Long

/**
* Returns a pseudorandom, uniformly distributed [Long] value within the specified long [range] (inclusive),
* Returns a pseudorandom, uniformly distributed [Long] value within the specified [longRange] (inclusive),
* drawn from this [random] number generator's sequence.
*
* @param longRange the range between minimum and maximum value
*/
fun nextLong(longRange: LongRange): Long

/**
* Returns a pseudorandom, uniformly distributed [Long] value between [min] (inclusive) and [max] (inclusive),
* drawn from this [random] number generator's sequence.
*
* @param min the minimum (inclusive) value
* @param max the maximum (exclusive) value
*/
fun nextLong(min: Long, max: Long): Long

Expand Down Expand Up @@ -266,23 +260,19 @@ interface IRandom {

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between Unix epoch (1970-01-01T00:00:00Z) (inclusive) and now (exclusive)
* between Unix epoch (1970-01-01T00:00:00Z) (inclusive) and now (exclusive) using UTC zone offset
*/
fun nextPastDate(): OffsetDateTime
fun randomPastDate(): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between now (inclusive) and now + 50 years (exclusive)
* between now (exclusive) and now + 50 years (inclusive) using UTC zone offset
*/
fun nextFutureDate(): OffsetDateTime
fun randomFutureDate(): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between [Instant] (inclusive) and [Instant] (exclusive)
*
* @param min minimum inclusive instant
* @param max maximum exclusive instant
* @param zoneOffset used ZoneOffset for generated date
* between [min] (inclusive) and [max] (inclusive) using the defined [zoneOffset]
*/
fun nextDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime
fun randomDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime
}
11 changes: 5 additions & 6 deletions core/src/main/kotlin/io/github/serpro69/kfaker/RandomService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.random.asKotlinRandom


/**
* Wrapper around [Random] that also contains some additional functions not covered by [Random].
*
Expand Down Expand Up @@ -226,17 +225,17 @@ class RandomService internal constructor(override val config: FakerConfig) : IRa
return randomSubset(set, nextInt(sizeRange), shuffled)
}

override fun nextPastDate(): OffsetDateTime {
return nextDate(Instant.ofEpochSecond(0), Instant.now(), ZoneOffset.UTC)
override fun randomPastDate(): OffsetDateTime {
return randomDate(Instant.ofEpochSecond(0), Instant.now().minusMillis(1), ZoneOffset.UTC)
}

override fun nextFutureDate(): OffsetDateTime {
override fun randomFutureDate(): OffsetDateTime {
val now = Instant.now()
val maxInstant = now.plus(Duration.ofDays(50 * 365))
return nextDate(now, maxInstant, ZoneOffset.UTC)
return randomDate(now.plusMillis(1), maxInstant, ZoneOffset.UTC)
}

override fun nextDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime {
override fun randomDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime {
val randomSeconds = nextLong(min.epochSecond, max.epochSecond)
val randomInstant = Instant.ofEpochSecond(randomSeconds)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ class RandomProvider internal constructor(
return resolveUnique(RANDOM_SUBSET) { rs.randomSubset(set = set, sizeRange = sizeRange, shuffled = shuffled) }
}

override fun nextPastDate(): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.nextPastDate() }
override fun randomPastDate(): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomPastDate() }

override fun nextFutureDate(): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.nextFutureDate() }
override fun randomFutureDate(): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomFutureDate() }

override fun nextDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime =
resolveUnique(RANDOM_SUBSET) { rs.nextDate(min, max, zoneOffset) }
override fun randomDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime =
resolveUnique(RANDOM_SUBSET) { rs.randomDate(min, max, zoneOffset) }

@PublishedApi
internal fun <T> resolveUnique(key: Key, f: () -> T): T = resolveUniqueValue(key.name, f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.types.beInstanceOf
import java.time.Duration
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset
Expand Down Expand Up @@ -325,16 +326,19 @@ internal class RandomServiceTest : DescribeSpec({
}
}

context("nexDate(min, max, zoneOffset) fun") {
val min = Instant.ofEpochSecond(0)
context("randomDate(min, max, zoneOffset) fun") {
val max = Instant.parse("2019-02-15T12:59:00Z") // Date of the first commit of kotlin-faker
val min = max.minus(Duration.ofDays(31))
val zoneOffset = ZoneOffset.UTC
val values = List(100) { randomService.nextDate(min, max, zoneOffset) }
val values = List(100) { randomService.randomDate(min, max, zoneOffset) }

it("return value should be within specified range") {
val minOffsetDateTime = OffsetDateTime.ofInstant(min, zoneOffset)
val maxOffsetDateTime = OffsetDateTime.ofInstant(max, zoneOffset)
values.all { (it.isEqual(minOffsetDateTime) || it.isAfter(minOffsetDateTime)) && it.isBefore(maxOffsetDateTime) } shouldBe true
values.all {
it.isEqual(minOffsetDateTime) || it.isEqual(maxOffsetDateTime) ||
(it.isAfter(minOffsetDateTime) && it.isBefore(maxOffsetDateTime))
} shouldBe true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ Provides data-generator-like functionality for the functions of `RandomService`,
====
[source,kotlin]
----
import java.time.Instant
import java.time.ZoneOffset
Faker().random.nextInt()
Faker().random.nextInt(bound = 42)
Expand Down Expand Up @@ -45,11 +42,11 @@ Faker().random.nextUUID()
Faker().random.randomSublist(listOf(1, 2, 3, 4, 5, 6))
Faker().random.nextPastDate()
Faker().random.randomPastDate()
Faker().random.nextFutureDate()
Faker().random.randomFutureDate()
Faker().random.nextDate(min = Instant.ofEpochSecond(0), max = Instant.now(), zoneOffset = ZoneOffset.UTC)
Faker().random.randomDate(min = Instant.ofEpochSecond(0), max = Instant.now(), zoneOffset = ZoneOffset.UTC)
// and more
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Database internal constructor(
fun columnName() = resolve("column_name")

fun mongodbObjectId() = resolveUniqueValue("objectId") {
val date = randomService.nextPastDate()
val date = randomService.randomPastDate()
val epochSeconds = date.toInstant().toEpochMilli() / 1000
val epochSecondsInHexa = epochSeconds.toString(16)

Expand Down

0 comments on commit 6219997

Please sign in to comment.