Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Country factory states_required attribute #4272

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions core/lib/spree/testing_support/factories/state_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
carmen_subregion do
carmen_country = Carmen::Country.coded(country.iso)

carmen_country.subregions.coded(state_code) ||
carmen_country.subregions.sort_by(&:name).first ||
unless carmen_country.subregions?
fail("Country #{country.iso} has no subregions")
end

carmen_regions = carmen_country.subregions
carmen_regions = carmen_regions.flat_map(&:subregions) if carmen_regions.first.subregions?
RyanofWoods marked this conversation as resolved.
Show resolved Hide resolved
region_collection = Carmen::RegionCollection.new(carmen_regions)

region_collection.coded(state_code) || region_collection.sort_by(&:name).first
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@
end
end

describe 'for a country with nested carmen states' do
context 'when not given a state_iso' do
let(:state) { build(:state, country_iso: "IT") }

it 'creates the first state for that country it finds in carmen' do
expect(state.abbr).to eq("AL")
expect(state.name).to eq("Alessandria")
end
end

context 'when given a state_iso' do
let(:state) { build(:state, country_iso: "IT", state_code: 'PE' ) }

it 'finds the corresponding state' do
expect(state.abbr).to eq("PE")
expect(state.name).to eq("Pescara")
end
end
end

describe 'when given a country record' do
let(:country) { build(:country, iso: "DE") }
let(:state) { build(:state, country: country) }
Expand Down