countries 5.7.0 → 5.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/spec/data_spec.rb DELETED
@@ -1,260 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'benchmark'
5
-
6
- describe ISO3166::Data do
7
- it 'responds to call' do
8
- data = ISO3166::Data.new('US').call
9
- expect(data['translated_names']).to be_a Array
10
- end
11
-
12
- it 'can load selective locales' do
13
- # ISO3166::Data.update_cache
14
- ISO3166.configuration.locales = %i[es de en]
15
- data = ISO3166::Data.new('US').call
16
- expect(data['translated_names'].size).to eq 3
17
- end
18
-
19
- it 'can load selective locales and reload efficiently' do
20
- ISO3166.configuration.locales = %i[es de en]
21
- data = ISO3166::Data.new('US').call
22
- expect(data['translations']).to eq('de' => 'Vereinigte Staaten', 'es' => 'Estados Unidos', 'en' => 'United States')
23
- expect(data['translated_names'].sort).to eq ['Vereinigte Staaten', 'Estados Unidos', 'United States'].sort
24
- ISO3166.configuration.locales = [:en]
25
- data = ISO3166::Data.new('US').call
26
- expect(data['translated_names'].size).to eq 1
27
- end
28
-
29
- it 'only loads subdivision translations for the configured locales' do
30
- ISO3166.configuration.locales = %i[en]
31
- ISO3166::Data.reset
32
- subdivisions = ISO3166::Data.subdivisions('US')
33
- expect(subdivisions.values.first['translations'].keys).to eq(%w[en])
34
- ISO3166.configuration.locales = %i[es de en]
35
- ISO3166::Data.reset
36
- subdivisions = ISO3166::Data.subdivisions('US')
37
- expect(subdivisions.values.first['translations'].keys).to eq(%w[es de en])
38
- end
39
-
40
- describe '#codes' do
41
- it 'returns an array' do
42
- data = ISO3166::Data.codes
43
- expect(data).to be_a Array
44
- expect(data.size).to eq 249
45
- end
46
- end
47
-
48
- it 'locales will load prior to return results' do
49
- # require 'memory_profiler'
50
- ISO3166.configuration.locales = %i[es de en]
51
- # report = MemoryProfiler.report do
52
- ISO3166::Data.update_cache
53
- # end
54
-
55
- # report.pretty_print(to_file: 'tmp/memory/3_locales')
56
- ISO3166::Data.update_cache
57
-
58
- ISO3166.configure do |config|
59
- config.locales = %i[af am ar as az be bg bn br bs ca cs cy da de dz el en
60
- eo es et eu fa fi fo fr ga gl gu he hi hr hu hy ia id
61
- is it ja ka kk km kn ko ku lt lv mi mk ml mn mr ms mt
62
- nb ne nl nn oc or pa pl ps pt ro ru rw si sk sl so sq
63
- sr sv sw ta te th ti tk tl tr tt ug uk ve vi wa wo xh
64
- zh zu]
65
- end
66
- # puts Benchmark.measure {ISO3166::Data.update_cache}
67
-
68
- # report = MemoryProfiler.report do
69
- ISO3166::Data.update_cache
70
- # end
71
-
72
- # report.pretty_print(to_file: 'tmp/memory/all_locales')
73
-
74
- expect(ISO3166::Country.new('DE').translations.size).to eq 92
75
-
76
- expect(ISO3166.configuration.loaded_locales.size).to eq 92
77
- end
78
-
79
- it 'locales will load prior and be cached' do
80
- ISO3166.reset
81
- ISO3166.configuration.locales = %i[es de en]
82
- expect(ISO3166::Data.send(:locales_to_load)).to eql(%w[es de en])
83
- ISO3166::Data.update_cache
84
- ISO3166.configuration.locales = %i[es de en]
85
- expect(ISO3166::Data.send(:locales_to_load)).to eql([])
86
- end
87
-
88
- it 'locales will load prior and be cached' do
89
- ISO3166.reset
90
- ISO3166.configuration.locales = %i[es de en]
91
- expect(ISO3166::Data.send(:locales_to_remove)).to eql([])
92
- expect(ISO3166::Country.new('DE').translation('de')).to eq 'Deutschland'
93
- ISO3166::Data.update_cache
94
- ISO3166.configuration.locales = %i[es en]
95
- expect(ISO3166::Data.send(:locales_to_remove)).to eql(['de'])
96
- expect(ISO3166::Country.new('DE').translation('de')).to eq nil
97
- end
98
-
99
- describe '#load_cache' do
100
- it 'will return an empty hash for an unsupported locale' do
101
- file_array = %w[locales unsupported.json]
102
- expect(ISO3166::Data.send(:load_cache, file_array)).to eql({})
103
- end
104
-
105
- it 'will return json for a supported locale' do
106
- file_array = %w[locales en.json]
107
- expect(ISO3166::Data.send(:load_cache, file_array)).not_to be_empty
108
- end
109
- end
110
-
111
- describe 'hotloading existing data' do
112
- before do
113
- ISO3166::Data.register(
114
- alpha2: 'TW',
115
- iso_short_name: 'NEW Taiwan',
116
- subdivisions: {
117
- CHA: { name: 'New Changhua' },
118
- CYI: { name: 'New Municipality' }
119
- },
120
- translations: {
121
- 'en' => 'NEW Taiwan',
122
- 'de' => 'NEW Taiwan'
123
- }
124
- )
125
- end
126
-
127
- subject { ISO3166::Country.new('TW') }
128
-
129
- it 'can be done' do
130
- data = ISO3166::Data.new('TW').call
131
- ISO3166.configuration.locales = %i[es de de]
132
- expect(data['iso_short_name']).to eq 'NEW Taiwan'
133
- expect(subject.iso_short_name).to eq 'NEW Taiwan'
134
- expect(subject.translations).to eq('en' => 'NEW Taiwan',
135
- 'de' => 'NEW Taiwan')
136
- expect(subject.subdivisions.keys).to eq(%w[CHA CYI])
137
- expect(subject.subdivisions.values.map(&:name)).to eq(['New Changhua', 'New Municipality'])
138
- end
139
-
140
- after do
141
- ISO3166.reset
142
- end
143
- end
144
-
145
- describe 'hotloading data' do
146
- before do
147
- ISO3166::Data.register(
148
- alpha2: 'LOL',
149
- iso_short_name: 'Happy Country',
150
- subdivisions: {
151
- LOL1: { name: 'Happy sub1' },
152
- LOL2: { name: 'Happy sub2' }
153
- },
154
- translations: {
155
- 'en' => 'Happy Country',
156
- 'de' => 'glückliches Land'
157
- }
158
- )
159
- end
160
-
161
- subject { ISO3166::Country.new('LOL') }
162
-
163
- it 'can be done' do
164
- data = ISO3166::Data.new('LOL').call
165
- expect(data['iso_short_name']).to eq 'Happy Country'
166
- expect(subject.iso_short_name).to eq 'Happy Country'
167
- expect(subject.subdivisions.keys).to eq(%w[LOL1 LOL2])
168
- expect(subject.subdivisions.values.map(&:name)).to eq(['Happy sub1', 'Happy sub2'])
169
- end
170
-
171
- it 'detect a stale cache' do
172
- ISO3166::Data.register(alpha2: 'SAD', iso_short_name: 'Sad Country')
173
- data = ISO3166::Data.new('SAD').call
174
- expect(data['iso_short_name']).to eq 'Sad Country'
175
- expect(ISO3166::Country.new('SAD').iso_short_name).to eq 'Sad Country'
176
- ISO3166::Data.unregister('SAD')
177
- end
178
-
179
- it 'will not override custom translations' do
180
- data = ISO3166::Data.new('LOL').call
181
- expect(data['translations']).to eq('en' => 'Happy Country',
182
- 'de' => 'glückliches Land')
183
- expect(subject.translations).to eq('en' => 'Happy Country',
184
- 'de' => 'glückliches Land')
185
- end
186
-
187
- it 'leaves remain countries intact after a hotload' do
188
- data = ISO3166::Data.new('US').call
189
- expect(data).to include('subregion')
190
- end
191
-
192
- it 'can be undone' do
193
- ISO3166::Data.unregister('lol')
194
- data = ISO3166::Data.new('LOL').call
195
- expect(data).to eq nil
196
- end
197
-
198
- after do
199
- ISO3166.reset
200
- end
201
- end
202
-
203
- describe 'data checks' do
204
- context 'subdivision YAML files' do
205
- it 'has a non-blank code for all subdivisions' do
206
- Dir['lib/countries/data/subdivisions/*.yaml'].each do |file|
207
- data = YAML.load_file(file)
208
- expect(data.values.none? { |s| s['code'].nil? }).to be_truthy, "empty subdivision code in #{file}"
209
- end
210
- end
211
-
212
- it 'has a non-blank, lowercase and snake_case type for all subdivisions' do
213
- Dir['lib/countries/data/subdivisions/*.yaml'].each do |file|
214
- data = YAML.load_file(file)
215
- no_type = data.select { |_k, v| v['type'].nil? }
216
- expect(no_type).to be_empty, "empty subdivision type in #{file} - #{no_type.keys}"
217
- uppercase = data.select { |_k, v| v['type'] =~ /[A-Z]/ }
218
- expect(uppercase).to be_empty, "uppercase characters in subdivision type in #{file} - #{uppercase.keys}"
219
- spaces = data.select { |_k, v| v['type'] =~ /\s/ }
220
- expect(spaces).to be_empty, "whitespace characters in subdivision type in #{file} - #{spaces.keys}"
221
- end
222
- end
223
-
224
- it 'has a non-blank name for all subdivisions' do
225
- Dir['lib/countries/data/subdivisions/*.yaml'].each do |file|
226
- data = YAML.load_file(file)
227
- expect(data.values.none? { |s| s['name'].nil? }).to be_truthy, "empty subdivision name in #{file}"
228
- end
229
- end
230
- end
231
-
232
- context 'cached country subdivision data' do
233
- it 'has a non-blank code for all subdivisions' do
234
- ISO3166::Country.all.each do |country|
235
- expect(country.subdivisions.values.none? do |s|
236
- s['code'].nil?
237
- end).to be_truthy, "empty subdivision code in #{country}"
238
- end
239
- end
240
-
241
- it 'has a non-blank name for all subdivisions' do
242
- ISO3166::Country.all.each do |country|
243
- expect(country.subdivisions.values.none? do |s|
244
- s['name'].nil?
245
- end).to be_truthy, "empty subdivision name in #{country}"
246
- end
247
- end
248
- end
249
-
250
- context 'names in Data' do
251
- it 'should be unique (to allow .find_by_any_name work properly)' do
252
- names = ISO3166::Data.cache.map do |_k, v|
253
- [v['iso_short_name'], v['unofficial_names']].flatten.uniq
254
- end.flatten
255
-
256
- expect(names.size).to eq(names.uniq.size)
257
- end
258
- end
259
- end
260
- end
data/spec/mongoid_spec.rb DELETED
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require File.expand_path('../lib/countries/mongoid', __dir__)
5
-
6
- describe 'Mongoid support' do
7
- let(:britain) { ISO3166::Country.new('GB') }
8
- context 'instance methods' do
9
- describe 'mongoize' do
10
- it 'should delegate mongoization to the class method' do
11
- expect(britain.mongoize).to eql ISO3166::Country.mongoize(britain)
12
- end
13
- end
14
- end
15
-
16
- context 'class methods' do
17
- describe 'mongoize' do
18
- it 'should store the alpha2 given a country object' do
19
- expect(ISO3166::Country.mongoize(britain)).to eql britain.alpha2
20
- end
21
-
22
- it 'should store the alpha2 given a valid alpha2' do
23
- expect(ISO3166::Country.mongoize('GB')).to eql britain.alpha2
24
- end
25
-
26
- it 'should store nil given an invalid object' do
27
- bad_types = [[], Time.now.utc, {}, Date.new(2029, 10, 1)]
28
- bad_types.each do |type|
29
- expect(ISO3166::Country.mongoize(type)).to eql nil
30
- end
31
- end
32
-
33
- it 'should store nil given an empty country object' do
34
- expect(ISO3166::Country.mongoize(ISO3166::Country.new(''))).to eql nil
35
- end
36
-
37
- it 'should store nil given a bad alpha2' do
38
- expect(ISO3166::Country.mongoize('bad_alpha_2')).to eql nil
39
- end
40
- end
41
-
42
- describe 'demongoize' do
43
- it 'should instantiate an equivalent object from stored alpha2 code' do
44
- expect(ISO3166::Country.demongoize(britain.mongoize).data)
45
- .to eql britain.data
46
- end
47
-
48
- it 'should be indifferent to storage by alpha2' do
49
- expect(ISO3166::Country.demongoize(ISO3166::Country.mongoize('GB'))
50
- .data).to eql britain.data
51
- end
52
- end
53
-
54
- describe 'evolve' do
55
- it 'should delegate to self.mongoize and return the mongoized object' do
56
- expect(ISO3166::Country).to receive(:mongoize).with(britain)
57
- ISO3166::Country.evolve(britain)
58
- end
59
- end
60
- end
61
- end
data/spec/perf_spec.rb DELETED
@@ -1,74 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- ALL_LOCALES = %i[
6
- af am ar as az be bg bn br bs ca cs cy da de
7
- dz el en eo es et eu fa fi fo fr ga gl gu he
8
- hi hr hu hy ia id is it ja ka kk km kn ko ku
9
- lt lv mi mk ml mn mr ms mt nb ne nl nn oc or
10
- pa pl ps pt ro ru rw si sk sl so sq sr sv sw
11
- ta te th ti tk tl tr tt ug uk ve vi wa wo xh
12
- zh zu
13
- ].freeze
14
-
15
- describe ISO3166::Data, perf: true, order: :defined do
16
- def perf_report(_name)
17
- require 'benchmark'
18
- require 'memory_profiler'
19
- require 'ruby-prof'
20
- # profile the code
21
- RubyProf.start
22
-
23
- 100.times do
24
- yield if block_given?
25
- end
26
-
27
- result = RubyProf.stop
28
-
29
- # print a flat profile to text
30
- printer = RubyProf::FlatPrinter.new(result)
31
- printer.print($stdout)
32
- end
33
-
34
- it 'responds to call' do
35
- perf_report('translations') do
36
- ISO3166.reset
37
- data = ISO3166::Data.new('US').call
38
- expect(data['translated_names']).to be_a Array
39
- end
40
- end
41
-
42
- it 'locales will load prior to return results' do
43
- ISO3166.configuration.locales = %i[es de en]
44
- report = MemoryProfiler.report do
45
- ISO3166::Data.update_cache
46
- end
47
-
48
- report.pretty_print(to_file: 'tmp/3_locales.txt')
49
- puts Benchmark.measure { ISO3166::Data.update_cache }
50
-
51
- ISO3166.configure do |config|
52
- config.locales = ALL_LOCALES
53
- end
54
- puts Benchmark.measure { ISO3166::Data.update_cache }
55
-
56
- report = MemoryProfiler.report do
57
- ISO3166::Data.update_cache
58
- end
59
-
60
- report.pretty_print(to_file: 'tmp/all_locales.txt')
61
- end
62
-
63
- it 'loades a specfic country quickly' do
64
- codes = Dir['lib/countries/data/countries/*.yaml'].map { |x| File.basename(x, File.extname(x)) }.uniq
65
- Benchmark.bmbm do |bm|
66
- bm.report('find_by_alpha2') do
67
- codes.map { |cc| ISO3166::Country.find_country_by_alpha2 cc }
68
- end
69
- bm.report('new') do
70
- codes.map { |cc| ISO3166::Country.new cc }
71
- end
72
- end
73
- end
74
- end
data/spec/spec_helper.rb DELETED
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'countries'
4
- require 'debug'
5
- require 'simplecov'
6
- SimpleCov.start
7
-
8
- RSpec.configure do |config|
9
- config.filter_run :focus
10
- config.run_all_when_everything_filtered = true
11
- config.filter_run_excluding perf: true
12
- config.example_status_persistence_file_path = 'spec/examples.txt'
13
-
14
- config.warnings = true
15
-
16
- config.default_formatter = 'doc' if config.files_to_run.one?
17
- # config.order = :random
18
- # Kernel.srand config.seed
19
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe ISO3166::Subdivision do
6
- before do
7
- ISO3166::Data.reset
8
- end
9
-
10
- let(:countries) { ISO3166::Country.all }
11
- let(:available_types) { [Hash, NilClass] }
12
-
13
- describe 'translations' do
14
- it 'should be hash or nil' do
15
- countries.each do |country|
16
- country.subdivisions.each do |_, region|
17
- expect(available_types).to include(region.translations.class)
18
- end
19
- end
20
- end
21
- end
22
-
23
- describe 'state codes' do
24
- it 'should all be strings' do
25
- countries.each do |country|
26
- expect(country.subdivisions.keys).to(
27
- all(be_a(String)),
28
- "Expected #{country.alpha2.inspect} to have string subdivision \
29
- codes but had #{country.subdivisions.keys.inspect}"
30
- )
31
- end
32
- end
33
- end
34
-
35
- describe 'code_with_translations' do
36
- before { ISO3166.configuration.locales = %i[en pt] }
37
- it 'returns a hash' do
38
- expect(ISO3166::Country.new('IT').subdivisions['NA'].code_with_translations).to eq({ 'NA' => { 'en' => 'Naples',
39
- 'pt' => 'Nápoles' } })
40
- end
41
- end
42
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe 'Accessing ISO3166::Country instances data in multiple threads' do
4
- before do
5
- if Thread.respond_to?(:report_on_exception)
6
- @report_on_exception_value = Thread.report_on_exception
7
- Thread.report_on_exception = false
8
- end
9
-
10
- ISO3166::Data.reset
11
- end
12
-
13
- def create_countries_threaded
14
- nthreads = 100
15
- threads = []
16
-
17
- alpha2_codes = %w[us es nl ca de fr mx ru ch jp]
18
-
19
- nthreads.times do
20
- threads << Thread.new do
21
- alpha2_codes.each do |a2|
22
- country = ISO3166::Country[a2]
23
- # This will fail if data['translations'] has been
24
- # left nil due to a race condition
25
- country.translation
26
- end
27
- end
28
- end
29
- threads.map(&:join)
30
- end
31
-
32
- it "doesn't raise any exceptions when using a mutex" do
33
- expect { create_countries_threaded }.to_not raise_error
34
- end
35
-
36
- it 'raises NoMethodError when not using a mutex' do
37
- allow(ISO3166::Data).to receive(:use_mutex?).and_return(false)
38
-
39
- expect { create_countries_threaded }.to raise_error(NoMethodError)
40
- end
41
-
42
- after do
43
- if Thread.respond_to?(:report_on_exception)
44
- Thread.report_on_exception = @report_on_exception_value
45
- end
46
- end
47
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe 'timezone Country class' do
4
- context "when loaded via 'timezone'" do
5
- require 'tzinfo'
6
- describe 'existence' do
7
- subject { defined?(TZInfo) }
8
-
9
- it { is_expected.to be_truthy }
10
- end
11
-
12
- describe 'GB' do
13
- subject { ISO3166::Country.new('GB') }
14
- it 'should return the tzinfo country object' do
15
- expect(subject.timezones).to be_a(TZInfo::Country)
16
- end
17
-
18
- it 'should return the tzinfo country object' do
19
- expect(subject.timezones.zone_info).to be_a(Array)
20
- end
21
-
22
- it 'should return the tzinfo country object' do
23
- expect(subject.timezones.zone_identifiers).to eq ['Europe/London']
24
- end
25
- end
26
-
27
- describe 'US' do
28
- subject { ISO3166::Country.new('US') }
29
- it 'should return the tzinfo country object' do
30
- expect(subject.timezones.zone_identifiers.size).to eq 29
31
- end
32
- end
33
- end
34
- end