spec/looksist/looksist_spec.rb in looksist-0.1.3 vs spec/looksist/looksist_spec.rb in looksist-0.1.4

- old
+ new

@@ -1,13 +1,14 @@ require 'spec_helper' describe Looksist do - before :each do + before(:each) do + @mock = {} Looksist.configure do |looksist| - looksist.lookup_store = double('store_lookup_client') - looksist.driver = Looksist::Serializers::Her + looksist.lookup_store = @mock + looksist.driver = Looksist::Serializers::Her end end context 'Serialization Support' do it 'should decorate for her models' do @@ -22,11 +23,11 @@ def as_json(opts) super(opts).merge(another_attr: 'Hello World') end end end - expect(Looksist.lookup_store).to receive(:get).with('employees/1').and_return('Employee Name') + expect(@mock).to receive(:get).once.with('employees/1').and_return('Employee Name') e = Her::Employee.new({employee_id: 1}) expect(e.name).to eq('Employee Name') expect(e.to_json).to eq({:employee_id => 1, :name => 'Employee Name', :another_attr => 'Hello World'}.to_json) end end @@ -42,11 +43,11 @@ def initialize(id) @id = id end end end - expect(Looksist.lookup_store).to receive(:get).with('employees/1').and_return('Employee Name') + expect(@mock).to receive(:get).once.with('employees/1').and_return('Employee Name') e = ExplicitBucket::Employee.new(1) expect(e.name).to eq('Employee Name') end end @@ -61,11 +62,11 @@ @id = id end end end it 'should not eager evaluate' do - expect(Looksist.lookup_store).to_not receive(:get) + expect(@mock).to_not receive(:get) LazyEval::Employee.new(1) end end context 'lookup attributes' do @@ -81,43 +82,46 @@ @id = @employee_id = id end end end - expect(Looksist.lookup_store).to receive(:get).with('ids/1').and_return('Employee Name') - expect(Looksist.lookup_store).to receive(:get).with('employees/1').and_return(nil) + expect(@mock).to receive(:get).once.with('ids/1').and_return('Employee Name') + expect(@mock).to receive(:get).once.with('employees/1').and_return(nil) e = SimpleLookup::Employee.new(1) expect(e.name).to eq('Employee Name') expect(e.unavailable).to be(nil) end it 'should generate declarative attributes on the model with object based lookup value' do module CompositeLookup class Employee include Looksist - attr_accessor :id, :employee_id + attr_accessor :id, :employee_id, :contact_id lookup [:name, :location], using=:id lookup [:age, :sex], using=:employee_id + lookup [:pager, :cell], using=:contact_id def initialize(id) - @id = @employee_id = id + @contact_id = @id = @employee_id = id end end end - expect(Looksist.lookup_store).to receive(:get).with('ids/1') - .and_return({name: 'Employee Name', location: 'Chennai'}.to_json) - expect(Looksist.lookup_store).to receive(:get).twice.with('employees/1') - .and_return(nil) + expect(@mock).to receive(:get).once.with('ids/1').and_return({name: 'Employee Name', location: 'Chennai'}.to_json) + expect(@mock).to receive(:get).once.with('contacts/1').and_return({pager: 'pager', cell: 'cell'}.to_json) + expect(@mock).to receive(:get).twice.with('employees/1').and_return(nil) e = CompositeLookup::Employee.new(1) expect(e.name).to eq('Employee Name') expect(e.location).to eq('Chennai') - expect(e.age).to be(nil) - expect(e.sex).to be(nil) + expect(e.age).to eq(nil) + expect(e.sex).to eq(nil) + + expect(e.pager).to eq('pager') + expect(e.cell).to eq('cell') end end context 'share storage between instances' do class Employee @@ -130,50 +134,16 @@ @id = id end end it 'should share storage between instances to improve performance' do employee_first_instance = Employee.new(1) - expect(Looksist.lookup_store).to receive(:get).with('ids/1') - .and_return({name: 'Employee Name', location: 'Chennai'}.to_json) + expect(@mock).to receive(:get).once.with('ids/1') + .and_return({name: 'Employee Name', location: 'Chennai'}.to_json) employee_first_instance.name employee_second_instance = Employee.new(1) - expect(Looksist.lookup_store).not_to receive(:get).with('ids/1') + expect(@mock).not_to receive(:get).with('ids/1') employee_second_instance.name - end - end - - context '.id_and_buckets' do - class Developer - include Looksist - lookup [:city], using=:city_id - lookup [:role], using=:role_id - end - it 'should hold all the id and buckets' do - expect(Developer.id_and_buckets).to eq([{id: :city_id, bucket: 'cities'}, {id: :role_id, bucket: 'roles'}]) - end - end - - context '.mmemoized' do - class AnotherDeveloperClass - include Looksist - lookup [:city], using=:city_id - lookup [:role], using=:role_id - end - - AnotherDeveloperClass.storage = OpenStruct.new - AnotherDeveloperClass.storage['cities/1'] = 'Chennai' - AnotherDeveloperClass.storage['cities/2'] = 'Delhi' - - it 'make single request for multiple values' do - expect(Looksist.lookup_store).to receive(:mapped_mget).with(%w(cities/4 cities/5)) - .and_return({'cities/4' => 'Bangalore', 'cities/5' => 'Kolkata'}) - AnotherDeveloperClass.mmemoized(:city_id, [1, 4, 5]) - - expect(AnotherDeveloperClass.storage.to_h.length).to eq(4) - expect(AnotherDeveloperClass.storage['cities/5']).to eq('Kolkata') - expect(AnotherDeveloperClass.storage['cities/4']).to eq('Bangalore') - end end end \ No newline at end of file