-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathtest_business_days.rb
More file actions
303 lines (260 loc) · 12.8 KB
/
Copy pathtest_business_days.rb
File metadata and controls
303 lines (260 loc) · 12.8 KB
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
require 'helper'
describe "business days" do
describe "with a standard Time object" do
describe "when adding/subtracting positive business days" do
it "should move to tomorrow if we add a business day" do
first = Time.parse("April 13th, 2010, 11:00 am")
later = 1.business_day.after(first)
expected = Time.parse("April 14th, 2010, 11:00 am")
assert_equal expected, later
end
it "should take into account the weekend when adding a day" do
first = Time.parse("April 9th, 2010, 12:33 pm")
after = 1.business_day.after(first)
expected = Time.parse("April 12th, 2010, 12:33 pm")
assert_equal expected, after
end
it "should pick next working day when adding zero days on the weekend" do
first = Time.parse("April 10th, 2010, 12:33 pm")
after = 0.business_days.after(first)
expected = Time.parse("April 12th, 2010, 09:00 am")
assert_equal expected, after
end
it "should pick previous working day when subtracting zero days on the weekend" do
first = Time.parse("January 30th, 2016, 12:33 pm")
after = 0.business_days.before(first)
expected = Time.parse("January 29th, 2016, 09:00 am")
assert_equal expected, after
end
it "should move forward one week when adding 5 business days" do
first = Time.parse("April 9th, 2010, 12:33 pm")
after = 5.business_days.after(first)
expected = Time.parse("April 16th, 2010, 12:33 pm")
assert_equal expected, after
end
it "should take into account a holiday when adding a day" do
three_day_weekend = Date.parse("July 5th, 2010")
BusinessTime::Config.holidays << three_day_weekend
friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
tuesday_afternoon = 1.business_day.after(friday_afternoon)
expected = Time.parse("July 6th, 2010, 4:50 pm")
assert_equal expected, tuesday_afternoon
end
it "should take into account a holiday on a weekend" do
july_4 = Date.parse("July 4th, 2010")
BusinessTime::Config.holidays << july_4
friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
monday_afternoon = 1.business_day.after(friday_afternoon)
expected = Time.parse("July 5th, 2010, 4:50 pm")
assert_equal expected, monday_afternoon
end
it "should move to Monday if we add one business day during a weekend" do
saturday = Time.parse("April 10th, 2010, 11:00 am")
later = 1.business_days.after(saturday)
expected = Time.parse("April 12th, 2010, 9:00 am")
assert_equal expected, later
end
it "should return a business hour when adding one business day from before business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 01:54 am")
later = 1.business_days.after(wednesday)
expected = Time.parse("Thursday October 15th, 2015, 09:00 am")
assert_equal expected, later
end
it "should move to thursday if we subtract one business day during a weekend" do
saturday = Time.parse("April 10th, 2010, 11:00 am")
before = 1.business_days.before(saturday)
expected = Time.parse("April 8th, 2010, 9:00 am")
assert_equal expected, before
end
it "should move to thursday if we subtract one business day during a weekend outside normal business hours" do
saturday = Time.parse("April 10th, 2010, 03:00 am")
before = 1.business_days.before(saturday)
expected = Time.parse("April 8th, 2010, 9:00 am")
assert_equal expected, before
end
it "should return a business hour when adding one business day from after business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 21:54 pm")
later = 1.business_days.after(wednesday)
expected = Time.parse("Friday October 16th, 2015, 09:00 am")
assert_equal expected, later
end
it "should return a business hour when subtracting one business day from before business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 01:54 am")
before = 1.business_days.before(wednesday)
expected = Time.parse("Monday October 12th, 2015, 09:00 am")
assert before.during_business_hours?
assert_equal expected, before
end
it "should return a business hour when subtracting one business day from after business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 21:54 pm")
before = 1.business_days.before(wednesday)
expected = Time.parse("Tuesday October 13th, 2015, 09:00 am")
assert before.during_business_hours?
assert_equal expected, before
end
it "should move to yesterday is we subtract a business day" do
first = Time.parse("April 13th, 2010, 11:00 am")
before = 1.business_day.before(first)
expected = Time.parse("April 12th, 2010, 11:00 am")
assert_equal expected, before
end
it "should take into account the weekend when subtracting a day" do
first = Time.parse("April 12th, 2010, 12:33 pm")
before = 1.business_day.before(first)
expected = Time.parse("April 9th, 2010, 12:33 pm")
assert_equal expected, before
end
it "should move backward one week when subtracting 5 business days" do
first = Time.parse("April 16th, 2010, 12:33 pm")
before = 5.business_days.before(first)
expected = Time.parse("April 9th, 2010, 12:33 pm")
assert_equal expected, before
end
it "should take into account a holiday passed as an option when adding a day" do
three_day_weekend = Date.parse("July 5th, 2010")
friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
tuesday_afternoon = 1.business_day.after(friday_afternoon, holidays: [three_day_weekend])
expected = Time.parse("July 6th, 2010, 4:50 pm")
assert_equal expected, tuesday_afternoon
end
it "should take into account a holiday passed as an option when adding a day with from_now" do
three_day_weekend = Date.parse("July 5th, 2010")
friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
tuesday_afternoon = 1.business_day.from_now(friday_afternoon, holidays: [three_day_weekend])
expected = Time.parse("July 6th, 2010, 4:50 pm")
assert_equal expected, tuesday_afternoon
end
it "should take into account a holiday passed as an option when subtracting a day with ago" do
tuesday_afternoon = Time.parse("July 6rd, 2010, 4:50 pm")
three_day_weekend = Date.parse("July 5th, 2010")
friday_afternoon = 1.business_day.ago(tuesday_afternoon, holidays: [three_day_weekend])
expected = Time.parse("July 2nd, 2010, 4:50 pm")
assert_equal expected, friday_afternoon
end
it "should take into account a holiday on a weekend when passed as an option" do
july_4 = Date.parse("July 4th, 2010")
friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
monday_afternoon = 1.business_day.after(friday_afternoon, holidays: [july_4])
expected = Time.parse("July 5th, 2010, 4:50 pm")
assert_equal expected, monday_afternoon
end
end
describe "when adding/subtracting negative business days" do
it "should move to yesterday if we add a negative business day" do
first = Time.parse("April 13th, 2010, 11:00 am")
before = -1.business_day.after(first)
expected = Time.parse("April 12th, 2010, 11:00 am")
assert_equal expected, before
end
it "should take into account the weekend when adding a negative day" do
first = Time.parse("April 12th, 2010, 12:33 pm")
before = -1.business_day.after(first)
expected = Time.parse("April 9th, 2010, 12:33 pm")
assert_equal expected, before
end
it "should move bacward one week when adding 5 negative business days" do
first = Time.parse("April 16th, 2010, 12:33 pm")
before = -5.business_days.after(first)
expected = Time.parse("April 9th, 2010, 12:33 pm")
assert_equal expected, before
end
it "should take into account a holiday on a weekend when adding a negative day" do
july_4 = Date.parse("July 4th, 2010")
BusinessTime::Config.holidays << july_4
monday_afternoon = Time.parse("July 5th, 2010, 4:50 pm")
friday_afternoon = -1.business_day.after(monday_afternoon)
expected = Time.parse("July 2nd, 2010, 4:50 pm")
assert_equal expected, friday_afternoon
end
it "should move to thursday if we add one negative business day during weekend" do
saturday = Time.parse("April 10th, 2010, 11:00 am")
before = -1.business_days.after(saturday)
expected = Time.parse("April 8th, 2010, 09:00 am")
assert_equal expected, before
end
it "should return a business hour when adding one negative business day from before business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 01:54 am")
before = -1.business_days.after(wednesday)
expected = Time.parse("Monday October 12th, 2015, 09:00 am")
assert_equal expected, before
end
it "should return a business hour when adding one negative business day from after business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 21:54 pm")
before = -1.business_days.after(wednesday)
expected = Time.parse("Tuesday October 13th, 2015, 09:00 am")
assert_equal expected, before
end
it "should move to tomorrow if we subtract a negative business day" do
first = Time.parse("April 13th, 2010, 11:00 am")
later = -1.business_day.before(first)
expected = Time.parse("April 14th, 2010, 11:00 am")
assert_equal expected, later
end
it "should take into account the weekend when subtracting a negative day" do
first = Time.parse("April 12th, 2010, 12:33 pm")
later = -1.business_day.before(first)
expected = Time.parse("April 13th, 2010, 12:33 pm")
assert_equal expected, later
end
it "should move forward one week when subtracting -5 business days" do
first = Time.parse("April 9th, 2010, 12:33 pm")
later = -5.business_days.before(first)
expected = Time.parse("April 16th, 2010, 12:33 pm")
assert_equal expected, later
end
it "should move to Monday if we subtract one negative business day during a weekend" do
saturday = Time.parse("April 10th, 2010, 11:00 am")
later = -1.business_days.before(saturday)
expected = Time.parse("April 12th, 2010, 09:00 am")
assert_equal expected, later
end
it "should return a business hour when subtracting one negative business day from before business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 01:54 am")
later = -1.business_days.before(wednesday)
expected = Time.parse("Thurdsay October 15th, 2015, 09:00 am")
assert later.during_business_hours?
assert_equal expected, later
end
it "should return a business hour when subtracting one negative business day from after business hours" do
wednesday = Time.parse("Wednesday October 14th, 2015, 21:54 pm")
after = -1.business_days.before(wednesday)
expected = Time.parse("Friday October 16th, 2015, 09:00 am")
assert after.during_business_hours?
assert_equal expected, after
end
it "should take into account a holiday on a weekend passed as an option when adding a negative day" do
july_4 = Date.parse("July 4th, 2010")
monday_afternoon = Time.parse("July 5th, 2010, 4:50 pm")
friday_afternoon = -1.business_day.after(monday_afternoon, holidays: [july_4])
expected = Time.parse("July 2nd, 2010, 4:50 pm")
assert_equal expected, friday_afternoon
end
end
it "responds appropriatly to <" do
assert 5.business_days < 10.business_days
assert !(10.business_days < 5.business_days)
assert(-1.business_day < 1.business_day)
assert !(1.business_day < -1.business_day)
end
it "responds appropriatly to >" do
assert !(5.business_days > 10.business_days)
assert 10.business_days > 5.business_days
assert 1.business_day > -1.business_day
assert !(-1.business_day > 1.business_day)
end
it "responds appropriatly to ==" do
assert 5.business_days == 5.business_days
assert 10.business_days != 5.business_days
assert(-1.business_day == -1.business_day)
assert(-1.business_day != -5.business_days)
end
it "won't compare days to hours" do
assert_raises ArgumentError do
5.business_days < 5.business_hours
end
assert_raises ArgumentError do
-5.business_days < 5.business_hours
end
end
end
end