subroutine 4.4.0 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,510 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module Subroutine
6
- class TypeCasterTest < TestCase
7
- def op
8
- @op ||= TypeCastOp.new
9
- end
10
-
11
- def test_integer_inputs
12
- op.integer_input = nil
13
- assert_nil op.integer_input
14
-
15
- op.integer_input = 'foo'
16
- assert_equal 0, op.integer_input
17
-
18
- op.integer_input = '4.5'
19
- assert_equal 4, op.integer_input
20
-
21
- op.integer_input = 0.5
22
- assert_equal 0, op.integer_input
23
-
24
- op.integer_input = 5.2
25
- assert_equal 5, op.integer_input
26
-
27
- op.integer_input = 6
28
- assert_equal 6, op.integer_input
29
- end
30
-
31
- def test_number_inputs
32
- op.number_input = nil
33
- assert_nil op.number_input
34
-
35
- op.number_input = 4
36
- assert_equal 4.0, op.number_input
37
-
38
- op.number_input = 0.5
39
- assert_equal 0.5, op.number_input
40
-
41
- op.number_input = 'foo'
42
- assert_equal 0.0, op.number_input
43
- end
44
-
45
- def test_decimal_inputs
46
- op.decimal_input = nil
47
- assert_nil op.decimal_input
48
-
49
- op.decimal_input = 4
50
- assert_equal 4.0, op.decimal_input
51
- assert op.decimal_input.is_a?(BigDecimal)
52
-
53
- op.decimal_input = 0.5
54
- assert_equal 0.5, op.decimal_input
55
-
56
- op.decimal_input = 'foo'
57
- assert_equal 0.0, op.decimal_input
58
- end
59
-
60
- def test_decimal_inputs_use_16_precision
61
- op.decimal_input = 0.07
62
- expected_bd_value = BigDecimal('0.07', 0)
63
- assert_equal expected_bd_value, op.decimal_input
64
- assert op.decimal_input.is_a?(BigDecimal)
65
-
66
- # Ruby 3+
67
- if op.decimal_input.respond_to?(:precision)
68
- assert_equal expected_bd_value.precision, op.decimal_input.precision
69
- end
70
- end
71
-
72
- def test_string_inputs
73
- op.string_input = nil
74
- assert_nil op.string_input
75
-
76
- op.string_input = "
77
- assert_equal ", op.string_input
78
-
79
- op.string_input = 'foo'
80
- assert_equal 'foo', op.string_input
81
-
82
- op.string_input = 4
83
- assert_equal '4', op.string_input
84
-
85
- op.string_input = 4.2
86
- assert_equal '4.2', op.string_input
87
- end
88
-
89
- def test_boolean_inputs
90
- op.boolean_input = nil
91
- assert_nil op.boolean_input
92
-
93
- op.boolean_input = 'yes'
94
- assert_equal true, op.boolean_input
95
-
96
- op.boolean_input = 'no'
97
- assert_equal false, op.boolean_input
98
-
99
- op.boolean_input = 'Yes'
100
- assert_equal true, op.boolean_input
101
-
102
- op.boolean_input = 'No'
103
- assert_equal false, op.boolean_input
104
-
105
- op.boolean_input = 'YES'
106
- assert_equal true, op.boolean_input
107
-
108
- op.boolean_input = 'NO'
109
- assert_equal false, op.boolean_input
110
-
111
- op.boolean_input = 'true'
112
- assert_equal true, op.boolean_input
113
-
114
- op.boolean_input = 'false'
115
- assert_equal false, op.boolean_input
116
-
117
- op.boolean_input = 'True'
118
- assert_equal true, op.boolean_input
119
-
120
- op.boolean_input = 'False'
121
- assert_equal false, op.boolean_input
122
-
123
- op.boolean_input = 'TRUE'
124
- assert_equal true, op.boolean_input
125
-
126
- op.boolean_input = 'FALSE'
127
- assert_equal false, op.boolean_input
128
-
129
- op.boolean_input = 'ok'
130
- assert_equal true, op.boolean_input
131
-
132
- op.boolean_input = 'OK'
133
- assert_equal true, op.boolean_input
134
-
135
- op.boolean_input = 'Ok'
136
- assert_equal true, op.boolean_input
137
-
138
- op.boolean_input = ''
139
- assert_equal false, op.boolean_input
140
-
141
- op.boolean_input = true
142
- assert_equal true, op.boolean_input
143
-
144
- op.boolean_input = false
145
- assert_equal false, op.boolean_input
146
-
147
- op.boolean_input = '1'
148
- assert_equal true, op.boolean_input
149
-
150
- op.boolean_input = '0'
151
- assert_equal false, op.boolean_input
152
-
153
- op.boolean_input = 1
154
- assert_equal true, op.boolean_input
155
-
156
- op.boolean_input = 0
157
- assert_equal false, op.boolean_input
158
- end
159
-
160
- def test_hash_inputs
161
- op.object_input = nil
162
- assert_nil op.object_input
163
-
164
- op.object_input = ''
165
- assert_equal({}, op.object_input)
166
-
167
- op.object_input = [[:a, :b]]
168
- assert_equal({ 'a' => :b }, op.object_input)
169
-
170
- op.object_input = false
171
- assert_equal({}, op.object_input)
172
-
173
- op.object_input = { foo: 'bar' }
174
- assert_equal({ 'foo' => 'bar' }, op.object_input)
175
-
176
- op.object_input = { 'foo' => { 'bar' => :baz } }
177
- assert_equal({ 'foo' => { 'bar' => :baz } }, op.object_input)
178
-
179
- op.object_input = { foo: "bar" }
180
- assert op.object_input.is_a?(ActiveSupport::HashWithIndifferentAccess)
181
- end
182
-
183
- def test_hash_inputs_can_opt_out_of_indifferent_access_behavior
184
- op.no_indifferent_object_input = nil
185
- assert_nil op.no_indifferent_object_input
186
-
187
- op.no_indifferent_object_input = ''
188
- assert_equal({}, op.no_indifferent_object_input)
189
-
190
- op.no_indifferent_object_input = [[:a, :b]]
191
- assert_equal({ a: :b }, op.no_indifferent_object_input)
192
-
193
- op.no_indifferent_object_input = { foo: "bar" }
194
- assert_equal({ foo: "bar" }, op.no_indifferent_object_input)
195
- assert op.no_indifferent_object_input.key?(:foo)
196
- refute op.no_indifferent_object_input.key?("foo")
197
- refute op.no_indifferent_object_input.is_a?(ActiveSupport::HashWithIndifferentAccess)
198
- assert op.no_indifferent_object_input.is_a?(Hash)
199
-
200
- op.no_indifferent_object_input = { "foo" => "bar" }
201
- assert_equal({ "foo" => "bar" }, op.no_indifferent_object_input)
202
- assert op.no_indifferent_object_input.key?("foo")
203
- refute op.no_indifferent_object_input.key?(:foo)
204
- refute op.no_indifferent_object_input.is_a?(ActiveSupport::HashWithIndifferentAccess)
205
- assert op.no_indifferent_object_input.is_a?(Hash)
206
- end
207
-
208
- def test_array_inputs
209
- op.array_input = nil
210
- assert_nil op.array_input
211
-
212
- op.array_input = ''
213
- assert_equal [], op.array_input
214
-
215
- op.array_input = 'foo'
216
- assert_equal ['foo'], op.array_input
217
-
218
- op.array_input = ['foo']
219
- assert_equal ['foo'], op.array_input
220
-
221
- op.array_input = { 'bar' => true }
222
- assert_equal [{ 'bar' => true }], op.array_input
223
- end
224
-
225
- def test_typed_array_inputs
226
- op.type_array_input = nil
227
- assert_nil op.type_array_input
228
-
229
- op.type_array_input = ''
230
- assert_equal [], op.type_array_input
231
-
232
- op.type_array_input = '3'
233
- assert_equal [3], op.type_array_input
234
-
235
- op.type_array_input = ['3.4']
236
- assert_equal [3], op.type_array_input
237
- end
238
-
239
- def test_date_inputs
240
- op.date_input = nil
241
- assert_nil op.date_input
242
-
243
- op.date_input = '2022-12-22'
244
- assert_equal ::Date, op.date_input.class
245
- refute_equal ::DateTime, op.date_input.class
246
-
247
- assert_equal 2022, op.date_input.year
248
- assert_equal 12, op.date_input.month
249
- assert_equal 22, op.date_input.day
250
-
251
- op.date_input = '2023-05-05T10:00:30'
252
- assert_equal ::Date, op.date_input.class
253
- refute_equal ::DateTime, op.date_input.class
254
-
255
- assert_equal 2023, op.date_input.year
256
- assert_equal 5, op.date_input.month
257
- assert_equal 5, op.date_input.day
258
-
259
- op.date_input = '2020-05-03 13:44:45 -0400'
260
-
261
- assert_equal ::Date, op.date_input.class
262
- refute_equal ::DateTime, op.date_input.class
263
-
264
- assert_equal 2020, op.date_input.year
265
- assert_equal 5, op.date_input.month
266
- assert_equal 3, op.date_input.day
267
-
268
- op.date_input = false
269
- assert_nil op.date_input
270
- end
271
-
272
- def test_time_inputs__with_seconds_precision
273
- op.time_input = nil
274
- assert_nil op.time_input
275
-
276
- op.time_input = '2022-12-22'
277
- assert_equal ::Time, op.time_input.class
278
- refute_equal ::DateTime, op.time_input.class
279
-
280
- assert_equal 2022, op.time_input.year
281
- assert_equal 12, op.time_input.month
282
- assert_equal 22, op.time_input.day
283
- assert_equal 0, op.time_input.hour
284
- assert_equal 0, op.time_input.min
285
- assert_equal 0, op.time_input.sec
286
-
287
- op.time_input = ::DateTime.new(2022, 12, 22)
288
- assert_equal ::Time, op.time_input.class
289
- refute_equal ::DateTime, op.time_input.class
290
-
291
- assert_equal 0, op.time_input.utc_offset
292
- assert_equal 2022, op.time_input.year
293
- assert_equal 12, op.time_input.month
294
- assert_equal 22, op.time_input.day
295
- assert_equal 0, op.time_input.hour
296
- assert_equal 0, op.time_input.min
297
- assert_equal 0, op.time_input.sec
298
-
299
- op.time_input = '2023-05-05T10:00:30.123456Z'
300
- assert_equal ::Time, op.time_input.class
301
- refute_equal ::DateTime, op.time_input.class
302
-
303
- assert_equal 0, op.time_input.utc_offset
304
- assert_equal 2023, op.time_input.year
305
- assert_equal 5, op.time_input.month
306
- assert_equal 5, op.time_input.day
307
- assert_equal 10, op.time_input.hour
308
- assert_equal 0, op.time_input.min
309
- assert_equal 30, op.time_input.sec
310
- assert_equal 0, op.time_input.usec
311
-
312
- op.time_input = '2023-05-05T10:00:30Z'
313
- assert_equal ::Time, op.time_input.class
314
- assert_equal 0, op.time_input.utc_offset
315
- assert_equal 2023, op.time_input.year
316
- assert_equal 5, op.time_input.month
317
- assert_equal 5, op.time_input.day
318
- assert_equal 10, op.time_input.hour
319
- assert_equal 0, op.time_input.min
320
- assert_equal 30, op.time_input.sec
321
- assert_equal 0, op.time_input.usec
322
-
323
- op.time_input = '2024-11-11T16:42:23.246+0100'
324
- assert_equal ::Time, op.time_input.class
325
- assert_equal 3600, op.time_input.utc_offset
326
- assert_equal 2024, op.time_input.year
327
- assert_equal 11, op.time_input.month
328
- assert_equal 11, op.time_input.day
329
- assert_equal 16, op.time_input.hour
330
- assert_equal 42, op.time_input.min
331
- assert_equal 23, op.time_input.sec
332
- assert_equal 0, op.time_input.usec
333
-
334
- time = Time.at(1678741605.123456).utc
335
- op.time_input = time
336
- refute_equal time, op.time_input
337
- refute_equal time.object_id, op.time_input.object_id
338
- assert_equal 2023, op.time_input.year
339
- assert_equal 3, op.time_input.month
340
- assert_equal 13, op.time_input.day
341
- assert_equal 21, op.time_input.hour
342
- assert_equal 6, op.time_input.min
343
- assert_equal 45, op.time_input.sec
344
- assert_equal 0, op.time_input.usec
345
- end
346
-
347
- def test_time_inputs__with_preserve_time_precision
348
- Subroutine.stubs(:preserve_time_precision?).returns(true)
349
-
350
- time = Time.at(1678741605.123456).utc
351
- op.time_input = time
352
- assert_equal 2023, op.time_input.year
353
- assert_equal 3, op.time_input.month
354
- assert_equal 13, op.time_input.day
355
- assert_equal 21, op.time_input.hour
356
- assert_equal 6, op.time_input.min
357
- assert_equal 45, op.time_input.sec
358
- assert_equal 123456, op.time_input.usec
359
- end
360
-
361
- def test_time_inputs__with_high_precision
362
- op.precise_time_input = nil
363
- assert_nil op.precise_time_input
364
-
365
- op.precise_time_input = '2022-12-22'
366
- assert_equal ::Time, op.precise_time_input.class
367
- refute_equal ::DateTime, op.precise_time_input.class
368
-
369
- assert_equal 2022, op.precise_time_input.year
370
- assert_equal 12, op.precise_time_input.month
371
- assert_equal 22, op.precise_time_input.day
372
- assert_equal 0, op.precise_time_input.hour
373
- assert_equal 0, op.precise_time_input.min
374
- assert_equal 0, op.precise_time_input.sec
375
-
376
- op.precise_time_input = ::DateTime.new(2022, 12, 22)
377
- assert_equal ::Time, op.precise_time_input.class
378
- refute_equal ::DateTime, op.precise_time_input.class
379
-
380
- assert_equal 0, op.precise_time_input.utc_offset
381
- assert_equal 2022, op.precise_time_input.year
382
- assert_equal 12, op.precise_time_input.month
383
- assert_equal 22, op.precise_time_input.day
384
- assert_equal 0, op.precise_time_input.hour
385
- assert_equal 0, op.precise_time_input.min
386
- assert_equal 0, op.precise_time_input.sec
387
-
388
- op.precise_time_input = '2023-05-05T10:00:30.123456Z'
389
- assert_equal ::Time, op.precise_time_input.class
390
- refute_equal ::DateTime, op.precise_time_input.class
391
-
392
- assert_equal 0, op.precise_time_input.utc_offset
393
- assert_equal 2023, op.precise_time_input.year
394
- assert_equal 5, op.precise_time_input.month
395
- assert_equal 5, op.precise_time_input.day
396
- assert_equal 10, op.precise_time_input.hour
397
- assert_equal 0, op.precise_time_input.min
398
- assert_equal 30, op.precise_time_input.sec
399
- assert_equal 123456, op.precise_time_input.usec
400
-
401
- op.precise_time_input = '2023-05-05T10:00:30Z'
402
- assert_equal ::Time, op.precise_time_input.class
403
- assert_equal 0, op.precise_time_input.utc_offset
404
- assert_equal 2023, op.precise_time_input.year
405
- assert_equal 5, op.precise_time_input.month
406
- assert_equal 5, op.precise_time_input.day
407
- assert_equal 10, op.precise_time_input.hour
408
- assert_equal 0, op.precise_time_input.min
409
- assert_equal 30, op.precise_time_input.sec
410
- assert_equal 0, op.precise_time_input.usec
411
-
412
- op.precise_time_input = '2024-11-11T16:42:23.246+0100'
413
- assert_equal ::Time, op.precise_time_input.class
414
- assert_equal 3600, op.precise_time_input.utc_offset
415
- assert_equal 2024, op.precise_time_input.year
416
- assert_equal 11, op.precise_time_input.month
417
- assert_equal 11, op.precise_time_input.day
418
- assert_equal 16, op.precise_time_input.hour
419
- assert_equal 42, op.precise_time_input.min
420
- assert_equal 23, op.precise_time_input.sec
421
- assert_equal 246000, op.precise_time_input.usec
422
-
423
- time = Time.at(1678741605.123456).utc
424
- op.precise_time_input = time
425
- assert_equal time, op.precise_time_input
426
- assert_equal time.object_id, op.precise_time_input.object_id
427
- assert_equal 2023, op.precise_time_input.year
428
- assert_equal 3, op.precise_time_input.month
429
- assert_equal 13, op.precise_time_input.day
430
- assert_equal 21, op.precise_time_input.hour
431
- assert_equal 6, op.precise_time_input.min
432
- assert_equal 45, op.precise_time_input.sec
433
- assert_equal 123456, op.precise_time_input.usec
434
- end
435
-
436
- def test_iso_date_inputs
437
- op.iso_date_input = nil
438
- assert_nil op.iso_date_input
439
-
440
- op.iso_date_input = '2022-12-22'
441
- assert_equal ::String, op.iso_date_input.class
442
- assert_equal '2022-12-22', op.iso_date_input
443
-
444
- op.iso_date_input = Date.parse('2022-12-22')
445
- assert_equal ::String, op.iso_date_input.class
446
- assert_equal '2022-12-22', op.iso_date_input
447
- end
448
-
449
- def test_iso_time_inputs
450
- op.iso_time_input = nil
451
- assert_nil op.iso_time_input
452
-
453
- op.iso_time_input = '2022-12-22T10:30:24Z'
454
- assert_equal ::String, op.iso_time_input.class
455
- assert_equal '2022-12-22T10:30:24.000Z', op.iso_time_input
456
-
457
- op.iso_time_input = Time.parse('2022-12-22T10:30:24.123456Z')
458
- assert_equal ::String, op.iso_time_input.class
459
- assert_equal '2022-12-22T10:30:24.123Z', op.iso_time_input
460
- end
461
-
462
- def test_file_inputs
463
- op.file_input = nil
464
- assert_nil op.file_input
465
-
466
- op.file_input = File.new(__FILE__)
467
- assert_equal ::File, op.file_input.class
468
-
469
- op.file_input = 'foobarbaz'
470
- assert_equal ::Tempfile, op.file_input.class
471
- assert_equal 'foobarbaz', op.file_input.read
472
- ensure
473
- op.file_input.close
474
- op.file_input.unlink
475
- end
476
-
477
- def test_when_a_type_cast_fails_a_type_cast_error_is_raised
478
- assert_raises Subroutine::TypeCaster::TypeCastError do
479
- op.date_input = "2015-13-01"
480
- end
481
-
482
- assert_raises "invalid date" do
483
- op.date_input = "2015-13-01"
484
- end
485
- end
486
-
487
- def test_foreign_key_inputs
488
- op.fk_input_owner_id = nil
489
- assert_nil op.fk_input_owner_id
490
-
491
- op.fk_input_owner_id = ""
492
- assert_nil op.fk_input_owner_id
493
-
494
- op.fk_input_owner_id = "19402"
495
- assert_equal 19402, op.fk_input_owner_id
496
-
497
- op.fk_input_owner_id = "19402.0"
498
- assert_equal 19402, op.fk_input_owner_id
499
-
500
- op.fk_input_email_address = nil
501
- assert_nil op.fk_input_email_address
502
-
503
- op.fk_input_email_address = ""
504
- assert_nil op.fk_input_email_address
505
-
506
- op.fk_input_email_address = "foo@bar.com"
507
- assert_equal "foo@bar.com", op.fk_input_email_address
508
- end
509
- end
510
- end