Sha256: 48279eeafc9614c18523db6015c1248bd8065df24e69c93a39d92ab7ce5cc393

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require 'helper'

class TestPostgresqlCursor < Test::Unit::TestCase

  def test_each
    c = PostgreSQLCursor.new("select * from records order by 1")
    nn = 0
    n = c.each { nn += 1}
    assert_equal nn, n
  end

  def test_enumerables
    assert_equal true, PostgreSQLCursor.new("select * from records order by 1").any?
    assert_equal false, PostgreSQLCursor.new("select * from records where id<0").any?
  end

  def test_each_while_until
    c = PostgreSQLCursor.new("select * from records order by 1", until:true)
    n = c.each { |r| r[:id].to_i > 100 }
    assert_equal 101, n

    c = PostgreSQLCursor.new("select * from records order by 1", while:true)
    n = c.each { |r| r[:id].to_i < 100 }
    assert_equal 100, n
  end

  def test_relation
    nn = 0
    Model.where("id>0").each_row {|r| nn += 1 }
    assert_equal 1000, nn
  end

  def test_activerecord
    nn = 0
    Model.each_row_by_sql("select * from records") {|r| nn += 1 }
    assert_equal 1000, nn

    nn = 0
    row = nil
    Model.each_instance_by_sql("select * from records") {|r| row = r; nn += 1 }
    assert_equal 1000, nn
    assert_equal Model, row.class
  end

  def test_exception
    begin
      Model.each_row_by_sql("select * from records") do |r|
        raise "Oops"
      end
    rescue Exception => e
      assert_equal e.message, 'Oops'
    end
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
postgresql_cursor-0.4.2 test/test_postgresql_cursor.rb
postgresql_cursor-0.4.1 test/test_postgresql_cursor.rb