ezshare/store/store_test.go

53 lines
1.2 KiB
Go
Raw Normal View History

2021-12-03 22:51:48 +00:00
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)
}
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)
}
})
}