From 88bb9a4f52b39190283800ec20bd757cbad7fe34 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Wed, 19 Oct 2022 20:34:50 -0800 Subject: [PATCH] WIP: repository tests --- backend/internal/repo/repo_items.go | 33 ++++++++++++++++++---- backend/internal/repo/repo_items_test.go | 36 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/backend/internal/repo/repo_items.go b/backend/internal/repo/repo_items.go index c7ca5e6..acecf73 100644 --- a/backend/internal/repo/repo_items.go +++ b/backend/internal/repo/repo_items.go @@ -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 diff --git a/backend/internal/repo/repo_items_test.go b/backend/internal/repo/repo_items_test.go index f600ad1..f4df2df 100644 --- a/backend/internal/repo/repo_items_test.go +++ b/backend/internal/repo/repo_items_test.go @@ -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)