spec/item_spec.rb in mida-0.3.2 vs spec/item_spec.rb in mida-0.3.3
- old
+ new
@@ -80,11 +80,13 @@
itemtype %r{http://example.com/vocab/person}
has_one 'name'
has_many 'date' do
extract Mida::DataType::ISO8601Date, Mida::DataType::Text
end
- has_many 'url'
+ has_many 'url' do
+ extract Mida::DataType::URL
+ end
end
itemscope = mock(Mida::Itemscope)
itemscope.stub!(:type).and_return("http://example.com/vocab/person")
itemscope.stub!(:id).and_return(nil)
@@ -104,37 +106,31 @@
it 'should return has_one properties as a single value' do
@item.properties['name'].should == 'Lorry Woodman'
end
it 'should return has_many properties as an array' do
- @item.properties['url'].should == ['http://example.com/user/lorry']
+ @item.properties['url'].length == 1
+ @item.properties['url'].first.to_s.should == 'http://example.com/user/lorry'
end
it 'should accept datatypes that are valid' do
@item.properties['date'][0].should == '2nd October 2009'
end
it 'should accept datatypes that are valid' do
- @item.properties['date'][1].should == Date.iso8601('2009-10-02')
+ @item.properties['date'][1].should.to_s == Date.iso8601('2009-10-02').rfc822
end
it '#properties should return the same properties as the itemscope' do
- @item.properties.should == {
- 'name' => 'Lorry Woodman',
- 'date' => ['2nd October 2009', Date.iso8601('2009-10-02')],
- 'url' => ['http://example.com/user/lorry']
- }
+ @item.properties.keys.should == ['name', 'date', 'url']
+ @item.properties['date'].length == 2
end
it '#to_h should return the correct type and properties' do
@item.to_h.should == {
type: 'http://example.com/vocab/person',
- properties: {
- 'name' => 'Lorry Woodman',
- 'date' => ['2nd October 2009', Date.iso8601('2009-10-02')],
- 'url' => ['http://example.com/user/lorry']
- }
+ properties: @item.properties
}
end
end
@@ -151,57 +147,34 @@
has_one 'dob' do
extract Mida::DataType::ISO8601Date
end
end
- @itemscope = mock(Mida::Itemscope)
- @itemscope.stub!(:type).and_return("http://example.com/vocab/person")
- @itemscope.stub!(:id).and_return(nil)
- @itemscope.stub!(:properties).and_return(
+ itemscope = mock(Mida::Itemscope)
+ itemscope.stub!(:type).and_return("http://example.com/vocab/person")
+ itemscope.stub!(:id).and_return(nil)
+ itemscope.stub!(:properties).and_return(
{ 'name' => ['Lorry Woodman'],
'tel' => ['000004847582', '111111857485'],
'url' => ['http://example.com/user/lorry'],
'city' => ['Bristol'],
'dob' => 'When I was born'
}
)
+ @item = Mida::Item.new(itemscope)
end
- context 'when validation selected' do
- before do
- @item = Mida::Item.new(@itemscope)
- end
+ it '#vocabulary should return the correct vocabulary' do
+ @item.vocabulary.should == Person
+ end
- it '#vocabulary should return the correct vocabulary' do
- @item.vocabulary.should == Person
- end
-
- it 'should not keep properties that have too many values' do
- @item.properties.should_not have_key('tel')
- end
-
- it 'should not keep properties that have the wrong DataType' do
- @item.properties.should_not have_key('dob')
- end
+ it 'should not keep properties that have too many values' do
+ @item.properties.should_not have_key('tel')
end
- context 'when validation not selected' do
- before do
- @item = Mida::Item.new(@itemscope, false)
- end
-
- it '#vocabulary should return the correct vocabulary' do
- @item.vocabulary.should == Person
- end
-
- it 'should keep properties even if they have too many values' do
- @item.properties.should have_key('tel')
- end
-
- it 'should keep properties even if they have the wrong DataType' do
- @item.properties.should have_key('dob')
- end
+ it 'should not keep properties that have the wrong DataType' do
+ @item.properties.should_not have_key('dob')
end
end
describe Mida::Item, 'when initialized with an itemscope containing another correct itemscope' do
@@ -253,10 +226,26 @@
it 'should accept the text tel' do
@item.properties['tel'][0].should == '000004847582'
end
+ it "#to_h shouldnt return nested items properly" do
+ @item.to_h.should == {
+ type: 'http://example.com/vocab/person',
+ properties: {
+ 'name' => 'Lorry Woodman',
+ 'tel' => [ '000004847582',
+ { type: 'http://example.com/vocab/tel',
+ properties: { 'dial_code' => '0248583',
+ 'number' => '000004847582'
+ }
+ }
+ ]
+ }
+ }
+ end
+
end
describe Mida::Item, 'when initialized with an itemscope that has a property type that is a child of the specified type' do
before do
class Person < Mida::Vocabulary
@@ -314,9 +303,13 @@
end
describe Mida::Item, 'when initialized with an itemscope containing another invalid itemscope' do
before do
+ # Make sure the class is redefined afresh to make sure that
+ # inherited() hook is called
+ Mida::Vocabulary.unregister(Person)
+ Object.send(:remove_const, :Person)
class Person < Mida::Vocabulary
itemtype %r{http://example.com/vocab/person}
has_one 'name'
has_many 'tel'
end