require 'spec_helper' require 'people' describe Gummi::RepositoryLayer::Repository do let(:repository_class) { People } let(:entity_class) { Person } let(:db_class) { DB::Person } describe 'Repobahn' do it 'is included' do repository_class.should respond_to :db_class repository_class.should respond_to :entity_class repository_class.new.should respond_to :db_instance_to_entity end end describe '.get', elastic: true do let (:people) { People.new } context 'existing record' do let (:db_person) { DB::Person.create name: 'Buzz Lightyear' } it 'return an entity' do person = people.get(db_person.id) person.id.should == db_person.id end end context 'missing record' do it 'returns nil' do person = people.get 'missing_id' person.should be_nil end end end describe '.search', elastic: true do let (:people) { People.new } let(:buzz) { DB::Person.create name: 'Buzz Lightyear' } let(:woody) { DB::Person.create name: 'Woody' } before do buzz && woody Gummi::DbLayer::DefaultIndex.refresh end it 'sorts correctly' do people.search(sort: { name: :asc }).records.map(&:name).should == [buzz.name, woody.name] people.search(sort: { name: :desc }).records.map(&:name).should == [woody.name, buzz.name] end it 'finds the correct documents' do result = people.search do |search| search.query_string = 'Woody' search.facets[:names] = { terms: { field: :name, all_terms: true, size: 100 } } end result.total.should == 1 result.facets.names.woody.should == 1 result.records.first.name.should == 'Woody' end it 'converts the result to entities' do result = people.search do |search| search.query_string = 'Woody' end woody = result.records.first result.facets.should be_nil woody.converted_name.should == 'ydooW' woody.should be_a Person end end end