🚧 Implement basic share handling

This commit is contained in:
2020-10-12 19:51:44 +02:00
parent e6a62bfeb1
commit 66c525aa7b
2 changed files with 251 additions and 8 deletions
+97
View File
@@ -3,6 +3,7 @@ package main
import (
"testing"
uuid "github.com/satori/go.uuid"
"github.com/tidwall/buntdb"
)
@@ -101,3 +102,99 @@ func TestStoreUserHandling(t *testing.T) {
}
})
}
func TestStoreShareHandling(t *testing.T) {
store, err := NewDBStore(":memory:")
if err != nil {
t.Fatalf("cannot create store: %v", err)
}
defer store.Close()
t.Run("store should be empty initially", func(t *testing.T) {
shares, err := store.GetShares()
if err != nil {
t.Errorf("no error should have been returned: %v", err)
}
if len(shares) != 0 {
t.Errorf("there should be no shares")
}
})
t.Run("creating a share should work", func(t *testing.T) {
share, err := store.CreateShare()
if err != nil {
t.Errorf("error creating share: %v", err)
return
}
var emptyUUID uuid.UUID
if share.UUID == emptyUUID {
t.Errorf("UUID is empty")
return
}
if share.Name != "" || share.Description != "" {
t.Errorf("share should not have attributes set (yet)")
}
t.Run("share attributes can be set", func(t *testing.T) {
share.Name = "a name"
share.Description = "some desc"
if err := store.UpdateShareAttributes(share); err != nil {
t.Errorf("cannot set attributes of share: %v", err)
}
})
t.Run("cannot set attributes of unknown share", func(t *testing.T) {
otherShare := Share{UUID: uuid.NewV4()}
otherShare.Name = "foo"
otherShare.Description = "bar"
err := store.UpdateShareAttributes(otherShare)
if err == nil {
t.Errorf("an error should have been returned")
} else if err != ErrShareNotFound {
t.Errorf("wrong error has been returned: %v", err)
}
})
t.Run("share can be listed", func(t *testing.T) {
shares, err := store.GetShares()
if err != nil {
t.Errorf("error getting shares: %v", err)
return
}
if len(shares) != 1 {
t.Errorf("invalid number of shares: %d", len(shares))
return
}
if shares[0].UUID != share.UUID {
t.Errorf("unexpected uuid")
}
if shares[0].Name != "a name" {
t.Errorf("unexpected name")
}
if shares[0].Description != "some desc" {
t.Errorf("unexpected description")
}
})
t.Run("share can be removed", func(t *testing.T) {
if err := store.RemoveShare(share.UUID); err != nil {
t.Errorf("removing share failed: %v", err)
}
})
})
t.Run("database should be empty now", func(t *testing.T) {
// checks that we properly deleted all keys
if err := store.db.View(func(tx *buntdb.Tx) error {
return tx.Ascend("", func(key, value string) bool {
t.Errorf("there should be no keys left")
return false
})
}); err != nil {
t.Errorf("iterating keys failed: %v", err)
}
})
}