package file import ( "fmt" "os" "path/filepath" "reflect" "testing" "git.andreafazzi.eu/andrea/probo/client" "git.andreafazzi.eu/andrea/probo/models" "github.com/remogatto/prettytest" ) type testSuite struct { prettytest.Suite } func TestRunner(t *testing.T) { prettytest.Run( t, new(testSuite), ) } func (t *testSuite) TestQuizFromMarkdown() { markdown := `Question text (1). Question text (2). Question text (3). * Answer 1 * Answer 2 * Answer 3 * Answer 4` expectedQuiz := &client.Quiz{ Question: &client.Question{Text: "Question text (1).\n\nQuestion text (2).\n\nQuestion text (3)."}, Answers: []*client.Answer{ {Text: "Answer 1", Correct: true}, {Text: "Answer 2", Correct: false}, {Text: "Answer 3", Correct: false}, {Text: "Answer 4", Correct: false}, }, } quiz, err := QuizFromMarkdown(markdown) t.Nil(err, fmt.Sprintf("Quiz should be parsed without errors: %v", err)) if !t.Failed() { t.True(reflect.DeepEqual(quiz, expectedQuiz), fmt.Sprintf("Expected %+v, got %+v", expectedQuiz, quiz)) } } func (t *testSuite) TestReadAllQuizzes() { store, err := NewFileProboCollectorStore("./test/quizzes") t.True(err == nil, fmt.Sprintf("A file store should be initialized without problems but an error occurred: %v", err)) if !t.Failed() { result, err := store.ReadAllQuizzes() t.True(err == nil, fmt.Sprintf("Quizzes should be returned without errors: %v", err)) if !t.Failed() { t.Equal( 2, len(result), fmt.Sprintf("The store contains 3 files but only 2 should be parsed (duplicated quiz). Total of parsed quizzes are instead %v", len(result)), ) t.Equal("Question text 1.", result[0].Question.Text) } } } func (t *testSuite) TestCreateQuiz() { dirname := "./test/quizzes" store, err := NewFileProboCollectorStore(dirname) t.True(err == nil, fmt.Sprintf("A file store should be initialized without problems but an error occurred: %v", err)) _, err = 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}, }, }, }) t.Nil(err, fmt.Sprintf("An error was raised when saving the quiz on disk: %v", err)) newFilename := filepath.Join( dirname, "94ed4e9cdf8e0a75a2c5ce925cb791ebc5977ce1801e12059f58ce4d66c0c7f6.md", ) exists, err := os.Stat(newFilename) t.Nil(err, "Stat should not return an error") if !t.Failed() { t.True(exists != nil, "The new quiz file was not created.") err := os.Remove(newFilename) t.Nil(err, "Stat should not return an error") } } func testsAreEqual(got, want []*models.Quiz) bool { return reflect.DeepEqual(got, want) }