ezshare/store/store_test.go
2021-12-04 11:30:42 +01:00

66 lines
1.5 KiB
Go

package store_test
import (
"testing"
"time"
"gitea.benny.dog/torjus/ezshare/pb"
"gitea.benny.dog/torjus/ezshare/store"
"google.golang.org/protobuf/types/known/timestamppb"
)
func doFileStoreTest(s store.FileStore, t *testing.T) {
t.Run("Basics", func(t *testing.T) {
// Create
file := &pb.File{
Data: []byte("testdata lol!"),
Metadata: &pb.File_Metadata{
UploadedOn: timestamppb.New(time.Now()),
ExpiresOn: timestamppb.New(time.Now().Add(24 * time.Hour)),
OriginalFilename: "data.txt",
},
}
id, err := s.StoreFile(file)
if err != nil {
t.Fatalf("Unable to store file: %s", err)
}
// List
list, err := s.ListFiles()
if err != nil {
t.Fatalf("error listing files: %s", err)
}
if len(list) != 1 {
t.Fatalf("List returned unexpected amount. Got %d want %d", len(list), 1)
}
if list[0].FileId != id {
t.Fatalf("List contains wrong id")
}
retrieved, err := s.GetFile(id)
if err != nil {
t.Fatalf("Unable to get file: %s", err)
}
if len(file.Data) != len(retrieved.Data) {
t.Fatalf("Mismatch in size between stored and retrieved. Got %d want %d", len(retrieved.Data), len(file.Data))
}
for i := range file.Data {
if file.Data[i] != retrieved.Data[i] {
t.Fatalf("Mismatch at %d", i)
}
}
if err := s.DeleteFile(id); err != nil {
t.Fatalf("Unable to delete file: %s", err)
}
if _, err := s.GetFile(id); err != store.ErrNoSuchFile {
t.Fatalf("Getting deleted file returned wrong error: %s", err)
}
})
}