使用specs2和FakeApplication()测试数据库会使进化插入失败 [英] Using specs2 and FakeApplication() to test database fails evolution inserts
问题描述
这是玩的! Framework 2.0.
This is for Play! Framework 2.0.
我正在尝试编写一个简单的测试用例,以确保我的用户模型能够正常运行并将数据持久存储在数据库中.我想尽可能在内存中运行它,以便在每次新运行时都能重新开始.
I'm trying to write a simple test case to ensure my user model is functioning properly and persisting data in my database. I'd like to run it in memory if possible so I can get a fresh start with every new run.
我遇到的问题是我的演化运行(创建了表,插入了数据,但是我不能在那里查询它).首先,我的代码.
The issue I have is that my evolutions run(tables are created, data is inserted, but I can't query it as being there). First, 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);
在application.conf中
In application.conf
db.default.driver=org.postgresql.Driver
db.default.url="postgres://user:password@localhost:5432/ME"
在build.scala中
In build.scala
val appDependencies = Seq(
// Add your project dependencies here,
"postgresql" % "postgresql" % "9.1-901-1.jdbc4"
)
测试代码
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
}
}
}
}
此代码将按书面形式通过,但不应通过.如果我取消注释正在运行的块中的第一个测试用例以测试我的findAll()函数的长度应为1,则它将立即失败.但是,如果将其更改为在计算机上使用持久化的PostgreSQL DB,它仍然会立即失败,但是当我查看PostgreSQL DB时,我的user_data表中已应用了单个Evolution插入,而play_evolutions表中有条目对于我的进化,并标记为state ="applied"和最后一个问题=".
This code will pass as written, however it shouldn't. If I uncomment the first test case inside the running block to test that my findAll() function should be a length of 1 it will fail immediately. However, if I change this to use a persisted PostgreSQL DB on my machine, it will still fail immediately, but when I look at the PostgreSQL DB, my user_data table has the single evolution applied insert in it, and the play_evolutions table has the entry for my evolution and is marked as state = "applied" and last problem = "".
感谢您的任何帮助.
(P.S.,我是第一次张贴海报,但对于那些愿意提供帮助的人,我会尽全力尽快接受答案)
(P.S., I am a first time poster, but will do my best to accept an answer as soon as possible for those willing to lend their help)
推荐答案
*更新*
正如Jakob所述,进化失败的原因可能是因为为MySQL编写的SQL与H2DB不兼容.您可以按照原始答案通过使用单独的MySQL进行测试来解决此问题,或者将H2DB置于MySQL兼容模式下,这可能会解决问题(请参见
As Jakob stated, the reason evolutions are failing is probably because the SQL written for MySQL is incompatible with H2DB. You can solve this by using a separate MySQL for testing as per the original answer, or put H2DB into MySQL compatibility mode which may fix the problem (see Fixtures in Play! 2 for Scala).
这篇关于使用specs2和FakeApplication()测试数据库会使进化插入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!