spec/controlled_spec.rb in linked_vocabs-0.2.0 vs spec/controlled_spec.rb in linked_vocabs-0.3.0

- old
+ new

@@ -4,12 +4,12 @@ before(:each) do ActiveTriples::Repositories.add_repository :default, RDF::Repository.new class DummyAuthority < ActiveTriples::Resource include LinkedVocabs::Controlled configure :repository => :default - use_vocabulary :dcmitype - property :title, :predicate => RDF::DC.title + use_vocabulary :dcmitype, class: RDF::Vocab::DCMIType + property :title, predicate: RDF::Vocab::DC.title end end after(:each) do Object.send(:remove_const, 'DummyAuthority') if Object @@ -27,11 +27,11 @@ describe 'vocabulary registration' do it 'should add vocabulary' do expect(subject.vocabularies).to include :dcmitype end it 'should find its vocabulary class' do - expect(subject.vocabularies[:dcmitype][:class]).to eq RDF::DCMITYPE + expect(subject.vocabularies[:dcmitype][:class]).to eq RDF::Vocab::DCMIType end it 'should allow multiple vocabularies' do subject.use_vocabulary :lcsh expect(subject.vocabularies).to include :dcmitype, :lcsh end @@ -60,31 +60,33 @@ end it "should return both" do expect(subject.title).to eq ["English", "French"] end end - # context "when there are english labels" do - # before do - # subject.title = RDF::Literal.new("English", :langauge => :en) - # end - # context "and plain labels" do - # before do - # subject.title = ["Plain", RDF::Literal.new("English", :language => :en)] - # end - # it "should return the english label" do - # expect(subject.rdf_label).to eq ["English"] - # end - # end - # context "and other language labels" do - # before do - # subject.title = [RDF::Literal.new("French", :language => :fr), RDF::Literal.new("English", :language => :en)] - # end - # it "should return the english label" do - # expect(subject.rdf_label).to eq ["English"] - # end - # end - # end + + context "when there are english labels" do + before do + subject.title = RDF::Literal.new("English", :langauge => :en) + end + context "and plain labels" do + before do + subject.title = ["Plain", RDF::Literal.new("English", :language => :en)] + end + it "should return the english label" do + expect(subject.rdf_label).to eq ["English"] + end + end + + context "and other language labels" do + before do + subject.title = [RDF::Literal.new("French", :language => :fr), RDF::Literal.new("English", :language => :en)] + end + it "should return the english label" do + expect(subject.rdf_label).to eq ["English"] + end + end + end end describe '#load_vocabularies' do it 'should load data' do subject.load_vocabularies @@ -106,11 +108,11 @@ expect(subject.new.search('ima').first[:id]).to eq RDF::URI('http://purl.org/dc/dcmitype/Image') end describe 'non-label matches' do before do doc = subject.new('Text') - doc << RDF::Statement(doc.rdf_subject, RDF::DC.description, "This is not an image!") + doc << RDF::Statement(doc.rdf_subject, RDF::Vocab::DC.description, "This is not an image!") doc.persist! end it 'should return non-label matches if no label matches exist' do image = subject.new('Image') @@ -125,22 +127,22 @@ end describe 'uris' do it 'should use a vocabulary uri' do dummy = DummyAuthority.new('Image') - expect(dummy.rdf_subject).to eq RDF::DCMITYPE.Image + expect(dummy.rdf_subject).to eq RDF::Vocab::DCMIType.Image end it 'should accept a full uri' do - dummy = DummyAuthority.new(RDF::DCMITYPE.Image) - expect(dummy.rdf_subject).to eq RDF::DCMITYPE.Image + dummy = DummyAuthority.new(RDF::Vocab::DCMIType.Image) + expect(dummy.rdf_subject).to eq RDF::Vocab::DCMIType.Image end it 'should accept a string for a full uri' do - dummy = DummyAuthority.new(RDF::DCMITYPE.Image.to_s) - expect(dummy.rdf_subject).to eq RDF::DCMITYPE.Image + dummy = DummyAuthority.new(RDF::Vocab::DCMIType.Image.to_s) + expect(dummy.rdf_subject).to eq RDF::Vocab::DCMIType.Image end it 'raises an error if the term is not in the vocabulary' do - expect{ DummyAuthority.new('FakeTerm') }.to raise_error + expect { DummyAuthority.new('FakeTerm') }.to raise_error RuntimeError, "could not make a valid RDF::URI from FakeTerm" end it 'is invalid if the uri is not in the vocabulary' do d = DummyAuthority.new(RDF::URI('http://example.org/blah')) expect(d).not_to be_valid end @@ -160,17 +162,40 @@ context 'with non-strict vocabularies' do before(:each) do DummyAuthority.use_vocabulary :geonames end it 'should make uri for terms not defined' do - expect(DummyAuthority.new('http://sws.geonames.org/FakeTerm').rdf_subject).to eq RDF::GEONAMES.FakeTerm - p end + expect(DummyAuthority.new('http://id.loc.gov/authorities/subjects/FakeTerm').rdf_subject).to eq RDF::Vocab::LCSH.FakeTerm + end it 'should use strict uri when one is available' do - expect(DummyAuthority.new('Image').rdf_subject).to eq RDF::DCMITYPE.Image + expect(DummyAuthority.new('Image').rdf_subject).to eq RDF::Vocab::DCMIType.Image end it 'should raise error for terms that are not clear' do DummyAuthority.use_vocabulary :lcsh - expect{ DummyAuthority.new('FakeTerm').rdf_subject }.to raise_error + expect { DummyAuthority.new('FakeTerm').rdf_subject }.to raise_error RuntimeError, 'could not make a valid RDF::URI from FakeTerm' end + end + end + + describe "#in_vocab?" do + context "with more than one vocabulary" do + before do + class DummyAuthorityWithTwoVocabs < ActiveTriples::Resource + include LinkedVocabs::Controlled + configure :repository => :default + use_vocabulary :dcmitype, class: RDF::Vocab::DCMIType + use_vocabulary :lcsh + property :title, predicate: RDF::Vocab::DC.title + end + end + + after do + Object.send(:remove_const, 'DummyAuthorityWithTwoVocabs') + end + + let(:uri) { RDF::URI.new('http://id.loc.gov/authorities/subjects/sh85062487') } + subject { DummyAuthorityWithTwoVocabs.new(uri) } + + it { is_expected.to be_in_vocab } end end end