File size: 7,444 Bytes
d6f631f | 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | package patch_test
import (
"errors"
"testing"
"time"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/openmeterio/openmeter/openmeter/productcatalog"
psubs "github.com/openmeterio/openmeter/openmeter/productcatalog/subscription"
"github.com/openmeterio/openmeter/openmeter/subscription"
"github.com/openmeterio/openmeter/openmeter/subscription/patch"
subscriptiontestutils "github.com/openmeterio/openmeter/openmeter/subscription/testutils"
"github.com/openmeterio/openmeter/openmeter/testutils"
"github.com/openmeterio/openmeter/pkg/clock"
"github.com/openmeterio/openmeter/pkg/currencyx"
"github.com/openmeterio/openmeter/pkg/datetime"
"github.com/openmeterio/openmeter/pkg/models"
)
func TestRemoveAdd(t *testing.T) {
now := testutils.GetRFC3339Time(t, "2021-01-01T00:00:01Z")
clock.SetTime(now)
t.Run("Can remove then add an item in a future phase", func(t *testing.T) {
s, _ := getDefaultSpec(t, now)
// Let's validate the spec looks as expected
require.GreaterOrEqual(t, len(s.Phases), 3)
p3, ok := s.Phases["test_phase_3"]
require.True(t, ok)
require.GreaterOrEqual(t, len(p3.ItemsByKey), 1)
v, ok := p3.ItemsByKey[subscriptiontestutils.ExampleFeatureKey]
require.True(t, ok)
require.GreaterOrEqual(t, len(v), 1)
// Let's remove an item from the last phase
rmP := &patch.PatchRemoveItem{
PhaseKey: "test_phase_3",
ItemKey: subscriptiontestutils.ExampleFeatureKey,
}
// Then add it back with changes
nSpec := *v[0]
nSpec.RateCard = nSpec.RateCard.Clone()
require.NoError(t, nSpec.CreateSubscriptionItemPlanInput.RateCard.ChangeMeta(func(m productcatalog.RateCardMeta) (productcatalog.RateCardMeta, error) {
m.Name = "new_name"
return m, nil
}))
assert.NotEqual(t, "new_name", s.Phases["test_phase_3"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey][0].RateCard.AsMeta().Name)
addP := &patch.PatchAddItem{
PhaseKey: "test_phase_3",
ItemKey: subscriptiontestutils.ExampleFeatureKey,
CreateInput: nSpec,
}
err := s.ApplyMany(lo.Map([]subscription.Patch{rmP, addP}, subscription.ToApplies), subscription.ApplyContext{
CurrentTime: now,
})
require.NoError(t, err)
// Let's validate that the new version of the item is present
found := s.Phases["test_phase_3"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey][0]
assert.Equal(t, "new_name", found.RateCard.AsMeta().Name)
})
t.Run("Can remove then add an item in the current phase", func(t *testing.T) {
s, _ := getDefaultSpec(t, now)
now := now.AddDate(0, 1, 1)
latestNowWeGoTu := now.Add(time.Hour * 3)
// Let's validate the spec looks as expected
require.GreaterOrEqual(t, len(s.Phases), 3)
// Let's make sure we are in the second phase
p2, ok := s.Phases["test_phase_2"]
require.True(t, ok)
p3, ok := s.Phases["test_phase_3"]
require.True(t, ok)
p2st, _ := p2.StartAfter.AddTo(s.ActiveFrom)
require.True(t, now.After(p2st))
require.True(t, latestNowWeGoTu.After(p2st))
p3st, _ := p3.StartAfter.AddTo(s.ActiveFrom)
require.True(t, now.Before(p3st))
require.True(t, latestNowWeGoTu.Before(p3st))
// Let's make sure the phase has the item we're trying to remove
require.GreaterOrEqual(t, len(p2.ItemsByKey), 1)
v, ok := p2.ItemsByKey[subscriptiontestutils.ExampleFeatureKey]
require.True(t, ok)
require.GreaterOrEqual(t, len(v), 1)
// Let's remove an item from the second phase
rmP := &patch.PatchRemoveItem{
PhaseKey: "test_phase_2",
ItemKey: subscriptiontestutils.ExampleFeatureKey,
}
// Then add it back with changes
nSpec := *v[0]
nSpec.RateCard = nSpec.RateCard.Clone()
require.NoError(t, nSpec.CreateSubscriptionItemPlanInput.RateCard.ChangeMeta(func(m productcatalog.RateCardMeta) (productcatalog.RateCardMeta, error) {
m.Name = "new_name"
return m, nil
}))
assert.NotEqual(t, "new_name", s.Phases["test_phase_2"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey][0].RateCard.AsMeta().Name)
addP := &patch.PatchAddItem{
PhaseKey: "test_phase_2",
ItemKey: subscriptiontestutils.ExampleFeatureKey,
CreateInput: nSpec,
}
err := s.ApplyMany(lo.Map([]subscription.Patch{rmP, addP}, subscription.ToApplies), subscription.ApplyContext{
CurrentTime: now,
})
require.NoError(t, err)
// Let's validate that the old version is kept expiring now, and the new version is added starting now
found := s.Phases["test_phase_2"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey]
assert.Len(t, found, 2)
assert.Equal(t, lo.ToPtr(datetime.MustParseDuration(t, "PT86400S")), found[0].ActiveToOverrideRelativeToPhaseStart)
assert.Equal(t, lo.ToPtr(datetime.MustParseDuration(t, "PT86400S")), found[1].ActiveFromOverrideRelativeToPhaseStart)
// Now lets simulate some time passing
now = now.Add(time.Hour * 1)
require.True(t, now.Before(latestNowWeGoTu))
// And lets repeat the same process
err = s.ApplyMany(lo.Map([]subscription.Patch{rmP, addP}, subscription.ToApplies), subscription.ApplyContext{
CurrentTime: now,
})
require.NoError(t, err)
// Let's validate that the previous one was closed and the new one is added
found = s.Phases["test_phase_2"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey]
assert.Len(t, found, 3)
assert.Equal(t, lo.ToPtr(datetime.MustParseDuration(t, "PT86400S")), found[0].ActiveToOverrideRelativeToPhaseStart)
assert.Equal(t, lo.ToPtr(datetime.MustParseDuration(t, "PT86400S")), found[1].ActiveFromOverrideRelativeToPhaseStart)
// 90000s = 25h = 1d + 1h
assert.Equal(t, lo.ToPtr(datetime.MustParseDuration(t, "PT90000S")), found[1].ActiveToOverrideRelativeToPhaseStart)
assert.Equal(t, lo.ToPtr(datetime.MustParseDuration(t, "PT90000S")), found[2].ActiveFromOverrideRelativeToPhaseStart)
})
}
// utils
type testcase[T subscription.AppliesToSpec] struct {
Name string
Patch T
GetSpec func(t *testing.T) *subscription.SubscriptionSpec
Ctx subscription.ApplyContext
GetExpectedSpec func(t *testing.T) subscription.SubscriptionSpec
ExpectedError error
}
type testsuite[T subscription.AppliesToSpec] struct {
SystemTime time.Time
TT []testcase[T]
}
func (ts *testsuite[T]) Run(t *testing.T) {
for _, tc := range ts.TT {
t.Run(tc.Name, func(t *testing.T) {
spec := tc.GetSpec(t)
// It's safe to use patch.ApplyTo here as we're only testing the patch logic
err := tc.Patch.ApplyTo(spec, tc.Ctx)
if tc.ExpectedError == nil {
assert.NoError(t, err)
} else {
assert.True(t, errors.As(err, lo.ToPtr(tc.ExpectedError.(any))))
assert.EqualError(t, err, tc.ExpectedError.Error())
}
if err == nil {
require.NotNil(t, tc.GetExpectedSpec)
expectedSpec := tc.GetExpectedSpec(t)
subscriptiontestutils.SpecsEqual(t, expectedSpec, *spec)
}
})
}
}
func getDefaultSpec(t *testing.T, activeFrom time.Time) (*subscription.SubscriptionSpec, *psubs.Plan) {
pInp := subscriptiontestutils.GetExamplePlanInput(t)
p := &psubs.Plan{
Plan: pInp.Plan,
Ref: &models.NamespacedID{Namespace: pInp.Namespace, ID: pInp.Key},
}
spec, err := subscription.NewSpecFromPlan(p, subscription.CreateSubscriptionCustomerInput{
Name: "Test Plan",
CustomerId: "test_customer",
Currency: currencyx.Code("USD"),
ActiveFrom: activeFrom,
BillingAnchor: activeFrom,
})
require.Nil(t, err)
return &spec, p
}
|