Add boolean return value for UpdateQuiz and fix tests
This commit is contained in:
parent
f21e77c682
commit
8652e738d5
3 changed files with 20 additions and 11 deletions
|
@ -114,12 +114,12 @@ func (s *FileProboCollectorStore) CreateQuiz(r *client.CreateUpdateQuizRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FileProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
err = s.createOrUpdateMarkdownFile(quiz)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -137,20 +137,20 @@ func (s *MemoryProboCollectorStore) CalculateQuizHash(quiz *client.Quiz) string
|
||||||
return hashes[len(hashes)-1]
|
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)
|
hashes := s.hasher.QuizHashes(r.Quiz)
|
||||||
quizHash := hashes[len(hashes)-1]
|
quizHash := hashes[len(hashes)-1]
|
||||||
|
|
||||||
quiz := s.getQuizFromHash(quizHash)
|
quiz := s.getQuizFromHash(quizHash)
|
||||||
if quiz != nil { // Quiz is already present in the store
|
if quiz != nil { // Quiz is already present in the store
|
||||||
return quiz, nil
|
return quiz, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if id != "" {
|
if id != "" {
|
||||||
quiz = s.getQuizFromID(id)
|
quiz = s.getQuizFromID(id)
|
||||||
if quiz == nil { // Quiz is not present in the store
|
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 {
|
} else {
|
||||||
id = uuid.New().String()
|
id = uuid.New().String()
|
||||||
|
@ -183,13 +183,14 @@ func (s *MemoryProboCollectorStore) createOrUpdateQuiz(r *client.CreateUpdateQui
|
||||||
quiz.Answers = append(quiz.Answers, a)
|
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) {
|
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)
|
return s.createOrUpdateQuiz(r, id)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package memory
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -61,7 +62,9 @@ func (t *testSuite) TestUpdateQuiz() {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
updatedQuiz, _ := store.CreateQuiz(
|
createdQuizHash := quiz.Hash
|
||||||
|
|
||||||
|
updatedQuiz, updated, err := store.UpdateQuiz(
|
||||||
&client.CreateUpdateQuizRequest{
|
&client.CreateUpdateQuizRequest{
|
||||||
Quiz: &client.Quiz{
|
Quiz: &client.Quiz{
|
||||||
Question: &client.Question{Text: "Updated question text."},
|
Question: &client.Question{Text: "Updated question text."},
|
||||||
|
@ -72,7 +75,12 @@ func (t *testSuite) TestUpdateQuiz() {
|
||||||
{Text: "Answer 4", Correct: false},
|
{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.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue