-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscription_spec.rb
348 lines (281 loc) · 13.8 KB
/
subscription_spec.rb
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
require 'rspec'
require_relative 'subscription'
module SubscriptionSpecHelpers
def residues(subscriptions)
subscriptions.map(&:residue)
end
def unique_residues(subscriptions)
subscriptions.map(&:residue).uniq
end
def beginning
Subscription.beginning
end
end
describe Subscription do
include SubscriptionSpecHelpers
describe '#initialize' do
context 'daily frequency' do
subject { Subscription.new(interval: 14, start_date: Date.tomorrow) }
its(:interval) { should eq(14) }
its(:start_date) { should eq(Date.tomorrow) }
its(:frequency) { should eq(:daily) }
end
context 'monthly frequency' do
subject { Subscription.new(interval: 14, start_date: Date.tomorrow, frequency: :monthly) }
its(:interval) { should eq(14) }
its(:start_date) { should eq(Date.tomorrow) }
its(:frequency) { should eq(:monthly) }
end
end
describe '#residue' do
context 'when the frequency is daily' do
context 'when interval is weekly' do
it 'computes members of the residue class 0' do
subscriptions = [
Subscription.new(interval: 7, start_date: beginning),
Subscription.new(interval: 7, start_date: beginning + 7.days),
Subscription.new(interval: 7, start_date: beginning + 70.days)
]
expect(unique_residues(subscriptions)).to eq([0])
end
it 'computes members of the residue class 1' do
subscriptions = [
Subscription.new(interval: 7, start_date: beginning + 1.day),
Subscription.new(interval: 7, start_date: beginning + 8.days),
Subscription.new(interval: 7, start_date: beginning + 71.days)
]
expect(unique_residues(subscriptions)).to eq([1])
end
it 'computes the complete residue system' do
subscriptions = [
Subscription.new(interval: 7, start_date: beginning),
Subscription.new(interval: 7, start_date: beginning + 1.day),
Subscription.new(interval: 7, start_date: beginning + 2.days),
Subscription.new(interval: 7, start_date: beginning + 3.days),
Subscription.new(interval: 7, start_date: beginning + 4.days),
Subscription.new(interval: 7, start_date: beginning + 5.days),
Subscription.new(interval: 7, start_date: beginning + 6.days),
Subscription.new(interval: 7, start_date: beginning + 7.days)
]
expect(residues(subscriptions)).to eq([0, 1, 2, 3, 4, 5, 6, 0])
end
end
context 'when interval is every 100 days' do
it 'computes members of the residue class 0' do
subscriptions = [
Subscription.new(interval: 100, start_date: beginning),
Subscription.new(interval: 100, start_date: beginning + 100.days),
Subscription.new(interval: 100, start_date: beginning + 500.days)
]
expect(unique_residues(subscriptions)).to eq([0])
end
it 'computes members of the residue class 25' do
subscriptions = [
Subscription.new(interval: 100, start_date: beginning + 25.day),
Subscription.new(interval: 100, start_date: beginning + 125.days),
Subscription.new(interval: 100, start_date: beginning + 525.days)
]
expect(unique_residues(subscriptions)).to eq([25])
end
end
end
context 'when the frequency is monthly' do
context 'when interval is every 5 months' do
it 'computes members of the residue class 0' do
subscriptions = [
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning),
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 5.months),
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 20.months)
]
expect(unique_residues(subscriptions)).to eq([0])
end
it 'computes members of the residue class 1' do
subscriptions = [
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 1.month),
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 6.months),
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 51.months)
]
expect(unique_residues(subscriptions)).to eq([1])
end
it 'computes the complete residue system' do
subscriptions = [
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning),
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 1.month),
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 2.months),
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 3.months),
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 4.months),
Subscription.new(frequency: :monthly, interval: 5, start_date: beginning + 5.months)
]
expect(residues(subscriptions)).to eq([0, 1, 2, 3, 4, 0])
end
end
end
end
describe '#interval=' do
it 'updates the residue when the interval changes' do
subscription = Subscription.new(interval: 30, start_date: beginning + 10.days)
subscription.interval = 9
expect(subscription.residue).to eq(1)
end
end
describe '#start_date=' do
it 'updates the residue when the start_date changes' do
subscription = Subscription.new(interval: 30, start_date: beginning + 10.days)
subscription.start_date = beginning + 5.days
expect(subscription.residue).to eq(5)
end
end
describe '#process_on?' do
context 'when the frequency is daily' do
context 'when the subscription is biweekly and starts 5 days after the beginning' do
let(:subscription) { Subscription.new(interval: 14, start_date: beginning + 5.days) }
it 'processes it on its start date' do
expect(subscription.process_on?(beginning + 5.days)).to be_true
end
it 'processes it on its next processing date' do
expect(subscription.process_on?(beginning + 5.days + 14.days)).to be_true
end
it 'does not process it on the day after its next processing date' do
expect(subscription.process_on?(beginning + 5.days + 15.days)).to be_false
end
end
context 'when the subscription is every 30 days and starts 11 days after the beginning' do
let(:subscription) { Subscription.new(interval: 30, start_date: beginning + 11.days) }
it 'processes it on its start date' do
expect(subscription.process_on?(beginning + 11.days)).to be_true
end
it 'processes it on its next processing date' do
expect(subscription.process_on?(beginning + 11.days + 30.days)).to be_true
end
it 'does not process it on the day before its next processing date' do
expect(subscription.process_on?(beginning + 11.days + 29.days)).to be_false
end
end
it 'should process subscriptions of different intervals on the same day when applicable' do
subscription_1 = Subscription.new(interval: 10, start_date: beginning)
subscription_2 = Subscription.new(interval: 8, start_date: beginning + 2.days)
subscription_3 = Subscription.new(interval: 15, start_date: beginning + 5.days)
subscription_4 = Subscription.new(interval: 10, start_date: beginning + 5.days)
common_date = Date.new(2014, 2, 20)
expect(subscription_1.process_on?(common_date)).to be_true
expect(subscription_2.process_on?(common_date)).to be_true
expect(subscription_3.process_on?(common_date)).to be_true
expect(subscription_4.process_on?(common_date)).to be_false
end
end
context 'when the frequency is monthly' do
context 'when the subscription is every 3 months and starts 2 months after the beginning' do
let(:subscription) { Subscription.new(frequency: :monthly, interval: 3, start_date: beginning + 2.months) }
it 'processes it on its start date' do
expect(subscription.process_on?(beginning + 2.months)).to be_true
end
it 'processes it on its next processing date' do
expect(subscription.process_on?(beginning + 2.months + 3.months)).to be_true
end
it 'does not process it on the month after its next processing date' do
expect(subscription.process_on?(beginning + 2.months + 4.months)).to be_false
end
end
end
end
describe '#next_processing_date' do
context 'when the frequency is daily' do
context 'when the subscription is every 21 days and starts 2 days after the beginning' do
let(:subscription) { Subscription.new(interval: 21, start_date: beginning + 2.days) }
it 'calculates the first processing date' do
expect(subscription.next_processing_date(beginning + 2.days)).to eq(beginning + 2.days)
end
it 'calculates the next processing date 1 day later' do
expect(subscription.next_processing_date(beginning + 2.days + 1.day)).to eq(beginning + 2.days + 21.days)
end
it 'calculates the next processing date 10 days later' do
expect(subscription.next_processing_date(beginning + 2.days + 10.days)).to eq(beginning + 2.days + 21.days)
end
it 'calculates the next processing date on the day of' do
expect(subscription.next_processing_date(beginning + 2.days + 21.days)).to eq(beginning + 2.days + 21.days)
end
it 'calculates the third processing date 25 days later' do
expect(subscription.next_processing_date(beginning + 2.days + 25.days)).to eq(beginning + 2.days + 42.days)
end
end
context 'when the subscription is every 10 days and starts 4 days after the beginning' do
let(:subscription) { Subscription.new(interval: 10, start_date: beginning + 4.days) }
it 'calculates the first processing date' do
expect(subscription.next_processing_date(beginning + 4.days)).to eq(beginning + 4.days)
end
it 'calculates the next processing date 1 day later' do
expect(subscription.next_processing_date(beginning + 4.days + 1.day)).to eq(beginning + 14.days)
end
it 'calculates the next processing date 9 days later' do
expect(subscription.next_processing_date(beginning + 4.days + 9.days)).to eq(beginning + 14.days)
end
it 'calculates the next processing date on the day of' do
expect(subscription.next_processing_date(beginning + 4.days + 10.days)).to eq(beginning + 14.days)
end
it 'calculates the third processing date 17 days later' do
expect(subscription.next_processing_date(beginning + 4.days + 17.days)).to eq(beginning + 24.days)
end
end
end
context 'when the frequency is monthly' do
context 'when the subscription is every 4 months and starts at the beginning' do
let(:subscription) { Subscription.new(frequency: :monthly, interval: 4, start_date: beginning) }
it 'calculates the first processing date' do
expect(subscription.next_processing_date(beginning)).to eq(beginning)
end
it 'calculates the next processing date 1 month later' do
expect(subscription.next_processing_date(beginning + 1.month)).to eq(beginning + 4.months)
end
it 'calculates the next processing date 4 months later' do
expect(subscription.next_processing_date(beginning + 4.months)).to eq(beginning + 4.months)
end
it 'calculates the next processing date 5 months later' do
expect(subscription.next_processing_date(beginning + 5.months)).to eq(beginning + 8.months)
end
it 'calculates the processing date 25 months later' do
expect(subscription.next_processing_date(beginning + 25.months)).to eq(beginning + 28.months)
end
end
end
end
describe '#next_n_processing_dates' do
context 'when the frequency is daily' do
context 'when the subscription is every 7 days and starts 31 days after the beginning' do
let(:subscription) { Subscription.new(interval: 7, start_date: beginning + 31.days) }
it 'calculates the next 5 processing dates 1 day after the first processing' do
expect(subscription.next_n_processing_dates(5, beginning + 31.days + 1.day)).to eq([
beginning + 38.days,
beginning + 45.days,
beginning + 52.days,
beginning + 59.days,
beginning + 66.days
])
end
end
context 'when the subscription is every 40 days and starts 11 days after the beginning' do
let(:subscription) { Subscription.new(interval: 40, start_date: beginning + 11.days) }
it 'calculates the next 3 processing dates 100 days after the first processing' do
expect(subscription.next_n_processing_dates(3, beginning + 11.days + 100.days)).to eq([
beginning + 131.days,
beginning + 171.days,
beginning + 211.days
])
end
end
end
context 'when the frequency is monthly' do
context 'when the subscription is every 7 months and starts 3 months after the beginning' do
let(:subscription) { Subscription.new(frequency: :monthly, interval: 7, start_date: beginning + 3.months) }
it 'calculates the next 5 processing dates 1 month after the first processing' do
expect(subscription.next_n_processing_dates(5, beginning + 3.months + 1.month)).to eq([
beginning + 10.months,
beginning + 17.months,
beginning + 24.months,
beginning + 31.months,
beginning + 38.months
])
end
end
end
end
end