package store import ( "reflect" "git.andreafazzi.eu/andrea/probo/pkg/models" "github.com/remogatto/prettytest" ) type quizTestSuite struct { prettytest.Suite } func (t *quizTestSuite) TestCreateQuiz() { store := NewQuizStore() quiz, err := store.Create( &models.Quiz{ Question: &models.Question{Text: "Newly created question text with #tag."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }) t.Nil(err, "Quiz should be created without error") if !t.Failed() { quizFromMemory, err := store.Read(quiz.GetID()) t.Nil(err, "Quiz should be found in the store") if !t.Failed() { t.True(quizFromMemory.GetID() != "") t.Equal(len(quizFromMemory.Tags), 1) t.Equal(len(store.questions.ids), 1) t.Equal(len(store.answers.ids), 4) if !t.Failed() { t.True(reflect.DeepEqual(quizFromMemory, quiz), "Quiz should be equal") } } } } func (t *quizTestSuite) TestDuplicateQuiz() { store := NewQuizStore() _, err := store.Create( &models.Quiz{ Question: &models.Question{Text: "Newly created question text."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }) t.Nil(err, "Quiz 1 should be created without error") _, err = store.Create( &models.Quiz{ Question: &models.Question{Text: "Newly created question text."}, Answers: []*models.Answer{ {Text: "Answer 2"}, {Text: "Answer 1"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 1, }) t.Not(t.Nil(err), "Quiz 2 should not be created") } func (t *quizTestSuite) TestReadQuiz() { store := NewQuizStore() quiz, err := store.Create( &models.Quiz{ Question: &models.Question{Text: "Newly created question text."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }) t.Nil(err, "Quiz should be created without error") if !t.Failed() { quizzes := store.ReadAll() t.Equal(1, len(quizzes)) storedQuiz, err := store.Read(quiz.GetID()) t.Nil(err, "Quiz should be read without error") if !t.Failed() { t.Equal(quiz.ID, storedQuiz.ID) } } } func (t *quizTestSuite) TestUpdateQuiz() { store := NewQuizStore() quiz, err := store.Create( &models.Quiz{ Question: &models.Question{Text: "Newly created question text."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }) t.Nil(err, "Quiz should be created without error") if !t.Failed() { updatedQuiz, err := store.Update( &models.Quiz{ Question: &models.Question{Text: "Updated question text with #tag."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }, quiz.GetID()) updatedQuizFromMemory, err := store.Read(quiz.GetID()) t.Nil(err, "Quiz should be found in the store") if !t.Failed() { t.Equal(updatedQuizFromMemory.GetID(), updatedQuiz.GetID()) t.Equal(updatedQuizFromMemory.Question.Text, updatedQuiz.Question.Text) t.Equal(len(updatedQuizFromMemory.Tags), 1) } } } func (t *quizTestSuite) TestDeleteQuiz() { store := NewQuizStore() _, err := store.Create( &models.Quiz{ Question: &models.Question{Text: "Newly created question text."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }) t.Nil(err, "Quiz should be created without error") if !t.Failed() { quiz_2, err := store.Create( &models.Quiz{ Question: &models.Question{Text: "Newly created question text 2."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }) t.Nil(err, "Quiz should be created without error") if !t.Failed() { _, err := store.Delete(quiz_2.GetID()) t.Nil(err, "Quiz should be deleted without error") t.Equal(1, len(store.ids)) t.Equal(1, len(store.hashes)) } } } func (t *quizTestSuite) TestFilter() { store := NewQuizStore() quiz_1, _ := store.Create( &models.Quiz{ Question: &models.Question{Text: "Question text with #tag1."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2 with #tag2"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }) quiz_2, _ := store.Create( &models.Quiz{ Question: &models.Question{Text: "Question text with #tag3."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2 with #tag4"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }) quizzes := store.Filter([]*models.Quiz{quiz_1, quiz_2}, func(q *models.Quiz) bool { for _, t := range q.Tags { if t.Name == "#tag1" { return true } } return false }) t.Equal(1, len(quizzes)) } func (t *quizTestSuite) TestParseTextForTags() { store := NewQuizStore() quiz, err := store.Create( &models.Quiz{ Question: &models.Question{Text: "Question text with #tag1."}, Answers: []*models.Answer{ {Text: "Answer 1"}, {Text: "Answer 2 with #tag2"}, {Text: "Answer 3"}, {Text: "Answer 4"}, }, CorrectPos: 0, }) t.Nil(err, "Quiz should be created without errors.") if !t.Failed() { storedQuiz, err := store.Read(quiz.GetID()) t.Nil(err, "Quiz should be found in the store.") t.Equal(2, len(storedQuiz.Tags)) if !t.Failed() { t.Equal("#tag1", storedQuiz.Tags[0].Name) t.Equal("#tag2", storedQuiz.Tags[1].Name) } } }