| package patch_test |
|
|
| import ( |
| "testing" |
|
|
| "github.com/samber/lo" |
| "github.com/stretchr/testify/require" |
|
|
| "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/datetime" |
| ) |
|
|
| func TestUnscheduleEdit(t *testing.T) { |
| now := testutils.GetRFC3339Time(t, "2021-01-01T00:00:01Z") |
| clock.SetTime(now) |
|
|
| t.Run("Patch should be valid", func(t *testing.T) { |
| patch := patch.PatchUnscheduleEdit{} |
| require.NoError(t, patch.Validate()) |
| }) |
|
|
| getSpec := func(t *testing.T) *subscription.SubscriptionSpec { |
| s, _ := getDefaultSpec(t, now) |
| return s |
| } |
|
|
| suite := testsuite[patch.PatchUnscheduleEdit]{ |
| SystemTime: now, |
| TT: []testcase[patch.PatchUnscheduleEdit]{ |
| { |
| Name: "Should not find phase", |
| Patch: patch.PatchUnscheduleEdit{}, |
| GetSpec: func(t *testing.T) *subscription.SubscriptionSpec { |
| s := getSpec(t) |
| delete(s.Phases, "test_phase_1") |
| return s |
| }, |
| Ctx: subscription.ApplyContext{CurrentTime: now}, |
| ExpectedError: &subscription.PatchConflictError{ |
| Msg: "current phase doesn't exist, cannot unschedule edits", |
| }, |
| }, |
| { |
| Name: "Should unschedule future edits in current phase", |
| Patch: patch.PatchUnscheduleEdit{}, |
| GetSpec: func(t *testing.T) *subscription.SubscriptionSpec { |
| s := getSpec(t) |
|
|
| |
| phase := s.Phases["test_phase_1"] |
| items := phase.ItemsByKey[subscriptiontestutils.ExampleFeatureKey] |
|
|
| |
| futureItem := *items[0] |
| items[0].ActiveToOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P2D")) |
|
|
| futureItem.ActiveFromOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P2D")) |
| items = append(items, &futureItem) |
|
|
| phase.ItemsByKey[subscriptiontestutils.ExampleFeatureKey] = items |
| return s |
| }, |
| Ctx: subscription.ApplyContext{CurrentTime: now}, |
| GetExpectedSpec: func(t *testing.T) subscription.SubscriptionSpec { |
| s := getSpec(t) |
| |
| require.Len(t, s.Phases["test_phase_1"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey], 1) |
| return *s |
| }, |
| }, |
| { |
| Name: "Should handle phase with no scheduled edits", |
| Patch: patch.PatchUnscheduleEdit{}, |
| GetSpec: func(t *testing.T) *subscription.SubscriptionSpec { |
| s := getSpec(t) |
| |
| return s |
| }, |
| Ctx: subscription.ApplyContext{CurrentTime: now}, |
| GetExpectedSpec: func(t *testing.T) subscription.SubscriptionSpec { |
| s := getSpec(t) |
| |
| return *s |
| }, |
| }, |
| { |
| Name: "Should handle multiple scheduled edits", |
| Patch: patch.PatchUnscheduleEdit{}, |
| GetSpec: func(t *testing.T) *subscription.SubscriptionSpec { |
| s := getSpec(t) |
|
|
| |
| phase := s.Phases["test_phase_1"] |
| items := phase.ItemsByKey[subscriptiontestutils.ExampleFeatureKey] |
|
|
| |
| futureItem1 := *items[0] |
| futureItem1.ActiveFromOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P2D")) |
|
|
| futureItem2 := *items[0] |
| futureItem2.ActiveFromOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P3D")) |
|
|
| items = append(items, &futureItem1, &futureItem2) |
| phase.ItemsByKey[subscriptiontestutils.ExampleFeatureKey] = items |
|
|
| return s |
| }, |
| Ctx: subscription.ApplyContext{CurrentTime: now}, |
| GetExpectedSpec: func(t *testing.T) subscription.SubscriptionSpec { |
| s := getSpec(t) |
| |
| require.Len(t, s.Phases["test_phase_1"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey], 1) |
| return *s |
| }, |
| }, |
| { |
| Name: "Should handle multiple scheduled edits #2", |
| Patch: patch.PatchUnscheduleEdit{}, |
| GetSpec: func(t *testing.T) *subscription.SubscriptionSpec { |
| s := getSpec(t) |
|
|
| |
| phase := s.Phases["test_phase_1"] |
| items := phase.ItemsByKey[subscriptiontestutils.ExampleFeatureKey] |
|
|
| |
| futureItem1 := *items[0] |
| items[0].ActiveToOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P1D")) |
|
|
| futureItem1.ActiveFromOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P1D")) |
| futureItem1.ActiveToOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P3D")) |
|
|
| futureItem2 := *items[0] |
| futureItem2.ActiveFromOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P3D")) |
| futureItem2.ActiveToOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P4D")) |
|
|
| futureItem3 := *items[0] |
| futureItem3.ActiveFromOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P4D")) |
|
|
| items = append(items, &futureItem1, &futureItem2, &futureItem3) |
| phase.ItemsByKey[subscriptiontestutils.ExampleFeatureKey] = items |
|
|
| return s |
| }, |
| Ctx: subscription.ApplyContext{CurrentTime: now.AddDate(0, 0, 2)}, |
| GetExpectedSpec: func(t *testing.T) subscription.SubscriptionSpec { |
| s := getSpec(t) |
|
|
| |
| phase := s.Phases["test_phase_1"] |
| items := phase.ItemsByKey[subscriptiontestutils.ExampleFeatureKey] |
| futureItem1 := *items[0] |
| items[0].ActiveToOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P1D")) |
|
|
| futureItem1.ActiveFromOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "P1D")) |
|
|
| items = append(items, &futureItem1) |
| phase.ItemsByKey[subscriptiontestutils.ExampleFeatureKey] = items |
|
|
| |
| require.Len(t, s.Phases["test_phase_1"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey], 2) |
| return *s |
| }, |
| }, |
| }, |
| } |
|
|
| suite.Run(t) |
| } |
|
|