PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/models/PostSpec.scala

http://github.com/leodagdag/persistance
Scala | 132 lines | 115 code | 17 blank | 0 comment | 2 complexity | 8757bc37b5f8ac8a42d6a95c3209b2f3 MD5 | raw file
  1. package models
  2. import org.specs2.mutable._
  3. import play.api.test.Helpers.running
  4. import com.mongodb.casbah.commons.Imports._
  5. import models._
  6. import play.api.test.FakeApplication
  7. import org.joda.time.DateTime
  8. class PostSpec extends Specification {
  9. com.mongodb.casbah.commons.conversions.scala.RegisterConversionHelpers()
  10. com.mongodb.casbah.commons.conversions.scala.RegisterJodaTimeConversionHelpers()
  11. "with Salat" should {
  12. var savedId: ObjectId = null
  13. var deleteId: ObjectId = null
  14. "Fake test" in {
  15. 1 mustEqual 1
  16. }
  17. "remove all" in {
  18. running(FakeApplication()) {
  19. Post.collection.drop()
  20. User.collection.drop()
  21. User.save(User(email = "test@b.com", username = "test", password = "test", firstName = Some("first"), lastName = Some("last")))
  22. User.count() mustEqual 1
  23. Post.count() mustEqual 0
  24. }
  25. }
  26. "create 1" in {
  27. running(FakeApplication()) {
  28. val post = Post(title = "titre", content = "content")
  29. Post.save(post)
  30. savedId = post._id
  31. savedId mustNotEqual null
  32. }
  33. }
  34. "find by criteria" in {
  35. running(FakeApplication()) {
  36. val newPost = Post.findOne(MongoDBObject("title" -> "titre"))
  37. newPost mustNotEqual None
  38. }
  39. }
  40. "findById" in {
  41. running(FakeApplication()) {
  42. val newPost = Post.findOneByID(savedId)
  43. newPost mustNotEqual None
  44. }
  45. }
  46. "create 12 Posts" in {
  47. running(FakeApplication()) {
  48. var post: Post = null
  49. (1 to 12).foreach {
  50. i =>
  51. post = Post(title = "titre" + i, content = "content " + i)
  52. Post.save(post)
  53. if (i == 12) {
  54. deleteId = post._id
  55. }
  56. }
  57. Post.count() mustEqual 13
  58. }
  59. }
  60. "find all" in {
  61. running(FakeApplication()) {
  62. val all = Post.find(MongoDBObject()).toList
  63. all.size mustEqual 13
  64. }
  65. }
  66. "count" in {
  67. running(FakeApplication()) {
  68. Post.count() mustEqual 13
  69. }
  70. }
  71. "find by page" in {
  72. running(FakeApplication()) {
  73. implicit val dao = Post
  74. Post.byPage(0).size mustEqual Post.byPage(1).size
  75. Post.byPage(1).size mustEqual 10
  76. Post.byPage(2).size mustEqual 3
  77. Post.byPage(3).size mustEqual 0
  78. }
  79. }
  80. "update" in {
  81. running(FakeApplication()) {
  82. Post.findOneByID(savedId).get.title mustNotEqual "new Title"
  83. val newPost = Post.findOneByID(savedId).get
  84. val cr = Post.update(MongoDBObject("_id" -> savedId), newPost.copy(title = "new Title"), false, false, Post.collection.writeConcern)
  85. cr mustEqual()
  86. Post.findOneByID(savedId).get.title mustEqual "new Title"
  87. }
  88. }
  89. "add a comment" in {
  90. running(FakeApplication()) {
  91. val user = User.findOne(MongoDBObject("username" -> "test")).get
  92. println("add a comment - user._id=[%s]".format(user._id))
  93. var comment = new Comment(created = new DateTime(), content = "new comment", user = user)
  94. Post.addComment(savedId, comment)
  95. var post = Post.findOneByID(savedId).get
  96. post.comments.size mustEqual 1
  97. }
  98. }
  99. "remove by Id" in {
  100. running(FakeApplication()) {
  101. Post.removeById(deleteId)
  102. Post.findOneByID(deleteId) mustEqual None
  103. Post.count() mustEqual 12
  104. }
  105. }
  106. "add a comment generate EntityNotFoundException" in {
  107. running(FakeApplication()) {
  108. val user = User.findOne(MongoDBObject("username" -> "test")).get
  109. println("add a comment generate EntityNotFoundException - user._id=[%s]".format(user._id))
  110. var comment = new Comment(created = new DateTime(), content = "new comment", user = user)
  111. Post.addComment(deleteId, comment) must throwAn[Exception]
  112. }
  113. }
  114. }
  115. }