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,94 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rdbi}
8
+ s.version = "0.9.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Erik Hollensbe"]
12
+ s.date = %q{2010-08-21}
13
+ s.description = %q{RDBI is a rearchitecture of the Ruby/DBI project by its maintainer and others. It intends to fully supplant Ruby/DBI in the future for similar database access needs.}
14
+ s.email = %q{erik@hollensbe.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "docs/external-api.pdf",
27
+ "docs/external-api.texi",
28
+ "lib/rdbi.rb",
29
+ "lib/rdbi/cursor.rb",
30
+ "lib/rdbi/database.rb",
31
+ "lib/rdbi/driver.rb",
32
+ "lib/rdbi/pool.rb",
33
+ "lib/rdbi/result.rb",
34
+ "lib/rdbi/schema.rb",
35
+ "lib/rdbi/statement.rb",
36
+ "lib/rdbi/types.rb",
37
+ "rdbi.gemspec",
38
+ "test/helper.rb",
39
+ "test/test_database.rb",
40
+ "test/test_pool.rb",
41
+ "test/test_rdbi.rb",
42
+ "test/test_result.rb",
43
+ "test/test_statement.rb",
44
+ "test/test_types.rb",
45
+ "test/test_util.rb"
46
+ ]
47
+ s.homepage = %q{http://github.com/RDBI/rdbi}
48
+ s.rdoc_options = ["--charset=UTF-8"]
49
+ s.require_paths = ["lib"]
50
+ s.rubygems_version = %q{1.3.7}
51
+ s.summary = %q{RDBI provides sane query-level database access with low magic.}
52
+ s.test_files = [
53
+ "test/helper.rb",
54
+ "test/test_database.rb",
55
+ "test/test_pool.rb",
56
+ "test/test_rdbi.rb",
57
+ "test/test_result.rb",
58
+ "test/test_statement.rb",
59
+ "test/test_types.rb",
60
+ "test/test_util.rb"
61
+ ]
62
+
63
+ if s.respond_to? :specification_version then
64
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
65
+ s.specification_version = 3
66
+
67
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
68
+ s.add_development_dependency(%q<rdbi-driver-mock>, [">= 0"])
69
+ s.add_development_dependency(%q<test-unit>, [">= 0"])
70
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
71
+ s.add_development_dependency(%q<fastercsv>, [">= 0"])
72
+ s.add_runtime_dependency(%q<methlab>, [">= 0.0.9"])
73
+ s.add_runtime_dependency(%q<epoxy>, [">= 0.3.1"])
74
+ s.add_runtime_dependency(%q<typelib>, [">= 0"])
75
+ else
76
+ s.add_dependency(%q<rdbi-driver-mock>, [">= 0"])
77
+ s.add_dependency(%q<test-unit>, [">= 0"])
78
+ s.add_dependency(%q<rdoc>, [">= 0"])
79
+ s.add_dependency(%q<fastercsv>, [">= 0"])
80
+ s.add_dependency(%q<methlab>, [">= 0.0.9"])
81
+ s.add_dependency(%q<epoxy>, [">= 0.3.1"])
82
+ s.add_dependency(%q<typelib>, [">= 0"])
83
+ end
84
+ else
85
+ s.add_dependency(%q<rdbi-driver-mock>, [">= 0"])
86
+ s.add_dependency(%q<test-unit>, [">= 0"])
87
+ s.add_dependency(%q<rdoc>, [">= 0"])
88
+ s.add_dependency(%q<fastercsv>, [">= 0"])
89
+ s.add_dependency(%q<methlab>, [">= 0.0.9"])
90
+ s.add_dependency(%q<epoxy>, [">= 0.3.1"])
91
+ s.add_dependency(%q<typelib>, [">= 0"])
92
+ end
93
+ end
94
+
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ gem 'rdbi-driver-mock'
3
+ gem 'test-unit'
4
+ require 'test/unit'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ require 'rdbi'
9
+ require 'rdbi/driver/mock'
10
+
11
+ class Test::Unit::TestCase
12
+ def mock_connect
13
+ RDBI.connect(:Mock, :username => 'foo', :password => 'bar')
14
+ end
15
+
16
+ # type conversion column
17
+ def tcc(type)
18
+ col = RDBI::Column.new
19
+ col.ruby_type = type
20
+ col
21
+ end
22
+
23
+ def out_convert(*args)
24
+ RDBI::Type::Out.convert(*args)
25
+ end
26
+
27
+ def in_convert(*args)
28
+ RDBI::Type::In.convert(*args)
29
+ end
30
+ end
31
+
32
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
@@ -0,0 +1,246 @@
1
+ require 'helper'
2
+
3
+ class TestDatabase < Test::Unit::TestCase
4
+ def setup
5
+ @dbh = mock_connect
6
+ end
7
+
8
+ def assert_transaction(count)
9
+ in_transaction = @dbh.instance_variable_get("@in_transaction") ||
10
+ @dbh.instance_variable_get(:@in_transaction)
11
+ assert_equal(count, in_transaction)
12
+
13
+ if in_transaction > 0
14
+ assert(@dbh.in_transaction?)
15
+ end
16
+ end
17
+
18
+ def test_01_ping
19
+ assert_equal(10, @dbh.ping)
20
+ end
21
+
22
+ def test_02_transaction
23
+ # commit works
24
+
25
+ @dbh.next_action = proc do |*args|
26
+ return true
27
+ end
28
+
29
+ res = @dbh.transaction do |dbh|
30
+ assert_transaction(1)
31
+ end
32
+
33
+ assert_transaction(0)
34
+ assert_equal(true, res)
35
+
36
+ # rollback works when commit fails
37
+ @dbh.next_action = proc do |*args|
38
+ raise StandardError, "should call rollback"
39
+ end
40
+
41
+ assert_raises(StandardError.new("should call rollback")) do
42
+ @dbh.transaction do |dbh|
43
+ assert_transaction(1)
44
+ true
45
+ end
46
+ end
47
+
48
+ assert_transaction(0)
49
+ assert_equal(true, res)
50
+
51
+ # rollback works when transaction fails
52
+
53
+ @dbh.next_action = proc { |*args| true }
54
+
55
+ assert_raises(StandardError.new("should call rollback")) do
56
+ @dbh.transaction do |dbh|
57
+ assert_transaction(1)
58
+
59
+ raise StandardError, "should call rollback"
60
+ nil
61
+ end
62
+ end
63
+
64
+ assert_transaction(0)
65
+ assert_equal(true, res)
66
+
67
+ # commit called within transaction
68
+
69
+ @dbh.next_action = proc { |*args| @dbh.next_action = proc { raise "shit" }; "commit called" }
70
+
71
+ res = @dbh.transaction do |dbh|
72
+ assert_transaction(1)
73
+
74
+ dbh.commit
75
+
76
+ assert_transaction(0)
77
+ true
78
+ end
79
+
80
+ assert_not_equal("rollback called", res)
81
+ assert_not_equal("commit called", res)
82
+
83
+ # rollback called within transaction
84
+
85
+ @dbh.next_action = proc { |*args| @dbh.next_action = proc { raise "shit" }; "commit called" }
86
+
87
+ res = @dbh.transaction do |dbh|
88
+ assert_transaction(1)
89
+
90
+ dbh.rollback
91
+
92
+ assert_transaction(0)
93
+ end
94
+
95
+ assert_not_equal("rollback called", res)
96
+ assert_not_equal("commit called", res)
97
+ end
98
+
99
+ def test_03_last_query
100
+ @dbh.prepare("here's the last query #1").finish
101
+ assert_equal("here's the last query #1", @dbh.last_query)
102
+
103
+ @dbh.prepare("here's the last query #2").finish
104
+ assert_equal("here's the last query #2", @dbh.last_query)
105
+
106
+ @dbh.execute("here's the last query #3").finish
107
+ assert_equal("here's the last query #3", @dbh.last_query)
108
+
109
+ @dbh.execute("here's the last query #4").finish
110
+ assert_equal("here's the last query #4", @dbh.last_query)
111
+
112
+ @dbh.preprocess_query("here's the last query #5")
113
+ assert_equal("here's the last query #5", @dbh.last_query)
114
+
115
+ @dbh.preprocess_query("here's the last query #6")
116
+ assert_equal("here's the last query #6", @dbh.last_query)
117
+ end
118
+
119
+ def test_04_preprocess_query
120
+ query = @dbh.preprocess_query("select * from foo where bind=? and bind2=?", "foo", "bar")
121
+ assert_equal("select * from foo where bind='foo' and bind2='bar'", query)
122
+
123
+ query = @dbh.preprocess_query("select * from foo where bind=? and bind2=?", "fo'o", "ba''r")
124
+ assert_equal("select * from foo where bind='fo''o' and bind2='ba''''r'", query)
125
+
126
+ query = @dbh.preprocess_query("select * from foo where bind=?foo and bind2=?bar", { :foo => "fo'o", :bar => "ba''r" })
127
+ assert_equal("select * from foo where bind='fo''o' and bind2='ba''''r'", query)
128
+
129
+ query = @dbh.preprocess_query("select * from foo where bind=?foo and bind2=?bar", { :foo => "fo'o"}, { :bar => "ba''r" })
130
+ assert_equal("select * from foo where bind='fo''o' and bind2='ba''''r'", query)
131
+
132
+ query = @dbh.preprocess_query("select * from foo where bind=?foo and bind2=?bar and bind3=?", { :foo => "fo'o"}, { :bar => "ba''r" }, "quux")
133
+ assert_equal("select * from foo where bind='fo''o' and bind2='ba''''r' and bind3='quux'", query)
134
+ end
135
+
136
+ def test_05_prepare_execute
137
+ sth = @dbh.prepare("some statement")
138
+ assert(sth)
139
+ assert_kind_of(RDBI::Statement, sth)
140
+
141
+ res = @dbh.execute("some other statement")
142
+ assert(res)
143
+ assert_kind_of(RDBI::Result, res)
144
+ sth.finish
145
+ end
146
+
147
+ def test_06_last_statement
148
+ sth = @dbh.prepare("some statement")
149
+ assert(sth)
150
+ assert_kind_of(RDBI::Statement, sth)
151
+
152
+ assert_equal(@dbh.last_statement.object_id, sth.object_id)
153
+
154
+ res = @dbh.execute("some other statement")
155
+ assert(res)
156
+ assert_kind_of(RDBI::Result, res)
157
+ assert_not_equal(@dbh.last_statement.object_id, sth.object_id)
158
+ sth.finish
159
+ end
160
+
161
+ def test_07_nested_transactions
162
+ @dbh.transaction do
163
+ @dbh.transaction do
164
+ assert_transaction(2)
165
+ @dbh.commit
166
+ assert_transaction(1)
167
+ # XXX should this be how it works?
168
+ @dbh.commit
169
+ assert_transaction(0)
170
+ end
171
+ end
172
+
173
+ @dbh.transaction do
174
+ @dbh.transaction do
175
+ assert_transaction(2)
176
+ @dbh.commit
177
+ assert_transaction(1)
178
+ end
179
+ @dbh.commit
180
+ assert_transaction(0)
181
+ end
182
+
183
+ @dbh.transaction do
184
+ @dbh.transaction do
185
+ assert_transaction(2)
186
+ @dbh.rollback
187
+ assert_transaction(1)
188
+ # XXX should this be how it works?
189
+ @dbh.rollback
190
+ assert_transaction(0)
191
+ end
192
+ end
193
+
194
+ @dbh.transaction do
195
+ @dbh.transaction do
196
+ assert_transaction(2)
197
+ @dbh.rollback
198
+ assert_transaction(1)
199
+ end
200
+ @dbh.rollback
201
+ assert_transaction(0)
202
+ end
203
+ end
204
+
205
+ def test_08_block_form
206
+ my_sth = nil
207
+ my_res = nil
208
+
209
+ @dbh.prepare("some statement") do |sth|
210
+ assert(sth)
211
+ assert_respond_to(sth, :execute)
212
+ res = sth.execute
213
+ assert(res)
214
+ my_sth = sth
215
+ end
216
+
217
+ assert(my_sth.finished?)
218
+
219
+ @dbh.execute("some statement") do |res|
220
+ assert(res)
221
+ assert_kind_of(RDBI::Result, res)
222
+ end
223
+ end
224
+
225
+ def test_09_statement_allocation
226
+ sth = @dbh.prepare("some statement")
227
+ assert(sth)
228
+
229
+ assert_equal(@dbh.open_statements.length, 1)
230
+
231
+ warn "The next message should appear *exactly once*"
232
+ @dbh.disconnect
233
+
234
+ @dbh = mock_connect
235
+ sth = @dbh.prepare("some statement")
236
+ sth.finish
237
+ assert_equal(@dbh.open_statements.length, 0)
238
+ @dbh.disconnect
239
+ end
240
+
241
+ def teardown
242
+ @dbh.disconnect
243
+ end
244
+ end
245
+
246
+ # vim: syntax=ruby ts=2 et sw=2 sts=2
@@ -0,0 +1,181 @@
1
+ require 'helper'
2
+
3
+ class TestPool < Test::Unit::TestCase
4
+ def create_pool(name=:default)
5
+ pool = RDBI::Pool.new(name, [:Mock, {:username => :foo}])
6
+ end
7
+
8
+ def test_01_construction
9
+ pool = create_pool(:test_01)
10
+ assert(pool)
11
+ assert_kind_of(RDBI::Pool, pool)
12
+ end
13
+
14
+ def test_02_class_accessors
15
+ assert(!RDBI::Pool[:test_02])
16
+ create_pool(:test_02)
17
+
18
+ assert(RDBI::Pool[:test_02])
19
+ end
20
+
21
+ def test_03_pooling!
22
+ pool = create_pool(:test_03)
23
+ assert_raise(ArgumentError.new("too many handles in this pool (max: 5)")) do
24
+ 6.times do
25
+ RDBI::Pool[:test_03].add_connection
26
+ end
27
+ end
28
+
29
+ assert_equal(0, pool.last_index)
30
+ dbh = pool.get_dbh
31
+ assert(dbh)
32
+ assert_kind_of(RDBI::Database, dbh)
33
+ assert(dbh.connected?)
34
+
35
+ pool.disconnect
36
+
37
+ assert_equal(1, pool.last_index)
38
+ dbh = pool.get_dbh
39
+ assert(dbh)
40
+ assert_kind_of(RDBI::Database, dbh)
41
+ assert(dbh.connected?)
42
+
43
+ # XXX HACK for testing
44
+ pool.instance_variable_set(:@last_index, 5)
45
+ dbh = pool.get_dbh
46
+ assert(dbh)
47
+ assert_kind_of(RDBI::Database, dbh)
48
+ assert(dbh.connected?)
49
+ assert_equal(1, pool.last_index)
50
+
51
+ pool = create_pool(:test_03_2)
52
+ 5.times do
53
+ dbh = pool.get_dbh
54
+ assert(dbh.connected?)
55
+ end
56
+ end
57
+
58
+ def test_04_remove
59
+ pool = create_pool(:test_04).add_connection
60
+ dbh = pool.get_dbh
61
+
62
+ assert(pool.handles.map(&:object_id).include?(dbh.object_id))
63
+ pool.remove(dbh)
64
+ assert(!pool.handles.map(&:object_id).include?(dbh.object_id))
65
+ end
66
+
67
+ def test_05_dis_reconnect
68
+ pool = create_pool(:test_05)
69
+ 4.times { pool.add_connection }
70
+
71
+ pool.disconnect
72
+
73
+ pool.handles.each do |dbh|
74
+ assert(!dbh.connected?)
75
+ end
76
+
77
+ pool.reconnect
78
+
79
+ pool.handles.each do |dbh|
80
+ assert(dbh.connected?)
81
+ end
82
+ end
83
+
84
+ def test_06_ping
85
+ pool = create_pool(:test_06)
86
+ 4.times { pool.add_connection }
87
+ assert_equal(10, pool.ping)
88
+ end
89
+
90
+ def test_07_resize
91
+ pool = create_pool(:test_07).add_connection
92
+ 4.times { pool.add_connection }
93
+
94
+ handles = pool.resize(2)
95
+
96
+ assert_equal(2, pool.max)
97
+
98
+ handles.each do |dbh|
99
+ assert(!pool.handles(&:object_id).include?(dbh.object_id))
100
+ end
101
+
102
+ assert_equal(2, pool.handles.size)
103
+
104
+ assert_equal(0, pool.last_index)
105
+
106
+ 3.times do
107
+ pool.get_dbh
108
+ end
109
+
110
+ assert_equal(1, pool.last_index)
111
+
112
+ pool = create_pool(:test_07_2)
113
+ pool.add_connection
114
+ handles = pool.resize(2)
115
+ assert_equal([], handles)
116
+
117
+ # check the ability to cull disconnected objects automatically while
118
+ # preferring connected ones.
119
+ pool = create_pool(:test_07_3)
120
+ 5.times { pool.add_connection }
121
+ dbh = pool.get_dbh
122
+ pool.disconnect
123
+ dbh.reconnect
124
+ handles = pool.resize(2)
125
+ assert_equal(2, pool.handles.size)
126
+ assert_equal(3, handles.size)
127
+ assert(!handles.map(&:object_id).include?(dbh.object_id))
128
+ end
129
+
130
+ def test_08_enumerable
131
+ assert_equal(
132
+ [
133
+ :test_01,
134
+ :test_02,
135
+ :test_03,
136
+ :test_03_2,
137
+ :test_04,
138
+ :test_05,
139
+ :test_06,
140
+ :test_07,
141
+ :test_07_2,
142
+ :test_07_3
143
+ ],
144
+ RDBI::Pool.keys.map(&:to_s).sort.map(&:to_sym)
145
+ )
146
+
147
+ assert_equal(10, RDBI::Pool.values.count)
148
+ assert_kind_of(RDBI::Pool, RDBI::Pool.values[0])
149
+
150
+ count = 0
151
+ RDBI::Pool.each do |name, pool|
152
+ count += 1
153
+ assert_kind_of(Symbol, name)
154
+ assert_kind_of(RDBI::Pool, pool)
155
+ end
156
+
157
+ assert(10, count)
158
+ assert_respond_to(RDBI::Pool, :map)
159
+
160
+ pool = create_pool(:test_08)
161
+
162
+ assert(pool)
163
+ assert_kind_of(RDBI::Pool, pool)
164
+ assert_respond_to(pool, :each)
165
+
166
+ count = 0
167
+
168
+ 3.times { pool.add_connection }
169
+
170
+ pool.each do |dbh|
171
+ count += 1
172
+ assert_kind_of(RDBI::Database, dbh)
173
+ end
174
+
175
+ assert_equal(3, count)
176
+
177
+ assert_respond_to(pool, :map)
178
+ end
179
+ end
180
+
181
+ # vim: syntax=ruby ts=2 et sw=2 sts=2