Issue with generated data with unique key constraint

5 views
Skip to first unread message

Leonor Boga

unread,
Feb 18, 2018, 11:53:02 AM2/18/18
to scalacheck
Hello,

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.

Are you able to explain me why this issue is happening?

Thank you in advance!

Code sample:

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()
}

Reply all
Reply to author
Forward
0 new messages