Skip to content

Commit

Permalink
Merge pull request bitzesty#166 from bitzesty/GA4
Browse files Browse the repository at this point in the history
add GA4 conformance rule
  • Loading branch information
matthewford committed Apr 24, 2014
2 parents b0375e0 + f0b0f8d commit b6b0075
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/validators/geographical_area_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ class GeographicalAreaValidator < TradeTariffBackend::Validator
validation :GA2, 'The start date must be less than or equal to the end date.' do
validates :validity_dates
end

validation :GA4, 'The referenced parent geographical area group must be an existing geographical area with area code = 1 (geographical area group)', on: [:create, :update] do |record|
parent_sid = record.parent_geographical_area_group_sid
if parent_sid.present?
parent = record.class[geographical_area_sid: parent_sid]
parent.try(:geographical_code).to_s == "1"
end
end
end

# TODO: GA3
# TODO: GA4
# TODO: GA5
# TODO: GA6
# TODO: GA7
Expand Down
31 changes: 31 additions & 0 deletions spec/models/geographical_area_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,36 @@
it { should validate_uniqueness.of([:geographical_area_id, :validity_start_date])}
# GA2 The start date must be less than or equal to the end date.
it { should validate_validity_dates }

describe "GA4" do
let(:geographical_area) {
build(:geographical_area, parent_geographical_area_group_sid: parent_id)
}

before { geographical_area.conformant? }

context "invalid parent id" do
let(:parent_id) { "435" }
it {
expect(geographical_area.conformance_errors).to have_key(:GA4)
}
end

context "invalid area code" do
let(:parent_id) {
create(:geographical_area, :country).geographical_area_sid
}
it {
expect(geographical_area.conformance_errors).to have_key(:GA4)
}
end

context "valid" do
let(:parent_id) {
create(:geographical_area, geographical_code: "1").geographical_area_sid
}
it { expect(geographical_area.conformance_errors).to be_empty }
end
end
end
end

0 comments on commit b6b0075

Please sign in to comment.