Sha256: 8931382f587e204d87f81539ac8ab7f66c8f67ea4d74fc95b54508c67e849c08

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

require "spec_helper"

describe "Simple::SQL.each" do
  context "when called without a block " do
    it "raises an ArgumentError" do
      expect {
        SQL.each("SELECT id FROM users", into: Hash)
      }.to raise_error(ArgumentError)
    end
  end

  def generate_users!
    1.upto(USER_COUNT).map { create(:user) }
  end
  
  def each!(sql, into: nil)
    @received = nil
    SQL.each(sql, into: into) do |id|
      @received ||= []
      @received << id
    end
  end

  let(:received) { @received }

  describe "each into: nil" do
    before { generate_users! }
    context "when called with matches" do
      it "receives rows as arrays" do
        each! "SELECT id, id FROM users ORDER BY id"

        expect(received).to eq(1.upto(USER_COUNT).map { |i| [ i,i ]})
      end

      it "receives single item row as individual objects" do
        each! "SELECT id FROM users ORDER BY id"

        expect(received).to eq(1.upto(USER_COUNT).to_a)
      end
    end

    context 'when called with no matches' do
      it "does not yield" do
        each! "SELECT id FROM users WHERE FALSE"
        expect(received).to be_nil
      end
    end
  end
  
  describe "each into: <something>" do
    before { generate_users! }

    it "receives rows as Hashes" do
      each! "SELECT id, id AS dupe FROM users ORDER BY id", into: Hash

      expect(received).to eq(1.upto(USER_COUNT).map { |i| { id: i, dupe: i }})
    end

    it "receives rows as immutable" do
      each! "SELECT id, id AS dupe FROM users ORDER BY id", into: :immutable

      expect(received.first.id).to eq(1)
      expect(received[1].dupe).to eq(2)
      expect(received.map(&:class).uniq).to eq([Simple::SQL::Helpers::Immutable])
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple-sql-0.5.27 spec/simple/sql/each_spec.rb
simple-sql-0.5.26 spec/simple/sql/each_spec.rb
simple-sql-0.5.25 spec/simple/sql/each_spec.rb
simple-sql-0.5.24 spec/simple/sql/each_spec.rb