Play Framework and Slick: Testing Database Related Services

I try to follow the most idiomatic way to have some fully tested DAO services.

I have several class classes, for example:

case class Person ( id :Int, firstName :String, lastName :String ) case class Car ( id :Int, brand :String, model :String ) 

Then I have a simple DAO class:

 class ADao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[JdbcProfile] { import driver.api._ private val persons = TableQuery[PersonTable] private val cars = TableQuery[CarTable] private val personCar = TableQuery[PersonCar] class PersonTable(tag: Tag) extends Table[Person](tag, "person") { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) def firstName = column[String]("name") def lastName = column[String]("description") def * = (id, firstName, lastName) <> (Person.tupled, Person.unapply) } class CarTable(tag: Tag) extends Table[Car](tag, "car") { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) def brand = column[String]("brand") def model = column[String]("model") def * = (id, brand, model) <> (Car.tupled, Car.unapply) } // relationship class PersonCar(tag: Tag) extends Table[(Int, Int)](tag, "person_car") { def carId = column[Int]("c_id") def personId = column[Int]("p_id") def * = (carId, personId) } // simple function that I want to test def getAll(): Future[Seq[((Person, (Int, Int)), Car)]] = db.run( persons .join(personCar).on(_.id === _.personId) .join(cars).on(_._2.carId === _.id) .result ) } 

And my application.conf looks like this:

 slick.dbs.default.driver="slick.driver.PostgresDriver$" slick.dbs.default.db.driver="org.postgresql.Driver" slick.dbs.default.db.url="jdbc:postgresql://super-secrete-prod-host/my-awesome-db" slick.dbs.default.db.user="myself" slick.dbs.default.db.password="yolo" 

Now, having gone through Database Testing and trying to emulate a play- smooth sample project, I get so much trouble and I canโ€™t figure out how to get my test to use a different database (I suppose I need to add another db to the conf file, say slick.dbs.test ), but then I could not find how to do this inside the test.

In addition, there is some โ€œmagicโ€ on the repo sample, such as Application.instanceCache[CatDAO] or app2dao(app) .

Can someone point me to some full-fledged example or repo that works correctly with game testing and spotting?

Thanks.

+5
source share
1 answer

I agree with this confusing. I don't know if this is the best solution, but in the end I have a separate test.conf configuration file that indicates the database in memory:

 slick.dbs { default { driver = "slick.driver.H2Driver$" db.driver = "org.h2.Driver" db.url = "jdbc:h2:mem:play-test" } } 

and then told sbt to use this when running tests:

 [..] javaOptions in Test ++= Seq("-Dconfig.file=conf/test.conf") 
+4
source

Source: https://habr.com/ru/post/1246009/


All Articles