active_shipping 1.8.6 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,68 +1,403 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class PackageTest < Minitest::Test
4
- include ActiveShipping::Test::Fixtures
5
-
6
- GRAMS_IN_AN_OUNCE = 28.349523125
7
- OUNCES_IN_A_GRAM = 0.0352739619495804
8
- INCHES_IN_A_CM = 0.393700787401575
9
- CM_IN_AN_INCH = 2.54
10
-
11
4
  def setup
12
- @imperial_package = Package.new(65, [3, 6, 8.5],
13
- :units => :imperial,
14
- :value => 10.65,
15
- :currency => 'USD'
16
- )
17
-
18
- @metric_package = Package.new(100, [5, 18.5, 40],
19
- :value => 860,
20
- :currency => 'CAD'
21
- )
22
- end
23
-
24
- def test_accessors
25
- # :wii => Package.new((7.5 * 16), [15, 10, 4.5], :units => :imperial, :value => 269.99, :currency => 'GBP')
26
- wii = package_fixtures[:wii]
27
- [:x, :max, :long, :length].each do |sym|
28
- assert_equal 15, wii.inches(sym)
29
- assert_equal 15, wii.in(sym)
30
- assert_equal 15 * CM_IN_AN_INCH, wii.centimetres(sym)
31
- assert_equal 15 * CM_IN_AN_INCH, wii.cm(sym)
32
- end
33
- [:y, :mid, :width, :wide].each do |sym|
34
- assert_equal 10, wii.inches(sym)
35
- assert_equal 10, wii.in(sym)
36
- assert_equal 10 * CM_IN_AN_INCH, wii.centimetres(sym)
37
- assert_equal 10 * CM_IN_AN_INCH, wii.cm(sym)
38
- end
39
- [:z, :min, :height, :high, :depth, :deep].each do |sym|
40
- assert_equal 4.5, wii.inches(sym)
41
- assert_equal 4.5, wii.in(sym)
42
- assert_equal 4.5 * CM_IN_AN_INCH, wii.centimetres(sym)
43
- assert_equal 4.5 * CM_IN_AN_INCH, wii.cm(sym)
44
- end
45
- [:pounds, :lbs, :lb].each do |sym|
46
- assert_equal 7.5, wii.send(sym)
47
- end
48
- [:ounces, :oz].each do |sym|
49
- assert_equal 120, wii.send(sym)
50
- end
51
- [:grams, :g].each do |sym|
52
- assert_equal 120 * GRAMS_IN_AN_OUNCE, wii.send(sym)
53
- end
54
- [:kilograms, :kgs, :kg].each do |sym|
55
- assert_equal 120 * GRAMS_IN_AN_OUNCE / 1000, wii.send(sym)
56
- end
57
- assert_equal 675.0, wii.inches(:volume)
58
- assert_equal 675.0, wii.inches(:box_volume)
5
+ @weight = 100
6
+ @dimensions = [5, 6, 7]
7
+ @value = 1299
8
+ @currency = "USD"
9
+ @cylinder = false
10
+ @tube = false
11
+ @gift = false
12
+ @oversized = false
13
+ @unpackaged = false
14
+ @dim_units = :metric
15
+ @units = :metric
16
+ @weight_units = :metric
17
+ @options = {
18
+ value: @value,
19
+ currency: @currency,
20
+ cylinder: @cylinder,
21
+ tube: @tube,
22
+ gift: @gift,
23
+ oversized: @oversized,
24
+ unpackaged: @unpackaged,
25
+ dim_units: @dim_units,
26
+ units: @units,
27
+ weight_units: @weight_units,
28
+ }
29
+
30
+ @package = Package.new(@weight, @dimensions, @options)
31
+ @imperial_package = Package.new(@weight, @dimensions, @options.merge(units: :imperial, dim_units: :imperial, weight_units: :imperial))
59
32
 
60
- assert_equal 'GBP', wii.currency
61
- assert_equal 26999, wii.value
33
+ @mass = ::Quantified::Mass.new(@weight, :grams)
62
34
  end
63
35
 
64
36
  def test_package_from_mass
