2023-07-10 13:23:46 +02:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2023-07-12 17:21:46 +02:00
|
|
|
"fmt"
|
2023-07-10 13:23:46 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/probo/client"
|
|
|
|
"git.andreafazzi.eu/andrea/probo/hasher/sha256"
|
|
|
|
"github.com/remogatto/prettytest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testSuite struct {
|
|
|
|
prettytest.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunner(t *testing.T) {
|
|
|
|
prettytest.Run(
|
|
|
|
t,
|
|
|
|
new(testSuite),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testSuite) TestReadQuizByHash() {
|
|
|
|
store := NewMemoryProboCollectorStore(
|
|
|
|
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
|
|
|
|
)
|
|
|
|
quiz, _ := store.CreateQuiz(
|
|
|
|
&client.CreateUpdateQuizRequest{
|
|
|
|
Quiz: &client.Quiz{
|
|
|
|
Question: &client.Question{Text: "Newly created question text."},
|
|
|
|
Answers: []*client.Answer{
|
|
|
|
{Text: "Answer 1", Correct: true},
|
|
|
|
{Text: "Answer 2", Correct: false},
|
|
|
|
{Text: "Answer 3", Correct: false},
|
|
|
|
{Text: "Answer 4", Correct: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
quizFromMemory, err := store.ReadQuizByHash(quiz.Hash)
|
|
|
|
t.Nil(err, "Quiz should be found in the store")
|
|
|
|
if !t.Failed() {
|
|
|
|
t.True(reflect.DeepEqual(quizFromMemory, quiz), "Quiz should be equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-02 12:55:03 +02:00
|
|
|
func (t *testSuite) TestParseTextForTags() {
|
|
|
|
store := NewMemoryProboCollectorStore(
|
|
|
|
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
|
|
|
|
)
|
|
|
|
quiz, _ := store.CreateQuiz(
|
|
|
|
&client.CreateUpdateQuizRequest{
|
|
|
|
Quiz: &client.Quiz{
|
|
|
|
Question: &client.Question{Text: "Newly created question text with #tag1."},
|
|
|
|
Answers: []*client.Answer{
|
|
|
|
{Text: "Answer 1", Correct: true},
|
|
|
|
{Text: "Answer 2 with #tag2", Correct: false},
|
|
|
|
{Text: "Answer 3", Correct: false},
|
|
|
|
{Text: "Answer 4", Correct: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
quizFromMemory, err := store.ReadQuizByHash(quiz.Hash)
|
|
|
|
t.Nil(err, "Quiz should be found in the store")
|
|
|
|
if !t.Failed() {
|
|
|
|
t.True(len(quizFromMemory.Tags) == 2, "Two tags should be present.")
|
|
|
|
t.Equal("#tag1", quizFromMemory.Tags[0].Name)
|
|
|
|
t.Equal("#tag2", quizFromMemory.Tags[1].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-10 13:23:46 +02:00
|
|
|
func (t *testSuite) TestUpdateQuiz() {
|
|
|
|
store := NewMemoryProboCollectorStore(
|
|
|
|
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
|
|
|
|
)
|
|
|
|
quiz, _ := store.CreateQuiz(
|
|
|
|
&client.CreateUpdateQuizRequest{
|
|
|
|
Quiz: &client.Quiz{
|
|
|
|
Question: &client.Question{Text: "Newly created question text."},
|
|
|
|
Answers: []*client.Answer{
|
|
|
|
{Text: "Answer 1", Correct: true},
|
|
|
|
{Text: "Answer 2", Correct: false},
|
|
|
|
{Text: "Answer 3", Correct: false},
|
|
|
|
{Text: "Answer 4", Correct: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-07-12 17:21:46 +02:00
|
|
|
createdQuizHash := quiz.Hash
|
|
|
|
|
|
|
|
updatedQuiz, updated, err := store.UpdateQuiz(
|
2023-07-10 13:23:46 +02:00
|
|
|
&client.CreateUpdateQuizRequest{
|
|
|
|
Quiz: &client.Quiz{
|
|
|
|
Question: &client.Question{Text: "Updated question text."},
|
|
|
|
Answers: []*client.Answer{
|
|
|
|
{Text: "Answer 1", Correct: true},
|
2023-07-13 06:46:36 +02:00
|
|
|
{Text: "Updated Answer 2", Correct: false},
|
2023-07-10 13:23:46 +02:00
|
|
|
{Text: "Answer 3", Correct: false},
|
|
|
|
{Text: "Answer 4", Correct: false},
|
|
|
|
},
|
|
|
|
},
|
2023-07-12 17:21:46 +02:00
|
|
|
}, quiz.ID)
|
|
|
|
|
|
|
|
t.Nil(err, fmt.Sprintf("The update returned an error: %v", err))
|
2023-07-10 13:23:46 +02:00
|
|
|
|
2023-07-12 17:21:46 +02:00
|
|
|
if !t.Failed() {
|
|
|
|
t.True(updated)
|
|
|
|
t.True(createdQuizHash != updatedQuiz.Hash, "The two hashes should not be equal.")
|
2023-07-13 06:46:36 +02:00
|
|
|
t.Equal(4, len(updatedQuiz.Answers))
|
|
|
|
t.Equal("Updated question text.", updatedQuiz.Question.Text)
|
|
|
|
t.Equal("Updated Answer 2", updatedQuiz.Answers[1].Text)
|
2023-07-12 17:21:46 +02:00
|
|
|
}
|
2023-07-10 13:23:46 +02:00
|
|
|
}
|
2023-09-01 11:48:09 +02:00
|
|
|
|
|
|
|
func (t *testSuite) TestDeleteQuiz() {
|
|
|
|
store := NewMemoryProboCollectorStore(
|
|
|
|
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
|
|
|
|
)
|
|
|
|
quiz, _ := store.CreateQuiz(
|
|
|
|
&client.CreateUpdateQuizRequest{
|
|
|
|
Quiz: &client.Quiz{
|
|
|
|
Question: &client.Question{Text: "This test should be removed."},
|
|
|
|
Answers: []*client.Answer{
|
|
|
|
{Text: "Answer 1", Correct: true},
|
|
|
|
{Text: "Answer 2", Correct: false},
|
|
|
|
{Text: "Answer 3", Correct: false},
|
|
|
|
{Text: "Answer 4", Correct: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
deletedQuiz, err := store.DeleteQuiz(quiz.ID)
|
|
|
|
|
|
|
|
t.Equal(quiz.ID, deletedQuiz.ID, "Returned deleted quiz ID should be equal to the request")
|
|
|
|
t.Nil(err, fmt.Sprintf("The update returned an error: %v", err))
|
|
|
|
|
|
|
|
_, err = store.ReadQuizByHash(quiz.Hash)
|
|
|
|
t.True(err != nil, "Reading a non existent quiz should return an error")
|
|
|
|
}
|