File: test_transactions.rb

package info (click to toggle)
ruby-dbd-pg 0.3.9%2Bgem2deb-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 348 kB
  • ctags: 231
  • sloc: ruby: 2,214; sql: 91; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,658 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class TestPostgresTransaction < DBDConfig.testbase(:postgresql)
    def test_rollback
        dbh = get_dbh
        dbh["AutoCommit"] = false
        @sth = dbh.prepare('insert into names (name, age) values (?, ?)')
        @sth.execute("Foo", 51)
        dbh.rollback
        assert_equal 1, @sth.rows
        @sth.finish


        @sth = dbh.prepare('select name, age from names where name=?')
        @sth.execute("Foo")
        assert !@sth.fetch
        @sth.finish
        dbh.disconnect 
    end

    def test_commit
        dbh = get_dbh
        dbh["AutoCommit"] = false
        @sth = dbh.prepare('insert into names (name, age) values (?, ?)')
        @sth.execute("Foo", 51)
        dbh.commit
        assert_equal 1, @sth.rows
        @sth.finish
        
        @sth = dbh.prepare('select name, age from names where name=?')
        @sth.execute("Foo")
        row = @sth.fetch
        assert row
        assert_equal "Foo", row[0]
        assert_equal 51, row[1]
        @sth.finish
        dbh.disconnect 
    end

    def test_select_transaction
        # per bug #10466
        dbh = get_dbh
        dbh["AutoCommit"] = false
        @sth = dbh.prepare('select * from test_insert(?, ?)');
        @sth.execute("Foo", 51)
        dbh.rollback
        @sth.finish

        @sth = dbh.prepare('select name, age from names where name=?')
        @sth.execute("Foo")
        assert !@sth.fetch
        @sth.finish
        dbh.disconnect 
    end

    def get_dbh
        config = DBDConfig.get_config
        DBI.connect("dbi:Pg:#{config['postgresql']['dbname']}", config['postgresql']['username'], config['postgresql']['password'])
    end
end