Skip to content

Commit a8ac7c3

Browse files
committed
fix: update hook tests and rename inputs
1 parent 840dfbd commit a8ac7c3

File tree

9 files changed

+36
-44
lines changed

9 files changed

+36
-44
lines changed

openmeter/customer/adapter/customer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,14 @@ func (a *adapter) GetCustomerByUsageAttribution(ctx context.Context, input custo
466466
customerdb.Or(
467467
// We lookup the customer by subject key in the subjects table
468468
customerdb.HasSubjectsWith(
469-
customersubjectsdb.SubjectKey(input.SubjectKey),
469+
customersubjectsdb.SubjectKey(input.Key),
470470
customersubjectsdb.Or(
471471
customersubjectsdb.DeletedAtIsNil(),
472472
customersubjectsdb.DeletedAtGT(now),
473473
),
474474
),
475475
// Or else we lookup the customer by key in the customers table
476-
customerdb.Key(input.SubjectKey),
476+
customerdb.Key(input.Key),
477477
),
478478
).
479479
Where(customerdb.DeletedAtIsNil())
@@ -486,7 +486,7 @@ func (a *adapter) GetCustomerByUsageAttribution(ctx context.Context, input custo
486486
if err != nil {
487487
if entdb.IsNotFound(err) {
488488
return nil, models.NewGenericNotFoundError(
489-
fmt.Errorf("customer with subject key %s not found in %s namespace", input.SubjectKey, input.Namespace),
489+
fmt.Errorf("customer with subject key %s not found in %s namespace", input.Key, input.Namespace),
490490
)
491491
}
492492

openmeter/customer/customer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,10 @@ func (c CustomerUsageAttribution) GetFirstSubjectKey() (string, error) {
231231

232232
// GetCustomerByUsageAttributionInput represents the input for the GetCustomerByUsageAttribution method
233233
type GetCustomerByUsageAttributionInput struct {
234-
Namespace string
235-
SubjectKey string
234+
Namespace string
235+
236+
// The key of either the customer or one of its subjects
237+
Key string
236238

237239
// Expand
238240
Expands Expands
@@ -243,7 +245,7 @@ func (i GetCustomerByUsageAttributionInput) Validate() error {
243245
return models.NewGenericValidationError(errors.New("namespace is required"))
244246
}
245247

246-
if i.SubjectKey == "" {
248+
if i.Key == "" {
247249
return models.NewGenericValidationError(errors.New("subject key is required"))
248250
}
249251

openmeter/customer/service/hooks/subjectcustomer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func (s subjectCustomerHook) PostDelete(ctx context.Context, sub *subject.Subjec
6868

6969
// Let's get the customer by usage attribution
7070
cus, err := s.provisioner.customer.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
71-
Namespace: sub.Namespace,
72-
SubjectKey: sub.Key,
71+
Namespace: sub.Namespace,
72+
Key: sub.Key,
7373
})
7474
if err != nil {
7575
if models.IsGenericNotFoundError(err) {
@@ -305,8 +305,8 @@ var ErrCustomerKeyConflict = errors.New("customer key conflict")
305305
func (p CustomerProvisioner) getCustomerForSubject(ctx context.Context, sub *subject.Subject) (*customer.Customer, error) {
306306
// Try to find Customer for Subject by usage attribution
307307
cus, err := p.customer.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
308-
Namespace: sub.Namespace,
309-
SubjectKey: sub.Key,
308+
Namespace: sub.Namespace,
309+
Key: sub.Key,
310310
})
311311
if err != nil && !models.IsGenericNotFoundError(err) {
312312
return nil, err

openmeter/customer/service/hooks/subjectcustomer_test.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ func TestCustomerProvisioner_EnsureCustomer(t *testing.T) {
150150
require.NoError(t, err, "creating subject should not fail")
151151
assert.NotNilf(t, sub, "subject must not be nil")
152152

153-
cus, err := env.CustomerService.CreateCustomer(ctx, customer.CreateCustomerInput{
153+
cusForSubject, err := provisioner.EnsureCustomer(ctx, &sub)
154+
require.NoError(t, err, "provisioning customer should not fail")
155+
assert.NotNilf(t, cusForSubject, "customer must not be nil")
156+
157+
_, err = env.CustomerService.CreateCustomer(ctx, customer.CreateCustomerInput{
154158
Namespace: namespace,
155159
CustomerMutate: customer.CustomerMutate{
156160
Key: lo.ToPtr(sub.Key),
@@ -170,22 +174,8 @@ func TestCustomerProvisioner_EnsureCustomer(t *testing.T) {
170174
Annotation: nil,
171175
},
172176
})
173-
require.NoError(t, err, "creating customer should not fail")
174-
assert.NotNilf(t, cus, "customer must not be nil")
175-
176-
cus, err = provisioner.EnsureCustomer(ctx, &sub)
177-
require.NoError(t, err, "provisioning customer should not fail")
178-
assert.NotNilf(t, cus, "customer must not be nil")
179177

180-
cus, err = env.CustomerService.GetCustomer(ctx, customer.GetCustomerInput{
181-
CustomerID: &customer.CustomerID{
182-
Namespace: cus.Namespace,
183-
ID: cus.ID,
184-
},
185-
})
186-
require.NoErrorf(t, err, "getting customer for subject should not fail")
187-
assert.NotNilf(t, cus, "customer must not be nil")
188-
AssertSubjectCustomerEqual(t, &sub, cus)
178+
require.True(t, models.IsGenericConflictError(err), "creating customer should fail with conflict")
189179
})
190180

191181
t.Run("CustomerKeyMismatch", func(t *testing.T) {

openmeter/customer/service/service_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func Test_CustomerService(t *testing.T) {
8686

8787
t.Run("ByUsageAttribution", func(t *testing.T) {
8888
cusByUsage, err := env.CustomerService.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
89-
Namespace: cus.Namespace,
90-
SubjectKey: cus.UsageAttribution.SubjectKeys[0],
89+
Namespace: cus.Namespace,
90+
Key: cus.UsageAttribution.SubjectKeys[0],
9191
})
9292
require.NoError(t, err, "getting customer usage attribution should not fail")
9393
assert.NotNilf(t, cusByUsage, "customer must not be nil")
@@ -128,8 +128,8 @@ func Test_CustomerService(t *testing.T) {
128128

129129
t.Run("ByUsageAttribution", func(t *testing.T) {
130130
cusByUsage, err := env.CustomerService.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
131-
Namespace: cus.Namespace,
132-
SubjectKey: subjectKeys[1],
131+
Namespace: cus.Namespace,
132+
Key: subjectKeys[1],
133133
})
134134
require.NoError(t, err, "getting customer usage attribution should not fail")
135135
assert.NotNilf(t, cusByUsage, "customer must not be nil")

openmeter/entitlement/driver/entitlement.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,8 @@ func (h *entitlementHandler) resolveCustomerFromSubject(ctx context.Context, nam
569569
}
570570

571571
cust, err := h.customerService.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
572-
Namespace: namespace,
573-
SubjectKey: subj.Key,
572+
Namespace: namespace,
573+
Key: subj.Key,
574574
})
575575
if err != nil {
576576
return nil, err

openmeter/entitlement/driver/metered.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ func (h *meteredEntitlementHandler) resolveCustomerFromSubject(ctx context.Conte
395395
}
396396

397397
cust, err := h.customerService.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
398-
Namespace: namespace,
399-
SubjectKey: subj.Key,
398+
Namespace: namespace,
399+
Key: subj.Key,
400400
})
401401
if err != nil {
402402
return nil, err

openmeter/meterevent/adapter/event.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ func (a *adapter) enrichEventsWithCustomerID(ctx context.Context, namespace stri
277277
// FIXME: do this in a batches to avoid hitting the database for each event
278278
// Get the customer by usage attribution subject key
279279
cust, err := a.customerService.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
280-
Namespace: namespace,
281-
SubjectKey: event.Subject,
280+
Namespace: namespace,
281+
Key: event.Subject,
282282
})
283283
if err != nil {
284284
if models.IsGenericNotFoundError(err) {

test/customer/customer.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,8 @@ func (s *CustomerHandlerTestSuite) TestGetByUsageAttribution(ctx context.Context
765765

766766
// Get the customer by usage attribution
767767
cus, err := service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
768-
Namespace: s.namespace,
769-
SubjectKey: TestSubjectKeys[0],
768+
Namespace: s.namespace,
769+
Key: TestSubjectKeys[0],
770770
})
771771

772772
require.NoError(t, err, "Fetching customer must not return error")
@@ -776,8 +776,8 @@ func (s *CustomerHandlerTestSuite) TestGetByUsageAttribution(ctx context.Context
776776

777777
// Get the customer by key
778778
cus, err = service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
779-
Namespace: s.namespace,
780-
SubjectKey: TestKey,
779+
Namespace: s.namespace,
780+
Key: TestKey,
781781
})
782782

783783
require.NoError(t, err, "Fetching customer must not return error")
@@ -787,8 +787,8 @@ func (s *CustomerHandlerTestSuite) TestGetByUsageAttribution(ctx context.Context
787787

788788
// Get the customer by key
789789
cus, err = service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
790-
Namespace: s.namespace,
791-
SubjectKey: TestKey,
790+
Namespace: s.namespace,
791+
Key: TestKey,
792792
})
793793

794794
require.NoError(t, err, "Fetching customer must not return error")
@@ -798,8 +798,8 @@ func (s *CustomerHandlerTestSuite) TestGetByUsageAttribution(ctx context.Context
798798

799799
// Get the customer by usage attribution with a non-existent subject key
800800
_, err = service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
801-
Namespace: s.namespace,
802-
SubjectKey: "non-existent-subject-key",
801+
Namespace: s.namespace,
802+
Key: "non-existent-subject-key",
803803
})
804804

805805
require.True(t, models.IsGenericNotFoundError(err), "Fetching customer with non-existent subject key must return not found error")

0 commit comments

Comments
 (0)