Using specs2 and FakeApplication () to validate a database crashes evolutionary inserts

This is for Play! Framework 2.0.

I am trying to write a simple test case to ensure that my user model works correctly and stores data in my database. I would like to run it in memory, if possible, so that I can start a new start with each new run.

The problem is that my evolutions are being executed (tables are being created, data is being inserted, but I cannot query it as it is). Firstly, my code.

CREATE TABLE user_data ( id SERIAL PRIMARY KEY, user_name varchar(256) UNIQUE NOT NULL, email varchar(256) NOT NULL, password varchar(256) NOT NULL, edits int NOT NULL, reports int NOT NULL, active BOOLEAN NOT NULL); INSERT INTO user_data(user_name, email, password, edits, reports, active) VALUES ('user1', ' user1@email.com ', '12345678', 0, 0, true); 

In application.conf

 db.default.driver=org.postgresql.Driver db.default.url="postgres://user: password@loc alhost:5432/ME" 

In build.scala

 val appDependencies = Seq( // Add your project dependencies here, "postgresql" % "postgresql" % "9.1-901-1.jdbc4" ) 

Security Code

 class User_dataSpec extends Specification { "The Database" should { "persist data properly" in { running(FakeApplication(additionalConfiguration = inMemoryDatabase())) { //User_data.findAll().length must beEqualTo(1) //Create users User_data.create("user1", "password1", " email@test1.com ") must beEqualTo(1) User_data.create("user2", "password2", " email@test2.com ") must beEqualTo(2) User_data.create("user1", "password3", " email@test3.com ") must beEqualTo(0) //Count users User_data.findAll().length must beEqualTo(2) //Verify users exist User_data.exists("user1") must beTrue User_data.exists("user2") must beTrue //Verify user doesn't exist User_data.exists("user3") must beFalse //Find users by ID User_data.findUser(1).get.user_name must beEqualTo("user1") User_data.findUser(2).get.user_name must beEqualTo("user2") //Fail to find users by ID User_data.findUser(3) must beNone //Find users by user_name User_data.findUser("user1").get.user_name must beEqualTo("user1") User_data.findUser("user2").get.user_name must beEqualTo("user2") //Fail to find users by user_name User_data.findUser("user3") must beNone //Authenticate users User_data.authenticate("user1", "password1") must beTrue User_data.authenticate("user2", "password2") must beTrue //Fail to authenticate users User_data.authenticate("user1", "password2") must beFalse User_data.authenticate("user3", "passwordX") must beFalse //Confirm the user was inserted properly val user = User_data.findUser("user1") user.get.user_name must beEqualTo("user1") user.get.email must beEqualTo(" email@test1.com ") user.get.password must beEqualTo("password1") user.get.edits must beEqualTo(0) user.get.reports must beEqualTo(0) user.get.active must beTrue } } } } 

This code will be transmitted as written, however it should not. If I uncomment the first test case inside the work block to verify that my findAll () function should be 1 long, it will work immediately. However, if I change this to use a permanent PostgreSQL DBMS on my machine, it will still fail, but when I look at the PostgreSQL database, my user_data table has one evolution insert embedded in it, and the play_evolutions table has an entry for my evolution and marked as state = "apply" and last problem = "".

Any help would be appreciated, thanks.

(PS, this is my first poster, but I will do my best to accept the answer as soon as possible for those who want to help them)

+6
source share
3 answers

* UPDATED *

As Jacob stated, the reason failure develops is probably due to the fact that SQL written for MySQL is not compatible with H2DB. You can solve this using separate MySQL for testing according to the original answer, or set H2DB to MySQL compatibility mode, which can fix the problem (see Fixtures in Play! 2 for Scala ).

+3
source

The problem with evolutions and H2 is that H2 does not match everything you can do with postgres or MySQL, for example. Thus, evolution will work fine in prod, but failing in testing. I had this problem in a project, and it was ultimately resolved by simply not using evolution and instead using Liquibase for the database material.

Or you need to make sure that the sql you are writing can be run on H2. In this case, evolution will work fine. I donโ€™t remember exactly what the problem is with H2 (something about the indices I think)

0
source

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


All Articles