I'm trying out ScalaCheck for the first time.
I testing code that filters data from a DB, after adding it.
My issue is that sometimes I get the error "ERROR: duplicate key value violates unique constraint "dish_pkey_"" when running the test.
The dish uuid is a UUID randomly generated, so I get the idea that scalaCheck re-uses test data.
case class Dish (
uuid: UUID = UUID.randomUUID(),
name: String,
description: String
)
implicit val arbitraryDish = Arbitrary[Dish] {
for {
name <- arbitrary[String]
description <- arbitrary[String]
isVegetarian <- arbitrary[Boolean]
} yield Dish(
name = name,
description = description)
}
property("add a new dish") = forAll { (dish:Dish) =>
val result = Await.result(DishTable.add(dish), defaultTimeout)
result == dish
}
property("query existing dishes successfully") = forAll { (dish:Dish) =>
Await.result(DishTable.add(dish), defaultTimeout)
Await.result(DishTable.exists(dish.uuid), defaultTimeout)
}
// I extend from BeforeAndAfterEach and clean the DB between tests
override def beforeEach {
cleanDatabase()
}