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

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

View file

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