merch_calendar 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,382 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe MerchCalendar::StitchFixFiscalYearCalendar do
4
+ describe "#weeks_in_year" do
5
+ it "returns 53 for a leap year - 2013" do
6
+ expect(subject.weeks_in_year(2013)).to eq 53
7
+ end
8
+
9
+ it "returns 52 for a normal year - 2014" do
10
+ expect(subject.weeks_in_year(2014)).to eq 52
11
+ end
12
+
13
+ it "returns 52 for a normal year - 2015" do
14
+ expect(subject.weeks_in_year(2015)).to eq 52
15
+ end
16
+
17
+ it "returns 52 for a normal year - 2016" do
18
+ expect(subject.weeks_in_year(2016)).to eq 52
19
+ end
20
+
21
+ it "returns 52 for a normal year - 2017" do
22
+ expect(subject.weeks_in_year(2017)).to eq 52
23
+ end
24
+
25
+ it "returns 52 for a normal year - 2018" do
26
+ expect(subject.weeks_in_year(2018)).to eq 52
27
+ end
28
+
29
+ it "returns 53 for a year that includes a 4-5-5 quarter - 2019" do
30
+ expect(subject.weeks_in_year(2019)).to eq 53
31
+ end
32
+
33
+ it "returns 52 for a normal year - 2020" do
34
+ expect(subject.weeks_in_year(2023)).to eq 52
35
+ end
36
+
37
+ it "returns 53 for a year that includes a 4-5-5 quarter - 2024" do
38
+ expect(subject.weeks_in_year(2024)).to eq 53
39
+ end
40
+
41
+ it "returns 52 for a normal year - 2025" do
42
+ expect(subject.weeks_in_year(2025)).to eq 52
43
+ end
44
+ end
45
+
46
+ describe "#start_of_week" do
47
+ it "returns the correct date for 2017-1-1 (2017-Aug-wk1)" do
48
+ expect(subject.start_of_week(2017, 1, 1)).to eq Date.new(2016, 7, 31)
49
+ end
50
+
51
+ it "returns the correct Date for 2018-1-1 (2018-Aug-wk1)" do
52
+ expect(subject.start_of_week(2018, 1, 1)).to eq Date.new(2017, 7, 30)
53
+ end
54
+
55
+ it "returns the correct Date for 2019-1-1 (2019-Aug-wk1)" do
56
+ expect(subject.start_of_week(2019, 1, 1)).to eq Date.new(2018, 7, 29)
57
+ end
58
+
59
+ it "returns the correct Date for 2020-1-1 (2020-Aug-wk1)" do
60
+ expect(subject.start_of_week(2020, 1, 1)).to eq Date.new(2019, 8, 4)
61
+ end
62
+ end
63
+
64
+ describe "#end_of_week" do
65
+ it "returns the correct date for 2017-6-1 (2017-Jan-wk1)" do
66
+ expect(subject.end_of_week(2017, 6, 1)).to eq Date.new(2017, 1, 7)
67
+ end
68
+
69
+ it "returns the correct Date for 2018-6-4 (2018-Jan-wk4)" do
70
+ expect(subject.end_of_week(2018, 6, 4)).to eq Date.new(2018, 1, 27)
71
+ end
72
+
73
+ it "returns the correct Date for 2019-10-3 (2019-May-wk3)" do
74
+ expect(subject.end_of_week(2019, 10, 3)).to eq Date.new(2019, 5, 18)
75
+ end
76
+
77
+ it "returns the correct Date for 2019-12-5 (2019-Jul-wk5)" do
78
+ expect(subject.end_of_week(2019, 12, 5)).to eq Date.new(2019, 8, 3)
79
+ end
80
+
81
+ it "returns the correct Date for 2020-2-5 (2020-Sept-wk5)" do
82
+ expect(subject.end_of_week(2020, 2, 5)).to eq Date.new(2019, 10, 5)
83
+ end
84
+ end
85
+
86
+ describe "#start_of_month" do
87
+ it "returns the correct date for 2018-1 AKA 2018-Aug" do
88
+ expect(subject.start_of_month(2018, 1)).to eq Date.new(2017, 7, 30)
89
+ end
90
+
91
+ it "returns the correct date for 2019-1 AKA 2019-Aug" do
92
+ expect(subject.start_of_month(2019, 1)).to eq Date.new(2018, 7, 29)
93
+ end
94
+
95
+ it "returns the correct date for 2019-1 AKA 2019-Aug" do
96
+ expect(subject.start_of_month(2020, 1)).to eq Date.new(2019, 8, 4)
97
+ end
98
+ end
99
+
100
+ describe "#end_of_month" do
101
+ it "returns the correct date for 2018-1 AKA 2018-Aug" do
102
+ expect(subject.end_of_month(2018, 1)).to eq Date.new(2017, 8, 26)
103
+ end
104
+
105
+ it "returns the correct date for 2019-1 AKA 2019-Aug" do
106
+ expect(subject.end_of_month(2019, 1)).to eq Date.new(2018, 8, 25)
107
+ end
108
+
109
+ it "returns the correct date for 2020-1 AKA 2020-Aug" do
110
+ expect(subject.end_of_month(2020, 1)).to eq Date.new(2019, 8, 31)
111
+ end
112
+
113
+ it "returns the correct date for 2020-12 AKA 2020-July" do
114
+ expect(subject.end_of_month(2020, 12)).to eq Date.new(2020, 8, 1)
115
+ end
116
+ end
117
+
118
+ describe "#start_of_quarter" do
119
+ it "returns the correct date for 2018-Q1" do
120
+ expect(subject.start_of_quarter(2018, 1)).to eq Date.new(2017, 7, 30)
121
+ end
122
+
123
+ it "returns the correct date for 2018-Q4" do
124
+ expect(subject.start_of_quarter(2018, 4)).to eq Date.new(2018, 4, 29)
125
+ end
126
+
127
+ it "returns the correct date for 2019-Q1" do
128
+ expect(subject.start_of_quarter(2019, 1)).to eq Date.new(2018, 7, 29)
129
+ end
130
+
131
+ it "returns the correct date for 2019-Q2" do
132
+ expect(subject.start_of_quarter(2019, 2)).to eq Date.new(2018, 10, 28)
133
+ end
134
+
135
+ it "returns the correct date for 2019-Q3" do
136
+ expect(subject.start_of_quarter(2019, 3)).to eq Date.new(2019, 1, 27)
137
+ end
138
+
139
+ it "returns the correct date for 2020-Q4" do
140
+ expect(subject.start_of_quarter(2020, 4)).to eq Date.new(2020, 5, 3)
141
+ end
142
+
143
+ it "raises an error when there is an invalid quarter" do
144
+ expect { subject.start_of_quarter(2019, 5) }.to raise_error "invalid quarter"
145
+ end
146
+ end
147
+
148
+ describe "#end_of_quarter" do
149
+ it "returns the correct date for 2018-Q1" do
150
+ expect(subject.end_of_quarter(2018, 1)).to eq Date.new(2017, 10, 28)
151
+ end
152
+
153
+ it "returns the correct date for 2018-Q4" do
154
+ expect(subject.end_of_quarter(2018, 4)).to eq Date.new(2018, 7, 28)
155
+ end
156
+
157
+ it "returns the correct date for 2019-Q1" do
158
+ expect(subject.end_of_quarter(2019, 1)).to eq Date.new(2018, 10, 27)
159
+ end
160
+
161
+ it "returns the correct date for 2019-Q2" do
162
+ expect(subject.end_of_quarter(2019, 2)).to eq Date.new(2019, 1, 26)
163
+ end
164
+
165
+ it "returns the correct date for 2019-Q3" do
166
+ expect(subject.end_of_quarter(2019, 3)).to eq Date.new(2019, 4, 27)
167
+ end
168
+
169
+ it "returns the correct date for 2020-Q4" do
170
+ expect(subject.end_of_quarter(2020, 4)).to eq Date.new(2020, 8, 1)
171
+ end
172
+
173
+ it "raises an error when there is an invalid quarter" do
174
+ expect { subject.end_of_quarter(2019, 5) }.to raise_error "invalid quarter"
175
+ end
176
+ end
177
+
178
+ describe "#quarter" do
179
+ it "returns the correct quarter number" do
180
+ expect(subject.quarter(5)).to eq 2
181
+ expect(subject.quarter(7)).to eq 3
182
+ expect(subject.quarter(2)).to eq 1
183
+ expect(subject.quarter(11)).to eq 4
184
+ end
185
+
186
+ it "raises an error when there is an invalid merch month" do
187
+ expect { subject.quarter(13) }.to raise_error "invalid merch month"
188
+ end
189
+ end
190
+
191
+ describe "#season" do
192
+ context "returns Fall/Winter from its merch_month" do
193
+ it { expect(subject.season(1)).to eq "Fall/Winter" }
194
+ it { expect(subject.season(2)).to eq "Fall/Winter" }
195
+ it { expect(subject.season(3)).to eq "Fall/Winter" }
196
+ it { expect(subject.season(4)).to eq "Fall/Winter" }
197
+ it { expect(subject.season(5)).to eq "Fall/Winter" }
198
+ it { expect(subject.season(6)).to eq "Fall/Winter" }
199
+ end
200
+
201
+ context "returns Spring Summer from its merch_month" do
202
+ it { expect(subject.season(7)).to eq "Spring/Summer" }
203
+ it { expect(subject.season(8)).to eq "Spring/Summer" }
204
+ it { expect(subject.season(9)).to eq "Spring/Summer" }
205
+ it { expect(subject.season(10)).to eq "Spring/Summer" }
206
+ it { expect(subject.season(11)).to eq "Spring/Summer" }
207
+ it { expect(subject.season(12)).to eq "Spring/Summer" }
208
+ end
209
+
210
+ it "raises an error when there is an invalid merch month" do
211
+ expect { subject.season(13) }.to raise_error "invalid merch month"
212
+ end
213
+ end
214
+
215
+ describe "#start_of_year" do
216
+ it "returns the correct date for 2018" do
217
+ expect(subject.start_of_year(2018)).to eq Date.new(2017, 7, 30)
218
+ end
219
+
220
+ it "returns the correct date for 2019" do
221
+ expect(subject.start_of_year(2019)).to eq Date.new(2018, 7, 29)
222
+ end
223
+
224
+ it "returns the correct date for 2020" do
225
+ expect(subject.start_of_year(2020)).to eq Date.new(2019, 8, 4)
226
+ end
227
+
228
+ it "returns the correct date for 2024, the next 53-week year" do
229
+ expect(subject.start_of_year(2024)).to eq Date.new(2023, 7, 30)
230
+ end
231
+
232
+ it "returns the correct date for 2025, the next year after the 53-week year" do
233
+ expect(subject.start_of_year(2025)).to eq Date.new(2024, 8, 4)
234
+ end
235
+ end
236
+
237
+ describe "#end_of_year" do
238
+ it "returns the correct date for 2017" do
239
+ expect(subject.end_of_year(2017)).to eq Date.new(2017, 7, 29)
240
+ end
241
+
242
+ it "returns the correct date for 2018" do
243
+ expect(subject.end_of_year(2018)).to eq Date.new(2018, 7, 28)
244
+ end
245
+
246
+ it "returns the correct date for 2019" do
247
+ expect(subject.end_of_year(2019)).to eq Date.new(2019, 8, 3)
248
+ end
249
+
250
+ it "returns the correct date for 2020" do
251
+ expect(subject.end_of_year(2020)).to eq Date.new(2020, 8, 1)
252
+ end
253
+
254
+ it "returns the correct date for 2024, the next 53-week year" do
255
+ expect(subject.end_of_year(2024)).to eq Date.new(2024, 8, 3)
256
+ end
257
+
258
+ it "returns the correct date for 2025, the next year after the 53-week year" do
259
+ expect(subject.end_of_year(2025)).to eq Date.new(2025, 8, 2)
260
+ end
261
+ end
262
+
263
+ describe "#merch_year_from_date" do
264
+ it "converts julian dates to its fiscal year" do
265
+ expect(subject.merch_year_from_date(Date.new(2018, 7, 24))).to eq 2018
266
+ expect(subject.merch_year_from_date(Date.new(2018, 7, 29))).to eq 2019
267
+ expect(subject.merch_year_from_date(Date.new(2018, 8, 1))).to eq 2019
268
+ expect(subject.merch_year_from_date(Date.new(2019, 8, 1))).to eq 2019
269
+ expect(subject.merch_year_from_date(Date.new(2019, 8, 4))).to eq 2020
270
+ expect(subject.merch_year_from_date(Date.new(2024, 2, 3))).to eq 2024
271
+ expect(subject.merch_year_from_date(Date.new(2024, 7, 30))).to eq 2024
272
+ expect(subject.merch_year_from_date(Date.new(2024, 8, 4))).to eq 2025
273
+ end
274
+ end
275
+
276
+
277
+ describe "#merch_months_in" do
278
+ it "returns merch date for start_date if start_date is the same as end_date" do
279
+ start_date = Date.new(2020,8,2)
280
+ end_date = start_date
281
+ start_merch_date = subject.start_of_month(start_date.year, start_date.month)
282
+
283
+ merch_months = subject.merch_months_in(start_date, end_date)
284
+
285
+ expect(merch_months.count).to be(1)
286
+ expect(merch_months[0].strftime('%Y-%m-%d')).to eq '2020-08-02'
287
+ end
288
+
289
+ it "returns valid merch dates for FY 2019" do
290
+ start_date = Date.new(2018, 8, 1)
291
+ end_date = Date.new(2019, 8, 1)
292
+
293
+ merch_months = subject.merch_months_in(start_date, end_date)
294
+
295
+ expect(merch_months.count).to be 13
296
+
297
+ expect(merch_months[0].year).to be 2018
298
+ expect(merch_months[6].year).to be 2019
299
+ expect(merch_months[12].year).to be 2019
300
+
301
+ expect(merch_months[0].strftime('%Y-%m-%d')).to eq '2018-07-29'
302
+ expect(merch_months[1].strftime('%Y-%m-%d')).to eq '2018-08-26'
303
+ expect(merch_months[2].strftime('%Y-%m-%d')).to eq '2018-09-30'
304
+ expect(merch_months[3].strftime('%Y-%m-%d')).to eq '2018-10-28'
305
+ expect(merch_months[4].strftime('%Y-%m-%d')).to eq '2018-11-25'
306
+ expect(merch_months[5].strftime('%Y-%m-%d')).to eq '2018-12-30'
307
+ expect(merch_months[6].strftime('%Y-%m-%d')).to eq '2019-01-27'
308
+ expect(merch_months[7].strftime('%Y-%m-%d')).to eq '2019-02-24'
309
+ expect(merch_months[8].strftime('%Y-%m-%d')).to eq '2019-03-31'
310
+ expect(merch_months[9].strftime('%Y-%m-%d')).to eq '2019-04-28'
311
+ expect(merch_months[10].strftime('%Y-%m-%d')).to eq '2019-05-26'
312
+ expect(merch_months[11].strftime('%Y-%m-%d')).to eq '2019-06-30'
313
+ expect(merch_months[12].strftime('%Y-%m-%d')).to eq '2019-08-04'
314
+ end
315
+ end
316
+
317
+ describe "#julian_to_merch" do
318
+ it "converts julian months to merch months" do
319
+ expect(subject.julian_to_merch(8)).to eq 1
320
+ expect(subject.julian_to_merch(9)).to eq 2
321
+ expect(subject.julian_to_merch(10)).to eq 3
322
+ expect(subject.julian_to_merch(11)).to eq 4
323
+ expect(subject.julian_to_merch(12)).to eq 5
324
+ expect(subject.julian_to_merch(1)).to eq 6
325
+ expect(subject.julian_to_merch(2)).to eq 7
326
+ expect(subject.julian_to_merch(3)).to eq 8
327
+ expect(subject.julian_to_merch(4)).to eq 9
328
+ expect(subject.julian_to_merch(5)).to eq 10
329
+ expect(subject.julian_to_merch(6)).to eq 11
330
+ expect(subject.julian_to_merch(7)).to eq 12
331
+ expect { subject.julian_to_merch(13) }.to raise_error ArgumentError
332
+ expect { subject.julian_to_merch(0) }.to raise_error ArgumentError
333
+ end
334
+ end
335
+
336
+ describe "#merch_to_julian" do
337
+ it "converts merch months to julian months" do
338
+ expect(subject.merch_to_julian(1)).to eq 8
339
+ expect(subject.merch_to_julian(2)).to eq 9
340
+ expect(subject.merch_to_julian(3)).to eq 10
341
+ expect(subject.merch_to_julian(4)).to eq 11
342
+ expect(subject.merch_to_julian(5)).to eq 12
343
+ expect(subject.merch_to_julian(6)).to eq 1
344
+ expect(subject.merch_to_julian(7)).to eq 2
345
+ expect(subject.merch_to_julian(8)).to eq 3
346
+ expect(subject.merch_to_julian(9)).to eq 4
347
+ expect(subject.merch_to_julian(10)).to eq 5
348
+ expect(subject.merch_to_julian(11)).to eq 6
349
+ expect(subject.merch_to_julian(12)).to eq 7
350
+ expect { subject.merch_to_julian(13) }.to raise_error ArgumentError
351
+ expect { subject.merch_to_julian(0) }.to raise_error ArgumentError
352
+ end
353
+ end
354
+
355
+ describe "#weeks_for_month" do
356
+ context "correct number of weeks given julian month and fiscal year" do
357
+ it "returns 4 weeks for a 4-week month Fiscal Year 2019 for Aug" do
358
+ weeks = subject.weeks_for_month(2019, 8)
359
+ expect(weeks.size).to eq 4
360
+ end
361
+
362
+ it "returns 5 weeks for a 5-week month Fiscal Year 2019 for Sept" do
363
+ weeks = subject.weeks_for_month(2019, 9)
364
+ expect(weeks.size).to eq 5
365
+ end
366
+
367
+ it "returns 5 weeks during a 4-5-5 quarter" do
368
+ weeks = subject.weeks_for_month(2019, 5)
369
+ expect(weeks.size).to eq 4
370
+
371
+ weeks = subject.weeks_for_month(2019, 6)
372
+ expect(weeks.size).to eq 5
373
+
374
+ weeks = subject.weeks_for_month(2019, 7)
375
+ expect(weeks.size).to eq 5
376
+
377
+ weeks = subject.weeks_for_month(2020, 8)
378
+ expect(weeks.size).to eq 4
379
+ end
380
+ end
381
+ end
382
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merch_calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitch Dempsey
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-07-05 00:00:00.000000000 Z
12
+ date: 2017-08-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -101,15 +101,15 @@ files:
101
101
  - README.md
