Skip to content

Commit

Permalink
Support new /records schema
Browse files Browse the repository at this point in the history
 Response for `/records` resource from country register will be changed very soon. It will start returning new map based response specified in register specification (https://openregister.github.io/specification/#records-resource) rather than current array based response.

  Changes in the PR confirms that the e-petitions is forward compatible with the new-schema from country register. This will keep e-petitions code working for both new and old schema. It allow allows country register to publish new response independently.

  Same set of tests are added for the new schema and implemented the solution to support both schemas. The old-schema tests/code can be  deleted after country register starts publishing the new-schema.
  • Loading branch information
om-sharma committed Apr 14, 2016
1 parent e3696eb commit eff28f6
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 75 deletions.
9 changes: 7 additions & 2 deletions app/jobs/fetch_country_register_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ def perform(*args)
private

def countries
fetch_register.body.map{ |r| r['entry'] }
items = fetch_register.body
if items.is_a?(Array)
items.map { |r| r['entry'] }
else
items.values
end
end

def faraday
Faraday.new(HOST) do |f|
f.response :follow_redirects
f.response :json
f.response :raise_error
f.adapter :net_http_persistent
f.adapter :net_http_persistent
end
end

Expand Down
287 changes: 214 additions & 73 deletions spec/jobs/fetch_petition_register_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ def stub_register
end

def json_response(body = "{}")
{ status: 200, headers: { "Content-Type" => "application/json" }, body: body }
{status: 200, headers: {"Content-Type" => "application/json"}, body: body}
end

def json_error(status = 404, body = "{}")
{ status: status, headers: { "Content-Type" => "application/json" }, body: body }
{status: status, headers: {"Content-Type" => "application/json"}, body: body}
end

context "when a country does not exist" do
before do
stub_register.to_return json_response <<-JSON
context "old (array-based) record schema" do
context "when a country does not exist" do
before do
stub_register.to_return json_response <<-JSON
[
{
"serial-number": 6,
Expand All @@ -30,49 +31,49 @@ def json_error(status = 404, body = "{}")
}
}
]
JSON
end
JSON
end

it "creates a record" do
expect {
perform_enqueued_jobs {
described_class.perform_later
}
}.to change { Location.count }.by(1)
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") }
describe "attribute assignment" do
let(:location) { Location.find_by!(code: "GB") }

before do
perform_enqueued_jobs {
described_class.perform_later
}
end
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 '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 '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 '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))
it "assigns 'end-date' to Location#end_date" do
expect(location.end_date).to eq(Date.civil(2017, 12, 31))
end
end
end
end

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

stub_register.to_return json_response <<-JSON
stub_register.to_return json_response <<-JSON
[
{
"serial-number": 6,
Expand All @@ -87,47 +88,47 @@ def json_error(status = 404, body = "{}")
}
}
]
JSON
end
JSON
end

it "updates an existing record" do
expect {
perform_enqueued_jobs {
described_class.perform_later
}
}.not_to change { Location.count }
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") }
describe "attribute assignment" do
let(:location) { Location.find_by!(code: "GB") }

before do
perform_enqueued_jobs {
described_class.perform_later
}
end
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#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#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))
it "updates Location#end_date" do
expect(location.end_date).to eq(Date.civil(2017, 12, 31))
end
end
end
end

context "when a country does not change" do
let(:location) { Location.find_by!(code: "GB") }
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")
before do
FactoryGirl.create(:location, code: "GB", name: "United Kingdom")

stub_register.to_return json_response <<-JSON
stub_register.to_return json_response <<-JSON
[
{
"serial-number": 6,
Expand All @@ -140,15 +141,155 @@ def json_error(status = 404, body = "{}")
}
}
]
JSON
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

it "doesn't update an existing record" do
expect {
perform_enqueued_jobs {
described_class.perform_later
context "new (map-based) record schema" do
context "when a country does not exist" do
before do
stub_register.to_return json_response <<-JSON
{
"GB" : {
"entry-number": "6",
"item-hash": "sha-256:6b18693874513ba13da54d61aafa7cad0c8f5573f3431d6f1c04b07ddb27d6bb",
"entry-timestamp": "2016-04-05T13:23:05Z",
"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"
}
}
}.not_to change { location.reload.updated_at }
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" : {
"entry-number": "6",
"item-hash": "sha-256:6b18693874513ba13da54d61aafa7cad0c8f5573f3431d6f1c04b07ddb27d6bb",
"entry-timestamp": "2016-04-05T13:23:05Z",
"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" : {
"entry-number": "6",
"item-hash": "sha-256:6b18693874513ba13da54d61aafa7cad0c8f5573f3431d6f1c04b07ddb27d6bb",
"entry-timestamp": "2016-04-05T13:23:05Z",
"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

Expand Down

0 comments on commit eff28f6

Please sign in to comment.