File size: 7,351 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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 TestRemoveItem(t *testing.T) {
	now := testutils.GetRFC3339Time(t, "2021-01-01T00:00:01Z")
	clock.SetTime(now)

	getSpec := func(t *testing.T) *subscription.SubscriptionSpec {
		s, _ := getDefaultSpec(t, now)
		return s
	}

	suite := testsuite[patch.PatchRemoveItem]{
		SystemTime: now,
		TT: []testcase[patch.PatchRemoveItem]{
			{
				Name: "Should not find phase",
				Patch: patch.PatchRemoveItem{
					PhaseKey: "notfound",
					ItemKey:  "item1",
				},
				GetSpec: getSpec,
				Ctx:     subscription.ApplyContext{CurrentTime: now},
				ExpectedError: &subscription.PatchValidationError{
					Msg: "phase notfound not found",
				},
			},
			{
				Name: "Should not find item",
				Patch: patch.PatchRemoveItem{
					PhaseKey: "test_phase_1",
					ItemKey:  "invalid",
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s, _ := getDefaultSpec(t, now)

					// Lets validate the spec looks as we expect it
					require.GreaterOrEqual(t, len(s.Phases), 1)
					ph, ok := s.Phases["test_phase_1"]
					require.True(t, ok)

					_, ok = ph.ItemsByKey["invalid"]
					require.False(t, ok)

					return s
				},
				Ctx: subscription.ApplyContext{CurrentTime: now},
				ExpectedError: &subscription.PatchValidationError{
					Msg: "items for key invalid doesn't exists in phase test_phase_1",
				},
			},
			{
				Name: "Should not remove item from past phase",
				Patch: patch.PatchRemoveItem{
					PhaseKey: "test_phase_1",
					ItemKey:  subscriptiontestutils.ExampleFeatureKey,
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s, _ := getDefaultSpec(t, now)

					// Lets validate the spec looks as we expect it
					require.GreaterOrEqual(t, len(s.Phases), 3)

					ph2, ok := s.Phases["test_phase_2"]
					require.True(t, ok)

					ph3, ok := s.Phases["test_phase_3"]
					require.True(t, ok)

					ts := now.AddDate(0, 1, 1)

					// Let's make sure we're in the second phase
					ph2st, _ := ph2.StartAfter.AddTo(s.ActiveFrom)
					require.True(t, ph2st.Before(ts))

					ph3st, _ := ph3.StartAfter.AddTo(s.ActiveFrom)
					require.True(t, ph3st.After(ts))

					// Let's make sure the 1st phase has the item we're removing
					ph1, ok := s.Phases["test_phase_1"]
					require.True(t, ok)

					v, ok := ph1.ItemsByKey[subscriptiontestutils.ExampleFeatureKey]
					require.True(t, ok)
					require.Greater(t, len(v), 0)

					return s
				},
				Ctx: subscription.ApplyContext{
					CurrentTime: now.AddDate(0, 1, 1), // same as ts above
				},
				ExpectedError: &subscription.PatchForbiddenError{
					Msg: "cannot remove item from phase test_phase_1 which starts before current phase",
				},
			},
			{
				Name: "Should not remove item from inactive subscription",
				Patch: patch.PatchRemoveItem{
					PhaseKey: "test_phase_1",
					ItemKey:  subscriptiontestutils.ExampleFeatureKey,
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s, _ := getDefaultSpec(t, now)

					s.ActiveTo = lo.ToPtr(s.ActiveFrom.AddDate(0, 9, 0))

					return s
				},
				Ctx: subscription.ApplyContext{
					CurrentTime: now.AddDate(1, 0, 0), // We're far in the future
				},
				ExpectedError: &subscription.PatchForbiddenError{
					Msg: "cannot remove item from phase test_phase_1 which starts before current phase",
				},
			},
			{
				Name: "Should remove item from future phase",
				Patch: patch.PatchRemoveItem{
					PhaseKey: "test_phase_3",
					ItemKey:  subscriptiontestutils.ExampleFeatureKey,
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s, _ := getDefaultSpec(t, now)

					// Lets validate the spec looks as we expect it
					require.GreaterOrEqual(t, len(s.Phases), 3)

					ph2, ok := s.Phases["test_phase_2"]
					require.True(t, ok)

					ph3, ok := s.Phases["test_phase_3"]
					require.True(t, ok)

					ts := now.AddDate(0, 1, 1)

					// Let's make sure we're in the second phase
					ph2st, _ := ph2.StartAfter.AddTo(s.ActiveFrom)
					require.True(t, ph2st.Before(ts))

					ph3st, _ := ph3.StartAfter.AddTo(s.ActiveFrom)
					require.True(t, ph3st.After(ts))

					// Let's make sure the 3rd phase has the item we're removing
					v, ok := ph3.ItemsByKey[subscriptiontestutils.ExampleFeatureKey]
					require.True(t, ok)
					require.Greater(t, len(v), 0)

					return s
				},
				Ctx: subscription.ApplyContext{
					CurrentTime: now.AddDate(0, 1, 1), // same as ts above
				},
				GetExpectedSpec: func(t *testing.T) subscription.SubscriptionSpec {
					s, _ := getDefaultSpec(t, now)

					// Let's make sure what items we have in the 3rd phase
					require.Equal(t, 2, len(s.Phases["test_phase_3"].ItemsByKey))

					require.NotNil(t, s.Phases["test_phase_3"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey])

					// Let's remove the item from the 3rd phase
					delete(s.Phases["test_phase_3"].ItemsByKey, subscriptiontestutils.ExampleFeatureKey)

					return *s
				},
			},
			{
				Name: "Should remove item from current phase by closing the previous version of it",
				Patch: patch.PatchRemoveItem{
					PhaseKey: "test_phase_2",
					ItemKey:  subscriptiontestutils.ExampleFeatureKey,
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s, _ := getDefaultSpec(t, now)

					// Lets validate the spec looks as we expect it
					require.GreaterOrEqual(t, len(s.Phases), 3)

					ph2, ok := s.Phases["test_phase_2"]
					require.True(t, ok)

					ph3, ok := s.Phases["test_phase_3"]
					require.True(t, ok)

					ts := now.AddDate(0, 1, 1)

					// Let's make sure we're in the second phase
					ph2st, _ := ph2.StartAfter.AddTo(s.ActiveFrom)
					require.True(t, ph2st.Before(ts))

					ph3st, _ := ph3.StartAfter.AddTo(s.ActiveFrom)
					require.True(t, ph3st.After(ts))

					// Let's make sure the 2nd phase has the item we're removing
					v, ok := ph2.ItemsByKey[subscriptiontestutils.ExampleFeatureKey]
					require.True(t, ok)
					require.Greater(t, len(v), 0)

					return s
				},
				Ctx: subscription.ApplyContext{
					CurrentTime: now.AddDate(0, 1, 1), // same as ts above
				},
				GetExpectedSpec: func(t *testing.T) subscription.SubscriptionSpec {
					s, _ := getDefaultSpec(t, now)

					// Let's make sure that's the only item in the 3rd phase
					require.GreaterOrEqual(t, len(s.Phases["test_phase_2"].ItemsByKey), 1)
					v, ok := s.Phases["test_phase_2"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey]
					require.True(t, ok)
					require.Equal(t, len(v), 1)

					// When removing an item from the current phase it should close that item with the current timestamp
					// We have to use seconds due to how duration is managed
					s.Phases["test_phase_2"].ItemsByKey[subscriptiontestutils.ExampleFeatureKey][0].ActiveToOverrideRelativeToPhaseStart = lo.ToPtr(datetime.MustParseDuration(t, "PT86400S"))

					return *s
				},
			},
		},
	}

	suite.Run(t)
}