Skip to content

Commit

Permalink
Fix package update from Create()
Browse files Browse the repository at this point in the history
`package_.Create()` expects the pkg pointer value to be updated in
place, but `persistence.CreatePackage()` creates a new Package from the
database data.
  • Loading branch information
djjuhasz committed Mar 15, 2024
1 parent c118444 commit 76ac796
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/package_/package_.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ func (svc *packageImpl) Goa() goapackage.Service {
}

func (svc *packageImpl) Create(ctx context.Context, pkg *datatypes.Package) error {
pkg, err := svc.perSvc.CreatePackage(ctx, pkg)
p, err := svc.perSvc.CreatePackage(ctx, pkg)
if err != nil {
return fmt.Errorf("package: create: %v", err)
}

ev := &goapackage.PackageCreatedEvent{ID: uint(pkg.ID), Item: pkg.Goa()}
event.PublishEvent(ctx, svc.evsvc, ev)
// Set the value of pkg to the returned package p.
*pkg = *p

event.PublishEvent(
ctx,
svc.evsvc,
&goapackage.PackageCreatedEvent{ID: uint(pkg.ID), Item: pkg.Goa()},
)

return nil
}
Expand Down
12 changes: 12 additions & 0 deletions internal/package_/package__test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"testing"
"time"

"github.com/go-logr/logr"
"go.artefactual.dev/tools/mockutil"
Expand Down Expand Up @@ -43,6 +44,7 @@ func TestCreatePackage(t *testing.T) {
name string
pkg datatypes.Package
mock func(*persistence_fake.MockService, datatypes.Package) *persistence_fake.MockService
want datatypes.Package
wantErr string
}
for _, tt := range []test{
Expand All @@ -60,11 +62,20 @@ func TestCreatePackage(t *testing.T) {
DoAndReturn(
func(ctx context.Context, p *datatypes.Package) (*datatypes.Package, error) {
p.ID = 1
p.CreatedAt = time.Date(2024, 3, 14, 15, 57, 25, 0, time.UTC)
return p, nil
},
)
return svc
},
want: datatypes.Package{
ID: 1,
Name: "test",
WorkflowID: "4258090a-e27b-4fd9-a76b-28deb3d16813",
RunID: "8f3a5756-6bc5-4d82-846d-59442dd6ad8f",
Status: enums.NewPackageStatus("new"),
CreatedAt: time.Date(2024, 3, 14, 15, 57, 25, 0, time.UTC),
},
},
{
name: "errors creating a package with a missing RunID",
Expand Down Expand Up @@ -102,6 +113,7 @@ func TestCreatePackage(t *testing.T) {
return
}

assert.DeepEqual(t, pkg, tt.want)
assert.NilError(t, err)
})
}
Expand Down

0 comments on commit 76ac796

Please sign in to comment.