spec/models/base_spec.rb in pupa-0.0.8 vs spec/models/base_spec.rb in pupa-0.0.9

- old
+ new

@@ -1,10 +1,12 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe Pupa::Base do +describe Pupa::Model do module Music - class Band < Pupa::Base + class Band + include Pupa::Model + self.schema = { '$schema' => 'http://json-schema.org/draft-03/schema#', 'properties' => { 'links' => { 'items' => { @@ -17,18 +19,14 @@ }, }, }, } - attr_accessor :label, :founding_date, :inactive, :label_id, :manager_id, :links - attr_reader :name + attr_accessor :name, :label, :founding_date, :inactive, :label_id, :manager_id, :links + dump :name, :label, :founding_date, :inactive, :label_id, :manager_id, :links foreign_key :label_id, :manager_id foreign_object :label - - def name=(name) - @name = name - end end end let :properties do {name: 'Moderat', label: {name: 'Mute'}, inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]} @@ -36,24 +34,18 @@ let :object do Music::Band.new(properties) end - describe '.attr_accessor' do + describe '.dump' do it 'should add properties' do - [:_id, :_type, :extras, :label, :founding_date, :inactive, :label_id, :manager_id, :links].each do |property| + [:_id, :_type, :extras, :name, :label, :founding_date, :inactive, :label_id, :manager_id, :links].each do |property| Music::Band.properties.to_a.should include(property) end end end - describe '.attr_reader' do - it 'should add properties' do - Music::Band.properties.to_a.should include(:name) - end - end - describe '.foreign_key' do it 'should add foreign keys' do Music::Band.foreign_keys.to_a.should == [:label_id, :manager_id] end end @@ -64,17 +56,19 @@ end end describe '.schema=' do let :klass_with_absolute_path do - Class.new(Pupa::Base) do + Class.new do + include Pupa::Model self.schema = '/path/to/schema.json' end end let :klass_with_relative_path do - Class.new(Pupa::Base) do + Class.new do + include Pupa::Model self.schema = 'schema' end end it 'should accept a hash' do @@ -176,11 +170,13 @@ end end describe '#validate!' do let :klass_without_schema do - Class.new(Pupa::Base) + Class.new do + include Pupa::Model + end end it 'should do nothing if the schema is not set' do klass_without_schema.new.validate!.should == nil end @@ -194,15 +190,15 @@ 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', inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]} + it 'should include all properties by default' do + object.to_h.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 include foreign objects if desired' do - 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/'}]} + it 'should exclude foreign objects if persisting' do + object.to_h(persist: true).should == {_id: object._id, _type: 'music/band', name: 'Moderat', 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