File: test_database.rb

package info (click to toggle)
ruby-dbd-mysql 0.4.4%2Bgem2deb-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 292 kB
  • ctags: 159
  • sloc: ruby: 1,403; sql: 47; makefile: 2
file content (206 lines) | stat: -rw-r--r-- 7,282 bytes parent folder | download | duplicates (10)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
@class = Class.new(DBDConfig.testbase(DBDConfig.current_dbtype)) do

    def test_last_statement
        @sth = @dbh.prepare("select * from names")
        @sth.finish
        assert_equal "select * from names", @dbh.last_statement

        @sth = @dbh.execute("select * from names")
        @sth.finish
        assert_equal "select * from names", @dbh.last_statement
         
        @dbh.do("select * from names")
        assert_equal "select * from names", @dbh.last_statement
    end

    def test_empty_query
        ["", " ", "\t"].each do |str|
            [:do, :prepare, :execute, :select_one, :select_all].each do |call|
                assert_raises(DBI::InterfaceError) do
                    @dbh.send(call, str)
                end
            end
        end
    end

    def test_ping
        assert @dbh.ping
        # XXX if it isn't obvious, this should be tested better. Not sure what
        # good behavior is yet.
    end

    def test_columns
        assert_nothing_raised do
            cols = @dbh.columns("precision_test")

            assert(cols)
            assert_kind_of(Array, cols)
            assert_equal(4, cols.length)

            # the first column should always be "text_field" and have the following
            # properties:
            assert_equal("text_field", cols[0]["name"])
            assert(!cols[0]["nullable"])

            assert_equal(20, cols[0]["precision"])
            # scale can be either nil or 0 for character types.
            case cols[0]["scale"]
            when nil
                assert_equal(nil, cols[0]["scale"])
            when 0
                assert_equal(0, cols[0]["scale"])
            else
                flunk "scale can be either 0 or nil for character types"
            end

            assert_equal(
                DBI::Type::Varchar.object_id,
                DBI::TypeUtil.type_name_to_module(cols[0]["type_name"]).object_id
            )

            # the second column should always be "integer_field" and have the following
            # properties:
            assert_equal("integer_field", cols[1]["name"])
            assert(cols[1]["nullable"])
            assert_equal(1, cols[2]["scale"])
            assert_equal(2, cols[2]["precision"])

            assert_equal(
                DBI::Type::Integer.object_id,
                DBI::TypeUtil.type_name_to_module(cols[1]["type_name"]).object_id
            )

            # the second column should always be "integer_field" and have the following
            # properties:
            assert_equal("decimal_field", cols[2]["name"])
            assert(cols[2]["nullable"])
            assert_equal(1, cols[2]["scale"])
            assert_equal(2, cols[2]["precision"])
            assert_equal(
                DBI::Type::Decimal.object_id,
                DBI::TypeUtil.type_name_to_module(cols[2]["type_name"]).object_id
            )

            # the second column should always be "numeric_field" and have the following
            # properties:
            assert_equal("numeric_field", cols[3]["name"])
            assert(cols[3]["nullable"])
            assert_equal(6, cols[3]["scale"])
            assert_equal(30, cols[3]["precision"])
            assert_equal(
                DBI::Type::Decimal.object_id,
                DBI::TypeUtil.type_name_to_module(cols[3]["type_name"]).object_id
            )

            # finally, we ensure that every column in the array is a ColumnInfo
            # object
            cols.each { |col| assert_kind_of(DBI::ColumnInfo, col) }
        end
    end

    def test_prepare
        @sth = @dbh.prepare('select * from names')

        assert @sth
        assert_kind_of DBI::StatementHandle, @sth

        @sth.finish
    end

    def test_do
        assert_equal 1, @dbh.do("insert into names (name, age) values (?, ?)", "Billy", 21)
        @sth = @dbh.prepare("select * from names where name = ?")
        @sth.execute("Billy")
        assert_equal ["Billy", 21], @sth.fetch
        @sth.finish
    end

    def test_tables
        tables = @dbh.tables.sort

        # since this is a general test, let's prune the system tables
        # FIXME not so sure if this should be a general test anymore.
        if dbtype == "odbc"
            tables -= [
                "administrable_role_authorizations",
                "applicable_roles",
                "attributes",
                "check_constraint_routine_usage",
                "check_constraints",
                "column_domain_usage",
                "column_privileges",
                "column_udt_usage",
                "columns",
                "constraint_column_usage",
                "constraint_table_usage",
                "data_type_privileges",
                "domain_constraints",
                "domain_udt_usage",
                "domains",
                "element_types",
                "enabled_roles",
                "information_schema_catalog_name",
                "key_column_usage",
                "parameters",
                "referential_constraints",
                "role_column_grants",
                "role_routine_grants",
                "role_table_grants",
                "role_usage_grants",
                "routine_privileges",
                "routines",
                "schemata",
                "sequences",
                "sql_features",
                "sql_implementation_info",
                "sql_languages",
                "sql_packages",
                "sql_parts",
                "sql_sizing",
                "sql_sizing_profiles",
                "table_constraints",
                "table_privileges",
                "tables",
                "triggered_update_columns",
                "triggers",
                "usage_privileges",
                "view_column_usage",
                "view_routine_usage",
                "view_table_usage",
                "views"
            ]
        end

        case dbtype
        when "postgresql"
            tables.reject! { |x| x =~ /^pg_/ }
            assert_equal %w(array_test bit_test blob_test boolean_test bytea_test db_specific_types_test enum_type_test field_types_test names precision_test time_test timestamp_test view_names), tables
        when 'sqlite3'
            assert_equal %w(bit_test blob_test boolean_test db_specific_types_test field_types_test names names_defined_with_spaces precision_test time_test timestamp_test view_names), tables
        else
            assert_equal %w(bit_test blob_test boolean_test db_specific_types_test field_types_test names precision_test time_test timestamp_test view_names), tables
        end
    end

    def test_attrs
        # test defaults
        assert @dbh["AutoCommit"] # should be true

        # test setting
        assert !(@dbh["AutoCommit"] = false)
        assert !@dbh["AutoCommit"]

        # test committing an outstanding transaction

        @sth = @dbh.prepare("insert into names (name, age) values (?, ?)")
        @sth.execute("Billy", 22)
        @sth.finish

        assert @dbh["AutoCommit"] = true # should commit at this point

        @sth = @dbh.prepare("select * from names where name = ?")
        @sth.execute("Billy")
        assert_equal [ "Billy", 22 ], @sth.fetch
        @sth.finish
    end
end