Hey when generating an arbitrary domain model inst...
# kotest
a
Hey when generating an arbitrary domain model instance how can one lock some properties? Suppose I have a Student data class with an id, number, name and some other fields. If I have the following generator
Copy code
fun Arb.Companion.student(): Arb<Student> = arbitrary { rs ->
    Student(
        id = Arb.uuid(V4,false).next(rs)
        name = Arb.string().next(rs),
        number = <http://Arb.int|Arb.int>().next(rs), ...
    )
}
What can I add here to lock the number I which my generated student have?
Copy code
Arb.student().<here>?
// Something like
Arb.student().with(number, 123)
Anyway I can do this?
e
Arb.student().copy(number = 123)
perhaps?
a
I need to call next first, but that way I’m generating a random of everything and then locking it
I’ve seen this in other frameworks wasn’t sure if for kotlin thats the way to go
p
I’ve been taking the approach similar to this:
Copy code
fun Arb.Companion.student(
    idArb: Arb<UUID> = uuid(V4, false),
    nameArb: Arb<String> = string(),
    numberArb: Arb<Int> = int()
): Arb<Student> = arbitrary {
    Student(
        id = idArb.bind(),
        name = nameArb.bind(),
        number = numberArb.bind(),
    )
}

val myStudent = Arb.student(numberArb = Arb.constant(123)).single()
which also allows for more control over fields than just locking in values:
Copy code
val namedStudent = Arb.student(nameArb = Arb.name().map { (first, last) -> "$last, $first" }).single()
a
ya seems nice!
p
Also, personal preference maybe, but I’ve been adding these arbitraries onto my domain model companions rather than onto
Arb
, i.e.
Copy code
fun Student.Companion.arbitrary(...) = ...
allowing for
Copy code
Student.arbitrary()
rather than
Copy code
Arb.student()
It’s just kept
Arb
a little cleaner as base arbitraries
👍 1
👍🏻 1
a
thx for the suggestions 😄
e
In case the type is unique, you could also use
Arb.data<Student>(mapOf(Int::class to Arb.constant(123))
to generate the entire thing.. 🙂
Arb.data<Student>()
will create your first exmaple there, though I’m unsure if uuid binding will work without adding it explicitly.. Phil’s approach gives nice flexibility from use sites though, gonna try that out myself 🙂
😄 1
a
Ya I’ve seen that with bind too
l
I think you could also map an arb
Copy code
Arb.student().map { it.copy(number = 123) }