65
- pkg = Package.new(Quantified::Mass.new(10, :pounds), [])
66
- assert_equal 10, pkg.weight
37
+ package = Package.new(Quantified::Mass.new(10, :pounds), [])
38
+ assert_equal 10, package.weight
39
+ end
40
+
41
+ def test_initialize_defaults
42
+ assert_equal @value, @package.value
43
+ assert_equal @currency, @package.currency
44
+ assert_equal @cylinder, @package.cylinder?
45
+ assert_equal @tube, @package.tube?
46
+ assert_equal @oversized, @package.oversized?
47
+ assert_equal @unpackaged, @package.unpackaged?
48
+ assert_equal @gift, @package.gift?
49
+ end
50
+
51
+ def test_currency_cents
52
+ @package = Package.new(@weight, @dimensions, value: money)
53
+ assert_equal @currency, @package.currency
54
+ assert_equal @value, @package.value
55
+ end
56
+
57
+ def test_initialize_sorted_dimensions
58
+ @package = Package.new(@weight, [9, 8, 7], @options)
59
+
60
+ assert_equal [7, 8, 9], @package.centimetres
61
+ end
62
+
63
+ def test_initialize_blank_dimensions
64
+ @package = Package.new(@weight, [], @options)
65
+
66
+ assert_equal [0, 0, 0], @package.centimetres
67
+ end
68
+
69
+ def test_initialize_increases_dimension_size_to_three
70
+ @package = Package.new(@weight, [2], @options)
71
+
72
+ assert_equal [2, 2, 2], @package.centimetres
73
+ end
74
+
75
+ def test_initialize_default_units
76
+ assert_equal @dimensions, @package.centimetres
77
+ assert_equal @weight, @package.grams
78
+ end
79
+
80
+ def test_initialize_units
81
+ assert_equal @dimensions, @imperial_package.inches
82
+ end
83
+
84
+ def test_initialize_weight_units
85
+ @package = Package.new(@weight, @dimensions, @options.merge(weight_units: :imperial))
86
+
87
+ assert_equal @weight, @package.ounces
88
+ end
89
+
90
+ def test_unpackaged
91
+ assert_predicate Package.new(@weight, @dimensions, unpackaged: true), :unpackaged?
92
+ refute_predicate Package.new(@weight, @dimensions, unpackaged: false), :unpackaged?
93
+ end
94
+
95
+ def test_oversized
96
+ assert_predicate Package.new(@weight, @dimensions, oversized: true), :oversized?
97
+ refute_predicate Package.new(@weight, @dimensions, oversized: false), :oversized?
98
+ end
99
+
100
+ def test_gift
101
+ assert_predicate Package.new(@weight, @dimensions, gift: true), :gift?
102
+ refute_predicate Package.new(@weight, @dimensions, gift: false), :gift?
103
+ end
104
+
105
+ def test_cylinder_tube
106
+ @package = Package.new(@weight, @dimensions, cylinder: false, tube: false)
107
+ refute_predicate @package, :cylinder?
108
+ refute_predicate @package, :tube?
109
+
110
+ @package = Package.new(@weight, @dimensions, cylinder: true, tube: false)
111
+ assert_predicate @package, :cylinder?
112
+ assert_predicate @package, :tube?
113
+
114
+ @package = Package.new(@weight, @dimensions, cylinder: false, tube: true)
115
+ assert_predicate @package, :cylinder?
116
+ assert_predicate @package, :tube?
117
+
118
+ @package = Package.new(@weight, @dimensions, cylinder: true, tube: true)
119
+ assert_predicate @package, :cylinder?
120
+ assert_predicate @package, :tube?
121
+ end
122
+
123
+ def test_inches_number_index
124
+ assert_equal @dimensions[0], @imperial_package.inches(0)
125
+ assert_equal @dimensions[1], @imperial_package.inches(1)
126
+ assert_equal @dimensions[2], @imperial_package.inches(2)
127
+ assert_nil @imperial_package.inches(3)
128
+ end
129
+
130
+ def test_inches_x
131
+ assert_equal @dimensions[2], @imperial_package.inches(:x)
132
+ assert_equal @dimensions[2], @imperial_package.inches(:max)
133
+ assert_equal @dimensions[2], @imperial_package.inches(:length)
134
+ assert_equal @dimensions[2], @imperial_package.inches(:long)
135
+ end
136
+
137
+ def test_inches_y
138
+ assert_equal @dimensions[1], @imperial_package.inches(:y)
139
+ assert_equal @dimensions[1], @imperial_package.inches(:mid)
140
+ assert_equal @dimensions[1], @imperial_package.inches(:width)
141
+ assert_equal @dimensions[1], @imperial_package.inches(:wide)
142
+ end
143
+
144
+ def test_inches_z
145
+ assert_equal @dimensions[0], @imperial_package.inches(:z)
146
+ assert_equal @dimensions[0], @imperial_package.inches(:min)
147
+ assert_equal @dimensions[0], @imperial_package.inches(:height)
148
+ assert_equal @dimensions[0], @imperial_package.inches(:depth)
149
+ assert_equal @dimensions[0], @imperial_package.inches(:high)
150
+ assert_equal @dimensions[0], @imperial_package.inches(:deep)
151
+ end
152
+
153
+ def test_inches_girth_cylinder
154
+ @imperial_package = Package.new(@weight, @dimensions, @options.merge(cylinder: true, units: :imperial, dim_units: :imperial))
155
+
156
+ assert_predicate @imperial_package, :cylinder?
157
+ assert_in_delta 17.2, @imperial_package.inches(:girth), 1
158
+ assert_in_delta 17.2, @imperial_package.inches(:around), 1
159
+ assert_in_delta 17.2, @imperial_package.inches(:circumference), 1
160
+
161
+ end
162
+
163
+ def test_inches_girth
164
+ refute_predicate @imperial_package, :cylinder?
165
+ assert_in_delta 22, @imperial_package.inches(:girth), 1
166
+ assert_in_delta 22, @imperial_package.inches(:around), 1
167
+ assert_in_delta 22, @imperial_package.inches(:circumference), 1
168
+ end
169
+
170
+ def test_inches_volume_cylinder
171
+ @imperial_package = Package.new(@weight, @dimensions, @options.merge(cylinder: true, units: :imperial, dim_units: :imperial))
172
+
173
+ assert_predicate @imperial_package, :cylinder?
174
+ assert_in_delta 522.4, @imperial_package.inches(:volume), 1
175
+ end
176
+
177
+ def test_inches_volume
178
+ refute_predicate @imperial_package, :cylinder?
179
+ assert_equal 210, @imperial_package.inches(:volume)
180
+ end
181
+
182
+ def test_inches_box_volume
183
+ assert_equal 210, @imperial_package.inches(:box_volume)
184
+ end
185
+
186
+ def test_inches_unknown
187
+ assert_nil @imperial_package.inches(:unknown)
188
+ end
189
+
190
+ def test_inches_alias_in
191
+ assert_equal @dimensions, @imperial_package.inches
192
+ assert_equal @dimensions, @imperial_package.in
193
+ end
194
+
195
+ def test_centimetres_number_index
196
+ assert_equal @dimensions[0], @package.centimetres(0)
197
+ assert_equal @dimensions[1], @package.centimetres(1)
198
+ assert_equal @dimensions[2], @package.centimetres(2)
199
+ assert_nil @package.centimetres(3)
200
+ end
201
+
202
+ def test_centimetres_x
203
+ assert_equal @dimensions[2], @package.centimetres(:x)
204
+ assert_equal @dimensions[2], @package.centimetres(:max)
205
+ assert_equal @dimensions[2], @package.centimetres(:length)
206
+ assert_equal @dimensions[2], @package.centimetres(:long)
207
+ end
208
+
209
+ def test_centimetres_y
210
+ assert_equal @dimensions[1], @package.centimetres(:y)
211
+ assert_equal @dimensions[1], @package.centimetres(:mid)
212
+ assert_equal @dimensions[1], @package.centimetres(:width)
213
+ assert_equal @dimensions[1], @package.centimetres(:wide)
214
+ end
215
+
216
+ def test_centimetres_z
217
+ assert_equal @dimensions[0], @package.centimetres(:z)
218
+ assert_equal @dimensions[0], @package.centimetres(:min)
219
+ assert_equal @dimensions[0], @package.centimetres(:height)
220
+ assert_equal @dimensions[0], @package.centimetres(:depth)
221
+ assert_equal @dimensions[0], @package.centimetres(:high)
222
+ assert_equal @dimensions[0], @package.centimetres(:deep)
223
+ end
224
+
225
+ def test_centimetres_girth_cylinder
226
+ @package = Package.new(@weight, @dimensions, @options.merge(cylinder: true, units: :metric, dim_units: :metric))
227
+
228
+ assert_predicate @package, :cylinder?
229
+ assert_in_delta 17.2, @package.centimetres(:girth), 1
230
+ assert_in_delta 17.2, @package.centimetres(:around), 1
231
+ assert_in_delta 17.2, @package.centimetres(:circumference), 1
232
+
233
+ end
234
+
235
+ def test_centimetres_girth
236
+ refute_predicate @package, :cylinder?
237
+ assert_in_delta 22, @package.centimetres(:girth), 1
238
+ assert_in_delta 22, @package.centimetres(:around), 1
239
+ assert_in_delta 22, @package.centimetres(:circumference), 1
240
+ end
241
+
242
+ def test_centimetres_volume_cylinder
243
+ @package = Package.new(@weight, @dimensions, @options.merge(cylinder: true, units: :metric, dim_units: :metric))
244
+
245
+ assert_predicate @package, :cylinder?
246
+ assert_in_delta 522.4, @package.centimetres(:volume), 1
247
+ end
248
+
249
+ def test_centimetres_volume
250
+ refute_predicate @package, :cylinder?
251
+ assert_equal 210, @package.centimetres(:volume)
252
+ end
253
+
254
+ def test_centimetres_box_volume
255
+ assert_equal 210, @package.centimetres(:box_volume)
256
+ end
257
+
258
+ def test_centimetres_unknown
259
+ assert_nil @package.centimetres(:unknown)
260
+ end
261
+
262
+ def test_centimetres_alias_cm
263
+ assert_equal @dimensions, @package.centimetres
264
+ assert_equal @dimensions, @package.cm
265
+ end
266
+
267
+ def test_weight
268
+ assert_equal @mass, @package.weight
269
+ assert_instance_of ::Quantified::Mass, @package.weight
270
+ end
271
+
272
+ def test_weight_actual
273
+ assert_equal @mass, @package.weight(type: :actual)
274
+ assert_instance_of ::Quantified::Mass, @package.weight(type: :actual)
275
+ end
276
+
277
+ def test_weight_volumetric
278
+ assert_equal ::Quantified::Mass.new(35, :grams), @package.weight(type: :volumetric)
279
+ end
280
+
281
+ def test_weight_dimensional
282
+ assert_equal ::Quantified::Mass.new(35, :grams), @package.weight(type: :dimensional)
283
+ end
284
+
285
+ def test_weight_billable_max_weight_and_volumetric
286
+ assert_equal ::Quantified::Mass.new(100, :grams), @package.weight(type: :billable)
287
+
288
+ @package = Package.new(500, [1, 1, 1], @options)
289
+ assert_equal ::Quantified::Mass.new(500, :grams), @package.weight(type: :billable)
290
+ end
291
+
292
+ def test_grams_value
293
+ assert_equal 100, @package.grams
294
+ end
295
+
296
+ def test_grams_accepts_options_with_type
297
+ assert_in_delta 35, @package.grams(type: :volumetric), 1
298
+ end
299
+
300
+ def test_grams_converts
301
+ @package = Package.new(@weight, @dimensions, weight_units: :imperial)
302
+
303
+ assert_in_delta 2834.9, @package.grams, 1
304
+ end
305
+
306
+ def test_grams_alias_g
307
+ assert_equal @package.grams, @package.g
308
+ end
309
+
310
+ def test_ounces_value
311
+ assert_equal 100, @imperial_package.ounces
312
+ end
313
+
314
+ def test_ounces_accepts_options_with_type
315
+ assert_in_delta 20.2, @imperial_package.ounces(type: :volumetric), 1
316
+ end
317
+
318
+ def test_ounces_converts
319
+ assert_in_delta 3.5, @package.ounces, 1
320
+ end
321
+
322
+ def test_ounces_alias_oz
323
+ assert_equal @imperial_package.ounces, @imperial_package.oz
324
+ end
325
+
326
+ def test_pounds_value
327
+ assert_equal 6.25, @imperial_package.pounds
328
+ end
329
+
330
+ def test_pounds_accepts_options_with_type
331
+ assert_in_delta 0.07, @package.pounds(type: :volumetric), 0.01
332
+ end
333
+
334
+ def test_pounds_converts
335
+ assert_in_delta 0.22, @package.pounds, 0.01
336
+ end
337
+
338
+ def test_pounds_alias_lb
339
+ assert_equal @package.pounds, @package.lb
340
+ end
341
+
342
+ def test_pounds_alias_lbs
343
+ assert_equal @package.pounds, @package.lbs
344
+ end
345
+
346
+ def test_kilograms_value
347
+ assert_equal 0.1, @package.kilograms
348
+ end
349
+
350
+ def test_kilograms_accepts_options_with_type
351
+ assert_equal 0.035, @package.kilograms(type: :volumetric)
352
+ end
353
+
354
+ def test_kilograms_converts
355
+ assert_in_delta 2.8, @imperial_package.kilograms, 1
356
+ end
357
+
358
+ def test_kilograms_alias_kg
359
+ assert_equal 0.1, @package.kg
360
+ end
361
+
362
+ def test_kilograms_alias_kgs
363
+ assert_equal @package.kilograms, @package.kgs
364
+ end
365
+
366
+ def test_cents_from_nil
367
+ assert_nil Package.cents_from(nil)
368
+ end
369
+
370
+ def test_cents_from_cents
371
+ assert_equal @value, Package.cents_from(money)
372
+ end
373
+
374
+ def test_cents_from_float
375
+ assert_equal 120, Package.cents_from(1.2)
376
+ end
377
+
378
+ def test_cents_from_string
379
+ assert_equal 120, Package.cents_from("1.20")
380
+ end
381
+
382
+ def test_cents_from_int
383
+ assert_equal 12, Package.cents_from(12)
384
+ end
385
+
386
+ def test_cents_from_nonsense
387
+ exception = assert_raises NoMethodError do
388
+ Package.cents_from(Object.new)
389
+ end
390
+ assert_match /undefined method `to_i'/, exception.message
391
+ end
392
+
393
+ private
394
+
395
+ def money
396
+ @money ||= begin
397
+ value = Class.new { attr_accessor :currency, :cents }.new
398
+ value.currency = @currency
399
+ value.cents = @value
400
+ value
401
+ end
67
402
  end
68
403
  end