Reputation: 1531
I am coding the simple getting started (http://slick.typesafe.com/doc/3.1.1/gettingstarted.html) examples from Slick 3.1.1 documentation.
I wrote the following test to assert on the count of coffees:
@Test def countCoffees() = {
// Read all coffees and print them to the console
val rf = db.run(coffees.result)
// Equivalent SQL code:
// select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES
val r = Await.result(rf, Duration.Inf)
assert(r.size == 5)
}
Some times the test pass and other times the result is the following:
[info] - countCoffees *** FAILED ***
[info] org.scalatest.junit.JUnitTestFailedError: Vector() had size 0 instead of expected size 5 (Test.scala:40)
The TestSuite is defined as follows:
class SlickScalaEjemplo extends FunSuite with SchemaEjemplo
Being SchemaEjemplo
as follows
trait SchemaEjemplo extends FunSuite with SlickBase with BeforeAndAfter {
val setup = DBIO.seq(
// Create the tables, including primary and foreign keys
(suppliers.schema ++ coffees.schema).create,
// Insert some suppliers
suppliers += (101, "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199"),
suppliers += (49, "Superior Coffee", "1 Party Place", "Mendocino", "CA", "95460"),
suppliers += (150, "The High Ground", "100 Coffee Lane", "Meadows", "CA", "93966"),
// Equivalent SQL code:
// insert into SUPPLIERS(SUP_ID, SUP_NAME, STREET, CITY, STATE, ZIP) values (?,?,?,?,?,?)
// Insert some coffees (using JDBC's batch insert feature, if supported by the DB)
coffees ++= Seq(
("Colombian", 101, 7.99, 0, 0),
("French_Roast", 49, 8.99, 0, 0),
("Espresso", 150, 9.99, 0, 0),
("Colombian_Decaf", 101, 8.99, 0, 0),
("French_Roast_Decaf", 49, 9.99, 0, 0)
)
// Equivalent SQL code:
// insert into COFFEES(COF_NAME, SUP_ID, PRICE, SALES, TOTAL) values (?,?,?,?,?)
)
val setupFuture = db.run(setup)
after {
db.close()
}
}
Why does the Await on this test don't work properly?
Upvotes: 1
Views: 122
Reputation: 1531
The problem was that I was not waiting for the following
val setupFuture = db.run(setup)
to execute so the tests were running without expecting the schema to be created or completed.
I changed the tests as follows:
test("countCoffees") {
setupFuture.map(x => {
// Read all coffees and print them to the console
val rf = db.run(coffees.result)
// Equivalent SQL code:
// select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES
val r = Await.result(rf, Duration.Inf)
assert(r.size == 5)
})
}
So the execution of the different Action
with db.run
are executed on a complete schema and now the tests are all green.
Upvotes: 1