Sha256: a13f07cd2c237f8c613fafe8541dc48ebcf0a78fa04fd0b1de65076dbb36ce89

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require "spec_helper"

require "support/object_store_setup"
require "support/seed_data_setup"
require "terrestrial"
require "terrestrial/configurations/conventional_configuration"

RSpec.describe "Predefined subset queries" do
  include_context "object store setup"
  include_context "seed data setup"

  subject(:users) { object_store[:users] }

  context "on the top level mapper" do
    context "subset is defined with a block" do
      before do
        mappings
          .setup_mapping(:users) { |users|
            users.has_many(:posts, foreign_key: :author_id)
            users.subset(:tricketts) { |dataset|
              dataset
                .where(last_name: "Trickett")
                .order(:first_name)
            }
          }
      end

      it "maps the result of the subset" do
        expect(users.subset(:tricketts).map(&:first_name)).to eq([
          "Hansel",
          "Jasper",
        ])
      end
    end
  end

  context "on a has many association" do
    before do
      mappings
        .setup_mapping(:posts) { |posts|
          posts.fields([:id, :subject, :body, :created_at, :updated_at])
          posts.has_many(:comments)
          posts.has_many_through(:categories)
          posts.subset(:body_contains) { |dataset, search_string|
            dataset.where(body: /#{search_string}/)
          }
        }
    end

    let(:user) { users.first }

    it "maps the datastore subset" do
      expect(user.posts.subset(:body_contains, "purrr").map(&:id))
        .to eq(["posts/2"])
    end

    it "returns an immutable collection" do
      expect(user.posts.subset(:body_contains, "purrr").public_methods)
        .not_to include(:push, :<<, :delete)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terrestrial-0.5.0 spec/predefined_queries_spec.rb