fix test cases

This commit is contained in:
Hayden 2022-09-24 11:28:28 -08:00
parent d7e3e1d326
commit 8f9054888e
5 changed files with 77 additions and 55 deletions

View file

@ -11,6 +11,13 @@ type AllServices struct {
}
func NewServices(repos *repo.AllRepos, root string) *AllServices {
if repos == nil {
panic("repos cannot be nil")
}
if root == "" {
panic("root cannot be empty")
}
return &AllServices{
User: &UserService{repos},
Admin: &AdminService{repos},

View file

@ -18,6 +18,7 @@ import (
var (
fk = faker.NewFaker()
tCtx = Context{}
tClient *ent.Client
tRepos *repo.AllRepos
tUser *ent.User
@ -66,7 +67,13 @@ func TestMain(m *testing.M) {
tSvc = NewServices(tRepos, "/tmp/homebox")
defer client.Close()
bootstrap()
tCtx = Context{
Context: context.Background(),
GID: tGroup.ID,
UID: tUser.ID,
}
os.Exit(m.Run())
}

View file

@ -0,0 +1,62 @@
package services
import (
"context"
"os"
"path"
"strings"
"testing"
"github.com/hay-kot/homebox/backend/internal/types"
"github.com/stretchr/testify/assert"
)
func TestItemService_AddAttachment(t *testing.T) {
temp := os.TempDir()
svc := &ItemService{
repo: tRepos,
filepath: temp,
}
loc, err := tSvc.Location.Create(context.Background(), tGroup.ID, types.LocationCreate{
Description: "test",
Name: "test",
})
assert.NoError(t, err)
assert.NotNil(t, loc)
itmC := types.ItemCreate{
Name: fk.Str(10),
Description: fk.Str(10),
LocationID: loc.ID,
}
itm, err := svc.Create(context.Background(), tGroup.ID, itmC)
assert.NoError(t, err)
assert.NotNil(t, itm)
t.Cleanup(func() {
err := svc.repo.Items.Delete(context.Background(), itm.ID)
assert.NoError(t, err)
})
contents := fk.Str(1000)
reader := strings.NewReader(contents)
// Setup
afterAttachment, err := svc.AttachmentAdd(tCtx, itm.ID, "testfile.txt", "attachment", reader)
assert.NoError(t, err)
assert.NotNil(t, afterAttachment)
// Check that the file exists
storedPath := afterAttachment.Attachments[0].Document.Path
// {root}/{group}/{item}/{attachment}
assert.Equal(t, path.Join(temp, tGroup.ID.String(), itm.ID.String(), "testfile.txt"), storedPath)
// Check that the file contents are correct
bts, err := os.ReadFile(storedPath)
assert.NoError(t, err)
assert.Equal(t, contents, string(bts))
}

View file

@ -2,13 +2,9 @@ package services
import (
"context"
"os"
"path"
"strings"
"testing"
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/types"
"github.com/stretchr/testify/assert"
)
@ -92,52 +88,3 @@ func TestItemService_CsvImport(t *testing.T) {
}
}
}
func TestItemService_AddAttachment(t *testing.T) {
temp := os.TempDir()
svc := &ItemService{
repo: tRepos,
}
loc, err := tSvc.Location.Create(context.Background(), tGroup.ID, types.LocationCreate{
Description: "test",
Name: "test",
})
assert.NoError(t, err)
assert.NotNil(t, loc)
itmC := types.ItemCreate{
Name: fk.Str(10),
Description: fk.Str(10),
LocationID: loc.ID,
}
itm, err := svc.Create(context.Background(), tGroup.ID, itmC)
assert.NoError(t, err)
assert.NotNil(t, itm)
t.Cleanup(func() {
err := svc.repo.Items.Delete(context.Background(), itm.ID)
assert.NoError(t, err)
})
contents := fk.Str(1000)
reader := strings.NewReader(contents)
// Setup
afterAttachment, err := svc.AttachmentAdd(Context{Context: context.Background(), GID: tGroup.ID}, itm.ID, "testfile.txt", "attachment", reader)
assert.NoError(t, err)
assert.NotNil(t, afterAttachment)
// Check that the file exists
storedPath := afterAttachment.Attachments[0].Document.Path
// {root}/{group}/{item}/{attachment}
assert.Equal(t, path.Join(temp, tGroup.ID.String(), itm.ID.String(), "testfile.txt"), storedPath)
// Check that the file contents are correct
bts, err := os.ReadFile(storedPath)
assert.NoError(t, err)
assert.Equal(t, contents, string(bts))
}

View file

@ -50,9 +50,8 @@ func Test_hasConflict(t *testing.T) {
func TestSafePath(t *testing.T) {
// override dirReader
dirReader = func(name string) []string {
return []string{"/foo/bar.pdf", "/foo/bar (1).pdf", "/foo/bar (2).pdf"}
return []string{"bar.pdf", "bar (1).pdf", "bar (2).pdf"}
}
type args struct {