Sha256: 26fa53feabc1bed7c09a869f9d44ec7647f5d191969739946bbe7e0791cf174f

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require "spec_helper"

require "support/object_store_setup"
require "support/seed_data_setup"
require "terrestrial"

RSpec.describe "Object identity" do
  include_context "object store setup"
  include_context "seed data setup"

  subject(:user_store) { object_store.fetch(:users) }

  let(:user) { user_store.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) { user_store.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

2 entries across 2 versions & 1 rubygems

Version Path
terrestrial-0.5.0 spec/object_identity_spec.rb
terrestrial-0.3.0 spec/object_identity_spec.rb