H2 in memory database tests: for some reason my unit test does not 'see' the schema being created

216 views
Skip to first unread message

Marco Mistroni

unread,
Jul 5, 2017, 6:17:32 PM7/5/17
to Squeryl
HI all
 i am seeking some advices here as i am still a squeryl noob
So i followed the sample pattern in the org.squeryl.test framework where ther is  a DbTestBase, a SchemaTester and a RunTestsInsideTransaction for my unit tests, where the
connector used during unit tests is an in memory H2 database.
the session / schema gets created during the SchemaTester.beforeAll method , where my data also gets populated when doing an insert

however, when my test is attempting to do a Query, i receive the exception at the end of this email
Perhaps i am querying in the wrong way?
i paste below the code in my test class, as i have literally copied SchemaTester, DbTestBase and RunTestsInsideTransactions from squeryl sample project

class TestData {
  import SharesSchema._

  val sharePricesTests = sharePrices.insert(
    SharePrice(new java.util.Date(), "TestTicker",
      -1.0, -2.0,
      -3.0, -4.0,
      -4.0, -5.0))
  println("FINISHING POPULATNG DATA")
}

@RunWith(classOf[JUnitRunner])
class SquerylPersistenceServiceTest extends SchemaTester with QueryTester with RunTestsInsideTransaction
        with Matchers  with BeforeAndAfterAll with H2_Connection {
  val schema = SharesSchema
  override def prePopulate = {
     println("Pre populating.............")
     val testData = new TestData()
  }

  test("Simple Insert/Query") {
    lazy val data = from(SharesSchema.sharePrices)(sharePrice => select(sharePrice)).toList
    data.size should be(1)
  }
}

The exception is below. It seems my test class cannot find the schema, even though the schema has been created in the parent class.  If i create the schema in the test("Simple Insert Query") then everything works fine.....

thanks  in advance and regards
 marco

PS this is the exception
org.squeryl.SquerylSQLException: Exception while executing statement : Table "SHAREPRICE" not found; SQL statement:
Select
  shareprice9.mvAvg50 as shareprice9_mvAvg50,
  shareprice9.peg as shareprice9_peg,
  shareprice9.mvAvg as shareprice9_mvAvg,
  shareprice9.LATEST_PRICE as shareprice9_LATEST_PRICE,
  shareprice9.eps as shareprice9_eps,
  shareprice9.ticker as shareprice9_ticker,
  shareprice9.fwdEps as shareprice9_fwdEps,
  shareprice9.DATE as shareprice9_DATE
From
  shareprice shareprice9 [42102-168]
errorCode: 42102, sqlState: 42S02
Select
  shareprice9.mvAvg50 as shareprice9_mvAvg50,
  shareprice9.peg as shareprice9_peg,
  shareprice9.mvAvg as shareprice9_mvAvg,
  shareprice9.LATEST_PRICE as shareprice9_LATEST_PRICE,
  shareprice9.eps as shareprice9_eps,
  shareprice9.ticker as shareprice9_ticker,
  shareprice9.fwdEps as shareprice9_fwdEps,
  shareprice9.DATE as shareprice9_DATE
From
  shareprice shareprice9


David Whittaker

unread,
Jul 20, 2017, 10:39:07 AM7/20/17
to squ...@googlegroups.com
It seems pretty clear from the exception that your table isn’t created before you run the query.  I’d look closely at your beforeAll method if I were you, making sure that it is run and that it creates the schema the way you expect it to.
--
You received this message because you are subscribed to the Google Groups "Squeryl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to squeryl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages