开发者

Fixtures.deleteDatabase() does not reset auto_increment

开发者 https://www.devze.com 2023-03-11 01:59 出处:网络
I\'m new to play! This is a really newbie question, but I just can\'t get around it... I ran through play-scala\'s tutorial (yabe), and was blocked at round 2.

I'm new to play! This is a really newbie question, but I just can't get around it...

I ran through play-scala's tutorial (yabe), and was blocked at round 2.

it should "retrieve Posts with author" in {

  User.create(User(NotAssigned, "bob@gmail.com", "secret", "Bob", false)) 
  Post.create(Post(NotAssigned, "My first post", "Hello world", new Date, 1))

  Post.count().single() should be (1)

  val posts = Post.find("author_id={id}").on("id" -> 1).as(Post*)

  posts.length should be (1)

  val firstPost = posts.headOption

  firstPost should not be (None)
  firstPost.get.author_id should be (1)
  firstPost.get.title should be ("My first post")
  firstPost.get.content should be ("Hello!")
}

The problem was each time this test is run, Fixture.deleteAll() is called, and the database is supposed to be empty. But the auto_increment is not reset, so the User object created on the first line has an ID bigger than 1, forbidding the Post object to be created (with author_id = 1).

开发者_高级运维

I have set %test config to %test.jpa.ddl=create-drop .

My question is, what should I do if I want the %test database to be really reset before each test ?


instead of passing '1' to the Post creation method, pass the previous user id. No test should depend on hardcoded constraints like that, as they are irrelevant to the program (you don't care about the id of the User, only that the Post references that id)

Something like:

 val user = User.create(User(NotAssigned, "bob@gmail.com", "secret", "Bob", false)) 
 Post.create(Post(NotAssigned, "My first post", "Hello world", new Date, user.id))
 [... replace all '1' by user.id ...]
0

精彩评论

暂无评论...
验证码 换一张
取 消