From 8652e738d53d49c5d6c95b6cd926b0415681543d Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 12 Jul 2023 17:21:46 +0200 Subject: [PATCH] Add boolean return value for UpdateQuiz and fix tests --- store/file/file.go | 4 ++-- store/memory/memory.go | 13 +++++++------ store/memory/memory_test.go | 14 +++++++++++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/store/file/file.go b/store/file/file.go index c1eeff2..ea3bb5c 100644 --- a/store/file/file.go +++ b/store/file/file.go @@ -114,12 +114,12 @@ func (s *FileProboCollectorStore) CreateQuiz(r *client.CreateUpdateQuizRequest) } func (s *FileProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) { - quiz, err := s.memoryStore.UpdateQuiz(r, id) + quiz, updated, err := s.memoryStore.UpdateQuiz(r, id) if err != nil { return nil, err } - if id != quiz.ID { // Update and re-index only if quiz hash is changed + if updated { // Update and re-index only if quiz hash is changed err = s.createOrUpdateMarkdownFile(quiz) if err != nil { return nil, err diff --git a/store/memory/memory.go b/store/memory/memory.go index 59b8a04..956a16e 100644 --- a/store/memory/memory.go +++ b/store/memory/memory.go @@ -137,20 +137,20 @@ func (s *MemoryProboCollectorStore) CalculateQuizHash(quiz *client.Quiz) string return hashes[len(hashes)-1] } -func (s *MemoryProboCollectorStore) createOrUpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) { +func (s *MemoryProboCollectorStore) createOrUpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, bool, error) { hashes := s.hasher.QuizHashes(r.Quiz) quizHash := hashes[len(hashes)-1] quiz := s.getQuizFromHash(quizHash) if quiz != nil { // Quiz is already present in the store - return quiz, nil + return quiz, false, nil } if id != "" { quiz = s.getQuizFromID(id) if quiz == nil { // Quiz is not present in the store - return nil, fmt.Errorf("Quiz ID %v doesn't exist in the store!", id) + return nil, false, fmt.Errorf("Quiz ID %v doesn't exist in the store!", id) } } else { id = uuid.New().String() @@ -183,13 +183,14 @@ func (s *MemoryProboCollectorStore) createOrUpdateQuiz(r *client.CreateUpdateQui quiz.Answers = append(quiz.Answers, a) } - return s.createQuizFromHash(id, quizHash, quiz), nil + return s.createQuizFromHash(id, quizHash, quiz), true, nil } func (s *MemoryProboCollectorStore) CreateQuiz(r *client.CreateUpdateQuizRequest) (*models.Quiz, error) { - return s.createOrUpdateQuiz(r, "") + q, _, err := s.createOrUpdateQuiz(r, "") + return q, err } -func (s *MemoryProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) { +func (s *MemoryProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, bool, error) { return s.createOrUpdateQuiz(r, id) } diff --git a/store/memory/memory_test.go b/store/memory/memory_test.go index cea9959..c166b57 100644 --- a/store/memory/memory_test.go +++ b/store/memory/memory_test.go @@ -1,6 +1,7 @@ package memory import ( + "fmt" "reflect" "testing" @@ -61,7 +62,9 @@ func (t *testSuite) TestUpdateQuiz() { }, }) - updatedQuiz, _ := store.CreateQuiz( + createdQuizHash := quiz.Hash + + updatedQuiz, updated, err := store.UpdateQuiz( &client.CreateUpdateQuizRequest{ Quiz: &client.Quiz{ Question: &client.Question{Text: "Updated question text."}, @@ -72,7 +75,12 @@ func (t *testSuite) TestUpdateQuiz() { {Text: "Answer 4", Correct: false}, }, }, - }) + }, quiz.ID) - t.True(quiz.Hash != updatedQuiz.Hash, "The two hashes should not be equal.") + t.Nil(err, fmt.Sprintf("The update returned an error: %v", err)) + + if !t.Failed() { + t.True(updated) + t.True(createdQuizHash != updatedQuiz.Hash, "The two hashes should not be equal.") + } }