spec/data_spec.rb in countries-5.0.2 vs spec/data_spec.rb in countries-5.1.0

- old
+ new

@@ -120,13 +120,17 @@ ISO3166.configuration.locales = [:es, :de, :de] expect(data['iso_short_name']).to eq 'NEW Taiwan' expect(subject.iso_short_name).to eq 'NEW Taiwan' expect(subject.translations).to eq('en' => 'NEW Taiwan', 'de' => 'NEW Taiwan') - expect(subject.subdivisions).to eq('CHA' => ISO3166::Subdivision.new({ name: 'New Changhua', code: 'CHA' }), - 'CYI' => ISO3166::Subdivision.new({ name: 'New Municipality', code: 'CYI' })) + expect(subject.subdivisions.keys).to eq(%w[CHA CYI]) + expect(subject.subdivisions.values.map(&:name)).to eq(['New Changhua', 'New Municipality']) end + + after do + ISO3166.reset + end end describe 'hotloading data' do before do ISO3166::Data.register( @@ -147,12 +151,12 @@ it 'can be done' do data = ISO3166::Data.new('LOL').call expect(data['iso_short_name']).to eq 'Happy Country' expect(subject.iso_short_name).to eq 'Happy Country' - expect(subject.subdivisions).to eq('LOL1' => ISO3166::Subdivision.new({name: 'Happy sub1', code: 'LOL1'}), - 'LOL2' => ISO3166::Subdivision.new({name: 'Happy sub2', code: 'LOL2'})) + expect(subject.subdivisions.keys).to eq(%w[LOL1 LOL2]) + expect(subject.subdivisions.values.map(&:name)).to eq(['Happy sub1', 'Happy sub2']) end it 'detect a stale cache' do ISO3166::Data.register(alpha2: 'SAD', iso_short_name: 'Sad Country') data = ISO3166::Data.new('SAD').call @@ -179,9 +183,63 @@ data = ISO3166::Data.new('LOL').call expect(data).to eq nil end after do - ISO3166::Data.unregister('lol') + ISO3166.reset + end + end + + describe 'data checks' do + context 'subdivision YAML files' do + it 'has a non-blank code for all subdivisions' do + Dir['lib/countries/data/subdivisions/*.yaml'].each do |file| + data = YAML.load_file(file) + expect(data.values.none?{|s| s['code'].nil? }).to be_truthy, "empty subdivision code in #{file}" + end + end + + it 'has a non-blank, lowercase and snake_case type for all subdivisions' do + Dir['lib/countries/data/subdivisions/*.yaml'].each do |file| + data = YAML.load_file(file) + no_type = data.select{|k,v| v['type'].nil? } + expect(no_type).to be_empty, "empty subdivision type in #{file} - #{no_type.keys}" + uppercase = data.select{|k,v| v['type'] =~ /[A-Z]/ } + expect(uppercase).to be_empty, "uppercase characters in subdivision type in #{file} - #{uppercase.keys}" + spaces = data.select{|k,v| v['type'] =~ /\s/ } + expect(spaces).to be_empty, "whitespace characters in subdivision type in #{file} - #{spaces.keys}" + end + end + + it 'has a non-blank name for all subdivisions' do + Dir['lib/countries/data/subdivisions/*.yaml'].each do |file| + data = YAML.load_file(file) + expect(data.values.none?{|s| s['name'].nil? }).to be_truthy, "empty subdivision name in #{file}" + end + end + end + + context 'cached country subdivision data' do + it 'has a non-blank code for all subdivisions' do + ISO3166::Country.all.each do |country| + expect(country.subdivisions.values.none?{|s| s['code'].nil? }).to be_truthy, "empty subdivision code in #{country}" + end + end + + it 'has a non-blank name for all subdivisions' do + ISO3166::Country.all.each do |country| + expect(country.subdivisions.values.none?{|s| s['name'].nil? }).to be_truthy, "empty subdivision name in #{country}" + end + end + end + + context 'names in Data' do + it 'should be unique (to allow .find_by_any_name work properly)' do + names = ISO3166::Data.cache.map do |_k, v| + [v['iso_short_name'], v['unofficial_names']].flatten.uniq + end.flatten + + expect(names.size).to eq(names.uniq.size) + end end end end