PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/scala/br/com/gfuture/mongodbhelper/DocumentSpec.scala

https://github.com/josa/mongodb-scala-helper
Scala | 106 lines | 79 code | 27 blank | 0 comment | 0 complexity | f7095155bfcff17366bd5235c37a67a3 MD5 | raw file
  1. package br.com.gfuture.mongodbhelper
  2. import org.scalatest.matchers.ShouldMatchers
  3. import org.scalatest.{Spec, BeforeAndAfterEach}
  4. import br.com.gfuture.mongodbhelper.mongodb.MongoProvider
  5. import com.mongodb.BasicDBObject
  6. import com.mongodb.DBObject
  7. import com.mongodb.casbah.commons.MongoDBObject
  8. import org.bson.types.ObjectId
  9. import org.junit.Test
  10. import org.junit.runner.RunWith
  11. @RunWith(classOf[org.scalatest.junit.JUnitRunner])
  12. class DocumentSpec extends Spec with ShouldMatchers with BeforeAndAfterEach {
  13. @Test
  14. def test_it = execute()
  15. var document = null.asInstanceOf[DocumentExample]
  16. var dbObject = null.asInstanceOf[DBObject]
  17. override def beforeEach() {
  18. document = new DocumentExample
  19. document.valueOne = "value one"
  20. document.valueTransient = "not included"
  21. dbObject = MongoDBObject("valueOne" -> "value one")
  22. }
  23. override def afterEach(){
  24. MongoProvider.getCollection(document.getClass).remove(new BasicDBObject())
  25. }
  26. describe("equals") {
  27. it("deveria comparar dois objetos") {
  28. val object1 = new DocumentExample()
  29. object1.setObjectId(org.bson.types.ObjectId.get)
  30. val object2 = new DocumentExample()
  31. object2.setObjectId(object1.getObjectId)
  32. object1.equals(object2) should equal(true)
  33. }
  34. }
  35. describe("marshall") {
  36. it("the _id should be null before persisting") {
  37. DocumentTools.toDBObject(document).get("_id") should equal(null)
  38. }
  39. it("should generate mongodbObject") {
  40. DocumentTools.toDBObject(document).get("valueOne") should equal("value one")
  41. }
  42. it("should generate a mongodbObject with only two element") {
  43. DocumentTools.toDBObject(document).keySet.size should equal(2)
  44. }
  45. it("should not include valueTransient") {
  46. DocumentTools.toDBObject(document).get("valueTransient") should equal(null)
  47. }
  48. }
  49. describe("unmarshall") {
  50. it("should generate object from mongodbObject") {
  51. val objectFrom = DocumentTools.fromMongoObject(dbObject, classOf[DocumentExample].asInstanceOf[Class[Document]])
  52. objectFrom.asInstanceOf[DocumentExample].valueOne should equal("value one")
  53. }
  54. it("should not load transientValue") {
  55. val objectFrom = DocumentTools.fromMongoObject(dbObject, classOf[DocumentExample])
  56. objectFrom.asInstanceOf[DocumentExample].valueTransient should equal(null)
  57. }
  58. }
  59. describe("persistence") {
  60. def findInMongo(id: ObjectId): DBObject = {
  61. MongoProvider.getCollection(document.getClass).findOne(MongoDBObject("_id" -> id))
  62. }
  63. it("should save object") {
  64. document.save
  65. findInMongo(document.getObjectId) should not equal (null)
  66. }
  67. it("should update object") {
  68. document.save
  69. document.valueOne = "value updated"
  70. document.save
  71. findInMongo(document.getObjectId).get("valueOne") should equal("value updated")
  72. }
  73. it("should delete object") {
  74. document.save
  75. document.delete
  76. findInMongo(document.getObjectId) should equal(null)
  77. }
  78. }
  79. }