validates_zipcode 0.3.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,380 +4,30 @@ require 'spec_helper'
4
4
  # NOTE: Spain has the full case specs.
5
5
  # Do not apply all checks to all countries for suite speed.
6
6
  describe ValidatesZipcode, '#validate_each' do
7
- context 'Argentina' do
8
- it 'does not add errors with a valid zipcode' do
9
- record = build_record('C1424CHN', 'AR')
10
- zipcode_should_be_valid(record)
11
- end
12
-
13
- it 'adds errors with an invalid Zipcode' do
14
- %w[1234 12345 D0D0D0 invalid_zip].each do |zipcode|
15
- record = build_record(zipcode, 'AR')
16
- zipcode_should_be_invalid(record, zipcode)
17
- end
18
- end
19
- end
20
-
21
- context 'Australia and New Zeland' do
22
- it 'does not add errors with a valid zipcode' do
23
- %w[AU NZ].each do |code|
24
- record = build_record('9310', code)
25
- zipcode_should_be_valid(record)
26
- end
27
- end
28
-
29
- it 'adds errors with an invalid Zipcode' do
30
- %w[AU NZ].each do |code|
31
- %w[C1424CHN 12345 invalid_zip].each do |zipcode|
32
- record = build_record(zipcode, code)
33
- zipcode_should_be_invalid(record, zipcode)
7
+ TEST_DATA.each do |country_code, values|
8
+ context country_code do
9
+ context 'validates with valid zipcodes' do
10
+ values[:valid].each do |zipcode|
11
+ it zipcode do
12
+ record = build_record(zipcode, country_code)
13
+ zipcode_should_be_valid(record)
14
+ end
34
15
  end
35
16
  end
36
- end
37
- end
38
-
39
- context 'Brazil' do
40
- it 'validates with a valid zipcode' do
41
- ['72315', '72315-500', '72315500', '24210-325', '24210325'].each do |zipcode|
42
- record = build_record(zipcode, 'BR')
43
- zipcode_should_be_valid(record)
44
- end
45
- end
46
-
47
- it 'does not validate with an invalid zipcode' do
48
- record = build_record('723155', 'BR')
49
- zipcode_should_be_invalid(record, '723155')
50
- end
51
- end
52
-
53
- context 'Canada' do
54
- it 'does not add errors with a valid zipcode' do
55
- record = build_record('A1J2Z9', 'CA')
56
- zipcode_should_be_valid(record)
57
- end
58
-
59
- it 'adds errors with an invalid Zipcode' do
60
- %w[C1424CHN 12345 D0D0D0 invalid_zip].each do |zipcode|
61
- record = build_record(zipcode, 'CA')
62
- zipcode_should_be_invalid(record, zipcode)
63
- end
64
- end
65
- end
66
-
67
- context 'Chile' do
68
- it 'does not add errors with a valid zipcode' do
69
- ['123-1233', '000-0000', '1234567'].each do |zipcode|
70
- record = build_record(zipcode, 'CL')
71
- zipcode_should_be_valid(record)
72
- end
73
- end
74
-
75
- it 'adds errors with an invalid Zipcode' do
76
- ['1234', '12345', 'D0D0D0', 'invalid_zip', 'C1424CHN', '122-12345'].each do |zipcode|
77
- record = build_record(zipcode, 'CL')
78
- zipcode_should_be_invalid(record, zipcode)
79
- end
80
- end
81
- end
82
17
 
