File: test_types.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 (281 lines) | stat: -rw-r--r-- 8,597 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@class = Class.new(DBDConfig.testbase(DBDConfig.current_dbtype)) do
    def skip_bit
        # FIXME this test fails because DBI's type system blows goats.
       @sth = nil

        assert_nothing_raised do
            @sth = @dbh.prepare("insert into bit_test (mybit) values (?)")
            @sth.bind_param(1, 0, DBI::SQL_TINYINT)
            @sth.execute
#             if dbtype == "postgresql"
#                 @sth.execute("0")
#             else
#                 @sth.execute(0)
#             end
            @sth.finish
        end

        assert_nothing_raised do
            @sth = @dbh.prepare("select * from bit_test")
            @sth.execute
            row = @sth.fetch
            @sth.finish

            assert_equal [0], row
        end
    end

    def test_numeric_types
        assert(@dbh.convert_types)

        @sth = @dbh.prepare("insert into precision_test (text_field, integer_field, decimal_field, numeric_field) values (?, ?, ?, ?)")
        assert(@sth.convert_types)
        1.step(5) do |x|
            @sth.execute("poop#{x}", x, x + 0.123, x + 0.234)
        end

        @sth.finish

        @sth = @dbh.prepare("select integer_field, decimal_field, numeric_field from precision_test")
        @sth.execute
        col_info = @sth.column_info
        1.step(5) do |x|
            row = @sth.fetch

            assert_kind_of(Integer, row[0])
            assert_kind_of(BigDecimal, row[1])
            assert_kind_of(BigDecimal, row[2])

            # FIXME BigDecimal requires a string and some databases will pad
            # decimal/numeric with constrained precision. We should account for
            # this, but I'm not quite sure how yet.
        end
        @sth.finish
    end

    # FIXME
    # Ideally, this test should be split across the DBI tests and DBD, but for
    # now testing against the DBDs really doesn't cost us anything other than
    # debugging time if something breaks.
    def test_bind_coltype
        # ensure type conv didn't get turned off somewhere.
        assert(DBI.convert_types)
        assert(@dbh.convert_types)

        assert_nothing_raised do
            @sth = @dbh.prepare("select name, age from names order by age")
            assert(@sth.convert_types) # again
            @sth.execute
            @sth.bind_coltype(2, DBI::Type::Varchar)
            assert_equal(
                [
                    ["Joe", "19"], 
                    ["Bob", "21"],
                    ["Jim", "30"], 
                ], @sth.fetch_all
            )
            @sth.finish
        end

        # just to be sure..
        assert_nothing_raised do
            @sth = @dbh.prepare("select name, age from names order by age")
            @sth.execute
            @sth.bind_coltype(2, DBI::Type::Float)
            @sth.fetch_all.collect { |x| assert_kind_of(Float, x[1]) }
            @sth.finish
        end

        # now, let's check some failure cases
        @sth = @dbh.prepare("select name, age from names order by age")

        # can't bind_coltype before execute
        assert_raises(DBI::InterfaceError) { @sth.bind_coltype(1, DBI::Type::Float) }
        # can't index < 1
        assert_raises(DBI::InterfaceError) { @sth.bind_coltype(0, DBI::Type::Float) }
    end

    def test_noconv
        # XXX this test will fail the whole test suite miserably if it fails at any point.
        assert(DBI.convert_types)

        DBI.convert_types = false
        @sth.finish rescue nil
        @dbh.disconnect
        set_base_dbh

        assert(!@dbh.convert_types)

        assert_nothing_raised do
            @sth = @dbh.prepare("select * from names order by age")
            assert(!@sth.convert_types)
            @sth.execute
            assert_equal(
                [
                    ["Joe", "19"], 
                    ["Bob", "21"],
                    ["Jim", "30"], 
                ], @sth.fetch_all
            )
            @sth.finish
        end

        DBI.convert_types = true
        @sth.finish rescue nil
        @dbh.disconnect
        set_base_dbh

        assert(DBI.convert_types)
        assert(@dbh.convert_types)

        assert_nothing_raised do
            @sth = @dbh.prepare("select * from names order by age")
            assert(@sth.convert_types)
            @sth.execute
            assert_equal(
                [
                    ["Joe", 19], 
                    ["Bob", 21],
                    ["Jim", 30], 
                ], @sth.fetch_all
            )
            @sth.finish
        end

        @dbh.convert_types = false

        assert_nothing_raised do
            @sth = @dbh.prepare("select * from names order by age")
            assert(!@sth.convert_types)
            @sth.execute
            assert_equal(
                [
                    ["Joe", "19"], 
                    ["Bob", "21"],
                    ["Jim", "30"], 
                ], @sth.fetch_all
            )
            @sth.finish
        end

        @dbh.convert_types = true

        assert_nothing_raised do
            @sth = @dbh.prepare("select * from names order by age")
            assert(@sth.convert_types)
            @sth.convert_types = false
            @sth.execute
            assert_equal(
                [
                    ["Joe", "19"], 
                    ["Bob", "21"],
                    ["Jim", "30"], 
                ], @sth.fetch_all
            )
            @sth.finish
        end
    rescue Exception => e
        DBI.convert_types = true
        @sth.finish
        @dbh.disconnect
        set_base_dbh
        raise e
    end

    def test_null
        assert_nothing_raised do
            @sth = @dbh.prepare('insert into names (name, age) values (?, ?)')
            @sth.execute("'NULL'", 201)
            @sth.execute(nil, 202)
            @sth.execute("NULL", 203)
            @sth.finish
        end

        assert_nothing_raised do
            @sth = @dbh.prepare('select * from names where age > 200 order by age')
            @sth.execute
            assert_equal(["'NULL'", 201], @sth.fetch)
            assert_equal([nil, 202], @sth.fetch)
            assert_equal(["NULL", 203], @sth.fetch)
            @sth.finish
        end
    end

    def test_time
        @sth = nil
        t = nil
        assert_nothing_raised do
            @sth = @dbh.prepare("insert into time_test (mytime) values (?)")
            t = Time.now
            @sth.execute(t)
            @sth.finish
        end

        assert_nothing_raised do
            @sth = @dbh.prepare("select * from time_test")
            @sth.execute
            row = @sth.fetch
            assert_kind_of DateTime, row[0]
            assert_equal t.hour, row[0].hour
            assert_equal t.min, row[0].min
            assert_equal t.sec, row[0].sec
            @sth.finish
        end
    end

    def test_timestamp
        @sth = nil
         # We omit fractional second testing here -- timestamp precision
         # is a very slippery, dependent on driver and driver version.
        t = DBI::Timestamp.new(2008, 3, 8, 10, 39, 1)
        assert_nothing_raised do
            @sth = @dbh.prepare("insert into timestamp_test (mytimestamp) values (?)")
            @sth.execute(t)
            @sth.finish
        end

        assert_nothing_raised do
            @sth = @dbh.prepare("select * from timestamp_test")
            @sth.execute
            row = @sth.fetch
            assert_kind_of DateTime, row[0]
            assert_equal t.year, row[0].year
            assert_equal t.month, row[0].month
            assert_equal t.day, row[0].day
            assert_equal t.hour, row[0].hour
            assert_equal t.min, row[0].min
            assert_equal t.sec, row[0].sec
             # omit fractional tests
            @sth.finish
        end
    end

    def test_boolean_return
        @sth = nil

        unless dbtype == "odbc" # ODBC has no boolean type
            assert_nothing_raised do
                @sth = @dbh.prepare("insert into boolean_test (num, mybool) values (?, ?)")
                @sth.execute(1, true)
                @sth.execute(2, false)
                @sth.finish
            end

            assert_nothing_raised do
                @sth = @dbh.prepare("select * from boolean_test order by num")
                @sth.execute

                pairs = @sth.fetch_all

                assert_equal(
                    [
                                 [1, true],
                                 [2, false],
                    ], pairs
                )

                @sth.finish
            end
        end
    end
end