File size: 4,832 Bytes
16cdcb7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | package subscriptiontestutils
import (
"context"
"testing"
"github.com/alpacahq/alpacadecimal"
"github.com/invopop/gobl/currency"
"github.com/samber/lo"
"github.com/openmeterio/openmeter/openmeter/productcatalog"
"github.com/openmeterio/openmeter/openmeter/productcatalog/addon"
"github.com/openmeterio/openmeter/pkg/models"
)
func BuildAddonForTesting(t *testing.T, period productcatalog.EffectivePeriod, typ productcatalog.AddonInstanceType, rcs ...productcatalog.RateCard) addon.CreateAddonInput {
t.Helper()
inp := addon.CreateAddonInput{
NamespacedModel: models.NamespacedModel{
Namespace: ExampleNamespace,
},
Addon: productcatalog.Addon{
AddonMeta: productcatalog.AddonMeta{
Name: "Test Addon",
Description: lo.ToPtr("Test Addon Description"),
EffectivePeriod: period,
Key: "test-addon",
Version: 1,
Currency: currency.USD,
InstanceType: typ,
Metadata: models.NewMetadata(map[string]string{
"test": "test",
}),
},
RateCards: rcs,
},
}
return inp
}
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,
}
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,
}
// Adds a bool entitlement and a 100 flat price without feature
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 100 flat price to ExampleFeatureKey2
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 50 usage to ExampleFeatureKey
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 a boolean entitlement to ExampleFeatureKey
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,
}
func GetExampleAddonInput(t *testing.T, effectivePeriod productcatalog.EffectivePeriod) addon.CreateAddonInput {
return BuildAddonForTesting(t, effectivePeriod, productcatalog.AddonInstanceTypeSingle, ExampleAddonRateCard1.Clone())
}
type testAddonService struct {
addon.Service
}
func NewTestAddonService(svc addon.Service) *testAddonService {
return &testAddonService{svc}
}
func (s *testAddonService) CreateTestAddon(t *testing.T, addInp addon.CreateAddonInput) addon.Addon {
t.Helper()
add, err := s.CreateAddon(context.Background(), addInp)
if err != nil {
t.Fatalf("failed to create addon: %v", err)
}
add, err = s.PublishAddon(context.Background(), addon.PublishAddonInput{
NamespacedID: add.NamespacedID,
EffectivePeriod: addInp.EffectivePeriod,
})
if err != nil {
t.Fatalf("failed to publish addon: %v", err)
}
return *add
}
|