has_addresses 0.0.1

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.
@@ -0,0 +1,11 @@
1
+ # Load the plugin testing framework
2
+ $:.unshift("#{File.dirname(__FILE__)}/../../../../test/plugin_test_helper/lib")
3
+ require 'rubygems'
4
+ require 'plugin_test_helper'
5
+
6
+ PluginAWeek::Has::Addresses.verbose = false
7
+
8
+ PluginAWeek::PluginMigrations.migrate('has_addresses')
9
+
10
+ # Run the migrations
11
+ ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
@@ -0,0 +1,171 @@
1
+ require "#{File.dirname(__FILE__)}/../test_helper"
2
+
3
+ class AddressTest < Test::Unit::TestCase
4
+ fixtures :companies, :countries, :regions, :addresses
5
+
6
+ def test_address_in_known_region_should_be_valid
7
+ assert_valid addresses(:in_known_region)
8
+ end
9
+
10
+ def test_address_in_unknown_region_should_be_valid
11
+ assert_valid addresses(:in_unknown_region)
12
+ end
13
+
14
+ def test_should_require_addressable_id
15
+ assert_invalid addresses(:google_headquarters), :addressable_id, nil
16
+ end
17
+
18
+ def test_should_require_addressable_type
19
+ assert_invalid addresses(:google_headquarters), :addressable_type, nil
20
+ end
21
+
22
+ def test_should_require_street_1
23
+ assert_invalid addresses(:google_headquarters), :street_1, nil
24
+ end
25
+
26
+ def test_should_require_city
27
+ assert_invalid addresses(:google_headquarters), :city, nil
28
+ end
29
+
30
+ def test_should_require_postal_code
31
+ assert_invalid addresses(:google_headquarters), :postal_code, nil
32
+ end
33
+
34
+ def test_should_require_five_digit_postal_codes
35
+ assert_invalid addresses(:google_headquarters), :postal_code, '123456', '1234', 'abcdef'
36
+ end
37
+
38
+ def test_should_require_region_id_if_country_has_regions
39
+ assert_invalid addresses(:in_known_region), :region_id, nil
40
+ end
41
+
42
+ def test_should_not_require_custom_region_if_country_has_regions
43
+ assert_valid addresses(:in_known_region), :custom_region, nil
44
+ end
45
+
46
+ def test_should_require_custom_region_if_country_has_no_regions
47
+ assert_invalid addresses(:in_unknown_region), :custom_region, nil
48
+ end
49
+
50
+ def test_should_not_require_region_id_if_country_has_no_regions
51
+ assert_valid addresses(:in_unknown_region), :region_id, nil
52
+ end
53
+
54
+ def test_should_have_addressable_association
55
+ assert_not_nil addresses(:google_headquarters).addressable
56
+ end
57
+
58
+ def test_known_region_should_have_region_association
59
+ assert_not_nil addresses(:in_known_region).region
60
+ end
61
+
62
+ def test_unknown_region_should_not_have_region_association
63
+ assert_nil addresses(:in_unknown_region).region
64
+ end
65
+
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
70
+ end
71
+
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
76
+ end
77
+
78
+ def test_should_clear_country_id_with_known_region
79
+ address = addresses(:in_known_region)
80
+ address.country = countries(:united_states)
81
+
82
+ assert_not_nil address.country_id
83
+ address.save!
84
+ assert_nil address.country_id
85
+ end
86
+
87
+ def test_should_clear_custom_region_with_known_region
88
+ address = addresses(:in_known_region)
89
+ address.custom_region = 'Somewhere'
90
+
91
+ assert_not_nil address.custom_region
92
+ address.save!
93
+ assert_nil address.custom_region
94
+ end
95
+
96
+ def test_region_name_shouild_use_region_association_when_known
97
+ assert_equal 'California', addresses(:in_known_region).region_name
98
+ end
99
+
100
+ def test_region_name_should_use_custom_region_when_unknown
101
+ assert_equal 'Anywhere', addresses(:in_unknown_region).region_name
102
+ end
103
+
104
+ def test_multiline
105
+ a = Address.new
106
+ a.street_1 = '123 Fake Street'
107
+ assert_equal ['123 Fake Street'], a.multi_line
108
+
109
+ a.street_2 = 'Apartment 456'
110
+ expected = [
111
+ '123 Fake Street',
112
+ 'Apartment 456'
113
+ ]
114
+ assert_equal expected, a.multi_line
115
+
116
+ a.city = 'Somewhere'
117
+ expected = [
118
+ '123 Fake Street',
119
+ 'Apartment 456',
120
+ 'Somewhere'
121
+ ]
122
+ assert_equal expected, a.multi_line
123
+
124
+ a.region = regions(:california)
125
+ expected = [
126
+ '123 Fake Street',
127
+ 'Apartment 456',
128
+ 'Somewhere, California',
129
+ 'United States'
130
+ ]
131
+ assert_equal expected, a.multi_line
132
+
133
+ a.postal_code = '12345'
134
+ expected = [
135
+ '123 Fake Street',
136
+ 'Apartment 456',
137
+ 'Somewhere, California 12345',
138
+ 'United States'
139
+ ]
140
+ assert_equal expected, a.multi_line
141
+
142
+ a.region = nil
143
+ a.custom_region = 'Anywhere'
144
+ expected = [
145
+ '123 Fake Street',
146
+ 'Apartment 456',
147
+ 'Somewhere, Anywhere 12345'
148
+ ]
149
+ assert_equal expected, a.multi_line
150
+
151
+ a.city = nil
152
+ expected = [
153
+ '123 Fake Street',
154
+ 'Apartment 456',
155
+ 'Anywhere 12345'
156
+ ]
157
+ assert_equal expected, a.multi_line
158
+
159
+ a.postal_code = nil
160
+ expected = [
161
+ '123 Fake Street',
162
+ 'Apartment 456',
163
+ 'Anywhere'
164
+ ]
165
+ assert_equal expected, a.multi_line
166
+ end
167
+
168
+ def test_single_line
169
+ assert_equal '1600 Amphitheatre Parkway, Mountain View, California 94043, United States', addresses(:google_headquarters).single_line
170
+ end
171
+ end
@@ -0,0 +1,119 @@
1
+ require "#{File.dirname(__FILE__)}/../test_helper"
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?
14
+ end
15
+
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')
21
+ end
22
+
23
+ def test_bootstrap_should_insert_downloaded_countries
24
+ Country.bootstrap
25
+ assert Country.count > 2
26
+ end
27
+
28
+ def test_bootstrap_should_return_true_if_successful
29
+ assert Country.bootstrap
30
+ end
31
+
32
+ def test_should_use_default_path_for_created_fixture
33
+ Country.create_fixtures
34
+ assert File.exists?("#{RAILS_ROOT}/db/bootstrap/countries.yml")
35
+ end
36
+
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")
40
+ end
41
+
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
46
+ end
47
+
48
+ def test_should_be_valid
49
+ assert_valid countries(:united_states)
50
+ end
51
+
52
+ def test_should_require_name
53
+ assert_invalid countries(:united_states), :name, nil
54
+ end
55
+
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
59
+ end
60
+
61
+ def test_should_require_unique_name
62
+ assert_invalid countries(:united_states).clone, :name
63
+ end
64
+
65
+ def test_should_require_alpha_2_code
66
+ assert_invalid countries(:united_states), :alpha_2_code, nil
67
+ end
68
+
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'
72
+ end
73
+
74
+ def test_should_require_unique_alpha_2_code
75
+ assert_invalid countries(:united_states).clone, :alpha_2_code
76
+ end
77
+
78
+ def test_should_require_alpha_3_code
79
+ assert_invalid countries(:united_states), :alpha_3_code, nil
80
+ end
81
+
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'
85
+ end
86
+
87
+ def test_should_require_unique_alpha_3_code
88
+ assert_invalid countries(:united_states).clone, :alpha_3_code
89
+ end
90
+
91
+ def test_abbreviation_2_should_be_same_as_alpha_2_code
92
+ assert_equal 'US', countries(:united_states).abbreviation_2
93
+ end
94
+
95
+ def test_abbreviation_3should_be_same_as_alpha_3_code
96
+ assert_equal 'USA', countries(:united_states).abbreviation_3
97
+ end
98
+
99
+ def test_should_have_an_official_name
100
+ assert_equal 'United States of America', countries(:united_states).official_name
101
+ end
102
+
103
+ def test_should_use_name_when_official_name_is_not_present
104
+ assert_equal 'Canada', countries(:canada).official_name
105
+ 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
110
+ end
111
+
112
+ def test_regions
113
+ assert_equal [regions(:california)], countries(:united_states).regions
114
+ end
115
+
116
+ def teardown
117
+ FileUtils.rmtree("#{RAILS_ROOT}/db/bootstrap")
118
+ end
119
+ end
@@ -0,0 +1,15 @@
1
+ require "#{File.dirname(__FILE__)}/../test_helper"
2
+
3
+ class HasAddresses < Test::Unit::TestCase
4
+ fixtures :companies, :addresses
5
+
6
+ def test_should_create_one_association
7
+ assert_equal addresses(:google_headquarters), companies(:google).headquarters_address
8
+ assert_equal addresses(:google_sales), companies(:google).sales_address
9
+ end
10
+
11
+ def test_should_create_many_association
12
+ assert_equal 2, companies(:google).addresses.size
13
+ assert_equal [addresses(:google_headquarters), addresses(:google_sales)], companies(:google).addresses
14
+ end
15
+ end
@@ -0,0 +1,101 @@
1
+ require "#{File.dirname(__FILE__)}/../test_helper"
2
+
3
+ unless ENV['LIVE']
4
+ Region::ISO_3166_2_URL = "#{RAILS_ROOT}/../files/iso_3166_2.xml"
5
+ end
6
+
7
+ class RegionTest < Test::Unit::TestCase
8
+ fixtures :countries, :regions, :addresses
9
+
10
+ def test_should_download_and_parse_regions_from_iso_standard
11
+ regions = Region.download_all
12
+ assert_instance_of Array, regions
13
+ assert !regions.empty?
14
+ end
15
+
16
+ def test_bootstrap_should_remove_existing_records
17
+ region = Region.create(:name => 'Test', :country_id => 840, :abbreviation => 'TE')
18
+ assert !region.new_record?
19
+ Region.bootstrap
20
+ assert_nil Region.find_by_name('Test')
21
+ end
22
+
23
+ def test_bootstrap_should_insert_downloaded_regions
24
+ Region.bootstrap
25
+ assert Region.count > 1
26
+ end
27
+
28
+ def test_bootstrap_should_return_true_if_successful
29
+ assert Region.bootstrap
30
+ end
31
+
32
+ def test_should_use_default_path_for_created_fixture
33
+ Region.create_fixtures
34
+ assert File.exists?("#{RAILS_ROOT}/db/bootstrap/regions.yml")
35
+ end
36
+
37
+ def test_should_allow_custom_path_for_created_fixture
38
+ Region.create_fixtures('db/bootstrap/the_regions.yml')
39
+ assert File.exists?("#{RAILS_ROOT}/db/bootstrap/the_regions.yml")
40
+ end
41
+
42
+ def test_created_fixture_should_load_into_table
43
+ Region.create_fixtures
44
+ Fixtures.create_fixtures("#{RAILS_ROOT}/db/bootstrap", 'regions')
45
+ assert Region.count > 1
46
+ end
47
+
48
+ def test_should_be_valid
49
+ assert_valid regions(:california)
50
+ end
51
+
52
+ def test_should_require_name
53
+ assert_invalid regions(:california), :name, nil
54
+ end
55
+
56
+ def test_should_restrict_name_length
57
+ assert_invalid regions(:california), :name, 'C', 'C' * 81
58
+ assert_valid regions(:california), :name, 'Ca', 'C' * 80
59
+ end
60
+
61
+ def test_should_require_unique_name_per_country
62
+ region = regions(:california).clone
63
+ assert_invalid region, :name
64
+
65
+ region.country = countries(:canada)
66
+ assert_valid region, :name
67
+ end
68
+
69
+ def test_should_require_abbreviation
70
+ assert_invalid regions(:california), :abbreviation, nil
71
+ end
72
+
73
+ def test_should_restrict_abbreviation_length
74
+ assert_invalid regions(:california), :abbreviation, '', 'C' * 6
75
+ assert_valid regions(:california), :abbreviation, 'CA', 'C' * 5
76
+ end
77
+
78
+ def test_should_require_unique_abbreviation_per_country
79
+ region = regions(:california).clone
80
+ assert_invalid region, :abbreviation
81
+
82
+ region.country = countries(:canada)
83
+ assert_valid region, :abbreviation
84
+ end
85
+
86
+ def test_should_belong_to_a_country
87
+ assert_equal countries(:united_states), regions(:california).country
88
+ end
89
+
90
+ def test_should_have_many_addresses
91
+ assert_equal [addresses(:in_known_region), addresses(:google_headquarters), addresses(:google_sales)], regions(:california).addresses
92
+ end
93
+
94
+ def test_should_use_name_for_stringification
95
+ assert_equal 'California', regions(:california).to_s
96
+ end
97
+
98
+ def teardown
99
+ FileUtils.rmtree("#{RAILS_ROOT}/db/bootstrap")
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: has_addresses
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-08-21 00:00:00 -04:00
8
+ summary: Adds a base skeleton for handling countries, regions, and addresses.
9
+ require_paths:
10
+ - lib
11
+ email: info@pluginaweek.org
12
+ homepage: http://www.pluginaweek.org
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: has_addresses
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Aaron Pfeifer, Neil Abraham
31
+ files:
32
+ - app/models
33
+ - app/models/country.rb
34
+ - app/models/region.rb
35
+ - app/models/address.rb
36
+ - db/migrate
37
+ - db/migrate/001_create_countries.rb
38
+ - db/migrate/003_create_addresses.rb
39
+ - db/migrate/002_create_regions.rb
40
+ - lib/has_addresses.rb
41
+ - tasks/has_addresses_tasks.rake
42
+ - test/test_helper.rb
43
+ - test/app_root
44
+ - test/unit
45
+ - test/files
46
+ - test/app_root/test
47
+ - test/app_root/config
48
+ - test/app_root/app
49
+ - test/app_root/db
50
+ - test/app_root/test/fixtures
51
+ - test/app_root/test/fixtures/regions.yml
52
+ - test/app_root/test/fixtures/companies.yml
53
+ - test/app_root/test/fixtures/addresses.yml
54
+ - test/app_root/test/fixtures/countries.yml
55
+ - test/app_root/config/environment.rb
56
+ - test/app_root/app/models
57
+ - test/app_root/app/models/company.rb
58
+ - test/app_root/db/migrate
59
+ - test/app_root/db/migrate/001_create_companies.rb
60
+ - test/app_root/db/migrate/002_add_address_kinds.rb
61
+ - test/unit/country_test.rb
62
+ - test/unit/region_test.rb
63
+ - test/unit/has_addresses_test.rb
64
+ - test/unit/address_test.rb
65
+ - test/files/iso_3166.xml
66
+ - test/files/iso_3166_2.xml
67
+ - CHANGELOG
68
+ - init.rb
69
+ - MIT-LICENSE
70
+ - Rakefile
71
+ - README
72
+ test_files:
73
+ - test/unit/country_test.rb
74
+ - test/unit/region_test.rb
75
+ - test/unit/has_addresses_test.rb
76
+ - test/unit/address_test.rb
77
+ rdoc_options: []
78
+
79
+ extra_rdoc_files: []
80
+
81
+ executables: []
82
+
83
+ extensions: []
84
+
85
+ requirements: []
86
+
87
+ dependencies: []
88
+