subscriptiontestutils

package
v1.0.0-beta.217 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2025 License: Apache-2.0 Imports: 56 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ExampleFeatureKey       = "test-feature-1"
	ExampleFeatureKey2      = "test-feature-2"
	ExampleFeatureKey3      = "test-feature-3"
	ExampleFeatureMeterSlug = "meter1"
)
View Source
var (
	ExamplePriceAmount int64                             = 100
	ExampleRateCard1   productcatalog.UsageBasedRateCard = productcatalog.UsageBasedRateCard{
		RateCardMeta: productcatalog.RateCardMeta{
			Key:         ExampleFeatureKey,
			Name:        "Rate Card 1",
			Description: lo.ToPtr("Rate Card 1 Description"),
			FeatureKey:  lo.ToPtr(ExampleFeatureKey),
			FeatureID:   nil,
			EntitlementTemplate: productcatalog.NewEntitlementTemplateFrom(productcatalog.MeteredEntitlementTemplate{
				IssueAfterReset: lo.ToPtr(100.0),
				UsagePeriod:     ISOMonth,
			}),
			TaxConfig: &productcatalog.TaxConfig{
				Stripe: &productcatalog.StripeTaxConfig{
					Code: "txcd_10000000",
				},
			},
			Price: productcatalog.NewPriceFrom(productcatalog.UnitPrice{
				Amount: alpacadecimal.NewFromInt(ExamplePriceAmount),
			}),
		},
		BillingCadence: ISOMonth,
	}
	ExampleRateCard2 productcatalog.FlatFeeRateCard = productcatalog.FlatFeeRateCard{
		RateCardMeta: productcatalog.RateCardMeta{
			Key:         "rate-card-2",
			Name:        "Rate Card 2",
			Description: lo.ToPtr("Rate Card 2 Description"),
			FeatureKey:  nil,
			FeatureID:   nil,
			Price: productcatalog.NewPriceFrom(productcatalog.FlatPrice{
				Amount:      alpacadecimal.NewFromInt(int64(0)),
				PaymentTerm: productcatalog.InAdvancePaymentTerm,
			}),
		},
		BillingCadence: &ISOMonth,
	}
	ExampleRateCard3ForAddons productcatalog.FlatFeeRateCard = productcatalog.FlatFeeRateCard{
		RateCardMeta: productcatalog.RateCardMeta{
			Key:         ExampleFeatureKey2,
			Name:        "Rate Card 3 for Addons",
			Description: lo.ToPtr("Rate Card 3 Description"),
			FeatureKey:  lo.ToPtr(ExampleFeatureKey2),
			FeatureID:   nil,
			EntitlementTemplate: productcatalog.NewEntitlementTemplateFrom(productcatalog.MeteredEntitlementTemplate{
				IssueAfterReset: lo.ToPtr(100.0),
				UsagePeriod:     ISOMonth,
			}),
			TaxConfig: &productcatalog.TaxConfig{
				Stripe: &productcatalog.StripeTaxConfig{
					Code: "txcd_10000000",
				},
			},
			Price: productcatalog.NewPriceFrom(productcatalog.FlatPrice{
				Amount:      alpacadecimal.NewFromInt(ExamplePriceAmount),
				PaymentTerm: productcatalog.InAdvancePaymentTerm,
			}),
		},
		BillingCadence: &ISOMonth,
	}
	// Has a boolean entitlement
	ExampleRateCard4ForAddons productcatalog.FlatFeeRateCard = productcatalog.FlatFeeRateCard{
		RateCardMeta: productcatalog.RateCardMeta{
			Key:                 ExampleFeatureKey,
			Name:                "Rate Card 4 for Addons",
			Description:         lo.ToPtr("Rate Card 4 Description"),
			FeatureKey:          lo.ToPtr(ExampleFeatureKey),
			FeatureID:           nil,
			EntitlementTemplate: productcatalog.NewEntitlementTemplateFrom(productcatalog.BooleanEntitlementTemplate{}),
		},
		BillingCadence: &ISOMonth,
	}
	// DOES NOT have a boolean entitlement
	ExampleRateCard5ForAddons productcatalog.FlatFeeRateCard = productcatalog.FlatFeeRateCard{
		RateCardMeta: productcatalog.RateCardMeta{
			Key:                 ExampleFeatureKey,
			Name:                "Rate Card 5 for Addons",
			Description:         lo.ToPtr("Rate Card 5 Description"),
			FeatureKey:          lo.ToPtr(ExampleFeatureKey),
			FeatureID:           nil,
			EntitlementTemplate: nil,
		},
		BillingCadence: &ISOMonth,
	}
	ExampleRateCardWithDiscounts productcatalog.FlatFeeRateCard = productcatalog.FlatFeeRateCard{
		RateCardMeta: productcatalog.RateCardMeta{
			Key:         ExampleFeatureKey,
			Name:        "Rate Card 1",
			Description: lo.ToPtr("Rate Card 1 Description"),
			FeatureKey:  lo.ToPtr(ExampleFeatureKey),
			FeatureID:   nil,
			EntitlementTemplate: productcatalog.NewEntitlementTemplateFrom(productcatalog.MeteredEntitlementTemplate{
				IssueAfterReset: lo.ToPtr(100.0),
				UsagePeriod:     ISOMonth,
			}),
			TaxConfig: &productcatalog.TaxConfig{
				Stripe: &productcatalog.StripeTaxConfig{
					Code: "txcd_10000000",
				},
			},
			Price: productcatalog.NewPriceFrom(productcatalog.UnitPrice{
				Amount: alpacadecimal.NewFromInt(ExamplePriceAmount),
			}),
			Discounts: productcatalog.Discounts{
				Percentage: &productcatalog.PercentageDiscount{
					Percentage: models.NewPercentage(10),
				},
			},
		},
		BillingCadence: &ISOMonth,
	}
)
View Source
var ExampleAddonRateCard1 = productcatalog.FlatFeeRateCard{
	RateCardMeta: productcatalog.RateCardMeta{
		Name:        "Test Addon Rate Card 1",
		Description: lo.ToPtr("Test Addon Rate Card 1 Description"),
		Key:         ExampleFeatureKey,
		FeatureKey:  lo.ToPtr(ExampleFeatureKey),
	},
	BillingCadence: &ISOMonth,
}
View Source
var ExampleAddonRateCard2 = productcatalog.FlatFeeRateCard{
	RateCardMeta: productcatalog.RateCardMeta{
		Name:        "Test Addon Rate Card 2",
		Description: lo.ToPtr("Test Addon Rate Card 2 Description"),
		Key:         "addon-rc-key-2",
	},
	BillingCadence: &ISOMonth,
}
View Source
var ExampleAddonRateCard3 = productcatalog.FlatFeeRateCard{
	RateCardMeta: productcatalog.RateCardMeta{
		Name:                "Test Addon Rate Card 3",
		Description:         lo.ToPtr("Test Addon Rate Card 3 Description"),
		Key:                 ExampleFeatureKey3,
		FeatureKey:          lo.ToPtr(ExampleFeatureKey3),
		EntitlementTemplate: productcatalog.NewEntitlementTemplateFrom(productcatalog.BooleanEntitlementTemplate{}),
		Price: productcatalog.NewPriceFrom(productcatalog.FlatPrice{
			Amount:      alpacadecimal.NewFromInt(100),
			PaymentTerm: productcatalog.InAdvancePaymentTerm,
		}),
	},
	BillingCadence: &ISOMonth,
}