102
102
  - Rakefile
103
103
  - lib/merch_calendar.rb
104
- - lib/merch_calendar/fiscal_year_calendar.rb
105
104
  - lib/merch_calendar/merch_week.rb
106
105
  - lib/merch_calendar/retail_calendar.rb
106
+ - lib/merch_calendar/stitch_fix_fiscal_year_calendar.rb
107
107
  - lib/merch_calendar/util.rb
108
108
  - lib/merch_calendar/version.rb
109
109
  - merch_calendar.gemspec
110
- - spec/merch_calendar/fiscal_year_calendar_spec.rb
111
110
  - spec/merch_calendar/merch_week_spec.rb
112
111
  - spec/merch_calendar/retail_calendar_spec.rb
112
+ - spec/merch_calendar/stitch_fix_fiscal_year_calendar_spec.rb
113
113
  - spec/merch_calendar/util_spec.rb
114
114
  - spec/merch_calendar_spec.rb
115
115
  - spec/spec_helper.rb
@@ -138,9 +138,9 @@ signing_key:
138
138
  specification_version: 4
139
139
  summary: Utility for manipulating dates within a 4-5-4 retail calendar
140
140
  test_files:
141
- - spec/merch_calendar/fiscal_year_calendar_spec.rb
142
141
  - spec/merch_calendar/merch_week_spec.rb
