spec/models/spotlight/resources/purl_spec.rb in spotlight-dor-resources-0.0.3 vs spec/models/spotlight/resources/purl_spec.rb in spotlight-dor-resources-0.0.4

- old
+ new

@@ -1,114 +1,120 @@ require 'spec_helper' describe Spotlight::Resources::Purl do let :exhibit do - double(solr_data: { }, blacklight_config: Blacklight::Configuration.new) + double(solr_data: {}, blacklight_config: Blacklight::Configuration.new) end let :blacklight_solr do double end - subject { Spotlight::Resources::Purl.new url: "http://purl.stanford.edu/xf680rd3068" } + subject { described_class.new url: 'http://purl.stanford.edu/xf680rd3068' } before do allow(subject).to receive(:exhibit).and_return(exhibit) allow(subject).to receive(:blacklight_solr).and_return(blacklight_solr) allow(subject).to receive(:to_global_id).and_return('x') end - describe ".can_provide?" do - subject { Spotlight::Resources::Purl } - it "should be able to provide any purl URL" do - expect(subject.can_provide?(double(url: "https://purl.stanford.edu/xyz"))).to eq true - expect(subject.can_provide?(double(url: "http://purl.stanford.edu/xyz"))).to eq true + describe '.can_provide?' do + subject { described_class } + it 'is true for a PURL URL' do + expect(subject.can_provide?(double(url: 'https://purl.stanford.edu/xyz'))).to eq true + expect(subject.can_provide?(double(url: 'http://purl.stanford.edu/xyz'))).to eq true end + + it 'is false other URLs' do + expect(subject.can_provide?(double(url: 'https://example.com/xyz'))).to eq false + end end - describe "#doc_id" do - it "should be able to extract DRUIDs from a PURL url" do - subject.url = "http://purl.stanford.edu/xyz" - expect(subject.doc_id).to eq "xyz" + describe '#doc_id' do + it 'extracts DRUIDs from a PURL url' do + subject.url = 'http://purl.stanford.edu/xyz' + expect(subject.doc_id).to eq 'xyz' end - it "should be able to extract DRUIDs from a PURL format url" do - subject.url = "http://purl.stanford.edu/xf680rd3068.xml" - expect(subject.doc_id).to eq "xf680rd3068" + it 'extracts DRUIDs from a PURL format url' do + subject.url = 'http://purl.stanford.edu/xf680rd3068.xml' + expect(subject.doc_id).to eq 'xf680rd3068' end - it "should be able to extract DRUIDs from a PURL's viewer url" do - subject.url = "http://purl.stanford.edu/xf680rd3068#image/1/small" - expect(subject.doc_id).to eq "xf680rd3068" + it "extracts DRUIDs from a PURL's viewer url" do + subject.url = 'http://purl.stanford.edu/xf680rd3068#image/1/small' + expect(subject.doc_id).to eq 'xf680rd3068' end end - describe "#resource" do - it "should be a Harvestdor::Indexer resource" do + describe '#resource' do + it 'is a Harvestdor::Indexer resource' do expect(subject.resource).to be_a_kind_of Harvestdor::Indexer::Resource end - it "should have the correct druid" do - expect(subject.resource.druid).to eq "xf680rd3068" + it 'has the correct druid' do + expect(subject.resource.druid).to eq 'xf680rd3068' end - it "should have the correct indexer" do + it 'has the correct indexer' do expect(subject.resource.indexer).to eq Spotlight::Dor::Resources.indexer.harvestdor end end - describe "#reindex" do + describe '#reindex' do before do - allow(Spotlight::Dor::Resources.indexer).to receive(:solr_document).and_return({upstream: true}) + allow(Spotlight::Dor::Resources.indexer).to receive(:solr_document).and_return(upstream: true) allow(subject.resource).to receive(:collection?).and_return(false) end - it "should add a document to solr" do - solr_data = [{spotlight_resource_id_ssim: nil, spotlight_resource_type_ssim: "spotlight/resources/purls", upstream: true}] - expect(blacklight_solr).to receive(:update).with({params: {commitWithin: 500}, data: solr_data.to_json, headers: {"Content-Type" => "application/json"}}) + it 'adds a document to solr' do + solr_data = [{ spotlight_resource_id_ssim: nil, spotlight_resource_type_ssim: 'spotlight/resources/purls', upstream: true }] + expect(blacklight_solr).to receive(:update).with(params: { commitWithin: 500 }, + data: solr_data.to_json, + headers: { 'Content-Type' => 'application/json' }) expect(subject).to receive(:update_index_time!) subject.reindex end end - describe "#to_solr" do + describe '#to_solr' do before do allow(Spotlight::Dor::Resources.indexer).to receive(:solr_document) end - context "with a collection" do + context 'with a collection' do before do allow(subject.resource).to receive(:collection?).and_return(true) end - it "should provide a solr document for the collection" do + it 'provides a solr document for the collection' do allow(subject.resource).to receive(:items).and_return([]) - expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(subject.resource).and_return({upstream: true}) + expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(subject.resource).and_return(upstream: true) expect(subject.to_solr.first).to include :upstream, :spotlight_resource_id_ssim, :spotlight_resource_type_ssim end - it "should provide a solr document for the items too" do + it 'provides a solr document for the items too' do item = double allow(subject.resource).to receive(:items).and_return([item]) - expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(subject.resource).and_return({collection: true}) - expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(item).and_return({item: true}) + expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(subject.resource).and_return(collection: true) + expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(item).and_return(item: true) solr_doc = subject.to_solr.to_a expect(solr_doc.first).to include :collection expect(solr_doc.last).to include :item end end - context "with a single item" do + context 'with a single item' do before do allow(subject.resource).to receive(:collection?).and_return(false) end - it "should provide a solr document for the resource" do - expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(subject.resource).and_return({upstream: true}) + it 'provides a solr document for the resource' do + expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(subject.resource).and_return(upstream: true) expect(subject.to_solr.first).to include :upstream, :spotlight_resource_id_ssim, :spotlight_resource_type_ssim end - it "should index outside the context of an exhibit" do + it 'indexs outside the context of an exhibit' do allow(subject).to receive(:exhibit).and_return(nil) - expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(subject.resource).and_return({upstream: true}) + expect(Spotlight::Dor::Resources.indexer).to receive(:solr_document).with(subject.resource).and_return(upstream: true) expect(subject.to_solr.first).to include :upstream, :spotlight_resource_id_ssim, :spotlight_resource_type_ssim end end end end