Adds a bool entitlement and a 100 flat price without feature

View Source
var ExampleAddonRateCard4 = productcatalog.FlatFeeRateCard{
	RateCardMeta: productcatalog.RateCardMeta{
		Name:        "Test Addon Rate Card 4",
		Description: lo.ToPtr("Test Addon Rate Card 4 Description"),
		Key:         ExampleFeatureKey2,
		FeatureKey:  lo.ToPtr(ExampleFeatureKey2),
		Price: productcatalog.NewPriceFrom(productcatalog.FlatPrice{
			Amount:      alpacadecimal.NewFromInt(100),
			PaymentTerm: productcatalog.InAdvancePaymentTerm,
		}),
	},
	BillingCadence: &ISOMonth,
}

Adds a 100 flat price to ExampleFeatureKey2

View Source
var ExampleAddonRateCard5 = productcatalog.FlatFeeRateCard{
	RateCardMeta: productcatalog.RateCardMeta{
		Name:        "Test Addon Rate Card 5",
		Description: lo.ToPtr("Test Addon Rate Card 5 Description"),
		Key:         ExampleFeatureKey,
		FeatureKey:  lo.ToPtr(ExampleFeatureKey),
		EntitlementTemplate: productcatalog.NewEntitlementTemplateFrom(productcatalog.MeteredEntitlementTemplate{
			IssueAfterReset: lo.ToPtr(50.0),
			UsagePeriod:     ISOMonth,
		}),
	},
	BillingCadence: &ISOMonth,
}

Adds 50 usage to ExampleFeatureKey

View Source
var ExampleAddonRateCard6 = productcatalog.FlatFeeRateCard{
	RateCardMeta: productcatalog.RateCardMeta{
		Name:                "Test Addon Rate Card 6",
		Description:         lo.ToPtr("Test Addon Rate Card 6 Description"),
		Key:                 ExampleFeatureKey,
		FeatureKey:          lo.ToPtr(ExampleFeatureKey),
		EntitlementTemplate: productcatalog.NewEntitlementTemplateFrom(productcatalog.BooleanEntitlementTemplate{}),
	},
	BillingCadence: &ISOMonth,
}

