Giter Club home page Giter Club logo

Comments (6)

loicdescotte avatar loicdescotte commented on June 7, 2024

Hi,
Can you show us the error message you have with your configuration?
It might me related to this issue #14

from play-slick.

yasuoza avatar yasuoza commented on June 7, 2024

Thanks for your reply. Actually there is no error message and the test passes. The problem is that test database is not used even though I specified the test database configuration.

Here is the way to reproduce the problem

  • Set database configuration like this
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/play_test"
db.default.user=test_user
db.default.password=test_user_pass

test.db.default.driver=com.mysql.jdbc.Driver
test.db.default.url="jdbc:mysql://localhost/play_test"
test.db.default.user=test_user
test.db.default.password=test_user_pass
  • play run to apply evolution
  • Insert some data to Cat table
  • Change database configuration to this
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
db.default.user=sa
db.default.password=""

test.db.default.driver=com.mysql.jdbc.Driver
test.db.default.url="jdbc:mysql://localhost/play_test"
test.db.default.user=test_user
test.db.default.password=test_user_pass
  • Run test
class SlickSpec extends Specification {

  // Assume there are more than 2 columns inserted at step 3
  // So this test should fail
  "Slick" should {
    "use test database" in {
      running(FakeApplication()) {
        import play.api.Play.current

         DB.withSession { implicit session =>
          val count = Query((for (c <- Query(Cats)) yield c.id).countDistinct).first
          count must beEqualTo(1) 
        }     
      }
    }
  }

}

Test should fail, but it passes. This proves test database(in this case MySQL) is not used.
Could you check this?

from play-slick.

loicdescotte avatar loicdescotte commented on June 7, 2024

Ok I get it now,
This is because the default datasource is always loaded for the slick session if you don't specify a name : https://github.com/freekh/play-slick/blob/master/src/main/scala/play/api/db/slick/DB.scala
You can load a specific DB session with the name parameter of the DB object

from play-slick.

loicdescotte avatar loicdescotte commented on June 7, 2024

But there is indeed a little problem.
You need to define DB like this :

      DB("test").withSession { implicit session =>
          val count = Query((for (c <- Query(Cats)) yield c.id).countDistinct).first
          count must beEqualTo(1) 
        }     

The datasource will look datasource named ("db.test") and not ("test.default.db")
I will do a little pull request to change this defaults (used in driver loading)

Right now you can configure your application like this :

test.db.default.driver=com.mysql.jdbc.Driver

db.test.driver=com.mysql.jdbc.Driver
db.test.url="jdbc:mysql://localhost/play_test"
db.test.user=test_user
db.test.password=test_user_pass

from play-slick.

loicdescotte avatar loicdescotte commented on June 7, 2024

With this pull request the convention for driver and datasource will be the same.
But there is another problem : if you want to test some controllers or models that use DB.withSession you will be binded to the default datasource, not to the test datasource

from play-slick.

yasuoza avatar yasuoza commented on June 7, 2024

I appreciate your PR and reply. I understand how and another problem.
I'll investigate the way I can use environment specified database.

from play-slick.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.