Class DbrbTest
In: tests/DbrbTest.rb
Parent: Test::Unit::TestCase

Since each test needs to be executed for several different database engines, DBrbTest maintains an array of instances, each connected to one of the backends. the `test_…` methods iterate over the connections and call the actual test methods, which are named like the `test_…` methods without the `test_` part, with each DBrb instance. E.g. test_create_test_table calls `create_test_table` once with each connection.

Methods

Public Instance methods

Each test case follows the same scheme: the same tests are executed for each database and wrapped in a `assert_nothing_raised` assertion that fails in case of database exceptions.

[Source]

# File tests/DbrbTest.rb, line 45
        def each_ok
                @connections.each {|conn|
                        assert_nothing_raised {
                                yield conn
                        }
                }
        end

[Source]

# File tests/DbrbTest.rb, line 26
        def setup
                @time = Time.new
                @connections = []
                @@credentials.each { |cred|
                        @connections.push DBrb.new(*cred)
                }
        end

[Source]

# File tests/DbrbTest.rb, line 34
        def teardown
                @connections.each {|conn|
                        conn.close
                }
        end

Set up the example table for the tests.

[Source]

# File tests/DbrbTest.rb, line 54
        def test_a_create_test_table
                each_ok { |conn|
                        # This should be as generic as possible to 
                        # accomodate different DB's
                        conn.sql "
                        CREATE TABLE test_table (
                                col_vc              VARCHAR(50),
                                col_num     NUMERIC,
                                col_bool    BOOLEAN,
                                col_double  DOUBLE PRECISION,
                                col_int             INTEGER,
                                col_date    DATE,
                                col_time    TIME,
                                col_timestamp       TIMESTAMP
                        )"
                }
        end

[Source]

# File tests/DbrbTest.rb, line 74
        def test_b_insert_values
                each_ok {|con|
                        %w{zero one two three four five six seven eight
                        nine ten}.each_with_index {|num, i|
                                con.sql("INSERT INTO test_table 
                                        VALUES (?,?,?,?,?,?,?,?)",
                                        num, i, i%2==0,"#{i}.#{i}".to_f,i, @time, @time, @time)
                        }
                }
        end

Date handling seems to be very implementation specific…

[Source]

# File tests/DbrbTest.rb, line 104
        def test_c_select_date
                each_ok { |db|        
                        date = db.sql("SELECT col_date FROM test_table LIMIT 1")
                        
                        assert_equal(@time.year, date.year, db.to_s)
                        assert_equal(@time.mon, date.month, db.to_s)
                        assert_equal(@time.day, date.day, db.to_s)
                        
                        time = db.sql("SELECT col_time FROM test_table LIMIT 1")

                        if time.class != DBI::Time 
                                # postgres driver doesn't return DBI::Time
                                puts "\nWarning, #{db} returning '#{time.class}' instead of DBI::TIME" 
                                assert_equal(@time.strftime("%H:%M:%S"), time, db)
                        else
                                assert_equal(@time.hour, time.hour, db)
                                assert_equal(@time.min, time.min,db)
                                assert_equal(@time.sec, time.sec,db)
                        end
                        
                        timestamp = db.sql("SELECT col_timestamp FROM test_table LIMIT 1")
                        
                        if timestamp.class != DBI::Timestamp
                                # mysql driver doesn't return DBI::Timestamp...
                                puts "\nWarning, #{db} returning '#{timestamp.class}' instead of DBI::TIMESTAMP"
                                assert_equal(@time.strftime("%Y-%m-%d %H:%M:%S"), timestamp, db.to_s)
                        else
                                assert_equal(@time.year, timestamp.year, db.to_s)
                                assert_equal(@time.mon, timestamp.month, db.to_s)
                                assert_equal(@time.day, timestamp.day, db.to_s)
                                assert_equal(@time.hour, timestamp.hour, db.to_s)
                                assert_equal(@time.min, timestamp.min, db.to_s)
                                assert_equal(@time.sec, timestamp.sec, db.to_s)
                        end
                }
        end

[Source]

# File tests/DbrbTest.rb, line 87
        def test_c_select_values 
                each_ok { |db|

                        tst = SelectTest.new db, self
                        tst.do "one",        "SELECT col_vc FROM test_table WHERE col_num=?", 1
                        tst.do "2",  "SELECT col_num FROM test_table WHERE col_vc=?", "two"
                        # postgres: 0 mysql: false
                        #tst.do 0,   "SELECT col_bool FROM test_table WHERE col_vc=?", "three"

                        #postgres 4.4 mysql "4.4"
                        #tst.do 4.4,         "SELECT col_double FROM test_table WHERE col_vc=?", "four"
                        tst.do 5,    "SELECT col_int FROM test_table WHERE col_vc=?", "five"
                        tst.do ["six",6],"SELECT col_vc, col_int FROM test_table WHERE col_double=?", 6.6
                }
        end

[Source]

# File tests/DbrbTest.rb, line 141
        def test_c_select_with_block 
                each_ok { |db|
                        comp_arr = [0,1,2,3,4,5,6,7,8,9,10]
                        
                        arr = []
                        db.sql "SELECT col_int FROM test_table ORDER BY col_int" do |i|
                                arr.push i
                        end
                        assert_equal comp_arr, arr
                                
                        arr=[]

                        db.sql "SELECT col_int, col_date FROM test_table ORDER BY col_int" do |row|
                                arr.push row.col_int
                        end
                        assert_equal comp_arr, arr
                }
        end

[Source]

# File tests/DbrbTest.rb, line 161
        def test_d_update_values
                each_ok do |db|
                        db.sql "UPDATE test_table SET col_int = col_int+1"
                        comp_arr = [1,2,3,4,5,6,7,8,9,10,11]
                        
                        arr = []
                        db.sql "SELECT col_int FROM test_table ORDER BY col_int" do |i|
                                arr.push i
                        end
                        assert_equal comp_arr, arr   
                end
        end

[Source]

# File tests/DbrbTest.rb, line 174
        def test_e_count
                each_ok do |db|
                        c = db.sql_count "UPDATE test_table SET col_int = col_int+2" 
                        assert_equal(11, c, db)

                        c = db.sql_count "SELECT * FROM test_table" do |row|
                                arr=row
                        end
                        
                        assert(c==0||c==11, db)
                end
        end

Drop the test table

[Source]

# File tests/DbrbTest.rb, line 188
        def test_z_drop_test_table
                each_ok {|con|
                        con.sql "DROP TABLE test_table"
                }
        end

[Validate]