You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bog/dataswamp/swampfile/file_contract.go

67 lines
1.3 KiB
Go

package swampfile
import (
"github.com/matryer/is"
"testing"
"time"
)
func RepositoryContract(fac func() Repository, t *testing.T) {
basicFileOperationContract(fac, t)
}
func basicFileOperationContract(fac func() Repository, t *testing.T) {
is := is.New(t)
repo := fac()
not_file, err := repo.Open("doesnot", "exist")
is.Equal(err, ErrNotExists)
is.Equal(not_file, nil)
new_file, err := repo.Create("newfile.new", "ua1")
is.NoErr(err)
is.True(new_file != nil)
var testdata = "testingdata"
new_file.Write([]byte(testdata))
new_file.Close()
reopened_file, err := repo.Open("newfile.new", "ua1")
is.NoErr(err)
is.True(reopened_file != nil)
readback := make([]byte, 128)
size, err := reopened_file.Read(readback)
is.Equal(string(readback[0:size]), testdata)
reopened_file.Close()
repo.Delete("newfile.new", "ua1")
deleted_file, err := repo.Open("newfile.new", "ua1")
is.Equal(err, ErrNotExists)
is.True(deleted_file == nil)
// DeleteOlderThan
expiring_file, err := repo.Create("deep/dir/expiring.new", "ua1")
is.NoErr(err)
expiring_file.Write([]byte(testdata))
expiring_file.Close()
_, err = repo.DeleteOlderThan("ua1", time.Now().Add(time.Hour))
is.NoErr(err)
expired_file, err := repo.Open("deep/dir/expiring.new", "ua1")
is.Equal(err, ErrNotExists)
is.True(expired_file == nil)
}