WIP: repository tests

This commit is contained in:
Hayden 2022-10-19 20:34:50 -08:00
parent c5617045b1
commit 88bb9a4f52
2 changed files with 64 additions and 5 deletions

View file

@ -39,15 +39,17 @@ type (
}
ItemCreate struct {
ImportRef string `json:"-"`
Name string `json:"name"`
Description string `json:"description"`
ImportRef string `json:"-"`
ParentID uuid.UUID `json:"parentId"`
Name string `json:"name"`
Description string `json:"description"`
// Edges
LocationID uuid.UUID `json:"locationId"`
LabelIDs []uuid.UUID `json:"labelIds"`
}
ItemUpdate struct {
ParentID uuid.UUID `json:"parentId"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
@ -100,6 +102,7 @@ type (
}
ItemOut struct {
Parent ItemSummary `json:"parent,omitempty"`
ItemSummary
SerialNumber string `json:"serialNumber"`
@ -126,8 +129,8 @@ type (
Notes string `json:"notes"`
Attachments []ItemAttachment `json:"attachments"`
// Future
Fields []ItemField `json:"fields"`
Fields []ItemField `json:"fields"`
Children []ItemSummary `json:"children"`
}
)
@ -194,7 +197,18 @@ func mapItemOut(item *ent.Item) ItemOut {
fields = mapFields(item.Edges.Fields)
}
var children []ItemSummary
if item.Edges.Children != nil {
children = mapEach(item.Edges.Children, mapItemSummary)
}
var parent ItemSummary
if item.Edges.Parent != nil {
parent = mapItemSummary(item.Edges.Parent)
}
return ItemOut{
Parent: parent,
ItemSummary: mapItemSummary(item),
LifetimeWarranty: item.LifetimeWarranty,
WarrantyExpires: item.WarrantyExpires,
@ -220,6 +234,7 @@ func mapItemOut(item *ent.Item) ItemOut {
Notes: item.Notes,
Attachments: attachments,
Fields: fields,
Children: children,
}
}
@ -231,6 +246,8 @@ func (e *ItemsRepository) getOne(ctx context.Context, where ...predicate.Item) (
WithLabel().
WithLocation().
WithGroup().
WithChildren().
WithParent().
WithAttachments(func(aq *ent.AttachmentQuery) {
aq.WithDocument()
}).
@ -398,6 +415,12 @@ func (e *ItemsRepository) UpdateByGroup(ctx context.Context, gid uuid.UUID, data
q.RemoveLabelIDs(set.Slice()...)
}
if data.ParentID != uuid.Nil {
q.SetParentID(data.ParentID)
} else {
q.ClearParent()
}
err = q.Exec(ctx)
if err != nil {
return ItemOut{}, err

View file

@ -41,6 +41,42 @@ func useItems(t *testing.T, len int) []ItemOut {
return items
}
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.Equal(t, uuid.Nil, updated.Parent.ID)
}
}
func TestItemsRepository_GetOne(t *testing.T) {
entity := useItems(t, 3)