Sha256: 3fe96a439b89ed65ab5634063fbf0d80cf90f2d2c45962cdea0dab5adad984c7

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require "spec_helper"

require "support/mapper_setup"
require "support/sequel_persistence_setup"
require "support/seed_data_setup"
require "sequel_mapper"

RSpec.describe "Object identity" do
  include_context "mapper setup"
  include_context "sequel persistence setup"
  include_context "seed data setup"

  subject(:mapper) { mappers.fetch(:users) }

  let(:user) { mapper.where(id: "users/1").first }
  let(:post) { user.posts.first }

  context "when using arbitrary where query" do
    it "returns the same object for a row's primary key" do
      expect(
        user.posts.where(id: post.id).first
      ).to be(post)
    end
  end

  context "when traversing deep into the graph" do
    context "via has many through" do
      it "returns the same object for a row's primary key" do
        expect(
          user.posts.first.categories.first.posts
            .find { |cat_post| cat_post.id == post.id }
        ).to be(post)
      end
    end

    context "via a belongs to" do
      it "returns the same object for a row's primary once loaded" do
        # TODO: Add another method to avoid using #__getobj__
        expect(
          user.posts.first.comments
            .find { |comment| comment.commenter.id == user.id }
            .commenter
            .__getobj__
        ).to be(user)
      end
    end

    context "when eager loading" do
      let(:user_query) { mapper.where(id: "users/1") }

      let(:eager_category) {
        user_query
          .eager_load(:posts => { :categories => { :posts => [] }})
          .first
          .posts
          .first
          .categories
          .first
      }

      it "returns the same object for a row's primary once loaded" do
        expect(
          eager_category
            .posts
            .find { |cat_post| cat_post.id == post.id }
        ).to be(post)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sequel_mapper-0.0.3 spec/object_identity_spec.rb