We will start by writing some data generators using the ScalaCheck library.
ScalaCheck already provides several generators for primitives, but for the data models, we have to do some more plumbing.
ScalaCheck generate LanguageCode
Let’s start by generating a language code.
1
val genLanguageCode: Gen[LanguageCode] = Gen.oneOf(LanguageCodes.all)
Generate LanguageCode
Using the Gen.oneOf helper from the library makes the code extremely simple.
ScalaCheck generate UUID
Generating a UUID is nothing special, either.
12
val genUuid: Gen[UUID] = Gen.delay(UUID.randomUUID)
)
Generate UUID
We might be tempted to use Gen.const here, but we don’t because it will then become immutable.
Another option is using a list of randomly generated UUID values from which we then chose one. That would be sufficient for generators that only generate a single product, but if we want to generate lists of products, we would have duplicate IDs sooner or later.