From 478d1e8815914e2b91a211b716a0ecf02cf06d77 Mon Sep 17 00:00:00 2001 From: andrea Date: Mon, 16 Oct 2023 12:48:19 +0200 Subject: [PATCH] First implementation of Collections --- store/file/collection_test.go | 75 +++++++++++++++++++ store/file/file.go | 18 +++++ ..._0386ac85-0701-48e7-a5a7-bf9713f63dac.json | 1 + ..._b30c4392-52f3-46c5-8865-319ae7d1fbe0.json | 1 + store/file/testdata/quizzes/quiz_5.md | 4 +- store/memory/memory.go | 8 ++ 6 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 store/file/testdata/collections/collection_0386ac85-0701-48e7-a5a7-bf9713f63dac.json create mode 100644 store/file/testdata/collections/collection_b30c4392-52f3-46c5-8865-319ae7d1fbe0.json diff --git a/store/file/collection_test.go b/store/file/collection_test.go index 472e522..3e02678 100644 --- a/store/file/collection_test.go +++ b/store/file/collection_test.go @@ -1,10 +1,13 @@ package file import ( + "encoding/json" "fmt" "os" + "reflect" "git.andreafazzi.eu/andrea/probo/client" + "git.andreafazzi.eu/andrea/probo/models" "github.com/remogatto/prettytest" ) @@ -96,3 +99,75 @@ func (t *collectionTestSuite) TestCreateCollection() { } } } + +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 +} diff --git a/store/file/file.go b/store/file/file.go index 28a45b4..fbeb23f 100644 --- a/store/file/file.go +++ b/store/file/file.go @@ -391,6 +391,10 @@ func (s *FileProboCollectorStore) WriteMetaHeaderToFile(filename string, meta *m return meta, nil } +func (s *FileProboCollectorStore) ReadAllCollections() ([]*models.Collection, error) { + return s.memoryStore.ReadAllCollections() +} + func (s *FileProboCollectorStore) CreateCollection(r *client.CreateUpdateCollectionRequest) (*models.Collection, error) { collection, err := s.memoryStore.CreateCollection(r) if err != nil { @@ -405,6 +409,20 @@ func (s *FileProboCollectorStore) CreateCollection(r *client.CreateUpdateCollect return s.memoryStore.ReadCollectionByID(collection.ID) } +func (s *FileProboCollectorStore) UpdateCollection(r *client.CreateUpdateCollectionRequest, id string) (*models.Collection, error) { + collection, _, err := s.memoryStore.UpdateCollection(r, id) + if err != nil { + return nil, err + } + + err = s.createOrUpdateCollectionFile(collection) + if err != nil { + return nil, err + } + + return s.memoryStore.ReadCollectionByID(collection.ID) +} + func (s *FileProboCollectorStore) removeMetaFromFile(filename string) (*models.Meta, error) { file, err := os.Open(path.Join(s.quizzesDir, filename)) if err != nil { diff --git a/store/file/testdata/collections/collection_0386ac85-0701-48e7-a5a7-bf9713f63dac.json b/store/file/testdata/collections/collection_0386ac85-0701-48e7-a5a7-bf9713f63dac.json new file mode 100644 index 0000000..f51b331 --- /dev/null +++ b/store/file/testdata/collections/collection_0386ac85-0701-48e7-a5a7-bf9713f63dac.json @@ -0,0 +1 @@ +{"id":"0386ac85-0701-48e7-a5a7-bf9713f63dac","name":"Updated collection name","query":"#tag2","ids":[]} \ No newline at end of file diff --git a/store/file/testdata/collections/collection_b30c4392-52f3-46c5-8865-319ae7d1fbe0.json b/store/file/testdata/collections/collection_b30c4392-52f3-46c5-8865-319ae7d1fbe0.json new file mode 100644 index 0000000..dc0c2b5 --- /dev/null +++ b/store/file/testdata/collections/collection_b30c4392-52f3-46c5-8865-319ae7d1fbe0.json @@ -0,0 +1 @@ +{"id":"b30c4392-52f3-46c5-8865-319ae7d1fbe0","name":"Updated collection name","query":"#tag2","ids":[]} \ No newline at end of file diff --git a/store/file/testdata/quizzes/quiz_5.md b/store/file/testdata/quizzes/quiz_5.md index 0c1e47a..6518986 100644 --- a/store/file/testdata/quizzes/quiz_5.md +++ b/store/file/testdata/quizzes/quiz_5.md @@ -1,6 +1,6 @@ --- -id: f7034ebc-d62d-43eb-a6cf-57f4886c3a7c -created_at: !!timestamp 2023-10-07T11:38:32.049804383+02:00 +id: e4ee7eb2-5608-4709-ad85-1381b457de35 +created_at: !!timestamp 2023-10-16T12:47:06.100160075+02:00 --- This quiz is initially without metadata. diff --git a/store/memory/memory.go b/store/memory/memory.go index 599246b..e42cfea 100644 --- a/store/memory/memory.go +++ b/store/memory/memory.go @@ -278,6 +278,14 @@ func (s *MemoryProboCollectorStore) DeleteQuiz(id string) (*models.Quiz, error) return s.deleteQuiz(id) } +func (s *MemoryProboCollectorStore) ReadAllCollections() ([]*models.Collection, error) { + result := make([]*models.Collection, 0) + for id := range s.collections { + result = append(result, s.getCollectionFromID(id)) + } + return result, nil +} + func (s *MemoryProboCollectorStore) CreateCollection(r *client.CreateUpdateCollectionRequest) (*models.Collection, error) { q, _, err := s.createOrUpdateCollection(r, "") return q, err