Skip to content

Commit

Permalink
Update to use new Registers API response format
Browse files Browse the repository at this point in the history
The response you receive from a request for the `/records` endpoint is
changing. This will be a breaking change. This change provides support
for both the old and new response style whilst we transition.

The biggest change here is the addition of a list of items under the
`item` [sic] key within each record. For the Country Register this will
only contain a single item.
  • Loading branch information
samcrang committed Mar 31, 2017
1 parent 7ee3323 commit 2f960cf
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/jobs/fetch_country_register_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ def perform(*args)
private

def countries
fetch_register.body.values
values = fetch_register.body.values
if !values.empty? && values.first.has_key?('item')
values.map { |x| x['item'].first }
else
values
end
end

def faraday
Expand Down
150 changes: 149 additions & 1 deletion spec/jobs/fetch_petition_register_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,155 @@ def json_error(status = 404, body = "{}")
{status: status, headers: {"Content-Type" => "application/json"}, body: body}
end

context "new (map-based) record schema" do
context "latest record schema" do
context "when a country does not exist" do
before do
stub_register.to_return json_response <<-JSON
{
"GB" : {
"index-entry-number": "6",
"entry-number": "6",
"entry-timestamp": "2016-04-05T13:23:05Z",
"key": "GB",
"item": [{
"citizen-names": "Briton;British citizen",
"country": "GB",
"name": "United Kingdom",
"official-name": "The United Kingdom of Great Britain and Northern Ireland",
"start-date": "1707-05-01",
"end-date": "2017-12-31"
}]
}
}
JSON
end

it "creates a record" do
expect {
perform_enqueued_jobs {
described_class.perform_later
}
}.to change { Location.count }.by(1)
end

describe "attribute assignment" do
let(:location) { Location.find_by!(code: "GB") }

before do
perform_enqueued_jobs {
described_class.perform_later
}
end

it "assigns 'country' to Location#code" do
expect(location.code).to eq("GB")
end

it "assigns 'name' to Location#name" do
expect(location.name).to eq("United Kingdom")
end

it "assigns 'start-date' to Location#start_date" do
expect(location.start_date).to eq(Date.civil(1707, 5, 1))
end

it "assigns 'end-date' to Location#end_date" do
expect(location.end_date).to eq(Date.civil(2017, 12, 31))
end
end
end

context "when a country does exist" do
before do
FactoryGirl.create(:location, code: "GB")

stub_register.to_return json_response <<-JSON
{
"GB" : {
"index-entry-number": "6",
"entry-number": "6",
"entry-timestamp": "2016-04-05T13:23:05Z",
"key": "GB",
"item": [{
"citizen-names": "Briton;British citizen",
"country": "GB",
"name": "United Kingdom",
"official-name": "The United Kingdom of Great Britain and Northern Ireland",
"start-date": "1707-05-01",
"end-date": "2017-12-31"
}]
}
}
JSON
end

it "updates an existing record" do
expect {
perform_enqueued_jobs {
described_class.perform_later
}
}.not_to change { Location.count }
end

describe "attribute assignment" do
let(:location) { Location.find_by!(code: "GB") }

before do
perform_enqueued_jobs {
described_class.perform_later
}
end

it "updates Location#name" do
expect(location.name).to eq("United Kingdom")
end

it "updates Location#start_date" do
expect(location.start_date).to eq(Date.civil(1707, 5, 1))
end

it "updates Location#end_date" do
expect(location.end_date).to eq(Date.civil(2017, 12, 31))
end
end
end

context "when a country does not change" do
let(:location) { Location.find_by!(code: "GB") }

before do
FactoryGirl.create(:location, code: "GB", name: "United Kingdom")

stub_register.to_return json_response <<-JSON
{
"GB" : {
"index-entry-number": "6",
"entry-number": "6",
"entry-timestamp": "2016-04-05T13:23:05Z",
"key": "GB",
"item": [{
"citizen-names": "Briton;British citizen",
"country": "GB",
"name": "United Kingdom",
"official-name": "The United Kingdom of Great Britain and Northern Ireland"
}]
}
}
JSON
end

it "doesn't update an existing record" do
expect {
perform_enqueued_jobs {
described_class.perform_later
}
}.not_to change { location.reload.updated_at }
end
end

end

context "old record schema" do
context "when a country does not exist" do
before do
stub_register.to_return json_response <<-JSON
Expand Down

0 comments on commit 2f960cf

Please sign in to comment.