Sha256: 5226602b40667065bd389ad38ab29483215263f08c9ee7c04a56ae08d2e4ff2d

Contents?: true

Size: 2 KB

Versions: 10

Compression:

Stored size: 2 KB

Contents

require 'spec_helper'

describe Riveter::Query do
  describe "#initialize" do
    it "should call #build_relation" do
      expect_any_instance_of(TestQuery).to receive(:build_relation).with(:filter)
      TestQuery.new(:filter)
    end

    it "should assign the filter" do
      TestQuery.new(:filter).query_filter.should eq(:filter)
    end

    it "should assign the options" do
      TestQuery.new(:filter, :key => :value).options.should eq({:key => :value})
    end

    it "should assign the relation" do
      query = TestQuery.new(nil)
      query.relation.should eq(query.build_relation(:filter))
    end
  end

  describe "#build_relation" do
    it "should raise NotImplementedError if not implemented" do
      klass = Class.new(Riveter::Query::Base)
      expect {
        klass.new(:filter)
      }.to raise_error(NotImplementedError)
    end
  end

  describe "#find_each" do
    it "should enumerate relation" do
      # Note: Array implements `find_each_with_order`
      allow_any_instance_of(TestQuery).to receive(:build_relation) { [1] }
      query = TestQuery.new(nil)
      expect(query.relation).to receive(:find_each_with_order)
      query.find_each {}
    end

    it "should invoke block" do
      block = Mock::Block.new()
      expect(block).to receive(:call).at_least(:once)

      fake_relation = [1]
      allow(fake_relation).to receive(:find_each_with_order) do |*args, &block|
        block.call(*args)
      end
      allow_any_instance_of(TestQuery).to receive(:relation) { fake_relation }

      query = TestQuery.new(nil)
      query.find_each &block
    end
  end

  describe "#has_data?" do
    it "should yield true when data available" do
      allow_any_instance_of(TestQuery).to receive(:relation) { [1] }
      query = TestQuery.new(nil)
      query.has_data?.should be_truthy
    end

    it "should yield false when no data available" do
      allow_any_instance_of(TestQuery).to receive(:relation) { [] }
      query = TestQuery.new(nil)
      query.has_data?.should be_falsey
    end
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
riveter-0.2.0 spec/riveter/query_spec.rb
riveter-0.1.1 spec/riveter/query_spec.rb
riveter-0.0.15 spec/riveter/query_spec.rb
riveter-0.0.14 spec/riveter/query_spec.rb
riveter-0.0.13 spec/riveter/query_spec.rb
riveter-0.0.12 spec/riveter/query_spec.rb
riveter-0.0.11 spec/riveter/query_spec.rb
riveter-0.0.10 spec/riveter/query_spec.rb
riveter-0.0.9 spec/riveter/query_spec.rb
riveter-0.0.8 spec/riveter/query_spec.rb