2022-08-31 03:21:18 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-28 20:53:00 +00:00
|
|
|
"encoding/json"
|
2022-08-31 03:21:18 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
2022-08-31 03:21:18 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func locationFactory() LocationCreate {
|
|
|
|
return LocationCreate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
func useLocations(t *testing.T, len int) []LocationOut {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
out := make([]LocationOut, len)
|
|
|
|
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
out[i] = loc
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
for _, loc := range out {
|
|
|
|
err := tRepos.Locations.Delete(context.Background(), loc.ID)
|
|
|
|
if err != nil {
|
|
|
|
assert.True(t, ent.IsNotFound(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepository_Get(t *testing.T) {
|
|
|
|
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Get by ID
|
2022-09-05 08:26:21 +00:00
|
|
|
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, loc.ID, foundLoc.ID)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Locations.Delete(context.Background(), loc.ID)
|
2022-09-03 09:52:05 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepositoryGetAllWithCount(t *testing.T) {
|
2022-09-03 09:17:48 +00:00
|
|
|
ctx := context.Background()
|
2023-01-28 20:53:00 +00:00
|
|
|
result := useLocations(t, 1)[0]
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
_, err := tRepos.Items.Create(ctx, tGroup.ID, ItemCreate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-09-03 09:17:48 +00:00
|
|
|
LocationID: result.ID,
|
|
|
|
})
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2022-11-02 19:54:43 +00:00
|
|
|
results, err := tRepos.Locations.GetAll(context.Background(), tGroup.ID, LocationQuery{})
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
for _, loc := range results {
|
|
|
|
if loc.ID == result.ID {
|
|
|
|
assert.Equal(t, 1, loc.ItemCount)
|
|
|
|
}
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepository_Create(t *testing.T) {
|
2023-01-28 20:53:00 +00:00
|
|
|
loc := useLocations(t, 1)[0]
|
2022-08-31 03:21:18 +00:00
|
|
|
|
|
|
|
// Get by ID
|
2022-09-05 08:26:21 +00:00
|
|
|
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, loc.ID, foundLoc.ID)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Locations.Delete(context.Background(), loc.ID)
|
2022-09-03 09:52:05 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepository_Update(t *testing.T) {
|
2023-01-28 20:53:00 +00:00
|
|
|
loc := useLocations(t, 1)[0]
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
updateData := LocationUpdate{
|
2022-08-31 03:21:18 +00:00
|
|
|
ID: loc.ID,
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
update, err := tRepos.Locations.Update(context.Background(), updateData)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, update.ID, foundLoc.ID)
|
|
|
|
assert.Equal(t, update.Name, foundLoc.Name)
|
|
|
|
assert.Equal(t, update.Description, foundLoc.Description)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Locations.Delete(context.Background(), loc.ID)
|
2022-09-03 09:52:05 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepository_Delete(t *testing.T) {
|
2023-01-28 20:53:00 +00:00
|
|
|
loc := useLocations(t, 1)[0]
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
err := tRepos.Locations.Delete(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
_, err = tRepos.Locations.Get(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2023-01-28 20:53:00 +00:00
|
|
|
|
|
|
|
func TestItemRepository_TreeQuery(t *testing.T) {
|
|
|
|
locs := useLocations(t, 3)
|
|
|
|
|
|
|
|
// Set relations
|
|
|
|
_, err := tRepos.Locations.UpdateOneByGroup(context.Background(), tGroup.ID, locs[0].ID, LocationUpdate{
|
|
|
|
ID: locs[0].ID,
|
|
|
|
ParentID: locs[1].ID,
|
|
|
|
Name: locs[0].Name,
|
|
|
|
Description: locs[0].Description,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
locations, err := tRepos.Locations.Tree(context.Background(), tGroup.ID, TreeQuery{WithItems: true})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 2, len(locations))
|
|
|
|
|
|
|
|
// Check roots
|
|
|
|
for _, loc := range locations {
|
|
|
|
if loc.ID == locs[1].ID {
|
|
|
|
assert.Equal(t, 1, len(loc.Children))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertLocationsToTree(t *testing.T) {
|
|
|
|
uuid1, uuid2, uuid3, uuid4 := uuid.New(), uuid.New(), uuid.New(), uuid.New()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
locations []FlatTreeItem
|
|
|
|
expected []TreeItem
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Convert locations to tree",
|
|
|
|
locations: []FlatTreeItem{
|
|
|
|
{
|
|
|
|
ID: uuid1,
|
|
|
|
Name: "Root1",
|
|
|
|
ParentID: uuid.Nil,
|
|
|
|
Level: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: uuid2,
|
|
|
|
Name: "Child1",
|
|
|
|
ParentID: uuid1,
|
|
|
|
Level: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: uuid3,
|
|
|
|
Name: "Child2",
|
|
|
|
ParentID: uuid1,
|
|
|
|
Level: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []TreeItem{
|
|
|
|
{
|
|
|
|
ID: uuid1,
|
|
|
|
Name: "Root1",
|
|
|
|
Children: []*TreeItem{
|
|
|
|
{
|
|
|
|
ID: uuid2,
|
|
|
|
Name: "Child1",
|
|
|
|
Children: []*TreeItem{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: uuid3,
|
|
|
|
Name: "Child2",
|
|
|
|
Children: []*TreeItem{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Convert locations to tree with deeply nested children",
|
|
|
|
locations: []FlatTreeItem{
|
|
|
|
{
|
|
|
|
ID: uuid1,
|
|
|
|
Name: "Root1",
|
|
|
|
ParentID: uuid.Nil,
|
|
|
|
Level: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: uuid2,
|
|
|
|
Name: "Child1",
|
|
|
|
ParentID: uuid1,
|
|
|
|
Level: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: uuid3,
|
|
|
|
Name: "Child2",
|
|
|
|
ParentID: uuid2,
|
|
|
|
Level: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: uuid4,
|
|
|
|
Name: "Child3",
|
|
|
|
ParentID: uuid3,
|
|
|
|
Level: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []TreeItem{
|
|
|
|
{
|
|
|
|
ID: uuid1,
|
|
|
|
Name: "Root1",
|
|
|
|
Children: []*TreeItem{
|
|
|
|
{
|
|
|
|
ID: uuid2,
|
|
|
|
Name: "Child1",
|
|
|
|
Children: []*TreeItem{
|
|
|
|
{
|
|
|
|
ID: uuid3,
|
|
|
|
Name: "Child2",
|
|
|
|
Children: []*TreeItem{
|
|
|
|
{
|
|
|
|
ID: uuid4,
|
|
|
|
Name: "Child3",
|
|
|
|
Children: []*TreeItem{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
result := ConvertLocationsToTree(tc.locations)
|
|
|
|
|
|
|
|
// Compare JSON strings
|
|
|
|
expected, _ := json.Marshal(tc.expected)
|
|
|
|
got, _ := json.Marshal(result)
|
|
|
|
assert.Equal(t, string(expected), string(got))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|