New tests added for Census calls

Added new spec files for `CensusCaller` and `LocalCensus` libraries.
This commit is contained in:
María Checa
2017-07-14 15:39:49 +02:00
parent 13409c2e35
commit dccf221b47
4 changed files with 99 additions and 7 deletions

View File

@@ -1,7 +1,11 @@
FactoryGirl.define do
factory :local_census_record do
factory :local_census_record, class: 'LocalCensusRecord' do
document_number '12345678A'
document_type 1
date_of_birth Date.new(1970, 1, 31)
postal_code '28002'
end
sequence(:document_number) { |n| "#{n.to_s.rjust(8, '0')}X" }
factory :user do

View File

@@ -0,0 +1,38 @@
require 'rails_helper'
describe CensusCaller do
let(:api) { described_class.new }
describe '#call' do
it "returns data from local_census_records if census API is not available" do
census_api_response = CensusApi::Response.new({:get_habita_datos_response=>{:get_habita_datos_return=>{:datos_habitante=>{}, :datos_vivienda=>{}}}})
local_census_response = LocalCensus::Response.new(create(:local_census_record))
CensusApi.any_instance.stub(:call).and_return(census_api_response)
LocalCensus.any_instance.stub(:call).and_return(local_census_response)
allow(CensusApi).to receive(:call).with(1, "12345678A")
allow(LocalCensus).to receive(:call).with(1, "12345678A")
response = api.call(1, "12345678A")
expect(response).to eq(local_census_response)
end
it "returns data from census API if it's available and valid" do
census_api_response = CensusApi::Response.new({get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {item: {fecha_nacimiento_string: "1-1-1980"}}}}})
local_census_response = LocalCensus::Response.new(create(:local_census_record))
CensusApi.any_instance.stub(:call).and_return(census_api_response)
LocalCensus.any_instance.stub(:call).and_return(local_census_response)
allow(CensusApi).to receive(:call).with(1, "12345678A")
allow(LocalCensus).to receive(:call).with(1, "12345678A")
response = api.call(1, "12345678A")
expect(response).to eq(census_api_response)
end
end
end

View File

@@ -0,0 +1,55 @@
require 'rails_helper'
describe LocalCensus do
let(:api) { described_class.new }
describe '#get_document_number_variants' do
it "trims and cleans up entry" do
expect(api.get_document_number_variants(2, ' 1 2@ 34')).to eq(['1234'])
end
it "returns only one try for passports & residence cards" do
expect(api.get_document_number_variants(2, '1234')).to eq(['1234'])
expect(api.get_document_number_variants(3, '1234')).to eq(['1234'])
end
it 'takes only the last 8 digits for dnis and resicence cards' do
expect(api.get_document_number_variants(1, '543212345678')).to eq(['12345678'])
end
it 'tries all the dni variants padding with zeroes' do
expect(api.get_document_number_variants(1, '0123456')).to eq(['123456', '0123456', '00123456'])
expect(api.get_document_number_variants(1, '00123456')).to eq(['123456', '0123456', '00123456'])
end
it 'adds upper and lowercase letter when the letter is present' do
expect(api.get_document_number_variants(1, '1234567A')).to eq(['1234567', '01234567', '1234567a', '1234567A', '01234567a', '01234567A'])
end
end
describe '#call' do
let(:invalid_body) { create(:local_census_record, postal_code: '') }
let(:valid_body) { create(:local_census_record) }
it "returns the response for the first valid variant" do
allow(api).to receive(:get_record).with(1, "00123456").and_return(invalid_body)
allow(api).to receive(:get_record).with(1, "123456").and_return(invalid_body)
expect(api).to receive(:get_record).with(1, "0123456").and_return(valid_body)
response = api.call(1, "123456")
expect(response).to be_valid
expect(response.date_of_birth).to eq(Date.new(1970, 1, 31))
end
it "returns the last failed response" do
expect(api).to receive(:get_record).with(1, "00123456").and_return(invalid_body)
expect(api).to receive(:get_record).with(1, "123456").and_return(invalid_body)
expect(api).to receive(:get_record).with(1, "0123456").and_return(invalid_body)
response = api.call(1, "123456")
expect(response).to_not be_valid
end
end
end

View File

@@ -1,5 +0,0 @@
require 'rails_helper'
RSpec.describe LocalCensusRecord, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end