rdbi 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
1
+ require 'helper'
2
+
3
+ class TestRDBI < Test::Unit::TestCase
4
+ def test_01_connect
5
+ dbh = RDBI.connect(:Mock, :username => :foo, :password => :bar)
6
+ assert(dbh)
7
+ assert_kind_of(RDBI::Database, dbh)
8
+
9
+ dbh = RDBI.connect(RDBI::Driver::Mock, :username => :foo, :password => :bar)
10
+ assert(dbh)
11
+ assert_kind_of(RDBI::Database, dbh)
12
+
13
+ dbh = RDBI.connect("Mock", :username => :foo, :password => :bar)
14
+ assert(dbh)
15
+ assert_kind_of(RDBI::Database, dbh)
16
+
17
+ assert_raise(ArgumentError) { RDBI.connect(1, :user => :blah) }
18
+
19
+ assert_nothing_raised { dbh = RDBI.connect(:Mock) }
20
+ end
21
+
22
+ def test_02_last_dbh
23
+ dbh = mock_connect
24
+
25
+ assert(RDBI.last_dbh)
26
+ assert(dbh.object_id == RDBI.last_dbh.object_id)
27
+ end
28
+
29
+ def test_03_ping
30
+ assert_equal(10, RDBI.ping(:Mock, :some => :arg))
31
+ assert_equal(10, mock_connect.ping)
32
+ end
33
+
34
+ def test_04_connect_cached
35
+ dbh = RDBI.connect_cached(:Mock, :username => :foo)
36
+
37
+ assert(dbh)
38
+ assert_kind_of(RDBI::Database, dbh)
39
+ assert_equal(RDBI::Pool[:default].handles[0], dbh)
40
+
41
+ new_dbh = RDBI.connect_cached(:Mock, :username => :foo)
42
+
43
+ assert(new_dbh)
44
+ assert_kind_of(RDBI::Database, new_dbh)
45
+ assert_equal(RDBI::Pool[:default].handles[1], new_dbh)
46
+ assert_not_equal(dbh, new_dbh)
47
+
48
+ 3.times { RDBI.connect_cached(:Mock, :username => :foo) }
49
+
50
+ assert_equal(dbh.object_id, RDBI.connect_cached(:Mock, :username => :foo).object_id)
51
+ assert_equal(new_dbh.object_id, RDBI.connect_cached(:Mock, :username => :foo).object_id)
52
+
53
+ # different pool
54
+
55
+ pool_dbh = RDBI.connect_cached(:Mock, :username => :foo, :pool_name => :foo)
56
+
57
+ assert_not_equal(dbh, pool_dbh)
58
+ assert_not_equal(new_dbh, pool_dbh)
59
+ assert_equal(RDBI::Pool[:foo].handles[0], pool_dbh)
60
+ end
61
+
62
+ def test_05_pool
63
+ dbh = RDBI.connect_cached(:Mock, :username => :foo, :pool_name => :test_05)
64
+ assert_equal(RDBI.pool(:test_05).handles[0], dbh)
65
+ end
66
+
67
+ def test_06_re_disconnect_all
68
+ RDBI.disconnect_all
69
+ connected_size = RDBI.all_connections.select(&:connected).size
70
+ assert_equal(0, connected_size)
71
+
72
+ total_size = RDBI.all_connections.size
73
+ RDBI.reconnect_all
74
+ connected_size = RDBI.all_connections.select(&:connected).size
75
+ assert_equal(connected_size, total_size)
76
+ end
77
+
78
+ def test_07_all_connections
79
+ total_size = RDBI.all_connections.size
80
+ mock_connect
81
+ assert_equal(RDBI.all_connections.size, total_size + 1)
82
+ end
83
+ end
84
+
85
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
@@ -0,0 +1,269 @@
1
+ require 'helper'
2
+
3
+ class TestResult < Test::Unit::TestCase
4
+ def setup
5
+ @dbh = mock_connect
6
+ end
7
+
8
+ def teardown
9
+ @dbh.disconnect
10
+ end
11
+
12
+ def generate_data
13
+ RDBI::Driver::Mock::Cursor.new((0..9).to_a.map { |x| [x-1, x, x+1] })
14
+ end
15
+
16
+ def mock_result
17
+ names = [:zero, :one, :two]
18
+ res = RDBI::Result.new(@dbh.prepare("foo"), [1], generate_data, RDBI::Schema.new((0..2).to_a.map { |x| RDBI::Column.new(names[x], :integer, :default) }), { :default => RDBI::Type.filterlist() })
19
+ end
20
+
21
+ def mock_empty_result
22
+ names = [:zero, :one, :two]
23
+ res = RDBI::Result.new(@dbh.prepare("foo"), [1], RDBI::Driver::Mock::Cursor.new([]), RDBI::Schema.new((0..2).to_a.map { |x| RDBI::Column.new(names[x], :integer, :default) }), { :default => RDBI::Type.filterlist() })
24
+ end
25
+
26
+ def get_index(res)
27
+ get_guts(get_guts(res)[:data])[:index]
28
+ end
29
+
30
+ def get_guts(res)
31
+ h = { }
32
+ %W[index schema binds sth data].collect(&:to_sym).each do |sym|
33
+ h[sym] = res.instance_variable_get("@#{sym}") || res.instance_variable_get("@#{sym}".to_sym)
34
+ end
35
+
36
+ return h
37
+ end
38
+
39
+ def test_01_init
40
+ res = mock_result
41
+ assert(res)
42
+ assert_kind_of(RDBI::Result, res)
43
+
44
+ guts = get_guts(res)
45
+
46
+ assert_kind_of(RDBI::Statement, guts[:sth])
47
+ assert_kind_of(RDBI::Statement, res.sth)
48
+ assert_equal(res.sth, guts[:sth])
49
+ res.sth.finish
50
+
51
+ assert_kind_of(RDBI::Schema, guts[:schema])
52
+ assert_kind_of(RDBI::Schema, res.schema)
53
+ assert_equal(res.schema, guts[:schema])
54
+
55
+ assert_kind_of(Array, guts[:binds])
56
+ assert_equal(res.binds, guts[:binds])
57
+ assert_equal([1], guts[:binds])
58
+ end
59
+
60
+ def test_02_responds
61
+ res = mock_result
62
+
63
+ %W[
64
+ schema
65
+ sth
66
+ driver
67
+ result_count
68
+ affected_count
69
+ complete
70
+ complete?
71
+ has_data
72
+ has_data?
73
+ each
74
+ inject
75
+ rewind
76
+ as
77
+ fetch
78
+ read
79
+ raw_fetch
80
+ finish
81
+ ].collect(&:to_sym).each do |sym|
82
+ assert_respond_to(res, sym)
83
+ end
84
+
85
+ res.sth.finish
86
+ end
87
+
88
+ def test_03_fetch
89
+ res = mock_result
90
+ assert_equal([[-1,0,1]], res.fetch)
91
+ assert_equal(1, get_index(res))
92
+
93
+ assert_equal(generate_data[1..9], res.fetch(9))
94
+ assert_equal(10, get_index(res))
95
+ assert_equal([], res.fetch)
96
+ res.sth.finish
97
+
98
+ res = mock_result
99
+ assert_equal(generate_data.all, res.fetch(:all))
100
+ res.sth.finish
101
+
102
+ res = mock_result
103
+ res.fetch
104
+ assert_equal(generate_data.all, res.fetch(:all))
105
+ assert_equal(1, get_index(res))
106
+ assert_equal(generate_data[1..9], res.fetch(:rest))
107
+ assert_equal(10, get_index(res))
108
+
109
+ assert_equal(generate_data[0], res.fetch(:first))
110
+ assert_equal(generate_data[-1], res.fetch(:last))
111
+
112
+ res.sth.finish
113
+ end
114
+
115
+ def test_04_finish
116
+ res = mock_result
117
+ res.finish
118
+
119
+ guts = get_guts(res)
120
+
121
+ guts.values.each { |value| assert_nil(value) }
122
+ end
123
+
124
+ def test_05_enumerable_and_index_predicates
125
+ res = mock_result
126
+
127
+ assert(res.has_data?)
128
+ assert(res.has_data)
129
+
130
+ assert(res.complete?)
131
+ assert(res.complete)
132
+
133
+ expected = generate_data
134
+
135
+ res.each_with_index do |x, i|
136
+ assert_equal(expected[i], x)
137
+ end
138
+
139
+ assert(res.complete?)
140
+ assert(res.complete)
141
+ assert(res.has_data?)
142
+ assert(res.has_data)
143
+ res.sth.finish
144
+ end
145
+
146
+ def test_06_as
147
+ res = mock_result
148
+ res.as(RDBI::Result::Driver::CSV)
149
+ assert_equal(
150
+ "-1,0,1\n",
151
+ res.fetch(1)
152
+ )
153
+
154
+ assert_equal(
155
+ "0,1,2\n1,2,3\n2,3,4\n3,4,5\n4,5,6\n5,6,7\n6,7,8\n7,8,9\n8,9,10\n",
156
+ res.fetch(:rest)
157
+ )
158
+
159
+ assert_equal(
160
+ "-1,0,1\n0,1,2\n1,2,3\n2,3,4\n3,4,5\n4,5,6\n5,6,7\n6,7,8\n7,8,9\n8,9,10\n",
161
+ res.fetch(:all)
162
+ )
163
+
164
+ # XXX reset intentionally.
165
+ res.as(:Array)
166
+ assert_equal([[-1, 0, 1]], res.fetch(1))
167
+
168
+ res.sth.finish
169
+ res.rewind
170
+
171
+ assert_equal(
172
+ "-1,0,1\n",
173
+ res.fetch(1, RDBI::Result::Driver::CSV)
174
+ )
175
+
176
+ # XXX this is intentional behavior, because I'm lazy. maybe we'll change it.
177
+ assert_equal(
178
+ "-1,0,1\n0,1,2\n1,2,3\n2,3,4\n3,4,5\n4,5,6\n5,6,7\n6,7,8\n7,8,9\n8,9,10\n",
179
+ res.fetch(:rest, RDBI::Result::Driver::CSV)
180
+ )
181
+
182
+ assert_equal(
183
+ "-1,0,1\n0,1,2\n1,2,3\n2,3,4\n3,4,5\n4,5,6\n5,6,7\n6,7,8\n7,8,9\n8,9,10\n",
184
+ res.fetch(:all, RDBI::Result::Driver::CSV)
185
+ )
186
+ res.sth.finish
187
+ end
188
+
189
+ def test_07_as_struct
190
+ res = mock_result
191
+ res.as(RDBI::Result::Driver::Struct)
192
+
193
+ results = res.fetch(1)
194
+ assert_kind_of(Array, results)
195
+ assert(results[0].kind_of?(::Struct))
196
+
197
+ hash = results[0]
198
+
199
+ assert_raises(NameError) { hash[:test] = "something" }
200
+ assert_raises(NameError) { hash['test'] = "something" }
201
+ assert_raises(NoMethodError) { hash.test = "something" }
202
+
203
+ assert_equal(-1, hash.zero)
204
+ assert_equal(0, hash.one)
205
+ assert_equal(1, hash.two)
206
+
207
+ hash = res.fetch(1)[0]
208
+
209
+ assert_equal(0, hash.zero)
210
+ assert_equal(1, hash.one)
211
+ assert_equal(2, hash.two)
212
+
213
+ hash = res.fetch(:first)
214
+
215
+ assert_equal(-1, hash.zero)
216
+ assert_equal(0, hash.one)
217
+ assert_equal(1, hash.two)
218
+
219
+ res.sth.finish
220
+
221
+ res = mock_empty_result
222
+
223
+ hash = res.fetch(:first, :Struct)
224
+ assert_nil(hash)
225
+ res.sth.finish
226
+ end
227
+
228
+ def test_08_reload
229
+ res = mock_result
230
+
231
+ assert_equal([-1, 0, 1], res.fetch(1)[0])
232
+ assert_equal(10, res.result_count)
233
+ assert_equal([:zero, :one, :two], res.schema.columns.map(&:name))
234
+
235
+ res.reload
236
+
237
+ # this will actually come back from the Mock driver, which will be
238
+ # completely different. not the best test, but it gets the job done.
239
+ assert_equal(%W[10], res.fetch(1)[0])
240
+ assert_equal(5, res.result_count)
241
+ assert_equal((0..9).to_a, res.schema.columns.map(&:name))
242
+ res.finish
243
+ end
244
+
245
+ def test_09_insert_results
246
+ dbh = mock_connect
247
+ sth = dbh.prepare('insert into blah (foo, bar) values (?, ?)')
248
+ sth.result = [[1,2,3], [1,2,3]]
249
+ sth.affected_count = 10
250
+
251
+ res = sth.execute(1,2)
252
+
253
+ assert_equal(16, res.affected_count)
254
+ assert_equal(2, res.result_count)
255
+ sth.finish
256
+ end
257
+
258
+ def test_10_null_results
259
+ res = RDBI::Result.new(@dbh.prepare("foo"), [1], RDBI::Driver::Mock::Cursor.new([]), [], 0)
260
+ assert_equal(nil, res.fetch(:first))
261
+ assert_equal(nil, res.fetch(:last))
262
+ assert_equal([], res.fetch(:all))
263
+ assert_equal([], res.fetch(:rest))
264
+ assert_equal([], res.fetch(1))
265
+ res.sth.finish
266
+ end
267
+ end
268
+
269
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
@@ -0,0 +1,66 @@
1
+ require 'helper'
2
+
3
+ class TestStatement < Test::Unit::TestCase
4
+ attr_accessor :dbh
5
+
6
+ def setup
7
+ @dbh = mock_connect
8
+ end
9
+
10
+ def teardown
11
+ @dbh.disconnect
12
+ end
13
+
14
+ def test_01_allocation
15
+ sth = dbh.new_statement("some query")
16
+ assert(sth)
17
+ assert_kind_of(RDBI::Statement, sth)
18
+ assert_kind_of(RDBI::Database, sth.dbh)
19
+ assert_equal(sth.query, "some query")
20
+ sth.finish
21
+ end
22
+
23
+ def test_02_accessors
24
+ sth = dbh.new_statement("some query")
25
+ assert(sth)
26
+ assert_kind_of(Mutex, sth.mutex)
27
+ assert(!sth.finished?)
28
+ assert(!sth.finished)
29
+ sth.finish
30
+ end
31
+
32
+ def test_03_execute
33
+ res = dbh.execute("select * from foo where bar=?", 1)
34
+ assert_kind_of(RDBI::Result, res)
35
+ assert_equal(%W[10 11 12 13 14].collect { |x| [x] }, res.fetch(:all))
36
+
37
+ sth = dbh.prepare("select * from foo where bar=?")
38
+ assert_kind_of(RDBI::Statement, sth)
39
+ assert_kind_of(RDBI::Driver::Mock::STH, sth)
40
+ assert_respond_to(sth, :new_execution)
41
+ assert_respond_to(sth, :execute)
42
+ res = sth.execute(1)
43
+ assert_equal(%W[10 11 12 13 14].map { |x| [x] }, res.fetch(:all))
44
+ sth.finish
45
+
46
+ sth = dbh.prepare("select * from foo where bar=?bar and foo=?")
47
+ res = sth.execute({:bar => "10"}, "1")
48
+ assert_equal(
49
+ [ { :bar => "10" }, "1" ],
50
+ res.binds
51
+ )
52
+ sth.finish
53
+ end
54
+
55
+ def test_04_finish
56
+ sth = dbh.prepare("select * from foo where bar=?")
57
+
58
+ assert(!sth.finished?)
59
+ sth.finish
60
+ assert(sth.finished?)
61
+ assert_raises(StandardError.new("you may not execute a finished handle")) { sth.execute }
62
+ sth.finish
63
+ end
64
+ end
65
+
66
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
@@ -0,0 +1,114 @@
1
+ require 'helper'
2
+
3
+ class TestTypes < Test::Unit::TestCase
4
+ def setup
5
+ @out_types = RDBI::Type.create_type_hash(RDBI::Type::Out)
6
+ @in_types = RDBI::Type.create_type_hash(RDBI::Type::In)
7
+ end
8
+
9
+ def test_01_basic
10
+ assert_respond_to(RDBI::Type, :create_type_hash)
11
+
12
+ assert(@out_types)
13
+ assert_kind_of(Hash, @out_types)
14
+ assert(@out_types.keys.include?(:integer))
15
+ assert(@out_types.keys.include?(:decimal))
16
+ assert(@out_types.keys.include?(:datetime))
17
+ assert(@out_types.keys.include?(:boolean))
18
+ assert(@out_types.keys.include?(:default))
19
+ assert_respond_to(RDBI::Type::Out, :convert)
20
+
21
+ assert(@in_types)
22
+ assert_kind_of(Hash, @in_types)
23
+ assert(@in_types.keys.include?(Integer))
24
+ assert(@in_types.keys.include?(BigDecimal))
25
+ assert(@in_types.keys.include?(DateTime))
26
+ assert(@in_types.keys.include?(TrueClass))
27
+ assert(@in_types.keys.include?(FalseClass))
28
+ assert(@in_types.keys.include?(:default))
29
+ assert_respond_to(RDBI::Type::In, :convert)
30
+ end
31
+
32
+ def test_02_out_basic_convert
33
+ assert_equal(1, out_convert("1", tcc(:integer), @out_types))
34
+ assert_equal(nil, out_convert(nil, tcc(:integer), @out_types))
35
+
36
+ assert_equal(BigDecimal("1.0"), out_convert("1.0", tcc(:decimal), @out_types))
37
+ assert_equal(nil, out_convert(nil, tcc(:decimal), @out_types))
38
+
39
+ assert_kind_of(DateTime, out_convert(DateTime.now, tcc(:default), @out_types))
40
+ assert_kind_of(Float, out_convert(1.0, tcc(:default), @out_types))
41
+
42
+ assert_equal(true, out_convert("TRUE", tcc(:boolean), @out_types))
43
+ assert_equal(true, out_convert("true", tcc(:boolean), @out_types))
44
+ assert_equal(true, out_convert("TRue", tcc(:boolean), @out_types))
45
+ assert_equal(true, out_convert("trUE", tcc(:boolean), @out_types))
46
+ assert_equal(true, out_convert("T", tcc(:boolean), @out_types))
47
+ assert_equal(true, out_convert("t", tcc(:boolean), @out_types))
48
+ assert_equal(true, out_convert("1", tcc(:boolean), @out_types))
49
+ assert_equal(false, out_convert("FALSE", tcc(:boolean), @out_types))
50
+ assert_equal(false, out_convert("false", tcc(:boolean), @out_types))
51
+ assert_equal(false, out_convert("FAlse", tcc(:boolean), @out_types))
52
+ assert_equal(false, out_convert("faLSE", tcc(:boolean), @out_types))
53
+ assert_equal(false, out_convert("F", tcc(:boolean), @out_types))
54
+ assert_equal(false, out_convert("f", tcc(:boolean), @out_types))
55
+ assert_equal(false, out_convert("0", tcc(:boolean), @out_types))
56
+ assert_equal(nil, out_convert(nil, tcc(:boolean), @out_types))
57
+ end
58
+
59
+ def test_03_out_datetime_convert
60
+ format = RDBI::Type::DEFAULT_STRFTIME_FILTER
61
+ dt = DateTime.now
62
+
63
+ conv = out_convert(dt, tcc(:datetime), @out_types).strftime(format)
64
+ formatted = dt.strftime(format)
65
+
66
+ assert_equal(formatted, conv)
67
+ end
68
+
69
+ def test_04_in_basic_convert
70
+ assert_equal("1", in_convert(1, @in_types))
71
+ assert_equal("1", in_convert(Integer("1"), @in_types))
72
+ assert_equal("1.0", in_convert(1.0, @in_types))
73
+ assert_equal("1.0", in_convert(BigDecimal("1.0"), @in_types))
74
+ assert_equal("artsy", in_convert("artsy", @in_types))
75
+ assert_equal("TRUE", in_convert(true, @in_types))
76
+ assert_equal("FALSE", in_convert(false, @in_types))
77
+ assert_equal(nil, in_convert(nil, @in_types))
78
+ end
79
+
80
+ def test_05_in_datetime_convert
81
+ format = RDBI::Type::DEFAULT_STRFTIME_FILTER
82
+ dt = DateTime.now
83
+
84
+ conv = in_convert(dt, @in_types)
85
+ formatted = dt.strftime(format)
86
+
87
+ assert_equal(formatted, conv)
88
+ end
89
+
90
+ def test_06_out_integration
91
+ dt = DateTime.now
92
+ dbh = mock_connect
93
+ sth = dbh.prepare("select * from foo")
94
+ sth.set_schema = RDBI::Schema.new(
95
+ [
96
+ RDBI::Column.new(:foo, :integer, :integer),
97
+ RDBI::Column.new(:bar, :datetime, :datetime),
98
+ RDBI::Column.new(:quux, :varchar, :string),
99
+ RDBI::Column.new(:baz, :decimal, :decimal)
100
+ ]
101
+ )
102
+
103
+ sth.result = [["1", dt.strftime(RDBI::Type::DEFAULT_STRFTIME_FILTER), "hello, world!", "1.0"]]
104
+
105
+ res = sth.execute().fetch[0]
106
+
107
+ # we can't test datetime objects directly... because datetime is fail.
108
+ # so, what we do here is compare each result individually.
109
+ assert_equal(1, res[0])
110
+ assert_equal(dt.strftime(RDBI::Type::DEFAULT_STRFTIME_FILTER), res[1].strftime(RDBI::Type::DEFAULT_STRFTIME_FILTER))
111
+ assert_equal("hello, world!", res[2])
112
+ assert_equal(BigDecimal("1.0"), res[3])
113
+ end
114
+ end