gpaste/filestore_test.go

64 lines
1.3 KiB
Go
Raw Normal View History

2022-01-15 18:07:33 +00:00
package gpaste_test
import (
"bytes"
"io"
"testing"
"git.t-juice.club/torjus/gpaste"
"github.com/google/uuid"
)
func RunFilestoreTest(s gpaste.FileStore, t *testing.T) {
t.Run("Basic", func(t *testing.T) {
// Create
dataString := "TEST_LOL_OMG"
id := uuid.Must(uuid.NewRandom()).String()
bodyBuf := &bytes.Buffer{}
bodyBuf.Write([]byte(dataString))
body := io.NopCloser(bodyBuf)
f := &gpaste.File{
ID: id,
MaxViews: 0,
Body: body,
}
err := s.Store(f)
if err != nil {
t.Fatalf("Unable to store file: %s", err)
}
// Retrieve
retrieved, err := s.Get(id)
if err != nil {
t.Fatalf("Unable to retrieve file: %s", err)
}
retrievedBuf := &bytes.Buffer{}
_, err = retrievedBuf.ReadFrom(retrieved.Body)
if err != nil {
t.Fatalf("Unable to read retrieved body: %s", err)
}
_ = retrieved.Body.Close()
if err != nil {
t.Fatalf("Error reading from retrieved file: %s", err)
}
if retrievedBuf.String() != dataString {
t.Fatalf("Data from retrieved body mismatch. Got %s want %s", retrievedBuf.String(), dataString)
}
// List
ids, err := s.List()
if err != nil {
t.Fatalf("Error doing list: %s", err)
}
if len(ids) != 1 {
t.Fatalf("List has wrong length: %d", len(ids))
}
if ids[0] != id {
t.Fatalf("ID is wrong. Got %s want %s", ids[0], id)
}
})
}