Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 resource and image_resource tests improved #29

Merged
merged 5 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/iiif/v3/presentation/resource.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
module IIIF
module V3
module Presentation
# class for generic content resource
class Resource < IIIF::V3::AbstractResource

def required_keys
%w{ id }
super + %w{ id }
end

def string_only_keys
super + %w{ format }
def prohibited_keys
super + PAGING_PROPERTIES + %w{ nav_date viewing_direction start_canvas content_annotations}
end

def numeric_only_keys
super + %w{ duration }
def uri_only_keys
super + %w{ id }
end

def validate
super

unless self['id'] =~ /^https?:/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the superclass takes care of validating against the more general URI::regexp, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does now! Good catch!

err_msg = "id must be an http(s) URI for #{self.class}"
raise IIIF::V3::Presentation::IllegalValueError, err_msg
end
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/iiif/v3/presentation/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ def required_keys

def prohibited_keys
super + CONTENT_RESOURCE_PROPERTIES + PAGING_PROPERTIES +
%w{ nav_date viewing_direction start_canvas content_annotation }
%w{ nav_date viewing_direction start_canvas content_annotations }
end

def uri_only_keys
super + %w{ @context id @id }
end

def any_type_keys
super + %w{ profile }
end

def validate
super
if IIIF_IMAGE_V2_CONTEXT == self['@context']
Expand Down
171 changes: 163 additions & 8 deletions spec/unit/iiif/v3/presentation/resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,174 @@
describe IIIF::V3::Presentation::Resource do
describe '#required_keys' do
it 'id' do
expect(subject.required_keys).to include('id')
end
it 'type' do
expect(subject.required_keys).to include('type')
end
end

describe '#prohibited_keys' do
it 'contains the expected key names' do
keys = described_class::PAGING_PROPERTIES +
%w{
nav_date
viewing_direction
start_canvas
content_annotations
}
expect(subject.prohibited_keys).to include(*keys)
end
end

describe "#{described_class}.abstract_resource_only_keys" do
it_behaves_like 'it has the appropriate methods for abstract_resource_only_keys v3'
describe '#uri_only_keys' do
it 'id' do
expect(subject.uri_only_keys).to include('id')
end
end

describe "#{described_class}.int_only_keys" do
it_behaves_like 'it has the appropriate methods for integer-only keys v3'
describe '#initialize' do
it 'allows subclasses to override type' do
subclass = Class.new(described_class) do
def initialize(hsh={})
hsh = { 'type' => 'a:SubClass' }
super(hsh)
end
end
sub = subclass.new
expect(sub['type']).to eq 'a:SubClass'
end
end

describe "#{described_class}.numeric_only_keys" do
it_behaves_like 'it has the appropriate methods for numeric-only keys v3'
describe '#validate' do
it 'raises an IllegalValueError if id is not URI' do
subject['id'] = 'foo'
subject['type'] = 'image/jpeg'
exp_err_msg = "id must be an http(s) URI for #{described_class}"
expect { subject.validate }.to raise_error(IIIF::V3::Presentation::IllegalValueError, exp_err_msg)
end
it 'raises an IllegalValueError if id is not http' do
subject['id'] = 'ftp://www.example.org'
subject['type'] = 'image/jpeg'
exp_err_msg = "id must be an http(s) URI for #{described_class}"
expect { subject.validate }.to raise_error(IIIF::V3::Presentation::IllegalValueError, exp_err_msg)
end
end

