Skip to content

fix: usage based ratecard validation - #4331

Merged
chrisgacsal merged 3 commits into
mainfrom
fix/rc-validation
May 11, 2026
Merged

chrisgacsal merged 3 commits into
mainfrom
fix/rc-validation

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented May 10, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

Ensure that usage based ratecards with usage-based pricing always have valid feature assigned to.

Summary by CodeRabbit

  • Bug Fixes

    • Usage-based rate cards now require an associated feature key; missing feature keys produce a validation error.
  • Tests

    • Extensive test refactors to standardize plan/phase construction and to exercise feature-backed rate cards across unit, graduated, addon, and e2e flows.
    • Updated fixtures and helpers to create and reference features dynamically for more robust round-trip and compatibility checks.

Review Change Stack

@chrisgacsal chrisgacsal self-assigned this May 10, 2026
@chrisgacsal
chrisgacsal requested a review from a team as a code owner May 10, 2026 09:39
@chrisgacsal chrisgacsal added the release-note/bug-fix Release note: Bug Fixes label May 10, 2026
@coderabbitai

coderabbitai Bot commented May 10, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Adds validation enforcing FeatureKey presence for usage-based rate cards; converts NewTestPlan to a transformer-based API (MonthPeriod, WithPlanPhases/WithPlanKey) and updates unit and e2e tests to use feature-backed rate cards and the new helpers.

Changes

Rate Card Validation Enhancement

Layer / File(s) Summary
Validation Error Definition
openmeter/productcatalog/errors.go
New error ErrRateCardUsageBasedPriceWithNoFeature with code usage_based_price_with_no_feature, featureKey field, warning severity, and HTTP 400.
Validation Logic
openmeter/productcatalog/ratecard.go
RateCardMeta.Validate now requires non-flat (usage-based) prices to have FeatureKey set; missing keys append the new validation error.

Test Utility and Suite Migration

Layer / File(s) Summary
Test Utility Refactoring
openmeter/productcatalog/testutils/plan.go
Adds MonthPeriod, generic TransformerFunc[T], WithPlanPhases and WithPlanKey; NewTestPlan builds a default plan and applies transformers instead of accepting positional phases.
Addon Service Tests
openmeter/productcatalog/addon/service/taxcode_test.go
Switches to pctestutils.NewTestPlan(..., pctestutils.WithPlanPhases(...)) and uses pctestutils.MonthPeriod rather than inline duration parsing.
Plan Adapter & Service Tests
openmeter/productcatalog/plan/adapter/adapter_test.go, openmeter/productcatalog/plan/service/*.go
Tests now assemble phases and pass them via WithPlanPhases; TestPostgresAdapter creates features per meter, paginates meter listing, and uses t.Context() for repo calls.
Plan Addon Tests
openmeter/productcatalog/planaddon/...
Wrap existing phase slices in WithPlanPhases when constructing test plans.
Subscription Fixture Updates
openmeter/subscription/...
Replace hardcoded dynamic rate card keys with subscriptiontestutils.ExampleFeatureKey2 / ExampleFeatureKey3 and set corresponding FeatureKey pointers.
E2E Helpers & Tests
e2e/v3helpers_test.go, e2e/*
E2E helpers accept apiv3.Feature and embed Feature.Id in rate cards; tests create meters/features dynamically, attach feature-backed rate cards, adjust assertions, and enable the defective-rate-card subtest.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

area/product-catalog, area/subscriptions

Suggested reviewers

  • tothandras
  • GAlexIHU
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.81% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'fix: usage based ratecard validation' accurately describes the main change—adding validation to ensure usage-based ratecards have a required feature assigned.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/rc-validation

Warning

Review ran into problems

🔥 Problems

Timed out fetching pipeline failures after 30000ms

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@openmeter/productcatalog/plan/adapter/adapter_test.go`:
- Around line 25-26: Replace the use of context.Background() when creating the
test context so the test's lifecycle is tied to the testing harness: in the test
where you call context.WithCancel to create ctx and cancel (symbols: ctx,
cancel, context.WithCancel), pass t.Context() instead of context.Background() so
cancellation and deadlines follow the test (use t.Context() as the parent).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 190f7302-73b7-4fd3-92d8-c9f4b4d03fa3

📥 Commits

Reviewing files that changed from the base of the PR and between b2e7c9d and e67c543.

📒 Files selected for processing (11)
  • openmeter/productcatalog/addon/service/taxcode_test.go
  • openmeter/productcatalog/errors.go
  • openmeter/productcatalog/plan/adapter/adapter_test.go
  • openmeter/productcatalog/plan/service/service_test.go
  • openmeter/productcatalog/plan/service/taxcode_test.go
  • openmeter/productcatalog/planaddon/adapter/adapter_test.go
  • openmeter/productcatalog/planaddon/service/service_test.go
  • openmeter/productcatalog/ratecard.go
  • openmeter/productcatalog/testutils/plan.go
  • openmeter/subscription/addon/diff/restore_test.go
  • openmeter/subscription/workflow/service/addon_test.go

@chrisgacsal
chrisgacsal marked this pull request as draft May 10, 2026 11:35
@chrisgacsal
chrisgacsal marked this pull request as ready for review May 11, 2026 19:38

@turip turip left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
e2e/e2e_test.go (1)

1569-1571: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Avoid strict Len == 1 here to reduce flaky e2e behavior.

This can still fail if the same meter already has multiple features (shared DB / reruns). It’s safer to assert that the created feature is present.

Suggested tweak
 		features, err := featureListResp.JSON200.AsListFeaturesResult0()
 		require.NoError(t, err)
-		require.Len(t, features, 1)
+		require.NotEmpty(t, features)
+		found := false
+		for _, f := range features {
+			if f.Id == featureId {
+				found = true
+				break
+			}
+		}
+		require.True(t, found, "created feature not found in filtered feature list")

As per coding guidelines **/*_test.go: “Make sure the tests are comprehensive and cover the changes.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@e2e/e2e_test.go` around lines 1569 - 1571, The test currently asserts
require.Len(t, features, 1) which is flaky; instead, decode the list via
featureListResp.JSON200.AsListFeaturesResult0() into features and assert that
the expected created feature exists in that slice (e.g., by matching its ID or
name). Replace the strict require.Len check with a presence check over features
(using require.True with a loop/any() predicate or
require.Contains/require.Equal on a mapped list of IDs/names) so the test passes
even if extra features exist.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@e2e/productcatalog_smoke_v3_test.go`:
- Around line 106-111: The comment is outdated: it claims the graduated rate
card has no feature, but the test now passes features[1] into
validGraduatedRateCard; update the block comment around the three rate-card
shapes to reflect that the graduated card does include a feature (e.g., mention
validGraduatedRateCard(features[1]) supplies a feature) so readers aren't
confused by the earlier statement that the graduated case has "no feature."

