spec/lib/shamu/services/service_spec.rb in shamu-0.0.9 vs spec/lib/shamu/services/service_spec.rb in shamu-0.0.11

- old
+ new

@@ -23,12 +23,14 @@ public :find_by_lookup public :cached_lookup public :lookup_association public :lazy_association - def build_entity( record, records = nil ) - scorpion.fetch ServiceSpec::Entity, { record: record }, {} + def build_entities( records ) + records.map do |record| + scorpion.fetch ServiceSpec::Entity, { record: record }, {} + end end end module Request class Change < Shamu::Services::Request @@ -49,24 +51,33 @@ describe Shamu::Services::Service do let( :service ) { scorpion.new ServiceSpec::Service } + def transformer( &block ) + ->( records ) { + records.map do |r| + yield || r + end + } + end + + describe "#entity_list" do it "maps each record" do expect do |b| - list = service.entity_list [ double ], &b + list = service.entity_list [ double ], &transformer( &b ) list.to_a end.to yield_control end it "returns an entity list" do expect( service.entity_list( [] ) ).to be_a Shamu::Entities::List end it "invokes build_entity if no transformer provided" do - expect( service ).to receive( :build_entity ).and_call_original + expect( service ).to receive( :build_entities ).and_call_original list = service.entity_list( [{}] ) list.first end end @@ -98,46 +109,46 @@ let( :record ) { double id: 5, amount: 17 } let( :records ) { [ record ] } it "yields for a matching id" do expect do |b| - service.entity_lookup_list( records, [record.id], ServiceSpec::NullEntity, &b ) + service.entity_lookup_list( records, [record.id], ServiceSpec::NullEntity, &transformer( &b ) ) end.to yield_control end it "returns an Entities::List" do expect( service.entity_lookup_list( [], [], ServiceSpec::NullEntity ) ).to be_a Shamu::Entities::List end it "matches on id by default" do - list = service.entity_lookup_list( records, [record.id], ServiceSpec::NullEntity ) do |r| - scorpion.fetch ServiceSpec::Entity, { record: r }, {} + list = service.entity_lookup_list( records, [record.id], ServiceSpec::NullEntity ) do |records| + records.map { |r| scorpion.fetch ServiceSpec::Entity, { record: r }, {} } end expect( list.first ).to be_present end it "matches on id with string numbers" do - list = service.entity_lookup_list( records, [record.id.to_s], ServiceSpec::NullEntity ) do |r| - scorpion.fetch ServiceSpec::Entity, { record: r }, {} + list = service.entity_lookup_list( records, [record.id.to_s], ServiceSpec::NullEntity ) do |records| + records.map { |r| scorpion.fetch ServiceSpec::Entity, { record: r }, {} } end expect( list.first ).to be_present end it "matches on a custom field" do - list = service.entity_lookup_list( records, [record.amount], ServiceSpec::NullEntity, match: :amount ) do |r| - scorpion.fetch ServiceSpec::Entity, { record: r }, {} + list = service.entity_lookup_list( records, [record.amount], ServiceSpec::NullEntity, match: :amount ) do |records| + records.map { |r| scorpion.fetch ServiceSpec::Entity, { record: r }, {} } end expect( list.first ).to be_present end it "matches with a custom proc" do matcher = ->( record ) { record.amount } - list = service.entity_lookup_list( records, [record.amount], ServiceSpec::NullEntity, match: matcher ) do |r| - scorpion.fetch ServiceSpec::Entity, { record: r }, {} + list = service.entity_lookup_list( records, [record.amount], ServiceSpec::NullEntity, match: matcher ) do |records| + records.map { |r| scorpion.fetch ServiceSpec::Entity, { record: r }, {} } end expect( list.first ).to be_present end @@ -302,6 +313,6 @@ expect( service.lazy_association( 1, service ) ).to be_a Shamu::Services::LazyAssociation end end -end \ No newline at end of file +end