PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/questions/src/test/scala/com/cyrusinnovation/inquisition/tags/mongodb/MongoTagsRepositoryTest.scala

https://github.com/cyrusinnovation/inquisition
Scala | 176 lines | 133 code | 43 blank | 0 comment | 1 complexity | 0d1ace803c60bb8219670afbae97144b MD5 | raw file
  1. package com.cyrusinnovation.inquisition.tags.mongodb
  2. import org.scalatest.matchers.ShouldMatchers
  3. import com.mongodb.casbah.MongoConnection
  4. import org.scalatest.{BeforeAndAfterEach, FunSuite}
  5. import com.cyrusinnovation.inquisition.questions.Question
  6. import com.cyrusinnovation.inquisition.questions.mongodb.MongoQuestionRepository
  7. import org.junit.runner.RunWith
  8. import org.scalatest.junit.JUnitRunner
  9. import com.mongodb.casbah.commons.MongoDBObject
  10. @RunWith(classOf[JUnitRunner])
  11. class MongoTagsRepositoryTest extends FunSuite with ShouldMatchers with BeforeAndAfterEach {
  12. val con = MongoConnection()
  13. val TestDbName = "test_inquisition"
  14. val db = con(TestDbName)
  15. val questionRepository = new MongoQuestionRepository(db)
  16. val tagRepository = new MongoTagRepository(db)
  17. override def beforeEach() {
  18. db("tags").remove(MongoDBObject())
  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("find all unqiue question tags") {
  25. questionRepository.save(uniqueQuestion().copy(tags = List("java", "spring")));
  26. questionRepository.save(uniqueQuestion().copy(tags = List("scala", "spring")));
  27. questionRepository.findQuestionCount() should equal(2)
  28. val uniqueTags = tagRepository.findUniqueTagNamesOrderedByTagName()
  29. uniqueTags should equal(List("java", "scala", "spring"))
  30. }
  31. test("find most popular tags") {
  32. val correctQuestion = questionRepository.save(uniqueQuestion().copy(tags = List("java", "spring")));
  33. questionRepository.save(uniqueQuestion().copy(tags = List("scala", "spring")));
  34. val tags = tagRepository.findMostPopularTags(1)
  35. tags should have size (1)
  36. tags.head should equal(("spring" -> 2))
  37. }
  38. test("find most popular tags in the right order") {
  39. questionRepository.save(uniqueQuestion().copy(tags = List("java", "spring")));
  40. questionRepository.save(uniqueQuestion().copy(tags = List("scala", "spring")));
  41. questionRepository.save(uniqueQuestion().copy(tags = List("java", "spring")));
  42. questionRepository.save(uniqueQuestion().copy(tags = List("java", "spring")));
  43. val tags = tagRepository.findMostPopularTags(3)
  44. tags should have size (3)
  45. tags.head should equal(("spring" -> 4))
  46. tags.get("java").get should equal(3.0)
  47. tags.get("scala").get should equal(1.0)
  48. }
  49. test("find most popular tags with no tags in the system") {
  50. val tags = tagRepository.findMostPopularTags(1)
  51. tags should have size (0)
  52. }
  53. test("find questions with given tag") {
  54. val correctQuestion = questionRepository.save(uniqueQuestion().copy(tags = List("java", "spring")));
  55. questionRepository.save(uniqueQuestion().copy(tags = List("scala", "spring")));
  56. questionRepository.findQuestionCount() should equal(2)
  57. val question = tagRepository.findQuestionsByTag("java")
  58. question.length should be(1)
  59. question.head should equal(correctQuestion)
  60. }
  61. test("can do simple tag search") {
  62. questionRepository.save(uniqueQuestion().copy(tags = List("java")));
  63. questionRepository.save(uniqueQuestion().copy(tags = List("scala")));
  64. questionRepository.findQuestionCount() should equal(2)
  65. val results = tagRepository.findQuestionsByTags(List("java", "scala"))
  66. results.length should be(2)
  67. }
  68. test("simple tag search only returns items with matching tags") {
  69. val answer = questionRepository.save(uniqueQuestion().copy(tags = List("java")));
  70. questionRepository.save(uniqueQuestion().copy(tags = List("scala")));
  71. questionRepository.findQuestionCount() should equal(2)
  72. val results = tagRepository.findQuestionsByTags(List("java"))
  73. results.length should be(1)
  74. results.head should equal(answer)
  75. }
  76. test("Can find tags matching a given prefix") {
  77. val question = uniqueQuestion().copy(tags = List("abc", "def", "ghi", "atag"))
  78. questionRepository.save(question)
  79. tagRepository.findMostPopularTags(1)
  80. val tags = tagRepository.findTagsByPrefix("a", 10)
  81. tags should have length (2)
  82. tags.head should equal("abc")
  83. tags.tail.head should equal("atag")
  84. }
  85. test("Can find tags matching a given prefix with a limit") {
  86. val question = uniqueQuestion().copy(tags = List("abc", "def", "ghi", "atag"))
  87. questionRepository.save(question)
  88. tagRepository.findMostPopularTags(1)
  89. val tags = tagRepository.findTagsByPrefix("a", 1)
  90. tags should have length (1)
  91. tags.head should equal("abc")
  92. tags should not contain ("atag")
  93. }
  94. test("Can remove a tag from a given question") {
  95. val tags = List("abc", "def", "ghi")
  96. val question = uniqueQuestion().copy(tags = "atag" :: tags)
  97. val savedQuestion = questionRepository.save(question)
  98. tagRepository.deleteTagFromQuestion(savedQuestion.id.get, "atag")
  99. val retrievedQuestion = questionRepository.findById(savedQuestion.id.get)
  100. retrievedQuestion.get.tags should equal(tags)
  101. }
  102. test("No exception is thrown if a non existant tag is removed from a question") {
  103. val tags = List("abc", "def", "ghi")
  104. val question = uniqueQuestion().copy(tags = tags)
  105. val savedQuestion = questionRepository.save(question)
  106. tagRepository.deleteTagFromQuestion(savedQuestion.id.get, "aNonExistant")
  107. val retrievedQuestion = questionRepository.findById(savedQuestion.id.get)
  108. retrievedQuestion.get.tags should equal(tags)
  109. }
  110. test("Can add a tag to a given question") {
  111. val question = uniqueQuestion()
  112. val savedQuestion = questionRepository.save(question)
  113. tagRepository.addTagToQuestion(savedQuestion.id.get, "atag")
  114. val retrievedQuestion = questionRepository.findById(savedQuestion.id.get)
  115. retrievedQuestion.get.tags should contain("atag")
  116. }
  117. test("No exception is thrown if an existant tag is added to a question") {
  118. val tags = List("abc", "def", "ghi")
  119. val question = uniqueQuestion().copy(tags = tags)
  120. val savedQuestion = questionRepository.save(question)
  121. tagRepository.addTagToQuestion(savedQuestion.id.get, "abc")
  122. val retrievedQuestion = questionRepository.findById(savedQuestion.id.get)
  123. retrievedQuestion.get.tags should equal(tags)
  124. }
  125. test("No duplicate tag is created if an existant tag is added to a question") {
  126. val tags = List("abc", "def", "ghi")
  127. val question = uniqueQuestion().copy(tags = tags)
  128. val savedQuestion = questionRepository.save(question)
  129. tagRepository.addTagToQuestion(savedQuestion.id.get, "abc")
  130. val retrievedQuestion = questionRepository.findById(savedQuestion.id.get)
  131. retrievedQuestion.get.tags.count(x => x == "abc") should be(1)
  132. }
  133. }