File: test_async.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 (121 lines) | stat: -rw-r--r-- 4,131 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class TestPostgresAsync < DBDConfig.testbase(:postgresql)
    def get_dbh
        config = DBDConfig.get_config['postgresql']
        DBI.connect("dbi:Pg:#{config['dbname']}", config['username'], config['password'])
    end

    def get_db_info
        config = DBDConfig.get_config['postgresql']
        dsn  = "dbi:Pg:database=#{config['dbname']}"
        user = config['username']
        pass = config['password']

        return [dsn, user, pass]
    end

    def test_async_default
        dsn, user, pass = get_db_info

        DBI.connect(dsn, user, pass) do |dbh|
            assert_equal false, dbh['pg_async']
            assert_equal false, dbh['NonBlocking']
            dbh.prepare('SELECT 1') do |sth|      # Statement inherits
                assert_equal false, sth['pg_async']
                assert_equal false, sth['NonBlocking']
            end
        end
    end

    def test_async_dsn_enable
        dsn, user, pass = get_db_info

        for enable in ['true', 'TRUE', 'tRuE']
            DBI.connect(dsn + ";pg_async=#{enable}", user, pass) do |dbh|
                assert_equal true, dbh['pg_async']
                assert_equal true, dbh['NonBlocking']
                dbh.prepare('SELECT 1') do |sth|      # Statement inherits
                    assert_equal true, sth['pg_async']
                    assert_equal true, sth['NonBlocking']
                end
            end
        end
    end

    def test_async_attr_enable
        dsn, user, pass = get_db_info

        for enable in ['true', 'TRUE', 'tRuE']
            DBI.connect(dsn, user, pass, { 'pg_async' => enable } ) do |dbh|
                assert_equal true, dbh['pg_async']
                assert_equal true, dbh['NonBlocking']
                dbh.prepare('SELECT 1') do |sth|      # Statement inherits
                    assert_equal true, sth['pg_async']
                    assert_equal true, sth['NonBlocking']
                end
            end
        end
    end

    def test_async_dsn_disable
        dsn, user, pass = get_db_info

        for disable in ['false', 'FALSE', 'fAlSe']
            DBI.connect(dsn + ";pg_async=#{disable}", user, pass) do |dbh|
                assert_equal false, dbh['pg_async']
                assert_equal false, dbh['NonBlocking']
                dbh.prepare('SELECT 1') do |sth|      # Statement inherits
                    assert_equal false, sth['pg_async']
                    assert_equal false, sth['NonBlocking']
                end
            end
        end
    end

    def test_async_attr_disable
        dsn, user, pass = get_db_info

        for disable in ['false', 'FALSE', 'fAlSe']
            DBI.connect(dsn, user, pass, { 'pg_async' => disable }) do |dbh|
                assert_equal false, dbh['pg_async']
                assert_equal false, dbh['NonBlocking']
                dbh.prepare('SELECT 1') do |sth|      # Statement inherits
                    assert_equal false, sth['pg_async']
                    assert_equal false, sth['NonBlocking']
                end
            end
        end
    end

    def test_manual_enable
        dsn, user, pass = get_db_info

        DBI.connect(dsn, user, pass) do |dbh|
            dbh['pg_async'] = true
            assert_equal true, dbh['pg_async']
            assert_equal true, dbh['NonBlocking']
            dbh.prepare('SELECT 1') do |sth|      # Statement inherits
                assert_equal true, sth['pg_async']
                assert_equal true, sth['NonBlocking']
            end
        end
    end

    def test_async_commands
        dsn, user, pass = get_db_info

        DBI.connect(dsn + ";pg_async=true", user, pass) do |dbh|
            assert_equal true, dbh['pg_async']
            assert_equal true, dbh['NonBlocking']
            ret = dbh.select_all('SELECT 1')
            assert_equal [[1]], ret

            ret = dbh.select_all(%q{SELECT 1 WHERE 'foo' = ?}, 'bar')
            assert_equal [], ret

            dbh.prepare(%q{SELECT 1 WHERE 'foo' = ?}) do |sth|
                sth.execute('bar')
                assert_equal [], sth.fetch_all
            end
        end
    end
end