Sha256: 1710c03ec476b2a8fa0fa4db72a5f7d6ad825d201d010e3df77b6c027a4ef4a0

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

# encoding: UTF-8

require 'spec_helper'

class TestQueryBuilder < ArelHelpers::QueryBuilder
  def initialize(query = nil)
    super(query || Post.unscoped)
  end

  def noop
    reflect(query)
  end
end

describe ArelHelpers::QueryBuilder do
  let(:builder) { TestQueryBuilder.new }

  it "returns (i.e. reflects) new instances of QueryBuilder" do
    builder.tap do |original_builder|
      original_builder.noop.object_id.should_not == original_builder.object_id
    end
  end

  it "forwards #to_a" do
    Post.create(title: "Foobar")
    builder.to_a.tap do |posts|
      posts.size.should == 1
      posts.first.title.should == "Foobar"
    end
  end

  it "forwards #to_sql" do
    builder.to_sql.strip.should == 'SELECT "posts".* FROM "posts"'
  end

  it "forwards #each" do
    created_post = Post.create(title: "Foobar")
    builder.each do |post|
      post.should == created_post
    end
  end

  it "forwards other enumerable methods via #each" do
    Post.create(title: "Foobar")
    builder.map(&:title).should == ["Foobar"]
  end

  ArelHelpers::QueryBuilder::TERMINAL_METHODS.each do |method|
    it "does not enumerate records for #{method}" do
      mock(builder).each.never
      builder.public_send(method)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
arel-helpers-2.4.0 spec/query_builder_spec.rb
arel-helpers-2.3.0 spec/query_builder_spec.rb
arel-helpers-2.2.0 spec/query_builder_spec.rb
arel-helpers-2.1.1 spec/query_builder_spec.rb
arel-helpers-2.1.0 spec/query_builder_spec.rb
arel-helpers-2.0.2 spec/query_builder_spec.rb
arel-helpers-2.0.1 spec/query_builder_spec.rb
arel-helpers-2.0.0 spec/query_builder_spec.rb