83
- context 'Czech' do
84
- it 'validates with a valid zipcode' do
85
- ['12000', '721 00'].each do |zipcode|
86
- record = build_record(zipcode, 'CZ')
87
- zipcode_should_be_valid(record)
88
- end
89
- end
90
-
91
- it 'does not validate with an invalid zipcode' do
92
- record = build_record('981 32', 'CZ')
93
- zipcode_should_be_invalid(record, '981 32')
94
- end
95
- end
96
-
97
- context 'Ireland' do
98
- it 'does not add errors with a valid postcode - old format' do
99
- ['D02 AF30', 'X91 PK81', 'V94 H2PP', 'D07 R6YE', 'D6W1234',
100
- 'A230984', 'D00AV92', 'Y631FHK', 'A000000', 'D44N4X4'].each do |zipcode|
101
- record = build_record(zipcode, 'IE')
102
- zipcode_should_be_valid(record)
103
- end
104
- end
105
-
106
- it 'adds errors with an invalid Zipcode' do
107
- ['1234', '12345-12345', 'invalid_zip'].each do |zipcode|
108
- record = build_record(zipcode, 'IE')
109
- zipcode_should_be_invalid(record, zipcode)
110
- end
111
- end
112
- end
113
-
114
- context 'Israel' do
115
- it 'validates with a valid zipcode' do
116
- %w[1029200 880000 90001].each do |zipcode|
117
- record = build_record(zipcode, 'IL')
118
- zipcode_should_be_valid(record)
119
- end
120
- end
121
-
122
- it 'does not validate with an invalid zipcode' do
123
- record = build_record('1200', 'IL')
124
- zipcode_should_be_invalid(record, '1200')
125
- end
126
- end
127
-
128
- context 'Japan' do
129
- it 'validates with a valid zipcode' do
130
- %w[1029200 102-9200 408-0301 207-1000].each do |zipcode|
131
- record = build_record(zipcode, 'JP')
132
- zipcode_should_be_valid(record)
133
- end
134
- end
135
-
136
- it 'does not validate with an invalid zipcode' do
137
- record = build_record('1200', 'JP')
138
- zipcode_should_be_invalid(record, '1200')
139
- end
140
- end
141
-
142
- context 'Lithuania' do
143
- it 'validates with a valid zipcode' do
144
- record = build_record('LT-00110', 'LT')
145
- zipcode_should_be_valid(record)
146
- record = build_record('00110', 'LT')
147
- zipcode_should_be_valid(record)
148
- end
149
-
150
- it 'does not validate with an invalid zipcode' do
151
- record = build_record('2100', 'LT')
152
- zipcode_should_be_invalid(record, '2100')
153
- record = build_record('LT-2100', 'LT')
154
- zipcode_should_be_invalid(record, 'LT-2100')
155
- record = build_record('LT21006', 'LT')
156
- zipcode_should_be_invalid(record, 'LT21006')
157
- end
158
- end
159
-
160
- context 'Luxembourg' do
161
- it 'does not add errors with a valid zipcode' do
162
- ['L-5635', '5635'].each do |zipcode|
163
- record = build_record(zipcode, 'LU')
164
- zipcode_should_be_valid(record)
165
- end
166
- end
167
-
168
- it 'adds errors with an invalid Zipcode' do
169
- ['124', '12345-12345', 'D0D0D0', 'invalid_zip'].each do |zipcode|
170
- record = build_record(zipcode, 'LU')
171
- zipcode_should_be_invalid(record, zipcode)
172
- end
173
- end
174
- end
175
-
176
- context 'Mauritius' do
177
- it 'validates with a valid zipcode' do
178
- %w[42602 A2101 r6414].each do |zipcode|
179
- record = build_record(zipcode, 'MU')
180
- zipcode_should_be_valid(record)
181
- end
182
- end
183
-
184
- it 'does not validate with an invalid zipcode' do
185
- %w[05678 B4242].each do |zipcode|
186
- record = build_record(zipcode, 'MU')
187
- zipcode_should_be_invalid(record, zipcode)
188
- end
189
- end
190
- end
191
-
192
- context 'Moldova' do
193
- it 'validates with a valid zipcode' do
194
- record = build_record('MD2001', 'MD')
195
- zipcode_should_be_valid(record)
196
- end
197
-
198
- it 'does not validate with an invalid zipcode' do
199
- record = build_record('MD-2100', 'MD')
200
- zipcode_should_be_invalid(record, 'MD-2100')
201
- end
202
- end
18
+ context 'does not validate with invalid zipcodes' do
19
+ values[:invalid].each do |zipcode|
20
+ display_name = zipcode.nil? ? 'nil' : zipcode.blank? ? 'blank' : zipcode
203
21
 
204
- context 'Netherlands' do
205
- it 'does not add errors with a valid zipcode' do
206
- record = build_record('1001 AA', 'NL')
207
- zipcode_should_be_valid(record)
208
- end
209
-
210
- it 'adds errors with an invalid Zipcode' do
211
- ['0100 AA', '1001 SS', 'invalid_zip'].each do |zipcode|
212
- record = build_record(zipcode, 'NL')
213
- zipcode_should_be_invalid(record, zipcode)
214
- end
215
- end
216
- end
217
- context 'Panama' do
218
- it 'validates with a valid zipcode' do
219
- %w[0800 6369].each do |zipcode|
220
- record = build_record(zipcode, 'PA')
221
- zipcode_should_be_valid(record)
222
- end
223
- end
224
-
225
- it 'does not validate with an invalid zipcode' do
226
- %w[10800 369 A341].each do |zipcode|
227
- record = build_record(zipcode, 'PA')
228
- zipcode_should_be_invalid(record, zipcode)
229
- end
230
- end
231
- end
232
-
233
- context 'Portugal' do
234
- it 'validates with a valid zipcode' do
235
- ['4500', '4500-250'].each do |zipcode|
236
- record = build_record(zipcode, 'PT')
237
- zipcode_should_be_valid(record)
238
- end
239
- end
240
-
241
- it 'does not validate with an invalid zipcode' do
242
- record = build_record('4500-2500', 'PT')
243
- zipcode_should_be_invalid(record, '4500-2500')
244
- end
245
- end
246
-
247
- context 'Serbia' do
248
- it 'validates with a valid zipcode' do
249
- record = build_record('21000', 'RS')
250
- zipcode_should_be_valid(record)
251
- end
252
-
253
- it 'does not validate with an invalid zipcode' do
254
- record = build_record('2100', 'RS')
255
- zipcode_should_be_invalid(record, '2100')
256
- end
257
- end
258
-
259
- context 'Slovakia' do
260
- it 'validates with a valid zipcode' do
261
- ['83102', '911 05', '094 12'].each do |zipcode|
262
- record = build_record(zipcode, 'SK')
263
- zipcode_should_be_valid(record)
264
- end
265
- end
266
-
267
- it 'does not validate with an invalid zipcode' do
268
- record = build_record('120 00', 'SK')
269
- zipcode_should_be_invalid(record, '120 00')
270
- end
271
- end
272
-
273
- context 'Spain' do
274
- it 'does not add errors with a valid zipcode' do
275
- record = build_record('93108', 'ES')
276
- zipcode_should_be_valid(record)
277
- end
278
-
279
- it 'adds errors with an invalid Zipcode' do
280
- ['1234', '12345-12345', 'D0D0D0', 'invalid_zip'].each do |zipcode|
281
- record = build_record(zipcode, 'ES')
282
- zipcode_should_be_invalid(record, zipcode)
283
- record.errors.clear
284
- zipcode_should_be_invalid_with_custom_message(record, zipcode)
285
- end
286
- end
287
- end
288
-
289
- context 'South Korea' do
290
- it 'validates with a valid zipcode' do
291
- ['72315', '723-150', '11011', '110-110'].each do |zipcode|
292
- record = build_record(zipcode, 'KR')
293
- zipcode_should_be_valid(record)
294
- end
295
- end
296
-
297
- it 'does not validate with an invalid zipcode' do
298
- record = build_record('723155', 'KR')
299
- zipcode_should_be_invalid(record, '723155')
300
- end
301
- end
302
-
303
- context 'Taiwan' do
304
- it 'validates with a valid zipcode' do
305
- ['833', '74144'].each do |zipcode|
306
- record = build_record(zipcode, 'TW')
307
- zipcode_should_be_valid(record)
308
- end
309
- end
310
-
311
- it 'does not validate with an invalid zipcode' do
312
- record = build_record('4329', 'TW')
313
- zipcode_should_be_invalid(record, '4329')
314
- end
315
- end
316
-
317
- %w[UK GB].each do |cc|
318
- context cc do
319
- it 'validates with a valid zipcode' do
320
- [
321
- 'ID1 1QD',
322
- 'M32 0JG',
323
- 'NE30 1DP',
324
- 'OX49 5NU',
325
- 'SW1A 2AA',
326
- 'W1K 7DA',
327
- 'id1 1qd',
328
- 'Sw1A 2aA',
329
- 'id11qd',
330
- 'SW1A2AA'
331
- ].each do |zipcode|
332
- record = build_record(zipcode, cc)
333
- zipcode_should_be_valid(record)
334
- end
335
- end
336
-
337
- it 'does not validate with an invalid zipcode' do
338
- [nil, '', 'nope', 'id11qd1000', '28001'].each do |zipcode|
339
- record = build_record(zipcode, cc)
340
- zipcode_should_be_invalid(record, nil)
22
+ it display_name.to_s do
23
+ record = build_record(zipcode, country_code)
24
+ zipcode_should_be_invalid(record, nil)
25
+ end
341
26
  end
342
27
  end
343
28
  end
344
29
  end
345
30
 
346
- context 'USA' do
347
- it 'does not add errors with a valid zipcode' do
348
- record = build_record('93108', 'US')
349
- zipcode_should_be_valid(record)
350
- [12_345, '12345', '12345-1234', '12345-123456', '12345 1234', '12345 123456'].each do |zipcode|
351
- record = build_record(zipcode, 'US')
352
- zipcode_should_be_valid(record)
353
- end
354
- end
355
-
356
- it 'adds errors with an invalid Zipcode' do
357
- ['1234', '12345-12345', 'D0D0D0', 'invalid_zip'].each do |zipcode|
358
- record = build_record(zipcode, 'US')
359
- zipcode_should_be_invalid(record, zipcode)
360
- end
361
- end
362
- end
363
-
364
- context 'Nicaragua' do
365
- it 'validates with a valid zipcode' do
366
- # @see https://en.wikipedia.org/wiki/Postal_codes_in_Nicaragua
367
- ['11002', '11034'].each do |zipcode|
368
- record = build_record(zipcode, 'NI')
369
- zipcode_should_be_valid(record)
370
- end
371
- end
372
-
373
- it 'does not validate with an invalid zipcode' do
374
- ['1002', '111034'].each do |zipcode|
375
- record = build_record(zipcode, 'NI')
376
- zipcode_should_be_invalid(record, zipcode)
377
- end
378
- end
379
- end
380
-
381
31
  context 'unknown country' do
382
32
  it 'does not add errors with a any zipcode' do
383
33
  record = build_record('A1J2Z9', 'ZZ')
@@ -390,18 +40,30 @@ describe ValidatesZipcode, '#validate_each' do
390
40
  it 'validates with a valid zipcode' do
391
41
  %w[0800 6369].each do |zipcode|
392
42
  record = build_record(zipcode, 'PA')
393
- zipcode_should_be_valid(record, excluded_country_codes: ['PA'])
43
+ zipcode_should_be_valid(record, excluded_country_codes: %w[PA])
394
44
  end
395
45
  end
396
46
 
397
47
  it 'validates with an invalid zipcode' do
398
48
  %w[10800 369 A341].each do |zipcode|
399
49
  record = build_record(zipcode, 'PA')
400
- zipcode_should_be_valid(record, excluded_country_codes: ['PA'])
50
+ zipcode_should_be_valid(record, excluded_country_codes: %w[PA])
401
51
  end
402
52
  end
403
53
  end
404
54
  end
55
+
56
+ context 'with formatting' do
57
+ it 'is true with a valid zipcode for a given country code' do
58
+ zipcode_should_be_valid(build_record('28 108', 'ES'), format: true)
59
+ zipcode_should_be_invalid(build_record('28 108', 'ES'), nil)
60
+ zipcode_should_be_invalid(build_record('28 108', 'ES'), nil, format: false)
61
+
62
+ zipcode_should_be_valid(build_record('V2r-1c8', 'CA'), format: true)
63
+ zipcode_should_be_invalid(build_record('V2r-1c8', 'CA'), nil)
64
+ zipcode_should_be_invalid(build_record('V2r-1c8', 'CA'), nil, format: false)
65
+ end
66
+ end
405
67
  end
406
68
 
407
69
  describe ValidatesZipcode, '.valid?' do
@@ -421,17 +83,27 @@ describe ValidatesZipcode, '.valid?' do
421
83
  end
422
84
 
423
85
  it "is true with an excluded country code - we don't want those to fail and nil is hairy" do
424
- expect(ValidatesZipcode.valid?('XXXX!!!XXXX', 'PA', excluded_country_codes: ['PA'])).to eq(true)
86
+ expect(ValidatesZipcode.valid?('XXXX!!!XXXX', 'PA', excluded_country_codes: %w[PA])).to eq(true)
87
+ end
88
+ end
89
+
90
+ context 'with formatting' do
91
+ it 'is true with a valid zipcode for a given country code' do
92
+ expect(ValidatesZipcode.valid?('28 108', 'ES', format: true)).to eq(true)
93
+ expect(ValidatesZipcode.valid?('28 108', 'ES', format: false)).to eq(false)
94
+
95
+ expect(ValidatesZipcode.valid?('V2r-1c8', 'CA', format: true)).to eq(true)
96
+ expect(ValidatesZipcode.valid?('V2r-1c8', 'CA', format: false)).to eq(false)
425
97
  end
426
98
  end
427
99
  end
428
100
 
429
101
  describe ValidatesZipcode, '.format' do
430
- it "formats valid zipcodes" do
102
+ it 'formats valid zipcodes' do
431
103
  expect(ValidatesZipcode.format('Ka9 2dj', 'UK')).to eq('KA9 2DJ')
432
104
  end
433
105
 
434
- it "raises for invalid zipcodes" do
106
+ it 'raises for invalid zipcodes' do
435
107
  expect { ValidatesZipcode.format('KA9 1tr', 'DE') }.to raise_error(ValidatesZipcode::InvalidZipcodeError)
436
108
  end
437
109
  end