backend: Attachment and document unit tests

This commit is contained in:
Kevin Lin 2024-02-03 16:21:47 -08:00
parent 03525db494
commit 9b06a6d1de
No known key found for this signature in database
GPG key ID: A8F9E05B9AB4D240
2 changed files with 20 additions and 10 deletions

View file

@ -2,11 +2,14 @@ package services
import ( import (
"context" "context"
"io"
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
"testing" "testing"
"github.com/hay-kot/homebox/backend/internal/core/blobstore"
"github.com/hay-kot/homebox/backend/internal/data/repo" "github.com/hay-kot/homebox/backend/internal/data/repo"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -15,6 +18,8 @@ import (
func TestItemService_AddAttachment(t *testing.T) { func TestItemService_AddAttachment(t *testing.T) {
temp := os.TempDir() temp := os.TempDir()
bs := blobstore.NewLocalBlobStore(filepath.Join(temp, "homebox"))
svc := &ItemService{ svc := &ItemService{
repo: tRepos, repo: tRepos,
filepath: temp, filepath: temp,
@ -53,10 +58,12 @@ func TestItemService_AddAttachment(t *testing.T) {
storedPath := afterAttachment.Attachments[0].Document.Path storedPath := afterAttachment.Attachments[0].Document.Path
// {root}/{group}/{item}/{attachment} // {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 // Check that the file contents are correct
bts, err := os.ReadFile(storedPath) bts, err := bs.Get(storedPath)
require.NoError(t, err) 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))
} }

View file

@ -4,11 +4,12 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"os" "io"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/core/blobstore"
"github.com/hay-kot/homebox/backend/internal/data/ent" "github.com/hay-kot/homebox/backend/internal/data/ent"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -47,8 +48,8 @@ func useDocs(t *testing.T, num int) []DocumentOut {
func TestDocumentRepository_CreateUpdateDelete(t *testing.T) { func TestDocumentRepository_CreateUpdateDelete(t *testing.T) {
temp := t.TempDir() temp := t.TempDir()
r := DocumentRepository{ r := DocumentRepository{
db: tClient, db: tClient,
dir: temp, bs: blobstore.NewLocalBlobStore(temp),
} }
type args struct { 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) got, err := r.Create(tt.args.ctx, tt.args.gid, tt.args.doc)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, tt.title, got.Title) 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() { ensureRead := func() {
// Read Document // Read Document
bts, err := os.ReadFile(got.Path) bts, err := r.bs.Get(got.Path)
require.NoError(t, err) 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() ensureRead()
@ -104,7 +107,7 @@ func TestDocumentRepository_CreateUpdateDelete(t *testing.T) {
err = r.Delete(tt.args.ctx, got.ID) err = r.Delete(tt.args.ctx, got.ID)
require.NoError(t, err) require.NoError(t, err)
_, err = os.Stat(got.Path) _, err = r.bs.Get(got.Path)
require.Error(t, err) require.Error(t, err)
}) })
} }