2022-09-05 08:26:21 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
"github.com/google/uuid"
|
2023-02-15 17:40:35 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/types"
|
2022-09-05 08:26:21 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-05 21:12:54 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-05 08:26:21 +00:00
|
|
|
)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func itemFactory() ItemCreate {
|
|
|
|
return ItemCreate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func useItems(t *testing.T, len int) []ItemOut {
|
2022-09-05 08:26:21 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
location, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
items := make([]ItemOut, len)
|
2022-09-05 08:26:21 +00:00
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
itm := itemFactory()
|
|
|
|
itm.LocationID = location.ID
|
|
|
|
|
|
|
|
item, err := tRepos.Items.Create(context.Background(), tGroup.ID, itm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
items[i] = item
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
t.Cleanup(func() {
|
2022-09-05 08:26:21 +00:00
|
|
|
for _, item := range items {
|
2022-09-12 22:47:27 +00:00
|
|
|
_ = tRepos.Items.Delete(context.Background(), item.ID)
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
2023-01-28 20:53:00 +00:00
|
|
|
|
|
|
|
_ = tRepos.Locations.Delete(context.Background(), location.ID)
|
2022-09-12 22:47:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return items
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 04:54:39 +00:00
|
|
|
func TestItemsRepository_RecursiveRelationships(t *testing.T) {
|
|
|
|
parent := useItems(t, 1)[0]
|
|
|
|
|
|
|
|
children := useItems(t, 3)
|
|
|
|
|
|
|
|
for _, child := range children {
|
|
|
|
update := ItemUpdate{
|
|
|
|
ID: child.ID,
|
|
|
|
ParentID: parent.ID,
|
|
|
|
Name: "note-important",
|
|
|
|
Description: "This is a note",
|
|
|
|
LocationID: child.Location.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append Parent ID
|
|
|
|
_, err := tRepos.Items.UpdateByGroup(context.Background(), tGroup.ID, update)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check Parent ID
|
|
|
|
updated, err := tRepos.Items.GetOne(context.Background(), child.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, parent.ID, updated.Parent.ID)
|
|
|
|
|
|
|
|
// Remove Parent ID
|
|
|
|
update.ParentID = uuid.Nil
|
|
|
|
_, err = tRepos.Items.UpdateByGroup(context.Background(), tGroup.ID, update)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check Parent ID
|
|
|
|
updated, err = tRepos.Items.GetOne(context.Background(), child.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, updated.Parent)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestItemsRepository_GetOne(t *testing.T) {
|
2022-09-12 22:47:27 +00:00
|
|
|
entity := useItems(t, 3)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
for _, item := range entity {
|
|
|
|
result, err := tRepos.Items.GetOne(context.Background(), item.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, item.ID, result.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestItemsRepository_GetAll(t *testing.T) {
|
|
|
|
length := 10
|
2022-09-12 22:47:27 +00:00
|
|
|
expected := useItems(t, length)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
results, err := tRepos.Items.GetAll(context.Background(), tGroup.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, length, len(results))
|
|
|
|
|
|
|
|
for _, item := range results {
|
|
|
|
for _, expectedItem := range expected {
|
|
|
|
if item.ID == expectedItem.ID {
|
|
|
|
assert.Equal(t, expectedItem.ID, item.ID)
|
|
|
|
assert.Equal(t, expectedItem.Name, item.Name)
|
|
|
|
assert.Equal(t, expectedItem.Description, item.Description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestItemsRepository_Create(t *testing.T) {
|
|
|
|
location, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
itm := itemFactory()
|
|
|
|
itm.LocationID = location.ID
|
|
|
|
|
|
|
|
result, err := tRepos.Items.Create(context.Background(), tGroup.ID, itm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, result.ID)
|
|
|
|
|
2022-09-13 04:54:30 +00:00
|
|
|
// Cleanup - Also deletes item
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Locations.Delete(context.Background(), location.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestItemsRepository_Create_Location(t *testing.T) {
|
|
|
|
location, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, location.ID)
|
|
|
|
|
|
|
|
item := itemFactory()
|
|
|
|
item.LocationID = location.ID
|
|
|
|
|
|
|
|
// Create Resource
|
|
|
|
result, err := tRepos.Items.Create(context.Background(), tGroup.ID, item)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, result.ID)
|
|
|
|
|
|
|
|
// Get Resource
|
|
|
|
foundItem, err := tRepos.Items.GetOne(context.Background(), result.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, result.ID, foundItem.ID)
|
2022-09-27 23:52:13 +00:00
|
|
|
assert.Equal(t, location.ID, foundItem.Location.ID)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-09-13 04:54:30 +00:00
|
|
|
// Cleanup - Also deletes item
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Locations.Delete(context.Background(), location.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestItemsRepository_Delete(t *testing.T) {
|
2022-09-12 22:47:27 +00:00
|
|
|
entities := useItems(t, 3)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
for _, item := range entities {
|
|
|
|
err := tRepos.Items.Delete(context.Background(), item.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
results, err := tRepos.Items.GetAll(context.Background(), tGroup.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, results)
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
func TestItemsRepository_Update_Labels(t *testing.T) {
|
|
|
|
entity := useItems(t, 1)[0]
|
|
|
|
labels := useLabels(t, 3)
|
|
|
|
|
|
|
|
labelsIDs := []uuid.UUID{labels[0].ID, labels[1].ID, labels[2].ID}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
labelIds []uuid.UUID
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []uuid.UUID
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "add all labels",
|
|
|
|
args: args{
|
|
|
|
labelIds: labelsIDs,
|
|
|
|
},
|
|
|
|
want: labelsIDs,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update with one label",
|
|
|
|
args: args{
|
|
|
|
labelIds: labelsIDs[:1],
|
|
|
|
},
|
|
|
|
want: labelsIDs[:1],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add one new label to existing single label",
|
|
|
|
args: args{
|
|
|
|
labelIds: labelsIDs[1:],
|
|
|
|
},
|
|
|
|
want: labelsIDs[1:],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
// Apply all labels to entity
|
2022-09-27 23:52:13 +00:00
|
|
|
updateData := ItemUpdate{
|
2022-09-12 22:47:27 +00:00
|
|
|
ID: entity.ID,
|
|
|
|
Name: entity.Name,
|
2022-09-27 23:52:13 +00:00
|
|
|
LocationID: entity.Location.ID,
|
2022-09-12 22:47:27 +00:00
|
|
|
LabelIDs: tt.args.labelIds,
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
updated, err := tRepos.Items.UpdateByGroup(context.Background(), tGroup.ID, updateData)
|
2022-09-12 22:47:27 +00:00
|
|
|
assert.NoError(t, err)
|
2022-09-27 23:52:13 +00:00
|
|
|
assert.Len(t, tt.want, len(updated.Labels))
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
for _, label := range updated.Labels {
|
2022-09-12 22:47:27 +00:00
|
|
|
assert.Contains(t, tt.want, label.ID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestItemsRepository_Update(t *testing.T) {
|
2022-09-12 22:47:27 +00:00
|
|
|
entities := useItems(t, 3)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
entity := entities[0]
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
updateData := ItemUpdate{
|
2022-09-09 18:20:38 +00:00
|
|
|
ID: entity.ID,
|
|
|
|
Name: entity.Name,
|
2022-09-27 23:52:13 +00:00
|
|
|
LocationID: entity.Location.ID,
|
2022-09-12 22:47:27 +00:00
|
|
|
SerialNumber: fk.Str(10),
|
2022-09-09 18:20:38 +00:00
|
|
|
LabelIDs: nil,
|
2022-09-12 22:47:27 +00:00
|
|
|
ModelNumber: fk.Str(10),
|
|
|
|
Manufacturer: fk.Str(10),
|
2023-02-15 17:40:35 +00:00
|
|
|
PurchaseTime: types.DateFromTime(time.Now()),
|
2022-09-12 22:47:27 +00:00
|
|
|
PurchaseFrom: fk.Str(10),
|
2022-09-09 18:20:38 +00:00
|
|
|
PurchasePrice: 300.99,
|
2023-02-15 17:40:35 +00:00
|
|
|
SoldTime: types.DateFromTime(time.Now()),
|
2022-09-12 22:47:27 +00:00
|
|
|
SoldTo: fk.Str(10),
|
2022-09-09 18:20:38 +00:00
|
|
|
SoldPrice: 300.99,
|
2022-09-12 22:47:27 +00:00
|
|
|
SoldNotes: fk.Str(10),
|
|
|
|
Notes: fk.Str(10),
|
2023-02-15 17:40:35 +00:00
|
|
|
WarrantyExpires: types.DateFromTime(time.Now()),
|
2022-09-12 22:47:27 +00:00
|
|
|
WarrantyDetails: fk.Str(10),
|
2022-09-09 18:20:38 +00:00
|
|
|
LifetimeWarranty: true,
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
updatedEntity, err := tRepos.Items.UpdateByGroup(context.Background(), tGroup.ID, updateData)
|
2022-09-05 08:26:21 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
got, err := tRepos.Items.GetOne(context.Background(), updatedEntity.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, updateData.ID, got.ID)
|
|
|
|
assert.Equal(t, updateData.Name, got.Name)
|
2022-09-27 23:52:13 +00:00
|
|
|
assert.Equal(t, updateData.LocationID, got.Location.ID)
|
2022-09-05 08:26:21 +00:00
|
|
|
assert.Equal(t, updateData.SerialNumber, got.SerialNumber)
|
|
|
|
assert.Equal(t, updateData.ModelNumber, got.ModelNumber)
|
|
|
|
assert.Equal(t, updateData.Manufacturer, got.Manufacturer)
|
|
|
|
// assert.Equal(t, updateData.PurchaseTime, got.PurchaseTime)
|
|
|
|
assert.Equal(t, updateData.PurchaseFrom, got.PurchaseFrom)
|
|
|
|
assert.Equal(t, updateData.PurchasePrice, got.PurchasePrice)
|
|
|
|
// assert.Equal(t, updateData.SoldTime, got.SoldTime)
|
|
|
|
assert.Equal(t, updateData.SoldTo, got.SoldTo)
|
|
|
|
assert.Equal(t, updateData.SoldPrice, got.SoldPrice)
|
|
|
|
assert.Equal(t, updateData.SoldNotes, got.SoldNotes)
|
|
|
|
assert.Equal(t, updateData.Notes, got.Notes)
|
2022-09-09 18:20:38 +00:00
|
|
|
// assert.Equal(t, updateData.WarrantyExpires, got.WarrantyExpires)
|
|
|
|
assert.Equal(t, updateData.WarrantyDetails, got.WarrantyDetails)
|
|
|
|
assert.Equal(t, updateData.LifetimeWarranty, got.LifetimeWarranty)
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
2023-02-05 21:12:54 +00:00
|
|
|
|
|
|
|
func TestItemRepository_GetAllCustomFields(t *testing.T) {
|
|
|
|
const FIELDS_COUNT = 5
|
|
|
|
|
|
|
|
entity := useItems(t, 1)[0]
|
|
|
|
|
|
|
|
fields := make([]ItemField, FIELDS_COUNT)
|
|
|
|
names := make([]string, FIELDS_COUNT)
|
|
|
|
values := make([]string, FIELDS_COUNT)
|
|
|
|
|
|
|
|
for i := 0; i < FIELDS_COUNT; i++ {
|
|
|
|
name := fk.Str(10)
|
|
|
|
fields[i] = ItemField{
|
|
|
|
Name: name,
|
|
|
|
Type: "text",
|
|
|
|
TextValue: fk.Str(10),
|
|
|
|
}
|
|
|
|
names[i] = name
|
|
|
|
values[i] = fields[i].TextValue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := tRepos.Items.UpdateByGroup(context.Background(), tGroup.ID, ItemUpdate{
|
|
|
|
ID: entity.ID,
|
|
|
|
Name: entity.Name,
|
|
|
|
LocationID: entity.Location.ID,
|
|
|
|
Fields: fields,
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Test getting all fields
|
|
|
|
{
|
|
|
|
results, err := tRepos.Items.GetAllCustomFieldNames(context.Background(), tGroup.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.ElementsMatch(t, names, results)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test getting all values from field
|
|
|
|
{
|
|
|
|
results, err := tRepos.Items.GetAllCustomFieldValues(context.Background(), tUser.GroupID, names[0])
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.ElementsMatch(t, values[:1], results)
|
|
|
|
}
|
|
|
|
}
|