probo/store/file/collection_test.go

174 lines
4.5 KiB
Go
Raw Normal View History

2023-10-07 11:43:12 +02:00
package file
import (
2023-10-16 12:48:19 +02:00
"encoding/json"
2023-10-07 11:43:12 +02:00
"fmt"
"os"
2023-10-16 12:48:19 +02:00
"reflect"
2023-10-07 11:43:12 +02:00
"git.andreafazzi.eu/andrea/probo/client"
2023-10-16 12:48:19 +02:00
"git.andreafazzi.eu/andrea/probo/models"
2023-10-07 11:43:12 +02:00
"github.com/remogatto/prettytest"
)
type collectionTestSuite struct {
prettytest.Suite
}
func (t *collectionTestSuite) TestCreateCollection() {
store, err := NewFileProboCollectorStore(testdataDir)
t.Nil(err, fmt.Sprintf("A file store should be initialized without problems but an error occurred: %v", err))
if !t.Failed() {
quiz_1, err := createQuizOnDisk(store, &client.CreateUpdateQuizRequest{
Quiz: &client.Quiz{
Question: &client.Question{Text: "Question text with #tag1."},
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, "The quiz to be updated should be created without issue")
path_1, _ := store.GetQuizPath(quiz_1)
if !t.Failed() {
quiz_2, err := createQuizOnDisk(store, &client.CreateUpdateQuizRequest{
Quiz: &client.Quiz{
Question: &client.Question{Text: "Another question text with #tag1."},
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, "The quiz to be updated should be created without issue")
path_2, _ := store.GetQuizPath(quiz_2)
if !t.Failed() {
quiz_3, err := createQuizOnDisk(store, &client.CreateUpdateQuizRequest{
Quiz: &client.Quiz{
Question: &client.Question{Text: "Question text without tags."},
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, "The quiz to be updated should be created without issue")
path_3, _ := store.GetQuizPath(quiz_3)
if !t.Failed() {
collection, err := store.CreateCollection(
&client.CreateUpdateCollectionRequest{
Collection: &client.Collection{
Name: "MyCollection",
Query: "#tag1",
},
})
t.Nil(err, "Creating a collection should not return an error")
collectionPath, _ := store.GetCollectionPath(collection)
if !t.Failed() {
2023-10-28 20:50:06 +02:00
t.Equal(2, len(collection.Quizzes))
2023-10-07 11:43:12 +02:00
os.Remove(path_1)
os.Remove(path_2)
os.Remove(path_3)
os.Remove(collectionPath)
}
}
}
}
}
}
2023-10-16 12:48:19 +02:00
func (t *collectionTestSuite) TestUpdateCollection() {
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() {
collection, err := createCollectionOnDisk(store, &client.CreateUpdateCollectionRequest{
Collection: &client.Collection{
Name: "Collection name",
Query: "#tag1",
},
})
t.Nil(err, "The collection to be updated should be created without issue")
if !t.Failed() {
clientCollection := &client.Collection{
Name: "Updated collection name",
Query: "#tag2",
}
updatedCollection, err := store.UpdateCollection(
&client.CreateUpdateCollectionRequest{
Collection: clientCollection,
}, collection.ID)
t.Nil(err, fmt.Sprintf("Collection should be updated without errors: %v", err))
t.Equal("#tag2", updatedCollection.Query)
if !t.Failed() {
path, err := store.GetCollectionPath(updatedCollection)
if !t.Failed() {
t.Nil(err, "GetPath should not raise an error.")
if !t.Failed() {
collectionFromDisk, err := readCollectionFromDisk(path)
t.Nil(err, fmt.Sprintf("Collection should be read from disk without errors but an issue was reported: %v", err))
if !t.Failed() {
t.True(reflect.DeepEqual(clientCollection, collectionFromDisk), "Collection should be updated.")
err := os.Remove(path)
t.Nil(err, "Stat should not return an error")
}
}
}
}
}
}
}
func createCollectionOnDisk(store *FileProboCollectorStore, req *client.CreateUpdateCollectionRequest) (*models.Collection, error) {
return store.CreateCollection(req)
}
func readCollectionFromDisk(path string) (*client.Collection, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
collection := new(client.Collection)
err = json.Unmarshal(content, &collection)
if err != nil {
return nil, err
}
return collection, nil
}