PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/questions/src/test/scala/com/cyrusinnovation/inquisition/response/mongodb/MongoResponseRepositoryTest.scala

https://github.com/cyrusinnovation/inquisition
Scala | 113 lines | 87 code | 26 blank | 0 comment | 0 complexity | 04d5263bc23263916e52571a7d2a02ee MD5 | raw file
  1. package com.cyrusinnovation.inquisition.response.mongodb
  2. import com.cyrusinnovation.inquisition.response.Response
  3. import com.cyrusinnovation.inquisition.questions.Question
  4. import org.scalatest.matchers.ShouldMatchers
  5. import org.scalatest.{BeforeAndAfterEach, FunSuite}
  6. import com.mongodb.casbah.MongoConnection
  7. import org.bson.types.ObjectId
  8. import com.cyrusinnovation.inquisition.questions.mongodb.{MongoTestConstants, MongoQuestionRepository}
  9. import org.junit.runner.RunWith
  10. import org.scalatest.junit.JUnitRunner
  11. import com.mongodb.casbah.commons.MongoDBObject
  12. import com.mongodb.casbah.commons.MongoDBObject._
  13. import java.lang.String
  14. @RunWith(classOf[JUnitRunner])
  15. class MongoResponseRepositoryTest extends FunSuite with ShouldMatchers with BeforeAndAfterEach {
  16. val con = MongoConnection()
  17. val TestDbName = "test_inquisition"
  18. val db = con(TestDbName)
  19. val questionRepository = new MongoQuestionRepository(db)
  20. val responseRepository = new MongoResponseRepository(db, questionRepository)
  21. override def beforeEach() {
  22. db("questions").remove(MongoDBObject())
  23. }
  24. def uniqueQuestion(title: String = "How do I use MongoDB?"): Question = {
  25. Question(id = None, title = title + " " + System.nanoTime(), creatorUsername = "tester", body = "The question body.")
  26. }
  27. test("should be able respond to a question with an anwser") {
  28. val savedQuestion = questionRepository.save(Question(None, "Title", "Tester", "Body"))
  29. val response = new Response(None, "Title", "Creator", "Body")
  30. responseRepository.save(savedQuestion.id.get, response)
  31. val updatedQuestion = questionRepository.findById(savedQuestion.id.get).get
  32. val savedResponse = updatedQuestion.responses.head;
  33. savedQuestion.id should be(updatedQuestion.id)
  34. savedResponse.id should be('defined)
  35. savedResponse should equal(response.copy(id = savedResponse.id))
  36. }
  37. test("should be able respond to a question with an anwser without creating a duplicate question.") {
  38. val savedQuestion = questionRepository.save(Question(None, "Title", "Tester", "Body"))
  39. responseRepository.save(savedQuestion.id.get, new Response(None, "Title", "Creator", "Body"))
  40. questionRepository.findQuestionCount() should equal(1)
  41. }
  42. test("Can update a question with an answer") {
  43. val answer = new Response(None, "Answer", "creator", "bodystring")
  44. val question: Question = questionRepository.save(uniqueQuestion())
  45. val savedResponse = responseRepository.save(question.id.get, answer)
  46. val retrievedQuestion = questionRepository.findById(question.id.get)
  47. val answers = retrievedQuestion.get.responses
  48. answers should have length (1)
  49. answers.head should equal(savedResponse)
  50. }
  51. test("Can save a question response with an existing id") {
  52. val response = new Response(Some(new ObjectId().toStringMongod), "Answer", "creator", "bodystring")
  53. val question: Question = questionRepository.save(uniqueQuestion())
  54. val savedResponse = responseRepository.save(question.id.get, response)
  55. val retrievedQuestion = questionRepository.findById(question.id.get)
  56. val responses = retrievedQuestion.get.responses
  57. responses should have length (1)
  58. savedResponse should equal(response)
  59. }
  60. test("Exception is thrown when a response is added to a non-existant question") {
  61. val response = new Response(Some(new ObjectId().toStringMongod), "Answer", "creator", "bodystring")
  62. evaluating {
  63. responseRepository.save(MongoTestConstants.DeadObjectIdString, response)
  64. } should produce[IllegalArgumentException]
  65. }
  66. test("Can retrieve a question by response id") {
  67. val savedQuestion = questionRepository.save(uniqueQuestion())
  68. val responseId: String = new ObjectId().toStringMongod
  69. val response = new Response(Some(responseId), "Answer", "creator", "bodystring")
  70. responseRepository.save(savedQuestion.id.get, response)
  71. val (retrievedQuestion, retrievedResponse) = responseRepository.getResponse(responseId).get
  72. retrievedResponse should be(response)
  73. }
  74. test("Empty option returned when retrieve a question by non-existant response id") {
  75. val retrievedResponse = responseRepository.getResponse(MongoTestConstants.DeadObjectIdString)
  76. retrievedResponse should not be('defined)
  77. }
  78. test("can update a response") {
  79. val savedQuestion = questionRepository.save(uniqueQuestion())
  80. val responseId: String = new ObjectId().toStringMongod
  81. val response = new Response(Some(responseId), "Answer", "creator", "bodystring")
  82. responseRepository.save(savedQuestion.id.get, response)
  83. responseRepository.updateResponse(response.copy(title = "something else"))
  84. val (_, updatedResponse) = responseRepository.getResponse(responseId).get
  85. updatedResponse.title should equal("something else")
  86. }
  87. }