package file import ( "fmt" "os" "path/filepath" "git.andreafazzi.eu/andrea/probo/models" "git.andreafazzi.eu/andrea/probo/store" "github.com/remogatto/prettytest" ) type quizTestSuite struct { prettytest.Suite } func (t *quizTestSuite) TestReadAll() { store, err := NewDefaultQuizFileStore() 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), ), ) files, _ := os.ReadDir(GetDefaultQuizzesDir()) t.Equal(5, len(files)) _, err = removeQuizHeader(filepath.Join(store.Dir, "quiz_5.md")) } } 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 }