143
142
  - spec/merch_calendar/retail_calendar_spec.rb
143
+ - spec/merch_calendar/stitch_fix_fiscal_year_calendar_spec.rb
144
144
  - spec/merch_calendar/util_spec.rb
145
145
  - spec/merch_calendar_spec.rb
146
146
  - spec/spec_helper.rb
@@ -1,118 +0,0 @@
1
- require "merch_calendar/retail_calendar"
2
-
3
- module MerchCalendar
4
- class FiscalYearCalendar
5
- # Stitch Fix's fiscal year starts two quarters *before* (hence the negative number) the start of the
6
- # merch/retail calendar year.
7
- STITCH_FIX_FY_QUARTER_OFFSET = -2
8
-
9
- QUARTER_1 = 1
10
- QUARTER_4 = 4
11
-
12
- # @param fy_quarter_offset [Fixnum]
13
- # The number of quarters before or after the start of the traditional NRF retail calendar that the year
14
- # should begin.
15
- # ex) Stitch Fix's fiscal year calendar starts in August of the prior gregorian calendar year.
16
- # February 2017 = Traditional retail month 1, year 2017
17
- # August 2016 = Offset retail month 1, year 2017 (2 quarters earlier)
18
- def initialize(fy_quarter_offset = STITCH_FIX_FY_QUARTER_OFFSET)
19
- @fy_quarter_offset = fy_quarter_offset
20
-
21
- # TODO: support other fiscal year offsets
22
- if fy_quarter_offset != STITCH_FIX_FY_QUARTER_OFFSET
23
- raise NotImplementedError.new("FY quarter offset of #{fy_quarter_offset} not yet supported")
24
- end
25
-
26
- @retail_calendar = RetailCalendar.new
27
- end
28
-
29
- # The date of the first day of the year
30
- def start_of_year(year)
31
- start_of_quarter(year, QUARTER_1)
32
- end
33
-
34
- # The date of the last day of the year
35
- def end_of_year(year)
36
- end_of_quarter(year, QUARTER_4)
37
- end
38
-
39
- # Return the starting date for a particular quarter
40
- def start_of_quarter(year, quarter)
41
- @retail_calendar.start_of_quarter(*offset_quarter(year, quarter))
42
- end
43
-
44
- # Return the ending date for a particular quarter
45
- def end_of_quarter(year, quarter)
46
- @retail_calendar.end_of_quarter(*offset_quarter(year, quarter))
47
- end
48
-
49
- # The date of the first day of the merch month
50
- # @param [Fixnum] year - the fiscal year
51
- # @param [Fixnum] merch_month - the nth month of the offset calendar
52
- # ex) for an offset of +/- 2 quarters, month 1 = August
53
- def start_of_month(year, merch_month)
54
- @retail_calendar.start_of_month(*offset_month(year, merch_month))
55
- end
56
-
57
- # The date of the last day of the merch month
58
- # @param [Fixnum] year - the fiscal year
59
- # @param [Fixnum] merch_month - the nth month of the offset calendar
60
- # ex) for an offset of +/- 2 quarters, month 1 = August
61
- def end_of_month(year, merch_month)
62
- @retail_calendar.end_of_month(*offset_month(year, merch_month))
63
- end
64
-
65
- # Returns the date that corresponds to the first day in the merch week
66
- def start_of_week(year, merch_month, merch_week)
67
- @retail_calendar.start_of_week(*offset_month(year, merch_month), merch_week)
68
- end
69
-
70
- # Returns the date that corresponds to the last day in the merch week
71
- def end_of_week(year, merch_month, merch_week)
72
- @retail_calendar.end_of_week(*offset_month(year, merch_month), merch_week)
73
- end
74
-
75
- # Returns the number of weeks in the fiscal year
76
- def weeks_in_year(year)
77
- @retail_calendar.weeks_in_year(offset_year(year))
78
- end
79
-
80
- private
81
-
82
- # Offsets the quarter based on the fiscal year quarter offset
83
- # returns: offset [year, quarter]
84
- def offset_quarter(year, quarter)
85
- # first quarter in fiscal calendar is Q3 of retail calendar of previous year
86
- if quarter >= 1 + @fy_quarter_offset.abs
87
- [year, quarter + @fy_quarter_offset]
88
- else
89
- [year - 1, quarter - @fy_quarter_offset]
90
- end
91
- end
92
-
93
- # Offsets the month based on the fiscal year quarter offset
94
- # returns: offset [year, month]
95
- def offset_month(year, month)
96
- # 3 - number of months in a quarter
97
- month_offset = @fy_quarter_offset * 3
98
-
99
- if month >= (month_offset.abs + 1)
100
- [year, month + month_offset]
101
- else
102
- [year - 1, month - month_offset]
103
- end
104
- end
105
-
106
- # Offsets the year based on the fiscal year quarter offset
107
- # returns: offset year
108
- def offset_year(year)
109
- if @fy_quarter_offset < 0
110
- year -= 1
111
- elsif @fy_quarter_offset > 0
112
- year += 1
113
- else
114
- year
115
- end
116
- end
117
- end
118
- end
@@ -1,145 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe MerchCalendar::FiscalYearCalendar do
4
- context "the default fy_offset - 2 quarters earlier" do
5
- describe "#weeks_in_year" do
6
- it "returns 53 for a leap year - 2013" do
7
- expect(subject.weeks_in_year(2013)).to eq 53
8
- end
9
-
10
- it "returns 52 for a normal year" do
11
- expect(subject.weeks_in_year(2014)).to eq 52
12
- end
13
-
14
- it "returns 52 for a normal year" do
15
- expect(subject.weeks_in_year(2015)).to eq 52
16
- end
17
-
18
- it "returns 52 for a normal year" do
19
- expect(subject.weeks_in_year(2016)).to eq 52
20
- end
21
-
22
- it "returns 52 for a normal year" do
23
- expect(subject.weeks_in_year(2017)).to eq 52
24
- end
25
-
26
- it "returns 53 for a leap year - 2018" do
27
- expect(subject.weeks_in_year(2018)).to eq 53
28
- end
29
-
30
- it "returns 52 for a normal year" do
31
- expect(subject.weeks_in_year(2019)).to eq 52
32
- end
33
- end
34
-
35
- describe "#start_of_week" do
36
- it "returns the correct date for 2017-1-1 (2017-Aug-wk1)" do
37
- expect(subject.start_of_week(2017, 1, 1)).to eq Date.new(2016, 7, 31)
38
- end
39
-
40
- it "returns the correct Date for 2018-1-1 (2018-Aug-wk1)" do
41
- expect(subject.start_of_week(2018, 1, 1)).to eq Date.new(2017, 7, 30)
42
- end
43
-
44
- it "returns the correct Date for 2019-1-1 (2019-Aug-wk1)" do
45
- expect(subject.start_of_week(2019, 1, 1)).to eq Date.new(2018, 8, 5)
46
- end
47
- end
48
-
49
- describe "#end_of_week" do
50
- it "returns the correct date for 2017-6-1 (2017-Jan-wk1)" do
51
- expect(subject.end_of_week(2017, 6, 1)).to eq Date.new(2017, 1, 7)
52
- end
53
-
54
- it "returns the correct Date for 2018-6-5 (2018-Jan-wk5)" do
55
- expect(subject.end_of_week(2018, 6, 5)).to eq Date.new(2018, 2, 3)
56
- end
57
-
58
- it "returns the correct Date for 2019-10-3 (2019-May-wk3)" do
59
- expect(subject.end_of_week(2019, 10, 3)).to eq Date.new(2019, 5, 25)
60
- end
61
- end
62
-
63
- describe "#start_of_month" do
64
- it "returns the correct date for 2018-1 AKA 2018-Aug" do
65
- expect(subject.start_of_month(2018, 1)).to eq Date.new(2017, 7, 30)
66
- end
67
-
68
- it "returns the correct date for 2019-1 AKA 2019-Aug" do
69
- expect(subject.start_of_month(2019, 1)).to eq Date.new(2018, 8, 5)
70
- end
71
- end
72
-
73
- describe "#end_of_month" do
74
- it "returns the correct date for 2018-1 AKA 2018-Aug" do
75
- expect(subject.end_of_month(2018, 1)).to eq Date.new(2017, 8, 26)
76
- end
77
-
78
- it "returns the correct date for 2019-1 AKA 2019-Aug" do
79
- expect(subject.end_of_month(2019, 1)).to eq Date.new(2018, 9, 1)
80
- end
81
- end
82
-
83
- describe "#start_of_quarter" do
84
- it "returns the correct date for 2018-Q1" do
85
- expect(subject.start_of_quarter(2018, 1)).to eq Date.new(2017, 7, 30)
86
- end
87
-
88
- it "returns the correct date for 2018-Q4" do
89
- expect(subject.start_of_quarter(2018, 4)).to eq Date.new(2018, 5, 6)
90
- end
91
-
92
- it "returns the correct date for 2019-Q1" do
93
- expect(subject.start_of_quarter(2019, 1)).to eq Date.new(2018, 8, 5)
94
- end
95
- end
96
-
97
- describe "#end_of_quarter" do
98
- it "returns the correct date for 2018-Q1" do
99
- expect(subject.end_of_quarter(2018, 1)).to eq Date.new(2017, 10, 28)
100
- end
101
-
102
- it "returns the correct date for 2018-Q4" do
103
- expect(subject.end_of_quarter(2018, 4)).to eq Date.new(2018, 8, 4)
104
- end
105
-
106
- it "returns the correct date for 2019-Q1" do
107
- expect(subject.end_of_quarter(2019, 1)).to eq Date.new(2018, 11, 3)
108
- end
109
- end
110
-
111
- describe "#start_of_year" do
112
- it "returns the correct date for 2018" do
113
- expect(subject.start_of_year(2018)).to eq Date.new(2017, 7, 30)
114
- end
115
-
116
- it "returns the correct date for 2019" do
117
- expect(subject.start_of_year(2019)).to eq Date.new(2018, 8, 5)
118
- end
119
- end
120
-
121
- describe "#end_of_year" do
122
- it "returns the correct date for 2017" do
123
- expect(subject.end_of_year(2017)).to eq Date.new(2017, 7, 29)
124
- end
125
-
126
- it "returns the correct date for 2018" do
127
- expect(subject.end_of_year(2018)).to eq Date.new(2018, 8, 4)
128
- end
129
-
130
- it "returns the correct date for 2019" do
131
- expect(subject.end_of_year(2019)).to eq Date.new(2019, 8, 3)
132
- end
133
- end
134
- end
135
-
136
- context "an alternative offset - 2 quarters after" do
137
- subject { described_class.new(2) }
138
-
139
- it "raises a NotImplementedError - this is still TODO" do
140
- expect {
141
- subject
142
- }.to raise_error(NotImplementedError)
143
- end
144
- end
145
- end