Sha256: 625524f7be00bfff3ef97043f16c8fab69e4ea275df336f759fb330a861da4a6

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

require 'spec_helper'

describe Dossier::Query do

  let(:report) { TestReport.new(:foo => 'bar') }
  let(:query)  { Dossier::Query.new(report) }

  before :each do
    report.stub(:salary).and_return(2)
    report.stub(:ids).and_return([1,2,3])
  end

  describe "replacing symbols by calling methods of the same name" do

    context "when the methods return single values" do

      before :each do
        report.stub(:sql).and_return("SELECT * FROM employees WHERE id = :id OR girth < :girth OR name = :name")
        report.stub(:id).and_return(92)
        report.stub(:girth).and_return(3.14)
        report.stub(:name).and_return('Jimmy')
      end

      it "escapes the values" do
        query.should_receive(:escape).with(92)
        query.should_receive(:escape).with(3.14)
        query.should_receive(:escape).with('Jimmy')
        query.to_s
      end

      it "inserts the values" do
        expect(query.to_s).to eq("SELECT * FROM employees WHERE id = 92 OR girth < 3.14 OR name = 'Jimmy'")
      end

    end

    context "when the methods return arrays" do

      before :each do
        report.stub(:sql).and_return("SELECT * FROM employees WHERE stuff IN :stuff")
        report.stub(:stuff).and_return([38, 'blue', 'mandible', 2])
      end

      it "escapes each value in the array" do
        Dossier.client.should_receive(:escape).with(38)
        Dossier.client.should_receive(:escape).with('blue')
        Dossier.client.should_receive(:escape).with('mandible')
        Dossier.client.should_receive(:escape).with(2)
        query.to_s
      end

      it "joins the return values with commas" do
        expect(query.to_s).to eq("SELECT * FROM employees WHERE stuff IN (38, 'blue', 'mandible', 2)")
      end

    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dossier-2.4.0 spec/dossier/query_spec.rb
dossier-2.3.0 spec/dossier/query_spec.rb
dossier-2.2.0 spec/dossier/query_spec.rb
dossier-2.1.1 spec/dossier/query_spec.rb
dossier-2.1.0 spec/dossier/query_spec.rb
dossier-2.0.1 spec/dossier/query_spec.rb
dossier-2.0.0 spec/dossier/query_spec.rb