memory_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package memory
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. "git.andreafazzi.eu/andrea/probo/client"
  7. "git.andreafazzi.eu/andrea/probo/hasher/sha256"
  8. "github.com/remogatto/prettytest"
  9. )
  10. type testSuite struct {
  11. prettytest.Suite
  12. }
  13. func TestRunner(t *testing.T) {
  14. prettytest.Run(
  15. t,
  16. new(testSuite),
  17. new(collectionTestSuite),
  18. )
  19. }
  20. func (t *testSuite) TestReadQuizByHash() {
  21. store := NewMemoryProboCollectorStore(
  22. sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
  23. )
  24. quiz, _ := store.CreateQuiz(
  25. &client.CreateUpdateQuizRequest{
  26. Quiz: &client.Quiz{
  27. Question: &client.Question{Text: "Newly created question text."},
  28. Answers: []*client.Answer{
  29. {Text: "Answer 1", Correct: true},
  30. {Text: "Answer 2", Correct: false},
  31. {Text: "Answer 3", Correct: false},
  32. {Text: "Answer 4", Correct: false},
  33. },
  34. },
  35. })
  36. quizFromMemory, err := store.ReadQuizByHash(quiz.Hash)
  37. t.Nil(err, "Quiz should be found in the store")
  38. if !t.Failed() {
  39. t.True(reflect.DeepEqual(quizFromMemory, quiz), "Quiz should be equal")
  40. }
  41. }
  42. func (t *testSuite) TestParseTextForTags() {
  43. store := NewMemoryProboCollectorStore(
  44. sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
  45. )
  46. quiz, _ := store.CreateQuiz(
  47. &client.CreateUpdateQuizRequest{
  48. Quiz: &client.Quiz{
  49. Question: &client.Question{Text: "Newly created question text with #tag1."},
  50. Answers: []*client.Answer{
  51. {Text: "Answer 1", Correct: true},
  52. {Text: "Answer 2 with #tag2", Correct: false},
  53. {Text: "Answer 3", Correct: false},
  54. {Text: "Answer 4", Correct: false},
  55. },
  56. },
  57. })
  58. quizFromMemory, err := store.ReadQuizByHash(quiz.Hash)
  59. t.Nil(err, "Quiz should be found in the store")
  60. if !t.Failed() {
  61. t.True(len(quizFromMemory.Tags) == 2, "Two tags should be present.")
  62. t.Equal("#tag1", quizFromMemory.Tags[0].Name)
  63. t.Equal("#tag2", quizFromMemory.Tags[1].Name)
  64. }
  65. }
  66. func (t *testSuite) TestUpdateQuiz() {
  67. store := NewMemoryProboCollectorStore(
  68. sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
  69. )
  70. quiz, _ := store.CreateQuiz(
  71. &client.CreateUpdateQuizRequest{
  72. Quiz: &client.Quiz{
  73. Question: &client.Question{Text: "Newly created question text."},
  74. Answers: []*client.Answer{
  75. {Text: "Answer 1", Correct: true},
  76. {Text: "Answer 2", Correct: false},
  77. {Text: "Answer 3", Correct: false},
  78. {Text: "Answer 4", Correct: false},
  79. },
  80. },
  81. })
  82. createdQuizHash := quiz.Hash
  83. updatedQuiz, updated, err := store.UpdateQuiz(
  84. &client.CreateUpdateQuizRequest{
  85. Quiz: &client.Quiz{
  86. Question: &client.Question{Text: "Updated question text."},
  87. Answers: []*client.Answer{
  88. {Text: "Answer 1", Correct: true},
  89. {Text: "Updated Answer 2", Correct: false},
  90. {Text: "Answer 3", Correct: false},
  91. {Text: "Answer 4", Correct: false},
  92. },
  93. },
  94. }, quiz.ID)
  95. t.Nil(err, fmt.Sprintf("The update returned an error: %v", err))
  96. if !t.Failed() {
  97. t.True(updated)
  98. t.True(createdQuizHash != updatedQuiz.Hash, "The two hashes should not be equal.")
  99. t.Equal(4, len(updatedQuiz.Answers))
  100. t.Equal("Updated question text.", updatedQuiz.Question.Text)
  101. t.Equal("Updated Answer 2", updatedQuiz.Answers[1].Text)
  102. }
  103. }
  104. func (t *testSuite) TestDeleteQuiz() {
  105. store := NewMemoryProboCollectorStore(
  106. sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
  107. )
  108. quiz, _ := store.CreateQuiz(
  109. &client.CreateUpdateQuizRequest{
  110. Quiz: &client.Quiz{
  111. Question: &client.Question{Text: "This test should be removed."},
  112. Answers: []*client.Answer{
  113. {Text: "Answer 1", Correct: true},
  114. {Text: "Answer 2", Correct: false},
  115. {Text: "Answer 3", Correct: false},
  116. {Text: "Answer 4", Correct: false},
  117. },
  118. },
  119. })
  120. deletedQuiz, err := store.DeleteQuiz(&client.DeleteQuizRequest{ID: quiz.ID})
  121. t.Equal(quiz.ID, deletedQuiz.ID, "Returned deleted quiz ID should be equal to the request")
  122. t.Nil(err, fmt.Sprintf("The update returned an error: %v", err))
  123. _, err = store.ReadQuizByHash(deletedQuiz.Hash)
  124. t.True(err != nil, "Reading a non existent quiz should return an error")
  125. }