0

I'm a trying to do a little integration-test in tdd for an idea I've been tinkering with.

I'm not new to TDD, but not an expert either. And I've started with it in ScalaTest just yesterday, so here I am.

This is what works:

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/teolha/test.sqlite", driver = "org.sqlite.JDBC")

  private val books = Books.all

  val setup = DBIO.seq(
    (books.schema).create,

    // Insert some dummy data
    books += new Book(-1, 0, "Awesome Title #1"),
    books += new Book(-1, 1, "Gorgeous Sequel"),
    books += new Book(-1, 2, "Mehmeh Prequel")
  )

  db.run(setup)

  "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]
      }

    }
  }

  "Books" should "contain the books we inserted" in
  {
    val bookList = db.run(books.result)
    whenReady(bookList)
    {
      result =>
      {
        result should have length 3
        (result(0).title shouldEqual "Awesome Title #1") (after being lowerCased)
        (result(1).title shouldEqual "Gorgeous Sequel") (after being lowerCased)
        (result(2).title shouldEqual "Mehmeh Prequel") (after being lowerCased)
      }
    }
  }
}

Now I have multiple problems with this setup:

  1. I would prefer an in-memory-database
  2. I would like to drop all the tables after each test and set them up again before the next one and do the inserts in the specific test

So I tried something like this:

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/teolha/test.sqlite", driver = "org.sqlite.JDBC")

  private val books = Books.all

  before {
    db.run(setup)
  }

  after {
    db.run(tearDown)
  }

  val setup = DBIO.seq(
    (books.schema).create
  )

  val tearDown = DBIO.seq(
    (books.schema).drop
  )

  "Books" should "be a Sequences of Books" in {
    // Insert some dummy data

    db.run(
      DBIO.seq(
        books += new Book(-1, 0, "Awesome Title #1"),
        books += new Book(-1, 1, "Gorgeous Sequel"),
        books += new Book(-1, 2, "Mehmeh Prequel")
      )
    )

    val bookList: Future[Seq[Book]] = db.run(books.result)
    whenReady(bookList) {
      result => {
        result shouldBe a[Seq[_]] // Remember type erasure
        result(0) shouldBe a[Book]
      }

    }
  }
}

Theoretically this should work, shouldn't it? Well, it doesn't. After the test there still is a table and it's still filled with data.

How to do this correctly? Afterwards I'd like to put this in an in-memory-database (as said), but currently for debugging it makes more sense this way.

1 Answers1

0

Actually my approach was correct. The only problem was that the timeout was still set too low. I thought 5 seconds would be enough, but I increased this to 10 and everything is working as expected now!