-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathtest_integration_resultset.rb
More file actions
173 lines (149 loc) · 4.53 KB
/
Copy pathtest_integration_resultset.rb
File metadata and controls
173 lines (149 loc) · 4.53 KB
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
require "helper"
class IntegrationResultSetTestCase < SQLite3::TestCase
def setup
@db = SQLite3::Database.new(":memory:")
@db.transaction do
@db.execute "create table foo ( a integer primary key, b text )"
@db.execute "insert into foo ( b ) values ( 'foo' )"
@db.execute "insert into foo ( b ) values ( 'bar' )"
@db.execute "insert into foo ( b ) values ( 'baz' )"
end
@stmt = @db.prepare("select * from foo where a in ( ?, ? )")
@result = @stmt.execute
end
def teardown
@stmt.close
@db.close
end
def test_column_names_should_be_frozen
assert @stmt.columns.all?(&:frozen?)
end
def test_reset_unused
assert_nothing_raised { @result.reset }
assert_empty @result.to_a
end
def test_reset_used
@result.to_a
assert_nothing_raised { @result.reset }
assert_empty @result.to_a
end
def test_reset_with_bind
@result.to_a
assert_nothing_raised { @result.reset(1, 2) }
assert_equal 2, @result.to_a.length
end
def test_eof_inner
@result.reset(1)
refute_predicate @result, :eof?
end
def test_eof_edge
@result.reset(1)
@result.next # to first row
@result.next # to end of result set
assert_predicate @result, :eof?
end
def test_next_eof
@result.reset(1)
assert_not_nil @result.next
assert_nil @result.next
end
def test_next_no_type_translation_no_hash
@result.reset(1)
assert_equal [1, "foo"], @result.next
end
def test_next_type_translation
@result.reset(1)
assert_equal [1, "foo"], @result.next
end
def test_next_type_translation_with_untyped_column
@db.query("select count(*) from foo") do |result|
assert_equal [3], result.next
end
end
def test_type_translation_with_null_column
time = "1974-07-25 14:39:00"
@db.execute "create table bar ( a integer, b time, c string )"
@db.execute "insert into bar (a, b, c) values (NULL, '#{time}', 'hello')"
@db.execute "insert into bar (a, b, c) values (1, NULL, 'hello')"
@db.execute "insert into bar (a, b, c) values (2, '#{time}', NULL)"
@db.query("select * from bar") do |result|
assert_equal [nil, time, "hello"], result.next
assert_equal [1, nil, "hello"], result.next
assert_equal [2, time, nil], result.next
end
end
def test_real_translation
@db.execute("create table foo_real(a real)")
@db.execute("insert into foo_real values (42)")
@db.query("select a, sum(a), typeof(a), typeof(sum(a)) from foo_real") do |result|
result = result.next
assert_kind_of Float, result[0]
assert_kind_of Float, result[1]
assert_kind_of String, result[2]
assert_kind_of String, result[3]
end
end
def test_next_results_as_hash
@db.results_as_hash = true
@result = @stmt.execute
@result.reset(1)
hash = @result.next
assert_equal({"a" => 1, "b" => "foo"},
hash)
assert_equal 1, hash[@result.columns[0]]
assert_equal "foo", hash[@result.columns[1]]
end
def test_each
called = 0
@result.reset(1, 2)
result = @result.each { |row| called += 1 }
result.reset # reset just to confirm we can chain the method after each
assert_equal @result, result
assert_equal 2, called
end
def test_each_enum
@result.reset(1, 2)
enum = @result.each
assert_instance_of Enumerator, enum
assert_equal 2, enum.to_a.length
end
def test_each_hash
called = 0
@result.reset(1, 2)
result = @result.each_hash { |row| called += 1 }
result.reset
assert_equal @result, result
assert_equal 2, called
end
def test_each_hash_enum
@result.reset(1, 2)
enum = @result.each_hash
assert_instance_of Enumerator, enum
assert_equal 2, enum.to_a.length
end
def test_enumerable
@result.reset(1, 2)
assert_equal 2, @result.to_a.length
end
def test_types
assert_equal ["integer", "text"], @result.types
end
def test_columns
assert_equal ["a", "b"], @result.columns
end
def test_close
stmt = @db.prepare("select * from foo")
result = stmt.execute
refute_predicate result, :closed?
result.close
assert_predicate result, :closed?
assert_predicate stmt, :closed?
assert_raise(SQLite3::Exception) { result.reset }
assert_raise(SQLite3::Exception) { result.next }
assert_raise(SQLite3::Exception) { result.each.next }
assert_raise(SQLite3::Exception) { result.each_hash.next }
assert_raise(SQLite3::Exception) { result.close }
assert_raise(SQLite3::Exception) { result.types }
assert_raise(SQLite3::Exception) { result.columns }
end
end