From 9b06a6d1de13f59a57972dd7de29ea6f87c8919f Mon Sep 17 00:00:00 2001 From: Kevin Lin Date: Sat, 3 Feb 2024 16:21:47 -0800 Subject: [PATCH] backend: Attachment and document unit tests --- .../services/service_items_attachments_test.go | 13 ++++++++++--- .../internal/data/repo/repo_documents_test.go | 17 ++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/backend/internal/core/services/service_items_attachments_test.go b/backend/internal/core/services/service_items_attachments_test.go index 4e2315e..a0e63e3 100644 --- a/backend/internal/core/services/service_items_attachments_test.go +++ b/backend/internal/core/services/service_items_attachments_test.go @@ -2,11 +2,14 @@ package services import ( "context" + "io" "os" "path" + "path/filepath" "strings" "testing" + "github.com/hay-kot/homebox/backend/internal/core/blobstore" "github.com/hay-kot/homebox/backend/internal/data/repo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -15,6 +18,8 @@ import ( func TestItemService_AddAttachment(t *testing.T) { temp := os.TempDir() + bs := blobstore.NewLocalBlobStore(filepath.Join(temp, "homebox")) + svc := &ItemService{ repo: tRepos, filepath: temp, @@ -53,10 +58,12 @@ func TestItemService_AddAttachment(t *testing.T) { storedPath := afterAttachment.Attachments[0].Document.Path // {root}/{group}/{item}/{attachment} - assert.Equal(t, path.Join(temp, "homebox", tGroup.ID.String(), "documents"), path.Dir(storedPath)) + assert.Equal(t, path.Join(tGroup.ID.String(), "documents"), path.Dir(storedPath)) // Check that the file contents are correct - bts, err := os.ReadFile(storedPath) + bts, err := bs.Get(storedPath) require.NoError(t, err) - assert.Equal(t, contents, string(bts)) + buf, err := io.ReadAll(bts) + require.NoError(t, err) + assert.Equal(t, contents, string(buf)) } diff --git a/backend/internal/data/repo/repo_documents_test.go b/backend/internal/data/repo/repo_documents_test.go index 4634235..4a3295c 100644 --- a/backend/internal/data/repo/repo_documents_test.go +++ b/backend/internal/data/repo/repo_documents_test.go @@ -4,11 +4,12 @@ import ( "bytes" "context" "fmt" - "os" + "io" "path/filepath" "testing" "github.com/google/uuid" + "github.com/hay-kot/homebox/backend/internal/core/blobstore" "github.com/hay-kot/homebox/backend/internal/data/ent" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -47,8 +48,8 @@ func useDocs(t *testing.T, num int) []DocumentOut { func TestDocumentRepository_CreateUpdateDelete(t *testing.T) { temp := t.TempDir() r := DocumentRepository{ - db: tClient, - dir: temp, + db: tClient, + bs: blobstore.NewLocalBlobStore(temp), } type args struct { @@ -83,13 +84,15 @@ func TestDocumentRepository_CreateUpdateDelete(t *testing.T) { got, err := r.Create(tt.args.ctx, tt.args.gid, tt.args.doc) require.NoError(t, err) assert.Equal(t, tt.title, got.Title) - assert.Equal(t, fmt.Sprintf("%s/%s/documents", temp, tt.args.gid), filepath.Dir(got.Path)) + assert.Equal(t, fmt.Sprintf("%s/documents", tt.args.gid), filepath.Dir(got.Path)) ensureRead := func() { // Read Document - bts, err := os.ReadFile(got.Path) + bts, err := r.bs.Get(got.Path) require.NoError(t, err) - assert.Equal(t, tt.content, string(bts)) + buf, err := io.ReadAll(bts) + require.NoError(t, err) + assert.Equal(t, tt.content, string(buf)) } ensureRead() @@ -104,7 +107,7 @@ func TestDocumentRepository_CreateUpdateDelete(t *testing.T) { err = r.Delete(tt.args.ctx, got.ID) require.NoError(t, err) - _, err = os.Stat(got.Path) + _, err = r.bs.Get(got.Path) require.Error(t, err) }) }