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

57 lines
1.1 KiB
Go

package swampfile
import (
"testing"
)
func RepositoryContract(fac func() Repository, t *testing.T) {
basicFileOperationContract(fac, t)
}
func basicFileOperationContract(fac func() Repository, t *testing.T) {
repo := fac()
not_file, err := repo.Open("doesnot", "exist")
if err != ErrNotExists || not_file != nil{
t.Errorf("Must raise not exists and file must not open")
}
new_file, err := repo.Create("newfile.new", "ua1")
if err != nil || new_file == nil {
t.Errorf("Create ")
}
var testdata = "testingdata"
new_file.Write([]byte(testdata))
new_file.Close()
reopened_file, err := repo.Open("newfile.new", "ua1")
if err != nil || reopened_file == nil{
t.Errorf("Must open existing files")
}
readback := make([]byte, 128)
size, err := reopened_file.Read(readback)
if string(readback[0:size]) != testdata {
t.Errorf("Must contain previously stored data '%s' '%s'", string(readback), testdata)
}
reopened_file.Close()
repo.Delete("newfile.new", "ua1")
deleted_file, err := repo.Open("newfile.new", "ua1")
if err != ErrNotExists || deleted_file != nil{
t.Errorf("Musn't open deleted files")
}
}