spec/lib/authorities_loc_spec.rb in qa-0.3.0 vs spec/lib/authorities_loc_spec.rb in qa-0.4.0

- old
+ new

@@ -1,91 +1,124 @@ require 'spec_helper' describe Qa::Authorities::Loc do - before :all do - stub_request(:get, "http://id.loc.gov/search/?format=json&q=haw*&q=cs:http://id.loc.gov/vocabulary/geographicAreas"). - with(:headers => {'Accept'=>'application/json'}). - to_return(:body => webmock_fixture("loc-response.txt"), :status => 200) - @authority = Qa::Authorities::Loc.new - @authority.search("haw*", "geographicAreas") + describe "#new" do + context "without a sub-authority" do + it "should raise an exception" do + expect { Qa::Authorities::Loc.new }.to raise_error + end + end + context "with an invalid sub-authority" do + it "should raise an exception" do + expect { Qa::Authorities::Loc.new("foo") }.to raise_error + end + end + context "with a valid sub-authority" do + it "should create the authority" do + expect(Qa::Authorities::Loc.new("subjects")).to be_kind_of Qa::Authorities::Loc + end + end end + + describe "urls" do - describe "geographic areas" do - - it "should instantiate with a query and return data" do - expect(@authority).not_to be_nil - @authority.raw_response.to_s.should include("id") - end - - it "should return a sub_authority url" do - @authority.should_not be_nil - url = @authority.sub_authorityURL("geographicAreas") - expect(url).not_to be_nil + let :authority do + Qa::Authorities::Loc.new("subjects") end - - it "should not return a url for an invalid sub_authority" do - @authority.should_not be_nil - url = @authority.sub_authorityURL("invalid sub_authority") - expect(url).to eq("") + + context "for searching" do + it "should return a url" do + url = 'http://id.loc.gov/search/?q=foo&q=cs%3Ahttp%3A%2F%2Fid.loc.gov%2Fauthorities%2Fsubjects&format=json' + expect(authority.build_query_url("foo")).to eq(url) + end end - - it "should return JSON" do - @authority.should_not be_nil - json = @authority.parse_authority_response(@authority.raw_response) - expect(json).not_to be_empty - end + context "for returning single terms" do + it "returns a url with an authority and id" do + url = "http://id.loc.gov/authorities/subjects/sh2002003586.json" + expect(authority.find_url("sh2002003586")).to eq(url) + end + end + end - describe "subject headings" do + describe "#search" do - before :all do - stub_request(:get, "http://id.loc.gov/search/?format=json&q=History--&q=cs:http://id.loc.gov/authorities/subjects"). - with(:headers => {'Accept'=>'application/json'}). - to_return(:body => webmock_fixture("loc-subjects-response.txt"), :status => 200) - end + context "any LOC authorities" do + let :authority do + stub_request(:get, "http://id.loc.gov/search/?format=json&q=s&q=cs:http://id.loc.gov/vocabulary/geographicAreas"). + with(:headers => {'Accept'=>'application/json'}). + to_return(:body => webmock_fixture("loc-response.txt"), :status => 200) + Qa::Authorities::Loc.new("geographicAreas") + end - it "should be retrieved via search" do - @authority.search("History--", "subjects") - @authority.results.first["label"].should == "History--Philosophy--History--20th century" - end + it "should retain the raw respsonse from the LC service in JSON" do + expect(authority.raw_response).to be_nil + json = Qa::Authorities::WebServiceBase.new.get_json(authority.build_query_url("s")) + authority.search("s") + expect(authority.raw_response).to eq(json) + end - end + describe "the returned results" do - describe "name headings" do + let :results do + authority.search("s") + end - before :all do - stub_request(:get, "http://id.loc.gov/search/?format=json&q=Berry&q=cs:http://id.loc.gov/authorities/names"). - with(:headers => {'Accept'=>'application/json'}). - to_return(:body => webmock_fixture("loc-names-response.txt"), :status => 200) + it "should have :id and :label elements" do + expect(results.first["label"]).to eq("West (U.S.)") + expect(results.first["id"]).to eq("info:lc/vocabulary/geographicAreas/n-usp") + expect(results.last["label"]).to eq("Baltic States") + expect(results.last["id"]).to eq("info:lc/vocabulary/geographicAreas/eb") + expect(results.size).to eq(20) + end + + end end - it "should be retrieved via search" do - @authority.search("Berry", "names") - @authority.results.first["label"].should == "Berry, James W. (James William), 1938-" + context "subject terms" do + let :results do + stub_request(:get, "http://id.loc.gov/search/?format=json&q=History--&q=cs:http://id.loc.gov/authorities/subjects"). + with(:headers => {'Accept'=>'application/json'}). + to_return(:body => webmock_fixture("loc-subjects-response.txt"), :status => 200) + Qa::Authorities::Loc.new("subjects").search("History--") + end + it "should have a URI for the id and a string label" do + expect(results.count).to eq(20) + expect(results.first["label"]).to eq("History--Philosophy--History--20th century") + expect(results.first["id"]).to eq("info:lc/authorities/subjects/sh2008121753") + expect(results[1]["label"]).to eq("History--Philosophy--History--19th century") + expect(results[1]["id"]).to eq("info:lc/authorities/subjects/sh2008121752") + end end - - end - describe "#parse_authority_response" do - before :all do - stub_request(:get, "http://id.loc.gov/search/?format=json&q=h&q=cs:http://id.loc.gov/authorities/subjects"). - with(:headers => {'Accept'=>'application/json'}). - to_return(:body => webmock_fixture("loc-response.txt"), :status => 200) - @authority.search("h", "subjects") + context "name terms" do + let :results do + stub_request(:get, "http://id.loc.gov/search/?format=json&q=Berry&q=cs:http://id.loc.gov/authorities/names"). + with(:headers => {'Accept'=>'application/json'}). + to_return(:body => webmock_fixture("loc-names-response.txt"), :status => 200) + Qa::Authorities::Loc.new("names").search("Berry") + end + it "should retrieve names via search" do + expect(results.first["label"]).to eq("Berry, James W. (James William), 1938-") + end end - let(:parsed_response) { @authority.parse_authority_response(@authority.raw_response) } + end - it "should return an array of entries returned in the JSON" do - expect(parsed_response.length).to eq(2) + describe "#find" do + context "using a subject id" do + let :results do + stub_request(:get, "http://id.loc.gov/authorities/subjects/sh2002003586.json"). + with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}). + to_return(:status => 200, :body => webmock_fixture("loc-subject-find-response.txt"), :headers => {}) + Qa::Authorities::Loc.new("subjects").find("sh2002003586") + end + it "returns the complete record for a given subject" do + expect(results.count).to eq(20) + expect(results.first).to be_kind_of(Hash) + end end - - it "should have a URI for the id and a string label" do - expect(parsed_response[0]["id"]).to eq("info:lc/authorities/names/n2008008718") - expect(parsed_response[0]["label"]).to eq("Haw, Lily, 1890?-1915") - expect(parsed_response[1]["id"]).to eq("info:lc/vocabulary/geographicAreas/n-us-hi") - expect(parsed_response[1]["label"]).to eq("Hawaii") - end end + end