这是玩游戏! 框架2.0。
我试图写一个简单的测试用例来保证我的用户模式运行正常,并且在我的数据库保存数据。 我想在内存中运行它,如果可能的话,所以我可以与每一个新的运行一个新的开始。
我的问题是,我的变阵运行(创建表,插入数据,但我不能查询它在那里)。 首先,我的代码。
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
db.default.driver=org.postgresql.Driver
db.default.url="postgres://user:password@localhost:5432/ME"
在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数据库,它仍然会立即失败,但是当我看的PostgreSQL数据库,我USER_DATA表中有一个演变的把它应用插入和play_evolutions表具有项我的演变和标记为状态=“应用”和最后一个问题=“”。
任何帮助,将不胜感激,谢谢。
(PS,我是第一次海报,但会尽我所能,尽快为那些愿意借给他们的帮助下接受一个答案)