PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/questions/src/test/scala/com/cyrusinnovation/inquisition/questions/mongodb/MongoQuestionRepositoryTest.scala

https://github.com/cyrusinnovation/inquisition
Scala | 324 lines | 231 code | 93 blank | 0 comment | 4 complexity | cf1215c695c7d952a36c47b76dff824e MD5 | raw file
  1. package com.cyrusinnovation.inquisition.questions.mongodb
  2. import org.scalatest.matchers.ShouldMatchers
  3. import com.mongodb.casbah.MongoConnection
  4. import org.scalatest.{BeforeAndAfterEach, FunSuite}
  5. import com.cyrusinnovation.inquisition.response.Response
  6. import com.cyrusinnovation.inquisition.questions.Question
  7. import org.junit.runner.RunWith
  8. import org.scalatest.junit.JUnitRunner
  9. import com.mongodb.casbah.commons.MongoDBObject
  10. import java.lang.String
  11. import org.bson.types.ObjectId
  12. @RunWith(classOf[JUnitRunner])
  13. class MongoQuestionRepositoryTest extends FunSuite with ShouldMatchers with BeforeAndAfterEach {
  14. val con = MongoConnection()
  15. val TestDbName = "test_inquisition"
  16. val db = con(TestDbName)
  17. val repository = new MongoQuestionRepository(db)
  18. override def beforeEach() {
  19. db("questions").remove(MongoDBObject())
  20. }
  21. def uniqueQuestion(title: String = "How do I use MongoDB?"): Question = {
  22. Question(id = None, title = title + " " + System.nanoTime(), creatorUsername = "tester", body = "The question body.")
  23. }
  24. test("should be able to save a question and get it back") {
  25. val q = uniqueQuestion()
  26. q.id should be(None)
  27. val savedQuestion = repository.save(q)
  28. savedQuestion.id should not be (None)
  29. savedQuestion.id should be('defined)
  30. val retrievedQuestion = repository.findById(savedQuestion.id.get).get
  31. retrievedQuestion.id should be(savedQuestion.id)
  32. }
  33. test("should be able to count the number of questions") {
  34. val q = uniqueQuestion()
  35. repository.save(q)
  36. repository.save(q)
  37. repository.save(q)
  38. val questionCount = repository.findQuestionCount()
  39. questionCount should be(3)
  40. }
  41. test("should be able to save a question with an existing id to update it") {
  42. val savedQuestion = repository.save(uniqueQuestion(title = "Original Title"));
  43. val updatedQuestion = repository.save(savedQuestion.copy(title = "Updated Title"))
  44. val questionCount = repository.findQuestionCount()
  45. questionCount should be(1)
  46. updatedQuestion.id should be(savedQuestion.id)
  47. updatedQuestion.title should not be (savedQuestion.title)
  48. val updatedQuestionReturned = repository.findById(updatedQuestion.id.get).get
  49. updatedQuestionReturned.title should be(updatedQuestion.title)
  50. }
  51. test("should return none if nothing has that id") {
  52. val result = repository.findById(MongoTestConstants.DeadObjectIdString)
  53. result should be(None)
  54. }
  55. test("should find recent questions") {
  56. val questions = List(uniqueQuestion(), uniqueQuestion("Why isn't IntelliJ working?"))
  57. val savedQuestions = questions.map(repository.save(_))
  58. val results = repository.findRecent()
  59. savedQuestions.foreach(results.contains(_) should be(true))
  60. }
  61. test("should not be able to find recent questions") {
  62. val questions = List(uniqueQuestion(), uniqueQuestion("Why isn't IntelliJ working?"))
  63. questions.map(repository.save(_))
  64. val results = repository.findRecent(1)
  65. results.size should be(1)
  66. }
  67. test("make sure we can add a list of tags to a question") {
  68. var question = uniqueQuestion()
  69. question = question.copy(tags = List("java", "spring"))
  70. val savedQuestion = repository.save(question);
  71. question = question.copy(id = savedQuestion.id)
  72. question should equal(savedQuestion)
  73. }
  74. test("make sure we can get a list of tags to a question") {
  75. var question = uniqueQuestion()
  76. question = question.copy(tags = List("java", "spring"))
  77. val savedQuestion = repository.save(question);
  78. val returnedQuestion = repository.findById(savedQuestion.id.get)
  79. savedQuestion.tags should equal(returnedQuestion.get.tags)
  80. }
  81. test("delete a question with a given object id") {
  82. val savedQuestion = repository.save(uniqueQuestion())
  83. val savedQuestionId = savedQuestion.id.get
  84. repository.deleteQuestion(savedQuestionId)
  85. val deletedQuestion = repository.findById(savedQuestionId)
  86. deletedQuestion should be(None)
  87. }
  88. test("deleting a non-existant question does not throw an exception") {
  89. repository.deleteQuestion(MongoTestConstants.DeadObjectIdString)
  90. }
  91. test("Can save a question with an answer") {
  92. val response = new Response(None, "Answer", "creator", "body string")
  93. val question: Question = uniqueQuestion().copy(responses = List(response))
  94. val savedQuestion = repository.save(question)
  95. val retrievedQuestion = repository.findById(savedQuestion.id.get)
  96. val answers = retrievedQuestion.get.responses
  97. answers should have length (1)
  98. answers.head should equal(response)
  99. }
  100. test("can get list of all clients") {
  101. val client1 = "ABC"
  102. val client2 = "Boston Capital"
  103. val client3 = "DEF"
  104. val client4 = "GHI"
  105. val client5 = "JKL"
  106. val client6 = "MNO"
  107. val client7 = "NFL"
  108. val client8 = "PQR"
  109. val client9 = "STU"
  110. val client10 = "VWX"
  111. val client11 = "YZ"
  112. val expectedList = List(client1, client2, client3, client4, client5, client6, client7, client8, client9,
  113. client10, client11)
  114. for(client <- expectedList) {
  115. repository.save(uniqueQuestion().copy(client = client))
  116. }
  117. val clientList = repository.getClientList(limit = 0)
  118. clientList should not be (null)
  119. clientList.isEmpty should be(false)
  120. clientList.size should be(11)
  121. clientList.corresponds(expectedList){_ == _} should be(true)
  122. }
  123. test("can get list of all clients in the correct order") {
  124. val client1 = "Boston Capital"
  125. val client2 = "NFL"
  126. val client3 = "Akami"
  127. val questions = List(uniqueQuestion().copy(client = client1), uniqueQuestion().copy(client = client2),
  128. uniqueQuestion().copy(client = client3))
  129. questions.map(repository.save(_))
  130. val clientList = repository.getClientList()
  131. clientList(0) should equal(client3)
  132. }
  133. test("can limit list of clients and in the correct order") {
  134. val client1 = "Boston Capital"
  135. val client2 = "NFL"
  136. val client3 = "Akami"
  137. val questions = List(uniqueQuestion().copy(client = client1), uniqueQuestion().copy(client = client2),
  138. uniqueQuestion().copy(client = client3))
  139. questions.map(repository.save(_))
  140. val clientList = repository.getClientList(limit = 1)
  141. clientList.size should be(1)
  142. clientList(0) should equal(client3)
  143. }
  144. test("Do Starts with list of clients") {
  145. val client1 = "Boston Capital"
  146. val client2 = "NFL"
  147. val client3 = "Akami"
  148. val questions = List(uniqueQuestion().copy(client = client1), uniqueQuestion().copy(client = client2),
  149. uniqueQuestion().copy(client = client3))
  150. questions.map(repository.save(_))
  151. val clientList = repository.getClientList("N")
  152. clientList.size should be(1)
  153. clientList(0) should equal(client2)
  154. }
  155. test("Do Starts with list of clients with multiple results") {
  156. val client1 = "Boston Capital"
  157. val client2 = "NFL"
  158. val client3 = "Nordstroms"
  159. val questions = List(uniqueQuestion().copy(client = client1), uniqueQuestion().copy(client = client2),
  160. uniqueQuestion().copy(client = client3))
  161. questions.map(repository.save(_))
  162. val clientList = repository.getClientList("N")
  163. clientList.size should be(2)
  164. clientList(0) should equal(client2)
  165. clientList(1) should equal(client3)
  166. }
  167. test("Do Starts with list of clients with multiple results case insenitive") {
  168. val client1 = "Boston Capital"
  169. val client2 = "NFL"
  170. val client3 = "Nordstroms"
  171. val questions = List(uniqueQuestion().copy(client = client1), uniqueQuestion().copy(client = client2),
  172. uniqueQuestion().copy(client = client3))
  173. questions.map(repository.save(_))
  174. val clientList = repository.getClientList("n")
  175. clientList.size should be(2)
  176. clientList(0) should equal(client2)
  177. clientList(1) should equal(client3)
  178. }
  179. test("Can find the question that a response belongs to") {
  180. val responseId: String = new ObjectId().toStringMongod
  181. val response = new Response(Some(responseId), "Title", creatorUsername = "tester", body = "body")
  182. val expectedQuestion = repository.save(uniqueQuestion().copy(responses = List(response)))
  183. val retrievedQuestion = repository.findResponseQuestion(responseId).get
  184. retrievedQuestion should equal(expectedQuestion)
  185. }
  186. test("No question returned for non-existant response id") {
  187. val retrievedQuestion = repository.findResponseQuestion(MongoTestConstants.DeadObjectIdString)
  188. retrievedQuestion should not be('defined)
  189. }
  190. test("get a list of questions without responses basic") {
  191. var question = uniqueQuestion()
  192. question = question.copy(tags = List("java", "spring"))
  193. val savedQuestion = repository.save(question);
  194. val retrievedQuestions = repository.findQuestionsWithoutResponses()
  195. retrievedQuestions should not be(null)
  196. retrievedQuestions.isEmpty should be(false)
  197. retrievedQuestions should contain(savedQuestion)
  198. }
  199. test("don't get a question with a response") {
  200. val response = new Response(id = None, title = "def", body = "abc", creatorUsername = "someUser")
  201. val questions = List(uniqueQuestion().copy(responses = List(response)))
  202. questions.map(repository.save(_))
  203. val retrievedQuestions = repository.findQuestionsWithoutResponses()
  204. retrievedQuestions should not be(null)
  205. retrievedQuestions.isEmpty should be(true)
  206. }
  207. test("get a list of questions without a response") {
  208. val questions = List(uniqueQuestion(), uniqueQuestion(), uniqueQuestion())
  209. val expectedList = questions.map(repository.save(_))
  210. val retrievedQuestions = repository.findQuestionsWithoutResponses()
  211. retrievedQuestions should not be(null)
  212. retrievedQuestions.isEmpty should be(false)
  213. retrievedQuestions.corresponds(expectedList){_ == _} should be(true)
  214. }
  215. test("get a limited list of questions without a response") {
  216. val questions = List(uniqueQuestion(), uniqueQuestion(), uniqueQuestion())
  217. val expectedList = questions.map(repository.save(_))
  218. val retrievedQuestions = repository.findQuestionsWithoutResponses(1)
  219. retrievedQuestions should not be(null)
  220. retrievedQuestions.isEmpty should be(false)
  221. retrievedQuestions.size should be(1)
  222. }
  223. test("get a limit of 0 of questions without a response") {
  224. val questions = List(uniqueQuestion(), uniqueQuestion(), uniqueQuestion())
  225. val expectedList = questions.map(repository.save(_))
  226. val retrievedQuestions = repository.findQuestionsWithoutResponses(0)
  227. retrievedQuestions should not be(null)
  228. retrievedQuestions.isEmpty should be(false)
  229. retrievedQuestions.corresponds(expectedList){_ == _} should be(true)
  230. }
  231. }