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

51 lines
958 B
Go

package swampfile
import (
"github.com/matryer/is"
"testing"
)
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)
}