spec/models/base_spec.rb in pupa-0.0.3 vs spec/models/base_spec.rb in pupa-0.0.4
- old
+ new
@@ -4,33 +4,39 @@
module Music
class Band < Pupa::Base
self.schema = {
'$schema' => 'http://json-schema.org/draft-03/schema#',
'properties' => {
- 'url' => {
- 'type' => 'string',
- 'format' => 'uri',
+ 'links' => {
+ 'items' => {
+ 'properties' => {
+ 'url' => {
+ 'type' => 'string',
+ 'format' => 'uri',
+ },
+ },
+ },
},
},
}
- attr_accessor :name, :url, :label, :founding_date, :inactive, :label_id, :manager_id
+ attr_accessor :name, :label, :founding_date, :inactive, :label_id, :manager_id, :links
foreign_key :label_id, :manager_id
foreign_object :label
end
end
let :properties do
- {name: 'Moderat', url: 'http://moderat.fm/', label: {name: 'Mute'}, inactive: false, manager_id: '1'}
+ {name: 'Moderat', label: {name: 'Mute'}, inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]}
end
let :object do
Music::Band.new(properties)
end
describe '#attr_accessor' do
it 'should add properties' do
- Music::Band.properties.to_a.should == [:_id, :_type, :extras, :name, :url, :label, :founding_date, :inactive, :label_id, :manager_id]
+ Music::Band.properties.to_a.should == [:_id, :_type, :extras, :name, :label, :founding_date, :inactive, :label_id, :manager_id, :links]
end
end
describe '#foreign_key' do
it 'should add foreign keys' do
@@ -59,13 +65,19 @@
it 'should accept a hash' do
Music::Band.json_schema.should == {
'$schema' => 'http://json-schema.org/draft-03/schema#',
'properties' => {
- 'url' => {
- 'type' => 'string',
- 'format' => 'uri',
+ 'links' => {
+ 'items' => {
+ 'properties' => {
+ 'url' => {
+ 'type' => 'string',
+ 'format' => 'uri',
+ },
+ },
+ },
},
},
}
end
@@ -87,11 +99,14 @@
object._id.should match(/\A[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/)
end
it 'should set properties' do
object.name.should == 'Moderat'
- object.url.should == 'http://moderat.fm/'
+ object.label.should == {name: 'Mute'}
+ object.inactive.should == false
+ object.manager_id.should == '1'
+ object.links.should == [{url: 'http://moderat.fm/'}]
end
end
describe '#[]' do
it 'should get a property' do
@@ -133,11 +148,11 @@
end
end
describe '#fingerprint' do
it 'should return the fingerprint' do
- object.fingerprint.should == {_type: 'music/band', name: 'Moderat', url: 'http://moderat.fm/', inactive: false, manager_id: '1'}
+ object.fingerprint.should == {_type: 'music/band', name: 'Moderat', inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]}
end
end
describe '#foreign_properties' do
it 'should return the foreign keys and foreign objects' do
@@ -157,21 +172,21 @@
it 'should return true if the object is valid' do
object.validate!.should == true
end
it 'should raise an error if the object is invalid' do
- object[:url] = 'invalid'
+ object[:links][0][:url] = 'invalid'
expect{object.validate!}.to raise_error(JSON::Schema::ValidationError)
end
end
describe '#to_h' do
it 'should not include foreign objects by default' do
- object.to_h.should == {_id: object._id, _type: 'music/band', name: 'Moderat', url: 'http://moderat.fm/', inactive: false, manager_id: '1'}
+ object.to_h.should == {_id: object._id, _type: 'music/band', name: 'Moderat', inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]}
end
it 'should include foreign objects if desired' do
- object.to_h(include_foreign_objects: true).should == {_id: object._id, _type: 'music/band', name: 'Moderat', url: 'http://moderat.fm/', label: {name: 'Mute'}, inactive: false, manager_id: '1'}
+ object.to_h(include_foreign_objects: true).should == {_id: object._id, _type: 'music/band', name: 'Moderat', label: {name: 'Mute'}, inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]}
end
it 'should not include blank properties' do
object.to_h.should_not have_key(:founding_date)
end