Adds a boolean entitlement to ExampleFeatureKey

View Source
var ExampleCreateCustomerInput customer.CreateCustomerInput = customer.CreateCustomerInput{
	Namespace: ExampleNamespace,
	CustomerMutate: customer.CustomerMutate{
		Name:             ExampleCustomerEntity.Name,
		PrimaryEmail:     ExampleCustomerEntity.PrimaryEmail,
		Currency:         ExampleCustomerEntity.Currency,
		UsageAttribution: ExampleCustomerEntity.UsageAttribution,
	},
}
View Source
var ExampleCustomerEntity customer.Customer = customer.Customer{
	ManagedResource: models.ManagedResource{
		Name: "John Doe",
	},
	PrimaryEmail: lo.ToPtr("mail@me.uk"),
	Currency:     lo.ToPtr(currencyx.Code("USD")),
	UsageAttribution: customer.CustomerUsageAttribution{
		SubjectKeys: []string{"john-doe"},
	},
}
View Source
var ExampleFeature = feature.CreateFeatureInputs{
	Name:      "Example Feature",
	Key:       ExampleFeatureKey,
	Namespace: ExampleNamespace,
	MeterSlug: lo.ToPtr(ExampleFeatureMeterSlug),
}
View Source
var ExampleFeature2 = feature.CreateFeatureInputs{
	Name:      "Example Feature 2",
	Key:       ExampleFeatureKey2,
	Namespace: ExampleNamespace,
	MeterSlug: lo.ToPtr(ExampleFeatureMeterSlug),
}
View Source
var ExampleFeature3 = feature.CreateFeatureInputs{
	Name:      "Example Feature 3",
	Key:       ExampleFeatureKey3,
	Namespace: ExampleNamespace,
	MeterSlug: lo.ToPtr(ExampleFeatureMeterSlug),
}
View Source
var ExampleNamespace = "test-namespace"
View Source
var ISOMonth, _ = datetime.ISODurationString("P1M").Parse()

Functions

func BuildTestPlan

func BuildTestPlan(t *testing.T) *testPlanbuilder

func CreateMultiInstanceAddonForSubscription

For most cases, use the workflow service instead!

func CreateSubscriptionFromPlan

func CreateSubscriptionFromPlan(t *testing.T, deps *SubscriptionDependencies, plan subscription.Plan, startAt time.Time) subscription.SubscriptionView

func GetExampleAddonInput

func GetExampleAddonInput(t *testing.T, effectivePeriod productcatalog.EffectivePeriod) addon.CreateAddonInput

func GetExamplePlanInput

func GetExamplePlanInput(t *testing.T) plan.CreatePlanInput

func NewCustomerAdapter

func NewCustomerAdapter(t *testing.T, dbDeps *DBDeps) *testCustomerRepo

func NewCustomerService

func NewCustomerService(t *testing.T, dbDeps *DBDeps) customer.Service

func NewPlanHelper

func NewPlanHelper(planService plan.Service) *planHelper

func NewSubscriptionItemRepo

func NewSubscriptionItemRepo(t *testing.T, dbDeps *DBDeps) testSubscriptionItemRepo

func NewSubscriptionPhaseRepo

func NewSubscriptionPhaseRepo(t *testing.T, dbDeps *DBDeps) testSubscriptionPhaseRepo

func NewSubscriptionRepo

func NewSubscriptionRepo(t *testing.T, dbDeps *DBDeps) testSubscriptionRepo

func NewTestAddonService

func NewTestAddonService(svc addon.Service) *testAddonService

func NewTestFeatureConnector

func NewTestFeatureConnector(conn feature.FeatureConnector) *testFeatureConnector

func SpecsEqual

func SpecsEqual(t *testing.T, s1, s2 subscription.SubscriptionSpec)

func SubscriptionAddonsEqual

func SubscriptionAddonsEqual(t *testing.T, a1, a2 subscriptionaddon.SubscriptionAddon)

func ValidateSpecAndView

func ValidateSpecAndView(t *testing.T, expected subscription.SubscriptionSpec, found subscription.SubscriptionView)

Ensures the created view matches the input spec

Types

type DBDeps

type DBDeps struct {
	DBClient  *db.Client
	EntDriver *entdriver.EntPostgresDriver
	PGDriver  *pgdriver.Driver
}

func SetupDBDeps

func SetupDBDeps(t *testing.T) *DBDeps

func (*DBDeps) Cleanup

func (d *DBDeps) Cleanup(t *testing.T)

type MockService