describe "#{described_class}.define_methods_for_string_only_keys" do
it_behaves_like 'it has the appropriate methods for string-only keys v3'
describe 'realistic examples' do
describe 'non-image examples from http://prezi3.iiif.io/api/presentation/3.0' do
describe 'audio' do
let(:file_id) { 'http://example.org/iiif/book1/res/music.mp3' }
let(:file_type) { 'dctypes:Sound' }
let(:file_mimetype) { 'audio/mpeg' }
let(:resource_object) { IIIF::V3::Presentation::Resource.new({
'id' => file_id,
'type' => file_type,
'format' => file_mimetype
})}
it 'validates' do
expect{resource_object.validate}.not_to raise_error
end
it 'has expected required values' do
expect(resource_object.id).to eq file_id
end
it 'has expected other values' do
expect(resource_object.type).to eq file_type
expect(resource_object.format).to eq file_mimetype
expect(resource_object.service).to eq []
end
end
describe 'text' do
let(:file_id) { 'http://example.org/iiif/book1/res/tei-text-p1.xml' }
let(:file_type) { 'dctypes:Text' }
let(:file_mimetype) { 'application/tei+xml' }
let(:resource_object) { IIIF::V3::Presentation::Resource.new({
'id' => file_id,
'type' => file_type,
'format' => file_mimetype
})}
it 'validates' do
expect{resource_object.validate}.not_to raise_error
end
it 'has expected required values' do
expect(resource_object.id).to eq file_id
end
it 'has expected other values' do
expect(resource_object.type).to eq file_type
expect(resource_object.format).to eq file_mimetype
expect(resource_object.service).to eq []
end
end

end
describe 'stanford' do
describe 'non-image resource per purl code' do
let(:file_id) { 'https://example.org/file/abc666/ocr.txt' }
let(:file_type) { 'Document' }
let(:file_mimetype) { 'text/plain' }
let(:resource_object) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i've seen rubocop complain about not using the do form for multiline blocks. not a thing that bothers me personally, and not sure how much others care about that convention, but just FYI in case that makes you want to change this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah - I've seen that too. At this point, I think I'm inclined to just let it ride.

resource = IIIF::V3::Presentation::Resource.new
resource['id'] = file_id
resource['type'] = file_type
resource.format = file_mimetype
resource
}
describe 'world visible' do
it 'validates' do
expect{resource_object.validate}.not_to raise_error
end
it 'has expected required values' do
expect(resource_object.id).to eq file_id
end
it 'has expected other values' do
expect(resource_object.type).to eq file_type
expect(resource_object.format).to eq file_mimetype
expect(resource_object.service).to eq []
end
end
describe 'requires login' do
# let(:auth_token_service) {
# IIIF::V3::Presentation::Service.new({
# 'id' => 'https://example.org/image/iiif/token',
# 'profile' => IIIF::V3::Presentation::Service::IIIF_AUTHENTICATION_V1_TOKEN_PROFILE
# })}
let(:service_label) { 'login message' }
let(:token_service_id) { 'https://example.org/iiif/token' }
let(:login_service) {
IIIF::V3::Presentation::Service.new(
'id' => 'https://example.org/auth/iiif',
'profile' => 'http://iiif.io/api/auth/1/login',
'label' => service_label,
'service' => [{
'id' => token_service_id,
'profile' => 'http://iiif.io/api/auth/1/token'
}]
)
}
let(:resource_object_w_login) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto multiline block on this and let(:login_service) (if you change the one in the preceding test)

resource = resource_object
resource.service = login_service
resource
}
it 'validates' do
expect{resource_object_w_login.validate}.not_to raise_error
end
it 'has expected service value' do
service_obj = resource_object_w_login.service
expect(service_obj.class).to eq IIIF::V3::Presentation::Service
expect(service_obj.keys.size).to eq 4
expect(service_obj.id).to eq 'https://example.org/auth/iiif'
expect(service_obj.profile).to eq IIIF::V3::Presentation::Service::IIIF_AUTHENTICATION_V1_LOGIN_PROFILE
expect(service_obj.label).to eq service_label
expect(service_obj.service.class).to eq Array
expect(service_obj.service.size).to eq 1
expect(service_obj.service.first.keys.size).to eq 2
expect(service_obj.service.first['id']).to eq token_service_id
expect(service_obj.service.first['profile']).to eq IIIF::V3::Presentation::Service::IIIF_AUTHENTICATION_V1_TOKEN_PROFILE
end
end
end
end
end

end
8 changes: 7 additions & 1 deletion spec/unit/iiif/v3/presentation/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
nav_date
viewing_direction
start_canvas
content_annotation
content_annotations
}
expect(subject.prohibited_keys).to include(*keys)
end
Expand All @@ -32,6 +32,12 @@
end
end

describe '#any_type_keys' do
it 'profile' do
expect(subject.any_type_keys).to include('profile')
end
end

let(:id_uri) { "https://example.org/image1" }

describe '#initialize' do
Expand Down