99 lines
2.6 KiB
Go
99 lines
2.6 KiB
Go
|
package memory
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"git.andreafazzi.eu/andrea/probo/client"
|
||
|
"git.andreafazzi.eu/andrea/probo/hasher/sha256"
|
||
|
"github.com/remogatto/prettytest"
|
||
|
)
|
||
|
|
||
|
type collectionTestSuite struct {
|
||
|
prettytest.Suite
|
||
|
}
|
||
|
|
||
|
func (t *collectionTestSuite) TestUpdateCollection() {
|
||
|
store := NewMemoryProboCollectorStore(
|
||
|
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
|
||
|
)
|
||
|
|
||
|
quiz_1, _ := store.CreateQuiz(
|
||
|
&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},
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
|
||
|
quiz_2, _ := store.CreateQuiz(
|
||
|
&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},
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
|
||
|
collection, _ := store.CreateCollection(
|
||
|
&client.CreateUpdateCollectionRequest{
|
||
|
Collection: &client.Collection{
|
||
|
Name: "MyCollection",
|
||
|
},
|
||
|
})
|
||
|
|
||
|
updatedCollection, updated, err := store.UpdateCollection(
|
||
|
&client.CreateUpdateCollectionRequest{
|
||
|
Collection: &client.Collection{
|
||
|
Name: "MyUpdatedCollection",
|
||
|
Query: "#tag1",
|
||
|
},
|
||
|
}, collection.ID)
|
||
|
|
||
|
t.Nil(err, fmt.Sprintf("The update returned an error: %v", err))
|
||
|
|
||
|
if !t.Failed() {
|
||
|
t.True(updated)
|
||
|
t.Equal("MyUpdatedCollection", updatedCollection.Name)
|
||
|
t.True(len(updatedCollection.Quizzes) == 2)
|
||
|
if !t.Failed() {
|
||
|
count := 0
|
||
|
for _, q := range updatedCollection.Quizzes {
|
||
|
if quiz_1.ID == q.ID || quiz_2.ID == q.ID {
|
||
|
count++
|
||
|
}
|
||
|
}
|
||
|
t.Equal(2, count)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (t *collectionTestSuite) TestDeleteCollection() {
|
||
|
store := NewMemoryProboCollectorStore(
|
||
|
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
|
||
|
)
|
||
|
collection, _ := store.CreateCollection(
|
||
|
&client.CreateUpdateCollectionRequest{
|
||
|
Collection: &client.Collection{
|
||
|
Name: "Collection to be deleted",
|
||
|
Query: "#tag1",
|
||
|
},
|
||
|
})
|
||
|
|
||
|
deletedCollection, err := store.DeleteCollection(&client.DeleteCollectionRequest{ID: collection.ID})
|
||
|
|
||
|
t.Equal(collection.ID, deletedCollection.ID, "Returned deleted collection ID should be equal to the request")
|
||
|
t.Nil(err, fmt.Sprintf("The update returned an error: %v", err))
|
||
|
|
||
|
_, err = store.ReadCollectionByID(deletedCollection.ID)
|
||
|
t.True(err != nil, "Reading a non existent quiz should return an error")
|
||
|
}
|