Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NikitaAvvakumov/3db3848deae0a3444a03 to your computer and use it in GitHub Desktop.
Save NikitaAvvakumov/3db3848deae0a3444a03 to your computer and use it in GitHub Desktop.
Spec for proposed validator for legacy province codes, OSRA
# Validator is here: https://gist.github.com/adc8cb0de0c6fa12dfb1.git
require 'rails_helper'
describe LegacyCodeMatchesProvinceValidator do
let(:test_class) do
Class.new do
include ActiveModel::Validations
attr_accessor :name
attr_accessor :code
validates_with LegacyCodeMatchesProvinceValidator
end
end
subject(:test_province) { test_class.new }
it 'passes when legacy code matches province name' do
[['Damascus & Rif Dimashq', 11],
['Aleppo', 12],
['Homs', 13],
['Hama', 14],
['Latakia', 15],
['Deir Al-Zor', 16],
['Daraa', 17],
['Idlib', 18],
['Ar Raqqah', 19],
['Al Ḥasakah', 20],
['Tartous', 21],
['Al-Suwayada', 22],
['Al-Quneitera', 23],
['Outside Syria', 29]].each do |pair|
test_province.name = pair[0]
test_province.code = pair[1]
expect(test_province).to be_valid
end
end
it 'fails when legacy code does not match province name' do
[['Damascus & Rif Dimashq', 12], ['Aleppo', 13], ['Homs', 14], ['Hama', 11]].each do |pair|
test_province.name = pair[0]
test_province.code = pair[1]
expect(test_province).not_to be_valid
end
end
it 'passes when province name is not one of original 14' do
[['New Province', 11], ['Newer Province', 77], ['Newest Province', 1024]].each do |pair|
test_province.name = pair[0]
test_province.code = pair[1]
expect(test_province).to be_valid
end
end
end
@NikitaAvvakumov
Copy link
Author

@PurityControl: fixed the validator name.
['New Province', 11] is valid here because 'New Province' is not an existing province name. However, it will fail the uniqueness validation on the code attribute, which won't allow new province records to reuse existing codes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment