First implementation of Collections
This commit is contained in:
parent
15830d888f
commit
478d1e8815
6 changed files with 105 additions and 2 deletions
|
@ -1,10 +1,13 @@
|
||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"git.andreafazzi.eu/andrea/probo/client"
|
"git.andreafazzi.eu/andrea/probo/client"
|
||||||
|
"git.andreafazzi.eu/andrea/probo/models"
|
||||||
"github.com/remogatto/prettytest"
|
"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
|
||||||
|
}
|
||||||
|
|
|
@ -391,6 +391,10 @@ func (s *FileProboCollectorStore) WriteMetaHeaderToFile(filename string, meta *m
|
||||||
return meta, nil
|
return meta, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *FileProboCollectorStore) ReadAllCollections() ([]*models.Collection, error) {
|
||||||
|
return s.memoryStore.ReadAllCollections()
|
||||||
|
}
|
||||||
|
|
||||||
func (s *FileProboCollectorStore) CreateCollection(r *client.CreateUpdateCollectionRequest) (*models.Collection, error) {
|
func (s *FileProboCollectorStore) CreateCollection(r *client.CreateUpdateCollectionRequest) (*models.Collection, error) {
|
||||||
collection, err := s.memoryStore.CreateCollection(r)
|
collection, err := s.memoryStore.CreateCollection(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -405,6 +409,20 @@ func (s *FileProboCollectorStore) CreateCollection(r *client.CreateUpdateCollect
|
||||||
return s.memoryStore.ReadCollectionByID(collection.ID)
|
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) {
|
func (s *FileProboCollectorStore) removeMetaFromFile(filename string) (*models.Meta, error) {
|
||||||
file, err := os.Open(path.Join(s.quizzesDir, filename))
|
file, err := os.Open(path.Join(s.quizzesDir, filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
1
store/file/testdata/collections/collection_0386ac85-0701-48e7-a5a7-bf9713f63dac.json
vendored
Normal file
1
store/file/testdata/collections/collection_0386ac85-0701-48e7-a5a7-bf9713f63dac.json
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"id":"0386ac85-0701-48e7-a5a7-bf9713f63dac","name":"Updated collection name","query":"#tag2","ids":[]}
|
1
store/file/testdata/collections/collection_b30c4392-52f3-46c5-8865-319ae7d1fbe0.json
vendored
Normal file
1
store/file/testdata/collections/collection_b30c4392-52f3-46c5-8865-319ae7d1fbe0.json
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"id":"b30c4392-52f3-46c5-8865-319ae7d1fbe0","name":"Updated collection name","query":"#tag2","ids":[]}
|
4
store/file/testdata/quizzes/quiz_5.md
vendored
4
store/file/testdata/quizzes/quiz_5.md
vendored
|
@ -1,6 +1,6 @@
|
||||||
---
|
---
|
||||||
id: f7034ebc-d62d-43eb-a6cf-57f4886c3a7c
|
id: e4ee7eb2-5608-4709-ad85-1381b457de35
|
||||||
created_at: !!timestamp 2023-10-07T11:38:32.049804383+02:00
|
created_at: !!timestamp 2023-10-16T12:47:06.100160075+02:00
|
||||||
---
|
---
|
||||||
This quiz is initially without metadata.
|
This quiz is initially without metadata.
|
||||||
|
|
||||||
|
|
|
@ -278,6 +278,14 @@ func (s *MemoryProboCollectorStore) DeleteQuiz(id string) (*models.Quiz, error)
|
||||||
return s.deleteQuiz(id)
|
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) {
|
func (s *MemoryProboCollectorStore) CreateCollection(r *client.CreateUpdateCollectionRequest) (*models.Collection, error) {
|
||||||
q, _, err := s.createOrUpdateCollection(r, "")
|
q, _, err := s.createOrUpdateCollection(r, "")
|
||||||
return q, err
|
return q, err
|
||||||
|
|
Loading…
Reference in a new issue