Skip to content

Commit 6ee7e16

Browse files
committed
fix: update hook tests and rename inputs
1 parent a1e75b1 commit 6ee7e16

File tree

10 files changed

+36
-44
lines changed

10 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
@@ -464,14 +464,14 @@ func (a *adapter) GetCustomerByUsageAttribution(ctx context.Context, input custo
464464
customerdb.Or(
465465
// We lookup the customer by subject key in the subjects table
466466
customerdb.HasSubjectsWith(
467-
customersubjectsdb.SubjectKey(input.SubjectKey),
467+
customersubjectsdb.SubjectKey(input.Key),
468468
customersubjectsdb.Or(
469469
customersubjectsdb.DeletedAtIsNil(),
470470
customersubjectsdb.DeletedAtGT(now),
471471
),
472472
),
473473
// Or else we lookup the customer by key in the customers table
474-
customerdb.Key(input.SubjectKey),
474+
customerdb.Key(input.Key),
475475
),
476476
).
477477
Where(customerdb.DeletedAtIsNil())
@@ -484,7 +484,7 @@ func (a *adapter) GetCustomerByUsageAttribution(ctx context.Context, input custo
484484
if err != nil {
485485
if entdb.IsNotFound(err) {
486486
return nil, models.NewGenericNotFoundError(
487-
fmt.Errorf("customer with subject key %s not found in %s namespace", input.SubjectKey, input.Namespace),
487+
fmt.Errorf("customer with subject key %s not found in %s namespace", input.Key, input.Namespace),
488488
)
489489
}
490490

openmeter/customer/customer.go

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

213213
// GetCustomerByUsageAttributionInput represents the input for the GetCustomerByUsageAttribution method
214214
type GetCustomerByUsageAttributionInput struct {
215-
Namespace string
216-
SubjectKey string
215+
Namespace string
216+
217+
// The key of either the customer or one of its subjects
218+
Key string
217219

218220
// Expand
219221
Expands Expands
@@ -224,7 +226,7 @@ func (i GetCustomerByUsageAttributionInput) Validate() error {
224226
return models.NewGenericValidationError(errors.New("namespace is required"))
225227
}
226228

227-
if i.SubjectKey == "" {
229+
if i.Key == "" {
228230
return models.NewGenericValidationError(errors.New("subject key is required"))
229231
}
230232

openmeter/customer/service/hooks/subjectcustomer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ var ErrCustomerKeyConflict = errors.New("customer key conflict")
230230
func (p CustomerProvisioner) getCustomerForSubject(ctx context.Context, sub *subject.Subject) (*customer.Customer, error) {
231231
// Try to find Customer for Subject by usage attribution
232232
cus, err := p.customer.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
233-
Namespace: sub.Namespace,
234-
SubjectKey: sub.Key,
233+
Namespace: sub.Namespace,
234+
Key: sub.Key,
235235
})
236236
if err != nil && !models.IsGenericNotFoundError(err) {
237237
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/hooks/subjectvalidator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type subjectValidatorHook struct {
2828

2929
func (s subjectValidatorHook) PreDelete(ctx context.Context, sub *subject.Subject) error {
3030
cus, err := s.customer.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
31-
Namespace: sub.Namespace,
32-
SubjectKey: sub.Key,
31+
Namespace: sub.Namespace,
32+
Key: sub.Key,
3333
})
3434
if err != nil {
3535
if models.IsGenericNotFoundError(err) {

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
@@ -700,8 +700,8 @@ func (s *CustomerHandlerTestSuite) TestGetByUsageAttribution(ctx context.Context
700700

701701
// Get the customer by usage attribution
702702
cus, err := service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
703-
Namespace: s.namespace,
704-
SubjectKey: TestSubjectKeys[0],
703+
Namespace: s.namespace,
704+
Key: TestSubjectKeys[0],
705705
})
706706

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

712712
// Get the customer by key
713713
cus, err = service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
714-
Namespace: s.namespace,
715-
SubjectKey: TestKey,
714+
Namespace: s.namespace,
715+
Key: TestKey,
716716
})
717717

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

723723
// Get the customer by key
724724
cus, err = service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
725-
Namespace: s.namespace,
726-
SubjectKey: TestKey,
725+
Namespace: s.namespace,
726+
Key: TestKey,
727727
})
728728

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

734734
// Get the customer by usage attribution with a non-existent subject key
735735
_, err = service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
736-
Namespace: s.namespace,
737-
SubjectKey: "non-existent-subject-key",
736+
Namespace: s.namespace,
737+
Key: "non-existent-subject-key",
738738
})
739739

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

0 commit comments

Comments
 (0)