spec/kamerling/uuid_entity_spec.rb in kamerling-0.0.2 vs spec/kamerling/uuid_entity_spec.rb in kamerling-0.0.3

- old
+ new

@@ -1,35 +1,63 @@ require_relative '../spec_helper' +require_relative '../../lib/kamerling/uuid_entity' -module Kamerling describe UUIDEntity do - describe '.from_h' do - it 'deserialises the object from a Hash' do - Trivial = Class.new(UUIDEntity) { attribute :question, Symbol } - Trivial.from_h(question: :answer).question.must_equal :answer +module Kamerling + describe UUIDEntity do + describe '.attrs' do + it 'allows defining attributes in a key → class manner' do + person = Class.new(UUIDEntity) { attrs name: String, born: Integer } + person.attribute_set.map(&:name).must_equal %i(uuid name born) + end end - end - describe '.new' do - it 'creates a class with an UUID property defaulting to a random UUID' do - AttrLess = Class.new UUIDEntity - AttrLess.new.uuid.must_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) - AttrLess.new.uuid.wont_equal AttrLess.new.uuid + describe '.defaults' do + it 'allows defining attribute defaults' do + song = Class.new(UUIDEntity) do + attrs title: String, genre: Symbol + defaults genre: :ragga + end + song.new.genre.must_equal :ragga + end end - end - describe '#==' do - it 'reports UUID-based euqality' do - Actor = Class.new(UUIDEntity) { attribute :name, Symbol } - Actor.new(name: :laurel).wont_equal Actor.new name: :laurel - uuid = UUID.new - Actor.new(name: :laurel, uuid: uuid) - .must_equal Actor.new name: :hardy, uuid: uuid + describe '.new' do + it 'creates a class with an UUID property defaulting to a random UUID' do + attr_less = Class.new(UUIDEntity) + attr_less.new.uuid.must_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) + attr_less.new.uuid.wont_equal attr_less.new.uuid + end + + it 'deserialises the object from a Hash' do + trivial = Class.new(UUIDEntity) { attrs question: Symbol } + trivial.new(question: :answer).question.must_equal :answer + end end - end - describe '#to_h' do - it 'serialises the object to a Hash' do - Hashble = Class.new(UUIDEntity) { attribute :param, Symbol } - Hashble.new(param: :val).to_h.must_equal param: :val, uuid: any(String) + describe '#==' do + it 'reports UUID-based euqality' do + actor = Class.new(UUIDEntity) { attrs name: Symbol } + actor.new(name: :laurel).wont_equal actor.new(name: :laurel) + uuid = UUID.new + actor.new(name: :laurel, uuid: uuid) + .must_equal actor.new(name: :hardy, uuid: uuid) + end end + + describe '#to_h' do + it 'serialises the object to a Hash' do + hashble = Class.new(UUIDEntity) { attrs param: Symbol } + hashble.new(param: :val).to_h.must_equal param: :val, uuid: any(String) + end + + it 'serialises related UUIDEntities' do + child = Class.new(UUIDEntity) { attrs name: String } + parent = Class.new(UUIDEntity) { attrs child: child, name: String } + zosia = child.new(name: 'Zosia') + marta = parent.new(child: zosia, name: 'Marta') + zosia_hash = { name: 'Zosia', uuid: zosia.uuid } + marta_hash = { child: zosia_hash, name: 'Marta', uuid: marta.uuid } + marta.to_h.must_equal marta_hash + end + end end -end end +end