probo/pkg/store/file/quiz_test.go

221 lines
4.7 KiB
Go
Raw Permalink Normal View History

2023-11-13 21:01:12 +01:00
package file
import (
"fmt"
"os"
"path/filepath"
2024-02-06 09:03:57 +01:00
"git.andreafazzi.eu/andrea/probo/pkg/models"
"git.andreafazzi.eu/andrea/probo/pkg/store"
2023-11-13 21:01:12 +01:00
"github.com/remogatto/prettytest"
)
type quizTestSuite struct {
prettytest.Suite
}
2023-11-13 21:04:16 +01:00
func (t *quizTestSuite) TestReadAll() {
2023-11-20 14:14:09 +01:00
store, err := NewDefaultQuizFileStore()
2023-11-13 21:01:12 +01:00
t.Nil(err)
if !t.Failed() {
result := store.ReadAll()
t.Equal(
4,
len(result),
fmt.Sprintf(
"The store contains 5 files but only 4 should be parsed (duplicated quiz). Total of parsed quizzes are instead %v",
len(result),
),
)
2023-11-18 12:01:57 +01:00
2023-11-20 14:14:09 +01:00
files, _ := os.ReadDir(GetDefaultQuizzesDir())
t.Equal(5, len(files))
2023-11-13 21:01:12 +01:00
2023-11-20 14:14:09 +01:00
_, err = removeQuizHeader(filepath.Join(store.Dir, "quiz_5.md"))
2023-11-13 21:01:12 +01:00
}
}
2023-11-21 15:12:13 +01:00
func (t *quizTestSuite) TestCreate() {
store, err := NewQuizFileStore(
&FileStoreConfig[*models.Quiz, *store.QuizStore]{
FilePathConfig: FilePathConfig{GetDefaultQuizzesDir(), "quiz", ".md"},
IndexDirFunc: DefaultQuizIndexDirFunc,
NoIndexOnCreate: true,
},
)
t.Nil(err)
if !t.Failed() {
quiz, err := store.Create(
&models.Quiz{
Question: &models.Question{Text: "Newly created question text with #tag1 #tag2."},
Answers: []*models.Answer{
{Text: "Answer 1"},
{Text: "Answer 2"},
{Text: "Answer 3"},
{Text: "Answer 4"},
},
CorrectPos: 0,
})
t.Nil(err)
t.Equal(2, len(quiz.Tags))
if !t.Failed() {
path := store.GetPath(quiz)
t.True(path != "", "Path should not be empty.")
exists, err := os.Stat(path)
t.Nil(err)
if !t.Failed() {
t.True(exists != nil, "The new quiz file was not created.")
if !t.Failed() {
quizFromDisk, err := readQuizFromDisk(path)
defer os.Remove(path)
quizFromDisk.Correct = quiz.Answers[0]
quizFromDisk.Tags = quiz.Tags
t.Nil(err)
if !t.Failed() {
t.Equal(quizFromDisk.Question.Text, quiz.Question.Text)
for i, a := range quizFromDisk.Answers {
t.Equal(a.Text, quiz.Answers[i].Text)
}
for i, tag := range quizFromDisk.Tags {
t.Equal(tag.Name, quiz.Tags[i].Name)
}
}
}
}
}
}
}
func (t *quizTestSuite) TestDelete() {
store, err := NewQuizFileStore(
&FileStoreConfig[*models.Quiz, *store.QuizStore]{
FilePathConfig: FilePathConfig{GetDefaultQuizzesDir(), "quiz", ".md"},
IndexDirFunc: DefaultQuizIndexDirFunc,
NoIndexOnCreate: true,
},
)
t.Nil(err)
if !t.Failed() {
quiz, err := store.Create(
&models.Quiz{
Question: &models.Question{Text: "This quiz should be deleted."},
Answers: []*models.Answer{
{Text: "Answer 1"},
{Text: "Answer 2"},
{Text: "Answer 3"},
{Text: "Answer 4"},
},
CorrectPos: 0,
})
t.Nil(err)
if !t.Failed() {
path := store.GetPath(quiz)
_, err := store.Delete(quiz.ID)
t.Nil(err, fmt.Sprintf("Quiz should be deleted without errors: %v", err))
if !t.Failed() {
_, err := os.Stat(path)
t.Not(t.Nil(err))
}
}
}
}
func (t *quizTestSuite) TestUpdate() {
store, err := NewQuizFileStore(
&FileStoreConfig[*models.Quiz, *store.QuizStore]{
FilePathConfig: FilePathConfig{GetDefaultQuizzesDir(), "quiz", ".md"},
IndexDirFunc: DefaultQuizIndexDirFunc,
NoIndexOnCreate: true,
},
)
t.Nil(err)
if !t.Failed() {
quiz, err := store.Create(
&models.Quiz{
Question: &models.Question{Text: "Newly created question text with #tag1 #tag2."},
Answers: []*models.Answer{
{Text: "Answer 1"},
{Text: "Answer 2"},
{Text: "Answer 3"},
{Text: "Answer 4"},
},
CorrectPos: 0,
})
t.Nil(err)
_, err = store.Update(&models.Quiz{
Question: &models.Question{Text: "Newly created question text with #tag1 #tag2 #tag3."},
Answers: []*models.Answer{
{Text: "Answer 1"},
{Text: "Answer 2"},
{Text: "Answer 3"},
{Text: "Answer 4"},
},
CorrectPos: 1,
}, quiz.ID)
t.Nil(err)
updatedQuizFromMemory, err := store.Read(quiz.ID)
t.Equal(len(updatedQuizFromMemory.Tags), 3)
t.Equal("Answer 2", updatedQuizFromMemory.Correct.Text)
defer os.Remove(store.GetPath(quiz))
}
}
func (t *quizTestSuite) TestAutowriteHeader() {
store, err := NewDefaultQuizFileStore()
t.Nil(err)
if !t.Failed() {
meta, err := readQuizHeader(filepath.Join(store.Dir, "quiz_5.md"))
t.Nil(err)
if !t.Failed() {
t.Not(t.Nil(meta))
if !t.Failed() {
t.True(meta.ID != "", "ID should not be empty")
if !t.Failed() {
_, err = removeQuizHeader(filepath.Join(store.Dir, "quiz_5.md"))
t.True(err == nil)
}
}
}
}
}
func readQuizFromDisk(path string) (*models.Quiz, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
result := new(models.Quiz)
err = result.Unmarshal(content)
if err != nil {
return nil, err
}
return result, nil
}