Explcit test contract for fs file repo and integration usage

master
Caj Larsson 3 years ago
parent e9fc400389
commit 96003ef900

@ -0,0 +1,21 @@
package test
import (
"path"
"testing"
"caj-larsson/bog/domain"
"caj-larsson/bog/integration"
"caj-larsson/bog/test/mock"
)
func TestFsFileRepo(t *testing.T) {
var fac = func()domain.FileDataRepository {
r := t.TempDir()
d := path.Join(r, "fs")
repo := integration.FileSystemBogRepository { d }
return &repo
}
mock.BogFileRepositoryContract(fac, t)
}

@ -0,0 +1,57 @@
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")
}
}

@ -2,61 +2,10 @@ package mock
import (
"testing"
"caj-larsson/bog/domain"
//"caj-larsson/bog/domain"
)
func TestMockFileRepo(t *testing.T) {
BogFileRepositoryContract(NewMockFileRepository, t)
}
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("Must open existing files")
}
}

Loading…
Cancel
Save