require 'spec_helper' require 'db/animal' require 'db/car' require 'db/enemy' require 'db/game' require 'db/person' require 'db/rating' require 'db/ship' describe Gummi::DbLayer::Document do context 'included' do it 'adds accessors for id and version' do model = DB::Ship.new model.should respond_to :id model.should respond_to :version end it 'adds default attributes methods' do model = DB::Ship.new name: 'Joe' model.name.should == 'Joe' end it 'does not accept attributes without explicit type definition' do expect { DB::Enemy.sync_mapping! }.to raise_error(Gummi::Errors::ImplicitMappingForbidden) end end describe '.index' do context 'without being specified' do it 'defaults to the default index' do DB::Game.index.should == Gummi::DbLayer::DefaultIndex end end context 'explicitly specified' do it 'accepts the specification' do DB::Car.index.should == Struct end end end describe '.document_type' do context 'without being specified' do it 'defaults to the demodulized name' do DB::Game.document_type.should == :game end end context 'explicitly specified' do it 'accepts the specification' do DB::Animal.document_type.should == :wild_animal end end end context 'attributes' do context 'date times' do it 'coerces from elastics strings to real Time' do time = Time.now person = DB::Person.new(born_at: time) person.overwrite person_from_es = DB::Person.get person.id person_from_es.born_at.should be_a Time end it 'always stores time in UTC' do time = Time.now.in_time_zone 'CET' person = DB::Person.new(born_at: time) person.born_at.zone.should == 'UTC' end end context 'computed_attributes' do let(:person) { DB::Person.new } it 'adds them to the attributes hash' do person.name = 'olof palme' person.attributes[:computed_name].should == 'OLOF PALME' end it 'computes every time' do person.name = 'olof palme' person.computed_name.should == person.name.upcase person.name = 'carl bildt' person.computed_name.should == person.name.upcase end it 'provides a mapping' do DB::Person.mapping.should include(computed_name: { type: 'string' }) end end end context 'getting from elastic' do let(:person) { DB::Person.create(name: 'Buzz Lightyear') } it 'returns an instance of the document_class' do person_from_es = DB::Person.get(person.id) person_from_es.should be_a DB::Person end end context 'child documents' do let(:person) { DB::Person.create name: 'Superman' } let(:rating) { DB::Rating.create person_id: person.id, stars: 5 } it 'creates and retrieves child documents' do persisted_rating = DB::Rating.get(rating.id, person.id) persisted_rating.person_id.should == person.id end describe '#get' do it 'demands the parent_id' do expect { DB::Rating.get(rating.id) }.to raise_error(ArgumentError) end end describe '#delete_children_by_query' do before do DB::Rating.get(rating.id, person.id) Gummi::DbLayer::DefaultIndex.refresh end it 'deletes children by a query' do DB::Rating.get(rating.id, person.id).should be_true DB::Rating.delete_children_by_query(person.id, term: { stars: 3 }).should be_true DB::Rating.get(rating.id, person.id).should be_true DB::Rating.delete_children_by_query(person.id, term: { stars: 5 }).should be_true DB::Rating.get(rating.id, person.id).should be_nil end end end end