Add test for persistance to store
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Torjus Håkestad 2022-01-19 03:10:50 +01:00
parent 1cb169318c
commit 9449b37ab1
2 changed files with 90 additions and 0 deletions

View File

@ -14,4 +14,13 @@ func TestFSFileStore(t *testing.T) {
}
RunFilestoreTest(s, t)
persistentDir := t.TempDir()
newFunc := func() gpaste.FileStore {
s, err := gpaste.NewFSFileStore(persistentDir)
if err != nil {
t.Fatalf("Error creating store: %s", err)
}
return s
}
RunPersistentFilestoreTest(newFunc, t)
}

View File

@ -3,9 +3,12 @@ package gpaste_test
import (
"bytes"
"io"
"strings"
"testing"
"time"
"git.t-juice.club/torjus/gpaste"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
)
@ -74,3 +77,81 @@ func RunFilestoreTest(s gpaste.FileStore, t *testing.T) {
}
})
}
func RunPersistentFilestoreTest(newStoreFunc func() gpaste.FileStore, t *testing.T) {
s := newStoreFunc()
files := []struct {
File *gpaste.File
ExpectedData string
}{
{
File: &gpaste.File{
ID: uuid.NewString(),
OriginalFilename: "testfile.txt",
MaxViews: 5,
ExpiresOn: time.Now().Add(10 * time.Minute),
Body: io.NopCloser(strings.NewReader("cocks!")),
},
ExpectedData: "cocks!",
},
{
File: &gpaste.File{
ID: uuid.NewString(),
OriginalFilename: "testfile2.txt",
MaxViews: 5,
ExpiresOn: time.Now().Add(10 * time.Minute),
Body: io.NopCloser(strings.NewReader("derps!")),
},
ExpectedData: "derps!",
},
}
for _, f := range files {
err := s.Store(f.File)
if err != nil {
t.Fatalf("Error storing file: %s", err)
}
}
for _, f := range files {
retrieved, err := s.Get(f.File.ID)
if err != nil {
t.Fatalf("Unable to retrieve file: %s", err)
}
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
if !cmp.Equal(retrieved, f.File, ignoreBody) {
t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
}
buf := new(strings.Builder)
if _, err := io.Copy(buf, retrieved.Body); err != nil {
t.Fatalf("Error reading from body: %s", err)
}
retrieved.Body.Close()
if buf.String() != f.ExpectedData {
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
}
}
// Reopen store, and fetch again
s = newStoreFunc()
for _, f := range files {
retrieved, err := s.Get(f.File.ID)
if err != nil {
t.Fatalf("Unable to retrieve file: %s", err)
}
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
if !cmp.Equal(retrieved, f.File, ignoreBody) {
t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
}
buf := new(strings.Builder)
if _, err := io.Copy(buf, retrieved.Body); err != nil {
t.Fatalf("Error reading from body: %s", err)
}
retrieved.Body.Close()
if buf.String() != f.ExpectedData {
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
}
}
}