Add boolean return value for UpdateQuiz and fix tests

This commit is contained in:
andrea 2023-07-12 17:21:46 +02:00
parent f21e77c682
commit 8652e738d5
3 changed files with 20 additions and 11 deletions

View file

@ -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

View file

@ -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)
}

View file

@ -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.")
}
}