spec/proxying_spec.rb in sequel_mapper-0.0.1 vs spec/proxying_spec.rb in sequel_mapper-0.0.3

- old
+ new

@@ -1,38 +1,32 @@ require "spec_helper" +require "support/mapper_setup" +require "support/sequel_persistence_setup" +require "support/seed_data_setup" require "sequel_mapper" -require "support/graph_fixture" RSpec.describe "Proxying associations" do - include SequelMapper::GraphFixture + include_context "mapper setup" + include_context "sequel persistence setup" + include_context "seed data setup" context "of type `has_many`" do - subject(:graph) { - SequelMapper::Graph.new( - top_level_namespace: :users, - datastore: datastore, - relation_mappings: relation_mappings, - ) - } + subject(:mapper) { user_mapper } let(:user) { - graph.where(id: "user/1").first + mapper.where(id: "users/1").first } let(:posts) { user.posts } - def identity - ->(x){x} - end - describe "limiting datastore reads" do context "when loading the root node" do it "only performs one read" do - user - - expect(query_counter.read_count).to eq(1) + expect { + user + }.to change { query_counter.read_count }.by(1) end end context "when getting a reference to an association proxy" do before { user } @@ -47,13 +41,25 @@ context "when iteratiing over a has many association" do before { posts } it "does a single additional read for the assocation collection" do expect { - user.posts.map(&identity) + user.posts.each { |x| x } }.to change { query_counter.read_count }.by(1) end + + context "when doing this more than once" do + before do + posts.each { |x| x } + end + + it "performs no additional reads" do + expect { + user.posts.each { |x| x } + }.not_to change { query_counter.read_count } + end + end end context "when getting a reference to a many to many assocation" do before { post } @@ -71,10 +77,10 @@ it "does 1 read" do post = user.posts.first expect { - post.categories.map(&:name).to_a + post.categories.each { |x| x } }.to change { query_counter.read_count }.by(1) end end end end