Skip to content

Commit bc5097c

Browse files
committed
Use query options on prepare through the statement specs
1 parent 054c155 commit bc5097c

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

spec/mysql2/statement_spec.rb

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@
145145
context "streaming result" do
146146
it "should be able to stream query result" do
147147
n = 1
148-
stmt = @client.prepare("SELECT 1 UNION SELECT 2")
149-
stmt.query_options.merge!({:stream => true, :cache_rows => false, :as => :array})
150-
148+
stmt = @client.prepare("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false, :as => :array)
151149
stmt.execute.each do |r|
152150
case n
153151
when 1
@@ -167,67 +165,55 @@
167165
# The drawback of this is that args of Result#each is ignored...
168166

169167
it "should yield rows as hash's" do
170-
@result = @client.prepare("SELECT 1").execute
171-
@result.each do |row|
168+
result = @client.prepare("SELECT 1").execute
169+
result.each do |row|
172170
expect(row.class).to eql(Hash)
173171
end
174172
end
175173

176174
it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
177-
@client.query_options[:symbolize_keys] = true
178-
@result = @client.prepare("SELECT 1").execute
179-
@result.each do |row|
175+
result = @client.prepare("SELECT 1", :symbolize_keys => true).execute
176+
result.each do |row|
180177
expect(row.keys.first.class).to eql(Symbol)
181178
end
182-
@client.query_options[:symbolize_keys] = false
183179
end
184180

185-
it "should be able to return results as an array" do
186-
@client.query_options[:as] = :array
181+
it "should be able to return results as a hash" do
182+
result = @client.prepare("SELECT 1", :as => :hash).execute
183+
result.each do |row|
184+
expect(row.class).to eql(Hash)
185+
end
186+
end
187187

188-
@result = @client.prepare("SELECT 1").execute
189-
@result.each do |row|
188+
it "should be able to return results as an array" do
189+
result = @client.prepare("SELECT 1", :as => :array).execute
190+
result.each do |row|
190191
expect(row.class).to eql(Array)
191192
end
192-
193-
@client.query_options[:as] = :hash
194193
end
195194

196195
it "should cache previously yielded results by default" do
197-
@result = @client.prepare("SELECT 1").execute
198-
expect(@result.first.object_id).to eql(@result.first.object_id)
196+
result = @client.prepare("SELECT 1").execute
197+
expect(result.first.object_id).to eql(result.first.object_id)
199198
end
200199

201200
it "should yield different value for #first if streaming" do
202-
@client.query_options[:stream] = true
203-
@client.query_options[:cache_rows] = false
204-
205-
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
201+
result = @client.prepare("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false).execute
206202
expect(result.first).not_to eql(result.first)
207-
208-
@client.query_options[:stream] = false
209-
@client.query_options[:cache_rows] = true
210203
end
211204

212205
it "should yield the same value for #first if streaming is disabled" do
213-
@client.query_options[:stream] = false
214-
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
206+
result = @client.prepare("SELECT 1 UNION SELECT 2", :stream => false).execute
215207
expect(result.first).to eql(result.first)
216208
end
217209

218210
it "should throw an exception if we try to iterate twice when streaming is enabled" do
219-
@client.query_options[:stream] = true
220-
@client.query_options[:cache_rows] = false
221-
222-
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
211+
result = @client.prepare("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false).execute
223212

224213
expect {
225214
result.each {}
226215
result.each {}
227216
}.to raise_exception(Mysql2::Error)
228-
229-
@client.query_options[:stream] = false
230-
@client.query_options[:cache_rows] = true
231217
end
232218
end
233219

0 commit comments

Comments
 (0)