Sha256: 4c1b91c0f77e14e234812046621205951780d32e937a6b73fdd398c9415765f4

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'helper'
# database: allen_test
# create table records ( id serial);

class TestPostgresqlCursor < Minitest::Test

  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

1 entries across 1 versions & 1 rubygems

Version Path
postgresql_cursor-0.4.3 test/test_postgresql_cursor.rb