package file import ( "fmt" "os" "path/filepath" "git.andreafazzi.eu/andrea/probo/models" "github.com/remogatto/prettytest" ) type quizTestSuite struct { prettytest.Suite } func (t *quizTestSuite) BeforeAll() { BaseDir = "testdata" } func (t *quizTestSuite) TestReadAllQuizzes() { store, err := NewQuizFileStore() 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), ), ) } } func (t *quizTestSuite) TestCreateQuiz() { store, err := NewQuizFileStore() 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) TestDeleteQuiz() { store, err := NewQuizFileStore() 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) TestUpdateQuiz() { store, err := NewQuizFileStore() 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 := NewQuizFileStore() 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 (t *quizTestSuite) TestReadMetaHeaderFromFile() { // store, err := NewFileProboCollectorStore(testdataDir) // t.True(err == nil, fmt.Sprintf("A file store should be initialized without problems but an error occurred: %v", err)) // meta, err := store.ReadMetaHeaderFromFile("quiz_4.md") // t.True(err == nil, fmt.Sprintf("An error occurred: %v", err)) // if !t.Failed() { // t.True(meta.ID != "") // t.True(meta.CreatedAt.String() != "") // } // } // func (t *quizTestSuite) TestWriteMetaHeaderToFile() { // store, err := NewFileProboCollectorStore(testdataDir) // t.True(err == nil, fmt.Sprintf("A file store should be initialized without problems but an error occurred: %v", err)) // if !t.Failed() { // meta, err := store.ReadMetaHeaderFromFile("quiz_5.md") // t.True(err == nil, fmt.Sprintf("Reading the header returns the following error: %v", err)) // if !t.Failed() { // t.True(meta != nil, "Meta header should not be nil") // if !t.Failed() { // t.True(meta.ID != "", "ID should not be empty") // if !t.Failed() { // _, err = store.removeMetaFromFile("quiz_5.md") // t.True(err == nil) // } // } // } // } // } // func createQuizOnDisk(store *FileProboCollectorStore, req *client.CreateUpdateQuizRequest) (*models.Quiz, error) { // return store.CreateQuiz(req) // } func readQuizFromDisk(path string) (*models.Quiz, *models.Meta, error) { content, err := os.ReadFile(path) if err != nil { return nil, nil, err } return models.MarkdownToQuiz(string(content)) }