Module: Cuprum::Collections::RSpec::Contracts::QueryContracts::ShouldBeAQueryBuilderContract

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/collections/rspec/contracts/query_contracts.rb

Overview

Contract validating the behavior of a QueryBuilder implementation.

Instance Method Summary collapse

Instance Method Details

#apply(example_group) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.



687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/cuprum/collections/rspec/contracts/query_contracts.rb', line 687

contract do
  describe '#base_query' do
    include_examples 'should define reader',
      :base_query,
      -> { base_query }
  end

  describe '#call' do
    let(:criteria)  { [['title', :equal, 'The Naked Sun']] }
    let(:expected)  { criteria }
    let(:filter)    { { title: 'The Naked Sun' } }
    let(:strategy)  { :custom }
    let(:parser) do
      instance_double(
        Cuprum::Collections::Queries::Parse,
        call: Cuprum::Result.new(value: criteria)
      )
    end
    let(:query) do
      builder.call(strategy: strategy, where: filter)
    end

    before(:example) do
      allow(Cuprum::Collections::Queries::Parse)
        .to receive(:new)
        .and_return(parser)
    end

    it 'should define the method' do
      expect(builder).to respond_to(:call)
        .with(0).arguments
        .and_keywords(:strategy, :where)
    end

    it 'should parse the criteria' do
      builder.call(strategy: strategy, where: filter)

      expect(parser)
        .to have_received(:call)
        .with(strategy: strategy, where: filter)
    end

    it { expect(query).to be_a base_query.class }

    it { expect(query).not_to be base_query }

    it { expect(query.criteria).to be == expected }

    describe 'with strategy: :unsafe' do
      let(:strategy) { :unsafe }
      let(:filter)   { criteria }

      it 'should not parse the criteria' do
        builder.call(strategy: strategy, where: filter)

        expect(parser).not_to have_received(:call)
      end

      it { expect(query.criteria).to be == expected }
    end

    context 'when the query has existing criteria' do
      let(:old_criteria) { [['genre', :eq, 'Science Fiction']] }
      let(:expected)     { old_criteria + criteria }
      let(:base_query)   { super().send(:with_criteria, old_criteria) }

      it { expect(query.criteria).to be == expected }
    end

    context 'when the parser is unable to parse the query' do
      let(:error)  { Cuprum::Error.new(message: 'Something went wrong.') }
      let(:result) { Cuprum::Result.new(error: error) }

      before(:example) do
        allow(parser).to receive(:call).and_return(result)
      end

      it 'should raise an exception' do
        expect do
          builder.call(strategy: strategy, where: filter)
        end
          .to raise_error Cuprum::Collections::QueryBuilder::ParseError,
            error.message
      end
    end
  end
end