Consider the following test:
class BookTest extends FlatSpec with Matchers with ScalaFutures with BeforeAndAfter
{
implicit val defaultPatience =
PatienceConfig(timeout = Span(5, Seconds), interval = Span(500, Millis))
val db = Database.forURL("jdbc:sqlite:/home/myhome/test.sqlite", driver = "org.sqlite.JDBC")
private val books = Books.all
val setup = DBIO.seq(
// Create the tables, including primary and foreign keys
(books.schema).create,
// Insert some dummy data
books += new Book(-1, 0, "Awesome Title #1"),
books += new Book(-1, 1, "Gorgeous successor"),
books += new Book(-1, 2, "Nasty prequel")
)
db.run(setup)
"Books" should "be a Sequences of Books" in
{
val bookList = db.run(books.result)
whenReady(bookList)
{
result => result shouldBe a [Seq[Int]]
}
}
"Books" should "contain the books we inserted" in
{
val bookList: Future[Seq[Book]] = db.run(books.result)
whenReady(bookList)
{
result =>
{
println("TYPE OF RESULT:" result)
result.length === 1334
// result(0).title === "Awesome Title #1"
}
}
}
I explicitly stated the type of bookList
, which is a Sequence of Books, wrapped in a future.
Now I would expect result shouldBe a [Seq[Int]]
to fail, because Seq[Int] != Seq[Book]
Also the second test returns true, but why? The length of the result should be 3... it always passes.
Where am I thinking wrong? Doesn't whenReady(bookList)
combined with the defaultPatience and the Future do what I expect it to do?
edit: Just to make this clear, the database is created and it's filled, so that works (verified with sqlite3
;))
edit: How I solved both problems:
1)
"Books" should "be a Sequences of Books" in
{
val bookList: Future[Seq[Book]] = db.run(books.result)
whenReady(bookList)
{
result => {
result shouldBe a [Seq[_]] // Remember type erasure
result(0) shouldBe a [Book]
}
}
}
2) (which was really dumb, but ya..)
"Books" should "contain the books we inserted" in
{
val bookList = db.run(books.result)
whenReady(bookList)
{
result =>
{
result.length shouldBe 3
result(0).title shouldBe "Awesome Title #1"
}
}
}