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

/test/models/MediaSpec.scala

http://github.com/leodagdag/persistance
Scala | 135 lines | 115 code | 20 blank | 0 comment | 3 complexity | 6883ffaf36ea44030e2e15c367ab50e6 MD5 | raw file
  1. package models
  2. import org.specs2.mutable._
  3. import play.api.test.Helpers.running
  4. import play.api.test.FakeApplication
  5. import com.mongodb.casbah.commons.Imports._
  6. import models._
  7. import java.io.File
  8. import play.api.test.FakeApplication
  9. import plugin.GridFSHelper
  10. class MediaSpec extends Specification {
  11. com.mongodb.casbah.commons.conversions.scala.RegisterConversionHelpers()
  12. com.mongodb.casbah.commons.conversions.scala.RegisterJodaTimeConversionHelpers()
  13. "with Salat" should {
  14. var savedId: ObjectId = null
  15. var deleteId: ObjectId = null
  16. val fileName: String = "thinking.jpg"
  17. val filePath: String = "public/images/test/thinking.jpg"
  18. val FileContentPath = "image/jpeg"
  19. val key = "key1"
  20. val value = "value1"
  21. "remove all" in {
  22. running(FakeApplication()) {
  23. Media.collection.drop()
  24. Media.count() mustEqual 0
  25. }
  26. }
  27. "create 1" in {
  28. running(FakeApplication()) {
  29. val media = new Media(title = "titre")
  30. Media.save(media)
  31. savedId = media._id
  32. savedId mustNotEqual None
  33. }
  34. }
  35. "find by criteria" in {
  36. running(FakeApplication()) {
  37. val newPost = Media.findOne(MongoDBObject("title" -> "titre"))
  38. newPost mustNotEqual None
  39. }
  40. }
  41. "findById" in {
  42. running(FakeApplication()) {
  43. val newPost = Media.findOneByID(savedId)
  44. newPost mustNotEqual None
  45. }
  46. }
  47. "create 12 Posts" in {
  48. running(FakeApplication()) {
  49. var media: Media = null
  50. for (i <- 1 to 12) {
  51. media = new Media(title = "titre" + i, description = Some("description"))
  52. Media.save(media)
  53. if (i == 1) { deleteId = media._id }
  54. }
  55. Media.count() mustEqual 13
  56. }
  57. }
  58. "find all" in {
  59. running(FakeApplication()) {
  60. val all = Media.find(MongoDBObject()).toList
  61. all.size mustEqual 13
  62. }
  63. }
  64. "count" in {
  65. running(FakeApplication()) {
  66. Media.count() mustEqual 13
  67. }
  68. }
  69. "find by page" in {
  70. running(FakeApplication()) {
  71. implicit val dao = Media
  72. Media.byPage(0) must haveTheSameElementsAs(Media.byPage(1))
  73. Media.byPage(1) must have size(10)
  74. Media.byPage(2) must have size(3)
  75. Media.byPage(3) must have size(0)
  76. }
  77. }
  78. "update" in {
  79. running(FakeApplication()) {
  80. val newPost = Media.findOneByID(savedId).get
  81. val cr = Media.update(MongoDBObject("_id" -> savedId), newPost.copy(title = "new Title"), false, false, Media.collection.writeConcern)
  82. cr mustEqual ()
  83. Media.findOneByID(savedId).get.title mustEqual "new Title"
  84. }
  85. }
  86. "remove by Id" in {
  87. running(FakeApplication()) {
  88. Media.removeById(deleteId)
  89. Media.findOneByID(deleteId) mustEqual None
  90. Media.count() mustEqual 12
  91. }
  92. }
  93. "create with a File" in {
  94. running(FakeApplication()) {
  95. val file: File = new File(filePath)
  96. val fileId: ObjectId = GridFSHelper.createNewFile(file, Map(key -> value))
  97. val media = new Media(title = "titre", fileId = Some(fileId))
  98. Media.save(media)
  99. val newMedia = Media.findOneByID(media._id).get
  100. newMedia._id mustEqual media._id
  101. newMedia.fileId mustNotEqual None
  102. }
  103. }
  104. "update with a File" in {
  105. running(FakeApplication()){
  106. val file: File = new File(filePath)
  107. val fileId: ObjectId = GridFSHelper.createNewFile(file, Map(key -> value))
  108. val media = Media.findOneByID(savedId).get
  109. media.fileId = Some(fileId)
  110. Media.save(media)
  111. Media.findOneByID(savedId).get.fileId must beAnInstanceOf[Option[ObjectId]]
  112. }
  113. }
  114. }
  115. }