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.
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
3 years ago
|
package mock
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
"caj-larsson/bog/domain"
|
||
|
)
|
||
|
|
||
|
|
||
|
func BogFileRepositoryContract(fac func() domain.FileDataRepository, t *testing.T) {
|
||
|
basicFileOperationContract(fac, t)
|
||
|
}
|
||
|
|
||
|
|
||
|
func basicFileOperationContract(fac func() domain.FileDataRepository, t *testing.T) {
|
||
|
repo := fac()
|
||
|
|
||
|
not_file, err := repo.Open("doesnot", "exist")
|
||
|
|
||
|
if err != domain.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 != domain.ErrNotExists || deleted_file != nil{
|
||
|
t.Errorf("Musn't open deleted files")
|
||
|
}
|
||
|
}
|