spec/lib/shamu/services/service_spec.rb in shamu-0.0.21 vs spec/lib/shamu/services/service_spec.rb in shamu-0.0.24

- old
+ new

@@ -6,14 +6,14 @@ include Shamu::Services::RequestSupport def process( params ) with_request( params, Request::Change ) do |request| request_hook - next error( :base, "nope" ) if request.level < 0 + next request.error( :base, "nope" ) if request.level < 0 record = OpenStruct.new( request.to_attributes ) - scorpion.fetch ServiceSpec::Entity, { record: record }, {} + scorpion.fetch ServiceSpec::Entity, record: record end end def request_hook end @@ -22,14 +22,15 @@ public :entity_list public :find_by_lookup public :cached_lookup public :lookup_association public :lazy_association + public :cache_for def build_entities( records ) records.map do |record| - scorpion.fetch ServiceSpec::Entity, { record: record }, {} + scorpion.fetch ServiceSpec::Entity, record: record end end end module Request @@ -119,36 +120,36 @@ 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 |records| - records.map { |r| scorpion.fetch ServiceSpec::Entity, { record: r }, {} } + 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 |records| - records.map { |r| scorpion.fetch ServiceSpec::Entity, { record: r }, {} } + 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 |records| # rubocop:disable Metrics/LineLength - records.map { |r| scorpion.fetch ServiceSpec::Entity, { record: r }, {} } + 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 |records| # rubocop:disable Metrics/LineLength - records.map { |r| scorpion.fetch ServiceSpec::Entity, { record: r }, {} } + records.map { |r| scorpion.fetch ServiceSpec::Entity, record: r } end expect( list.first ).to be_present end @@ -267,52 +268,40 @@ allow( service ).to receive( :lookup ) do |*ids| ids.map { |id| ServiceSpec::Entity.null_entity.new( id: id ) } end end + let( :cache ) { service.cache_for( entity: service ) } + it "returns nil if id is nil" do - expect( service.lookup_association( nil, service ) ).to be_nil + expect( service.lookup_association( nil, service, cache ) ).to be_nil end it "yields to get all association links" do expect do |b| - service.lookup_association( 1, service, &b ) + service.lookup_association( 1, service, cache, &b ) end.to yield_control end it "finds assocation from cache" do - service.lookup_association( 1, service ) do + service.lookup_association( 1, service, cache ) do [ 1, 2 ] end expect do |b| - service.lookup_association( 1, service, &b ) + service.lookup_association( 1, service, cache, &b ) end.not_to yield_control end it "returns the found entity with no records" do - result = service.lookup_association( 1, service ) + result = service.lookup_association( 1, service, cache ) expect( result ).to be_a ServiceSpec::Entity end it "returns the found entity with bulk records" do - result = service.lookup_association( 1, service ) do + result = service.lookup_association( 1, service, cache ) do [ 1, 2 ] end expect( result ).to be_a ServiceSpec::Entity end - end - - describe "#lazy_association" do - before( :each ) do - allow( service ).to receive( :lookup ) do |*ids| - ids.map { |id| ServiceSpec::Entity.null_entity.new( id: id ) } - end - end - - it "gets a lazy association" do - expect( service.lazy_association( 1, service ) ).to be_a Shamu::Services::LazyAssociation - end - - end end