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 (
"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))
}

View file

@ -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)
})
}