type MockService struct {
	CreateFn                 func(ctx context.Context, namespace string, spec subscription.SubscriptionSpec) (subscription.Subscription, error)
	UpdateFn                 func(ctx context.Context, subscriptionID models.NamespacedID, target subscription.SubscriptionSpec) (subscription.Subscription, error)
	DeleteFn                 func(ctx context.Context, subscriptionID models.NamespacedID) error
	CancelFn                 func(ctx context.Context, subscriptionID models.NamespacedID, at subscription.Timing) (subscription.Subscription, error)
	ContinueFn               func(ctx context.Context, subscriptionID models.NamespacedID) (subscription.Subscription, error)
	GetFn                    func(ctx context.Context, subscriptionID models.NamespacedID) (subscription.Subscription, error)
	GetViewFn                func(ctx context.Context, subscriptionID models.NamespacedID) (subscription.SubscriptionView, error)
	ListFn                   func(ctx context.Context, params subscription.ListSubscriptionsInput) (subscription.SubscriptionList, error)
	GetAllForCustomerSinceFn func(ctx context.Context, customerID models.NamespacedID, at time.Time) ([]subscription.Subscription, error)
	Validators               []subscription.SubscriptionValidator
}

func (*MockService) Cancel

func (*MockService) Continue

func (s *MockService) Continue(ctx context.Context, subscriptionID models.NamespacedID) (subscription.Subscription, error)

func (*MockService) Create

func (*MockService) Delete

func (s *MockService) Delete(ctx context.Context, subscriptionID models.NamespacedID) error

func (*MockService) Get

func (*MockService) GetAllForCustomerSince

func (s *MockService) GetAllForCustomerSince(ctx context.Context, customerID models.NamespacedID, at time.Time) ([]subscription.Subscription, error)

func (*MockService) GetView

func (s *MockService) GetView(ctx context.Context, subscriptionID models.NamespacedID) (subscription.SubscriptionView, error)

func (*MockService) RegisterValidator

func (s *MockService) RegisterValidator(validator subscription.SubscriptionValidator) error

func (*MockService) Update

type MockWorkflowService

type MockWorkflowService struct {
	CreateFromPlanFn func(ctx context.Context, inp subscriptionworkflow.CreateSubscriptionWorkflowInput, plan subscription.Plan) (subscription.SubscriptionView, error)
	EditRunningFn    func(ctx context.Context, subscriptionID models.NamespacedID, customizations []subscription.Patch) (subscription.SubscriptionView, error)
	ChangeToPlanFn   func(ctx context.Context, subscriptionID models.NamespacedID, inp subscriptionworkflow.ChangeSubscriptionWorkflowInput, plan subscription.Plan) (current subscription.Subscription, new subscription.SubscriptionView, err error)
}

func (*MockWorkflowService) EditRunning

func (s *MockWorkflowService) EditRunning(ctx context.Context, subscriptionID models.NamespacedID, customizations []subscription.Patch) (subscription.SubscriptionView, error)

type SubscriptionDependencies

type SubscriptionDependencies struct {
	ItemRepo                 subscription.SubscriptionItemRepository
	CustomerAdapter          *testCustomerRepo
	CustomerService          customer.Service
	FeatureConnector         *testFeatureConnector
	MeterService             meter.Service
	MockStreamingConnector   *streamingtestutils.MockStreamingConnector
	EntitlementAdapter       subscription.EntitlementAdapter
	PlanHelper               *planHelper
	PlanService              plan.Service
	DBDeps                   *DBDeps
	EntitlementRegistry      *registry.Entitlement
	SubscriptionService      subscription.Service
	WorkflowService          subscriptionworkflow.Service
	SubscriptionAddonService subscriptionaddon.Service
	AddonService             *testAddonService
	PlanAddonService         planaddon.Service
}

func NewService

func NewService(t *testing.T, dbDeps *DBDeps) SubscriptionDependencies

type TestPatch

type TestPatch struct {
	PatchValue     any
	PatchOperation subscription.PatchOperation
	PatchPath      subscription.SpecPath

	ApplyToFn  func(s *subscription.SubscriptionSpec, c subscription.ApplyContext) error
	ValdiateFn func() error
}

func (*TestPatch) ApplyTo

func (*TestPatch) MarshalJSON

func (p *TestPatch) MarshalJSON() ([]byte, error)

func (*TestPatch) Op

func (*TestPatch) Path

func (p *TestPatch) Path() subscription.SpecPath

func (*TestPatch) UnmarshalJSON

func (p *TestPatch) UnmarshalJSON(data []byte) error

func (*TestPatch) Validate

func (p *TestPatch) Validate() error

func (*TestPatch) Value

func (p *TestPatch) Value() any

func (*TestPatch) ValueAsAny

func (p *TestPatch) ValueAsAny() any

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL