has_addresses 0.0.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,171 +1,302 @@
1
1
  require "#{File.dirname(__FILE__)}/../test_helper"
2
2
 
3
- class AddressTest < Test::Unit::TestCase
4
- fixtures :companies, :countries, :regions, :addresses
3
+ class AddressByDefaultTest < ActiveRecord::TestCase
4
+ def setup
5
+ @address = Address.new
6
+ end
7
+
8
+ def test_should_not_have_an_addressable_id
9
+ assert_nil @address.addressable_id
10
+ end
5
11
 
6
- def test_address_in_known_region_should_be_valid
7
- assert_valid addresses(:in_known_region)
12
+ def test_should_not_have_an_addressable_type
13
+ assert @address.addressable_type.blank?
8
14
  end
9
15
 
10
- def test_address_in_unknown_region_should_be_valid
11
- assert_valid addresses(:in_unknown_region)
16
+ def test_should_not_have_a_street_1
17
+ assert @address.street_1.blank?
12
18
  end
13
19
 
14
- def test_should_require_addressable_id
15
- assert_invalid addresses(:google_headquarters), :addressable_id, nil
20
+ def test_should_not_have_a_street_2
21
+ assert @address.street_2.blank?
16
22
  end
17
23
 
18
- def test_should_require_addressable_type
19
- assert_invalid addresses(:google_headquarters), :addressable_type, nil
24
+ def test_should_not_have_a_city
25
+ assert @address.city.blank?
20
26
  end
21
27
 
22
- def test_should_require_street_1
23
- assert_invalid addresses(:google_headquarters), :street_1, nil
28
+ def test_should_not_have_a_region
29
+ assert_nil @address.region_id
24
30
  end
25
31
 
26
- def test_should_require_city
27
- assert_invalid addresses(:google_headquarters), :city, nil
32
+ def test_should_not_have_a_custom_region
33
+ assert @address.custom_region.blank?
28
34
  end
29
35
 
30
- def test_should_require_postal_code
31
- assert_invalid addresses(:google_headquarters), :postal_code, nil
36
+ def test_should_not_have_a_region_name
37
+ assert @address.region_name.blank?
32
38
  end
33
39
 
34
- def test_should_require_five_digit_postal_codes
35
- assert_invalid addresses(:google_headquarters), :postal_code, '123456', '1234', 'abcdef'
40
+ def test_should_not_have_a_postal_code
41
+ assert @address.postal_code.blank?
36
42
  end
37
43
 
38
- def test_should_require_region_id_if_country_has_regions
39
- assert_invalid addresses(:in_known_region), :region_id, nil
44
+ def test_should_not_have_a_country
45
+ assert_nil @address.country_id
40
46
  end
41
47
 
42
- def test_should_not_require_custom_region_if_country_has_regions
43
- assert_valid addresses(:in_known_region), :custom_region, nil
48
+ def test_should_not_have_any_address_lines
49
+ assert @address.multi_line.empty?
50
+ end
51
+ end
52
+
53
+ class AddressTest < ActiveRecord::TestCase
54
+ def test_should_be_valid_with_a_set_of_valid_attributes
55
+ address = new_address
56
+ assert address.valid?
44
57
  end
45
58
 
46
- def test_should_require_custom_region_if_country_has_no_regions
47
- assert_invalid addresses(:in_unknown_region), :custom_region, nil
59
+ def test_should_require_an_addressable_id
60
+ address = new_address(:addressable => nil)
61
+ assert !address.valid?
62
+ assert address.errors.invalid?(:addressable_id)
48
63
  end
49
64
 
50
- def test_should_not_require_region_id_if_country_has_no_regions
51
- assert_valid addresses(:in_unknown_region), :region_id, nil
65
+ def test_should_require_an_addressable_type
66
+ address = new_address(:addressable => nil)
67
+ assert !address.valid?
68
+ assert address.errors.invalid?(:addressable_type)
52
69
  end
53
70
 
54
- def test_should_have_addressable_association
55
- assert_not_nil addresses(:google_headquarters).addressable
71
+ def test_should_require_a_street_1
72
+ address = new_address(:street_1 => nil)
73
+ assert !address.valid?
74
+ assert address.errors.invalid?(:street_1)
56
75
  end
57
76
 
58
- def test_known_region_should_have_region_association
59
- assert_not_nil addresses(:in_known_region).region
77
+ def test_should_not_require_a_street_2
78
+ address = new_address(:street_2 => nil)
79
+ assert address.valid?
60
80
  end
61
81
 
62
- def test_unknown_region_should_not_have_region_association
63
- assert_nil addresses(:in_unknown_region).region
82
+ def test_should_require_a_city
83
+ address = new_address(:city => nil)
84
+ assert !address.valid?
85
+ assert address.errors.invalid?(:city)
64
86
  end
65
87
 
66
- def test_known_region_should_have_country_association
67
- assert_nil addresses(:in_known_region).country_id
68
- assert_not_nil addresses(:in_known_region).country
69
- assert_equal countries(:united_states), addresses(:in_known_region).country
88
+ def test_should_require_a_postal_code
89
+ address = new_address(:postal_code => nil)
90
+ assert !address.valid?
91
+ assert address.errors.invalid?(:postal_code)
70
92
  end
71
93
 
72
- def test_unknown_region_should_have_country_association
73
- assert_not_nil addresses(:in_unknown_region).country_id
74
- assert_not_nil addresses(:in_unknown_region).country
75
- assert_equal countries(:canada), addresses(:in_unknown_region).country
94
+ def test_should_require_a_region_if_country_has_regions
95
+ country = create_country
96
+ create_region(:country => country)
97
+
98
+ address = new_address(:country => country, :region => nil)
99
+ assert !address.valid?
100
+ assert address.errors.invalid?(:region_id)
76
101
  end
77
102
 
78
- def test_should_clear_country_id_with_known_region
79
- address = addresses(:in_known_region)
80
- address.country = countries(:united_states)
103
+ def test_should_not_require_a_region_if_country_has_no_regions
104
+ country = create_country
81
105
 
82
- assert_not_nil address.country_id
83
- address.save!
84
- assert_nil address.country_id
106
+ address = new_address(:country => country, :region => nil, :custom_region => 'Somewhere')
107
+ assert address.valid?
85
108
  end
86
109
 
87
- def test_should_clear_custom_region_with_known_region
88
- address = addresses(:in_known_region)
89
- address.custom_region = 'Somewhere'
110
+ def test_should_require_a_custom_region_if_country_has_no_regions
111
+ country = create_country
90
112
 
91
- assert_not_nil address.custom_region
92
- address.save!
93
- assert_nil address.custom_region
113
+ address = new_address(:country => country, :region => nil, :custom_region => nil)
114
+ assert !address.valid?
115
+ assert address.errors.invalid?(:custom_region)
116
+ end
117
+
118
+ def test_should_not_require_a_custom_region_if_country_has_regions
119
+ country = create_country
120
+ region = create_region(:country => country)
121
+
122
+ address = new_address(:country => country, :region => region, :custom_region => nil)
123
+ assert address.valid?
124
+ end
125
+
126
+ def test_should_require_a_country
127
+ address = new_address(:country => nil, :region => nil)
128
+ assert !address.valid?
129
+ assert address.errors.invalid?(:country_id)
130
+ end
131
+
132
+ def test_should_protect_attributes_from_mass_assignment
133
+ country = create_country
134
+ region = create_region(:country => country)
135
+
136
+ address = Address.new(
137
+ :id => 1,
138
+ :addressable_id => 1,
139
+ :addressable_type => 'User',
140
+ :street_1 => '1600 Amphitheatre Parkway',
141
+ :street_2 => 'Apt. 1',
142
+ :city => 'Mountain View',
143
+ :region => region,
144
+ :custom_region => 'Anywhere',
145
+ :postal_code => '94043',
146
+ :country => country
147
+ )
148
+
149
+ assert_nil address.id
150
+ assert_nil address.addressable_id
151
+ assert address.addressable_type.blank?
152
+ assert_equal '1600 Amphitheatre Parkway', address.street_1
153
+ assert_equal 'Apt. 1', address.street_2
154
+ assert_equal 'Mountain View', address.city
155
+ assert_equal region, address.region
156
+ assert_equal 'Anywhere', address.custom_region
157
+ assert_equal '94043', address.postal_code
158
+ assert_equal country, address.country
159
+ end
160
+ end
161
+
162
+ class AddressAfterBeingCreatedTest < ActiveRecord::TestCase
163
+ def setup
164
+ @company = create_company
165
+ @address = create_address(:addressable => @company)
166
+ end
167
+
168
+ def test_should_have_an_addressable_association
169
+ assert_equal @company, @address.addressable
170
+ end
171
+ end
172
+
173
+ class AddressInKnownRegionTest < ActiveRecord::TestCase
174
+ def setup
175
+ @country = create_country
176
+ @region = create_region(:country => @country, :name => 'California')
177
+ @address = create_address(:region => @region)
94
178
  end
95
179
 
96
- def test_region_name_shouild_use_region_association_when_known
97
- assert_equal 'California', addresses(:in_known_region).region_name
180
+ def test_should_have_a_region
181
+ assert_not_nil @address.region
98
182
  end
99
183
 
100
- def test_region_name_should_use_custom_region_when_unknown
101
- assert_equal 'Anywhere', addresses(:in_unknown_region).region_name
184
+ def test_should_have_a_region_name
185
+ assert_equal 'California', @address.region_name
102
186
  end
103
187
 
104
- def test_multiline
105
- a = Address.new
106
- a.street_1 = '123 Fake Street'
107
- assert_equal ['123 Fake Street'], a.multi_line
188
+ def test_should_have_a_country
189
+ assert_equal @country, @address.country
190
+ end
191
+
192
+ def test_should_clear_custom_region
193
+ @address.custom_region = 'Somewhere'
194
+ @address.valid?
195
+ assert_nil @address.custom_region
196
+ end
197
+ end
198
+
199
+ class AddressInUnknownRegionTest < ActiveRecord::TestCase
200
+ def setup
201
+ @country = create_country
202
+ @address = create_address(:country => @country, :region => nil, :custom_region => 'Somewhere')
203
+ end
204
+
205
+ def test_should_not_have_a_region
206
+ assert_nil @address.region
207
+ end
208
+
209
+ def test_should_have_a_country
210
+ assert_equal @country, @address.country
211
+ end
212
+
213
+ def test_should_have_a_region_name
214
+ assert_equal 'Somewhere', @address.region_name
215
+ end
216
+ end
217
+
218
+ class AddressConstructionTest < ActiveRecord::TestCase
219
+ def setup
220
+ @country = create_country(:alpha_2_code => 'US', :name => 'United States')
221
+ @region = create_region(:country => @country, :abbreviation => 'CA', :name => 'California')
222
+ end
223
+
224
+ def test_should_construct_multiline_based_on_current_information
225
+ address = Address.new
226
+ address.street_1 = '123 Fake Street'
227
+ assert_equal ['123 Fake Street'], address.multi_line
108
228
 
109
- a.street_2 = 'Apartment 456'
229
+ address.street_2 = 'Apartment 456'
110
230
  expected = [
111
231
  '123 Fake Street',
112
232
  'Apartment 456'
113
233
  ]
114
- assert_equal expected, a.multi_line
234
+ assert_equal expected, address.multi_line
115
235
 
116
- a.city = 'Somewhere'
236
+ address.city = 'Somewhere'
117
237
  expected = [
118
238
  '123 Fake Street',
119
239
  'Apartment 456',
120
240
  'Somewhere'
121
241
  ]
122
- assert_equal expected, a.multi_line
242
+ assert_equal expected, address.multi_line
123
243
 
124
- a.region = regions(:california)
244
+ address.region = @region
245
+ address.country = @country
125
246
  expected = [
126
247
  '123 Fake Street',
127
248
  'Apartment 456',
128
249
  'Somewhere, California',
129
250
  'United States'
130
251
  ]
131
- assert_equal expected, a.multi_line
252
+ assert_equal expected, address.multi_line
132
253
 
133
- a.postal_code = '12345'
254
+ address.postal_code = '12345'
134
255
  expected = [
135
256
  '123 Fake Street',
136
257
  'Apartment 456',
137
258
  'Somewhere, California 12345',
138
259
  'United States'
139
260
  ]
140
- assert_equal expected, a.multi_line
261
+ assert_equal expected, address.multi_line
262
+
263
+ address.region = nil
264
+ address.custom_region = 'Anywhere'
265
+ expected = [
266
+ '123 Fake Street',
267
+ 'Apartment 456',
268
+ 'Somewhere, Anywhere 12345',
269
+ 'United States'
270
+ ]
271
+ assert_equal expected, address.multi_line
141
272
 
142
- a.region = nil
143
- a.custom_region = 'Anywhere'
273
+ address.country = nil
144
274
  expected = [
145
275
  '123 Fake Street',
146
276
  'Apartment 456',
147
277
  'Somewhere, Anywhere 12345'
148
278
  ]
149
- assert_equal expected, a.multi_line
279
+ assert_equal expected, address.multi_line
150
280
 
151
- a.city = nil
281
+ address.city = nil
152
282
  expected = [
153
283
  '123 Fake Street',
154
284
  'Apartment 456',
155
285
  'Anywhere 12345'
156
286
  ]
157
- assert_equal expected, a.multi_line
287
+ assert_equal expected, address.multi_line
158
288
 
159
- a.postal_code = nil
289
+ address.postal_code = nil
160
290
  expected = [
161
291
  '123 Fake Street',
162
292
  'Apartment 456',
163
293
  'Anywhere'
164
294
  ]
165
- assert_equal expected, a.multi_line
166
- end
295
+ assert_equal expected, address.multi_line
296
+ end
167
297
 
168
- def test_single_line
169
- assert_equal '1600 Amphitheatre Parkway, Mountain View, California 94043, United States', addresses(:google_headquarters).single_line
298
+ def test_should_construct_single_line_based_on_current_information
299
+ address = create_address(:street_1 => '1600 Amphitheatre Parkway', :city => 'Mountain View', :region => @region, :postal_code => '94043')
300
+ assert_equal '1600 Amphitheatre Parkway, Mountain View, California 94043, United States', address.single_line
170
301
  end
171
- end
302
+ end
@@ -1,119 +1,148 @@
1
1
  require "#{File.dirname(__FILE__)}/../test_helper"
2
2
 
3
- unless ENV['LIVE']
4
- Country::ISO_3166_URL = "#{RAILS_ROOT}/../files/iso_3166.xml"
5
- end
6
-
7
- class CountryTest < Test::Unit::TestCase
8
- fixtures :countries, :regions
9
-
10
- def test_should_download_and_parse_countries_from_iso_standard
11
- countries = Country.download_all
12
- assert_instance_of Array, countries
13
- assert !countries.empty?
3
+ class CountryByDefaultTest < ActiveRecord::TestCase
4
+ def setup
5
+ @country = Country.new
14
6
  end
15
7
 
16
- def test_bootstrap_should_remove_existing_records
17
- country = Country.create(:name => 'Test', :alpha_2_code => 'TE', :alpha_3_code => 'TES')
18
- assert !country.new_record?
19
- Country.bootstrap
20
- assert_nil Country.find_by_name('Test')
8
+ def test_should_not_have_a_name
9
+ assert_nil @country.name
21
10
  end
22
11
 
23
- def test_bootstrap_should_insert_downloaded_countries
24
- Country.bootstrap
25
- assert Country.count > 2
12
+ def test_should_not_have_an_official_name
13
+ assert_nil @country.official_name
26
14
  end
27
15
 
28
- def test_bootstrap_should_return_true_if_successful
29
- assert Country.bootstrap
16
+ def test_should_not_have_an_alpha_2_code
17
+ assert_nil @country.alpha_2_code
30
18
  end
31
19
 
32
- def test_should_use_default_path_for_created_fixture
33
- Country.create_fixtures
34
- assert File.exists?("#{RAILS_ROOT}/db/bootstrap/countries.yml")
20
+ def test_should_not_have_an_alpha_3_code
21
+ assert_nil @country.alpha_3_code
35
22
  end
36
23
 
37
- def test_should_allow_custom_path_for_created_fixture
38
- Country.create_fixtures('db/bootstrap/the_countries.yml')
39
- assert File.exists?("#{RAILS_ROOT}/db/bootstrap/the_countries.yml")
24
+ def test_should_use_name_for_official_name
25
+ country = Country.new(:name => 'United States')
26
+ assert_equal 'United States', country.official_name
40
27
  end
41
28
 
42
- def test_created_fixture_should_load_into_table
43
- Country.create_fixtures
44
- Fixtures.create_fixtures("#{RAILS_ROOT}/db/bootstrap", 'countries')
45
- assert Country.count > 2
29
+ def test_should_not_use_name_for_official_name_if_specified
30
+ country = Country.new(:official_name => nil)
31
+ assert_nil country.official_name
46
32
  end
47
-
48
- def test_should_be_valid
49
- assert_valid countries(:united_states)
33
+ end
34
+
35
+ class CountryTest < ActiveRecord::TestCase
36
+ def test_should_be_valid_with_a_set_of_valid_attributes
37
+ country = new_country
38
+ assert country.valid?
50
39
  end
51
40
 
52
- def test_should_require_name
53
- assert_invalid countries(:united_states), :name, nil
41
+ def test_should_require_a_name
42
+ country = new_country(:name => nil)
43
+ assert !country.valid?
44
+ assert country.errors.invalid?(:name)
54
45
  end
55
46
 
56
- def test_should_restrict_name_length
57
- assert_invalid countries(:united_states), :name, 'U', 'U' * 81
58
- assert_valid countries(:united_states), :name, 'US', 'U' * 80
47
+ def test_should_not_require_an_official_name
48
+ country = new_country(:official_name => nil)
49
+ assert country.valid?
59
50
  end
60
51
 
61
- def test_should_require_unique_name
62
- assert_invalid countries(:united_states).clone, :name
52
+ def test_should_require_an_alpha_2_code
53
+ country = new_country(:alpha_2_code => nil)
54
+ assert !country.valid?
55
+ assert country.errors.invalid?(:alpha_2_code)
63
56
  end
64
57
 
65
- def test_should_require_alpha_2_code
66
- assert_invalid countries(:united_states), :alpha_2_code, nil
58
+ def test_should_require_a_unique_alpha_2_code
59
+ create_country(:alpha_2_code => 'US')
60
+
61
+ country = new_country(:alpha_2_code => 'US')
62
+ assert !country.valid?
63
+ assert country.errors.invalid?(:alpha_2_code)
67
64
  end
68
65
 
69
- def test_should_restrict_alpha_2_code_length
70
- assert_invalid countries(:united_states), :alpha_2_code, 'U', 'USA'
71
- assert_valid countries(:united_states), :alpha_2_code, 'US'
66
+ def test_should_not_allow_alpha_2_codes_shorter_or_longer_than_2_characters
67
+ country = new_country(:alpha_2_code => 'a')
68
+ assert !country.valid?
69
+ assert country.errors.invalid?(:alpha_2_code)
70
+
71
+ country.alpha_2_code += 'a'
72
+ assert country.valid?
73
+
74
+ country.alpha_2_code += 'a'
75
+ assert !country.valid?
76
+ assert country.errors.invalid?(:alpha_2_code)
72
77
  end
73
78
 
74
- def test_should_require_unique_alpha_2_code
75
- assert_invalid countries(:united_states).clone, :alpha_2_code
79
+ def test_should_require_an_alpha_3_code
80
+ country = new_country(:alpha_3_code => nil)
81
+ assert !country.valid?
82
+ assert country.errors.invalid?(:alpha_3_code)
76
83
  end
77
84
 
78
- def test_should_require_alpha_3_code
79
- assert_invalid countries(:united_states), :alpha_3_code, nil
85
+ def test_should_not_allow_alpha_3_codes_shorter_or_longer_than_3_characters
86
+ country = new_country(:alpha_3_code => 'aa')
87
+ assert !country.valid?
88
+ assert country.errors.invalid?(:alpha_3_code)
89
+
90
+ country.alpha_3_code += 'a'
91
+ assert country.valid?
92
+
93
+ country.alpha_3_code += 'a'
94
+ assert !country.valid?
95
+ assert country.errors.invalid?(:alpha_3_code)
80
96
  end
81
97
 
82
- def test_should_restrict_alpha_3_code_length
83
- assert_invalid countries(:united_states), :alpha_3_code, 'US', 'USAC'
84
- assert_valid countries(:united_states), :alpha_3_code, 'USA'
98
+ def test_should_use_alpha_2_code_for_abbreviation_2
99
+ country = new_country(:alpha_2_code => 'US')
100
+ assert_equal 'US', country.abbreviation_2
85
101
  end
86
102
 
87
- def test_should_require_unique_alpha_3_code
88
- assert_invalid countries(:united_states).clone, :alpha_3_code
103
+ def test_should_use_alpha_3_code_for_abbreviation_3
104
+ country = new_country(:alpha_3_code => 'USA')
105
+ assert_equal 'USA', country.abbreviation_3
89
106
  end
90
107
 
91
- def test_abbreviation_2_should_be_same_as_alpha_2_code
92
- assert_equal 'US', countries(:united_states).abbreviation_2
108
+ def test_should_use_alpha_2_code_for_stringification
109
+ country = new_country(:alpha_2_code => 'US')
110
+ assert_equal 'US', country.to_s
93
111
  end
94
-
95
- def test_abbreviation_3_should_be_same_as_alpha_3_code
96
- assert_equal 'USA', countries(:united_states).abbreviation_3
112
+ end
113
+
114
+ class CountryAfterBeingCreatedTest < ActiveRecord::TestCase
115
+ def setup
116
+ @country = create_country
97
117
  end
98
118
 
99
- def test_should_have_an_official_name
100
- assert_equal 'United States of America', countries(:united_states).official_name
119
+ def test_should_not_have_any_regions
120
+ assert @country.regions.empty?
101
121
  end
102
122
 
103
- def test_should_use_name_when_official_name_is_not_present
104
- assert_equal 'Canada', countries(:canada).official_name
123
+ def test_should_not_have_any_addresses
124
+ assert @country.addresses.empty?
105
125
  end
106
-
107
- def test_should_return_name_for_to_s
108
- assert_equal 'United States', countries(:united_states).to_s
109
- assert_equal 'Canada', countries(:canada).to_s
126
+ end
127
+
128
+ class CountryWithRegionsTest < ActiveRecord::TestCase
129
+ def setup
130
+ @country = create_country
131
+ @region = create_region(:country => @country)
110
132
  end
111
133
 
112
- def test_regions
113
- assert_equal [regions(:california)], countries(:united_states).regions
134
+ def test_should_have_regions
135
+ assert_equal [@region], @country.regions
136
+ end
137
+ end
138
+
139
+ class CountryWithAddressesTest < ActiveRecord::TestCase
140
+ def setup
141
+ @country = create_country
142
+ @address = create_address(:country => @country)
114
143
  end
115
144
 
116
- def teardown
117
- FileUtils.rmtree("#{RAILS_ROOT}/db/bootstrap")
145
+ def test_should_have_addresses
146
+ assert_equal [@address], @country.addresses
118
147
  end
119
148
  end