From 96003ef900c0437c337663b3944bee60d5f7243a Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Sun, 24 Apr 2022 22:35:31 +0800 Subject: [PATCH] Explcit test contract for fs file repo and integration usage --- test/integration/filesystem_file_test.go | 21 +++++++++ test/mock/file_contract.go | 57 ++++++++++++++++++++++++ test/mock/file_test.go | 53 +--------------------- 3 files changed, 79 insertions(+), 52 deletions(-) create mode 100644 test/integration/filesystem_file_test.go create mode 100644 test/mock/file_contract.go diff --git a/test/integration/filesystem_file_test.go b/test/integration/filesystem_file_test.go new file mode 100644 index 0000000..e649a81 --- /dev/null +++ b/test/integration/filesystem_file_test.go @@ -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) +} diff --git a/test/mock/file_contract.go b/test/mock/file_contract.go new file mode 100644 index 0000000..e1c0caf --- /dev/null +++ b/test/mock/file_contract.go @@ -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") + } +} diff --git a/test/mock/file_test.go b/test/mock/file_test.go index 7426641..dff2759 100644 --- a/test/mock/file_test.go +++ b/test/mock/file_test.go @@ -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") - } -}