---

Outside diff comments:
In `@e2e/e2e_test.go`:
- Around line 1569-1571: The test currently asserts require.Len(t, features, 1)
which is flaky; instead, decode the list via
featureListResp.JSON200.AsListFeaturesResult0() into features and assert that
the expected created feature exists in that slice (e.g., by matching its ID or
name). Replace the strict require.Len check with a presence check over features
(using require.True with a loop/any() predicate or
require.Contains/require.Equal on a mapped list of IDs/names) so the test passes
even if extra features exist.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: bc11a356-3983-4d6d-81ba-671149989b91

📥 Commits

Reviewing files that changed from the base of the PR and between e67c543 and abdce99.

📒 Files selected for processing (15)
  • e2e/addons_v3_test.go
  • e2e/e2e_test.go
  • e2e/productcatalog_smoke_v3_test.go
  • e2e/v3helpers_test.go
  • openmeter/productcatalog/addon/service/taxcode_test.go
  • openmeter/productcatalog/errors.go
  • openmeter/productcatalog/plan/adapter/adapter_test.go
  • openmeter/productcatalog/plan/service/service_test.go
  • openmeter/productcatalog/plan/service/taxcode_test.go
  • openmeter/productcatalog/planaddon/adapter/adapter_test.go
  • openmeter/productcatalog/planaddon/service/service_test.go
  • openmeter/productcatalog/ratecard.go
  • openmeter/productcatalog/testutils/plan.go
  • openmeter/subscription/addon/diff/restore_test.go
  • openmeter/subscription/workflow/service/addon_test.go
🚧 Files skipped from review as they are similar to previous changes (11)
  • openmeter/productcatalog/ratecard.go
  • openmeter/subscription/addon/diff/restore_test.go
  • openmeter/productcatalog/planaddon/service/service_test.go
  • openmeter/productcatalog/errors.go
  • openmeter/productcatalog/planaddon/adapter/adapter_test.go
  • openmeter/subscription/workflow/service/addon_test.go
  • openmeter/productcatalog/addon/service/taxcode_test.go
  • openmeter/productcatalog/testutils/plan.go
  • openmeter/productcatalog/plan/service/taxcode_test.go
  • openmeter/productcatalog/plan/service/service_test.go
  • openmeter/productcatalog/plan/adapter/adapter_test.go

Comment thread e2e/productcatalog_smoke_v3_test.go Resolved
@chrisgacsal
chrisgacsal merged commit 50cdf62 into main May 11, 2026
42 of 43 checks passed
@chrisgacsal
chrisgacsal deleted the fix/rc-validation branch May 11, 2026 19:53

This branch was previously deployed

1 inactive deployment
dev — abdce990 Deployed May 11, 2026 by chrisgacsal via Migration Checks #14297
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants