Sha256: e98128c3e3413c5c2946b245245c3ed3bf56f3c630180a2a05b084552c5e2133

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require "spec_helper"

require "sequel_mapper"
require "support/graph_fixture"

RSpec.describe "Proxying associations" do
  include SequelMapper::GraphFixture

  context "of type `has_many`" do
    subject(:graph) {
      SequelMapper::Graph.new(
        top_level_namespace: :users,
        datastore: datastore,
        relation_mappings: relation_mappings,
      )
    }

    let(:user) {
      graph.where(id: "user/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)
        end
      end

      context "when getting a reference to an association proxy" do
        before { user }

        it "does no additional reads" do
          expect{
            user.posts
          }.to change { query_counter.read_count }.by(0)
        end
      end

      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)
          }.to change { query_counter.read_count }.by(1)
        end
      end

      context "when getting a reference to a many to many assocation" do
        before { post }

        let(:post) { user.posts.first }

        it "does no additional reads" do
          expect {
            post.categories
          }.to change { query_counter.read_count }.by(0)
        end
      end

      context "when iterating over a many to many assocation" do
        let(:category_count) { 3 }

        it "does 1 read" do
          post = user.posts.first

          expect {
            post.categories.map(&:name).to_a
          }.to change { query_counter.read_count }.by(1)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sequel_mapper-0.0.1 spec/proxying_spec.rb