Skip to content
This repository
Newer
Older
100644 200 lines (165 sloc) 6.821 kb
2c52e215 » metaskills
2010-10-12 Add a Result#do alias to #cancel which is just syntactic for things l…
1 # encoding: utf-8
12ea5cef » metaskills
2010-09-26 A lot more built out for the result structure and a basic #execute fo…
2 require 'test_helper'
3
4 class ResultTest < TinyTds::TestCase
5
fe270f5f » metaskills
2010-09-29 Enough code to return an array of one result set, no row value castin…
6 context 'Basic query and result' do
12ea5cef » metaskills
2010-09-26 A lot more built out for the result structure and a basic #execute fo…
7
fe270f5f » metaskills
2010-09-29 Enough code to return an array of one result set, no row value castin…
8 setup do
2c52e215 » metaskills
2010-10-12 Add a Result#do alias to #cancel which is just syntactic for things l…
9 @@current_schema_loaded ||= load_current_schema
fe270f5f » metaskills
2010-09-29 Enough code to return an array of one result set, no row value castin…
10 @client = TinyTds::Client.new(connection_options)
11 @query1 = 'SELECT 1 AS [one]'
12 end
13
14 should 'have included Enumerable' do
15 assert TinyTds::Result.ancestors.include?(Enumerable)
16 end
17
18 should 'respond to #each' do
19 result = @client.execute(@query1)
20 assert result.respond_to?(:each)
21 end
22
23 should 'return all results for #each with no block' do
24 result = @client.execute(@query1)
25 data = result.each
26 row = data.first
27 assert_instance_of Array, data
28 assert_equal 1, data.size
29 assert_instance_of Hash, row, 'hash is the default query option'
30 end
31
32 should 'return all results for #each with a block yielding a row at a time' do
33 result = @client.execute(@query1)
34 data = result.each do |row|
35 assert_instance_of Hash, row, 'hash is the default query option'
36 end
37 assert_instance_of Array, data
38 end
b95fac88 » metaskills
2010-09-29 Successive calls to enumerate result always returns the rows.
39
40 should 'allow successive calls to each returning the same data' do
41 result = @client.execute(@query1)
42 data = result.each
43 assert_nothing_raised() { result.each }
44 assert_equal data.object_id, result.each.object_id
45 assert_equal data.first.object_id, result.each.first.object_id
46 end
7580b0b2 » metaskills
2010-09-29 Testing query options for array/keys. Add a #fields accessor to the r…
47
48 should 'return hashes with string keys' do
49 result = @client.execute(@query1)
50 row = result.each(:as => :hash, :symbolize_keys => false).first
51 assert_instance_of Hash, row
52 assert_equal ['one'], row.keys
53 assert_equal ['one'], result.fields
54 end
55
56 should 'return hashes with symbol keys' do
57 result = @client.execute(@query1)
58 row = result.each(:as => :hash, :symbolize_keys => true).first
59 assert_instance_of Hash, row
60 assert_equal [:one], row.keys
61 assert_equal [:one], result.fields
62 end
63
64 should 'return arrays with string fields' do
65 result = @client.execute(@query1)
66 row = result.each(:as => :array, :symbolize_keys => false).first
67 assert_instance_of Array, row
68 assert_equal ['one'], result.fields
69 end
70
71 should 'return arrays with symbol fields' do
72 result = @client.execute(@query1)
73 row = result.each(:as => :array, :symbolize_keys => true).first
74 assert_instance_of Array, row
75 assert_equal [:one], result.fields
76 end
77
5b7eff0d » metaskills
2010-10-12 Allow :cache_rows option. Future AR performance feature. Allows raw c…
78 should 'be able to turn :cache_rows option off' do
79 result = @client.execute(@query1)
80 local = []
81 result.each(:cache_rows => false) do |row|
82 local << row
83 end
84 assert local.first, 'should have iterated over each row'
85 assert_equal [], result.each, 'should not have been cached'
86 assert_equal ['one'], result.fields, 'should still cache field names'
87 end
88
a551efd8 » metaskills
2010-10-12 Allow a :first option to be used with the query options to load only …
89 should 'be able to get the first result row only' do
90 big_query = "SELECT [id] FROM [datatypes]"
91 one = @client.execute(big_query).each(:first => true)
92 many = @client.execute(big_query).each
93 assert many.size > 1
94 assert one.size == 1
95 end
96
4d565848 » metaskills
2010-10-12 Make sure :first query option handles no results properly.
97 should 'cope with no results when using first option' do
98 data = @client.execute("SELECT [id] FROM [datatypes] WHERE [id] = -1").each(:first => true)
99 assert_equal [], data
100 end
101
2c52e215 » metaskills
2010-10-12 Add a Result#do alias to #cancel which is just syntactic for things l…
102 should 'delete, insert and find data' do
103 text = 'test insert and delete'
104 @client.execute("DELETE FROM [datatypes] WHERE [varchar_50] IS NOT NULL").do
105 @client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").do
106 row = @client.execute("SELECT [varchar_50] FROM [datatypes] WHERE [varchar_50] IS NOT NULL").each.first
107 assert row
108 assert_equal text, row['varchar_50']
109 end
110
111 should 'insert and find unicode data' do
112 text = '✓'
113 @client.execute("DELETE FROM [datatypes] WHERE [nvarchar_50] IS NOT NULL").do
114 @client.execute("INSERT INTO [datatypes] ([nvarchar_50]) VALUES (N'#{text}')").do
115 row = @client.execute("SELECT [nvarchar_50] FROM [datatypes] WHERE [nvarchar_50] IS NOT NULL").each.first
116 assert_equal text, row['nvarchar_50']
117 end
118
7580b0b2 » metaskills
2010-09-29 Testing query options for array/keys. Add a #fields accessor to the r…
119 should 'have a #fields accessor with logic default and valid outcome' do
120 result = @client.execute(@query1)
121 assert_nil result.fields
122 result.each
123 assert_instance_of Array, result.fields
124 end
125
6ca40dd1 » metaskills
2010-09-29 Add a TinyTds::Result#cancel method and a few tests that show the imp…
126 should 'allow the result to be canceled before reading' do
127 result = @client.execute(@query1)
128 result.cancel
129 assert_nothing_raised() { @client.execute(@query1).each }
130 end
131
efe8d7bd » metaskills
2010-10-11 New TODO. Test shared object id for keys in result.
132 should 'use same string object for hash keys' do
133 data = @client.execute("SELECT [id], [bigint] FROM [datatypes]").each
134 assert_equal data.first.keys.map{ |r| r.object_id }, data.last.keys.map{ |r| r.object_id }
135 end
531f204b » metaskills
2010-09-29 Starting to do value conversions of raw row data.
136
137 context 'when casting to native ruby values' do
2a8b8e94 » metaskills
2010-09-30 Slippery slope while handling "messages" from the server that are err…
138
531f204b » metaskills
2010-09-29 Starting to do value conversions of raw row data.
139 should 'return fixnum for 1' do
ce7216cd » metaskills
2010-10-04 Testing under 1.8.6, forcing a version of activesupport that is frien…
140 value = @client.execute('SELECT 1 AS [fixnum]').each.first['fixnum']
531f204b » metaskills
2010-09-29 Starting to do value conversions of raw row data.
141 assert_equal 1, value
142 end
b4eb145a » metaskills
2010-09-29 Account for NULL to nil type cast.
143
144 should 'return nil for NULL' do
ce7216cd » metaskills
2010-10-04 Testing under 1.8.6, forcing a version of activesupport that is frien…
145 value = @client.execute('SELECT NULL AS [null]').each.first['null']
b4eb145a » metaskills
2010-09-29 Account for NULL to nil type cast.
146 assert_equal nil, value
147 end
2a8b8e94 » metaskills
2010-09-30 Slippery slope while handling "messages" from the server that are err…
148
149 end
150
151 context 'when shit happens' do
9a8100ba » metaskills
2010-09-30 Testing more errors and handle a bit differently.
152
153 should 'cope with nil or empty buffer' do
154 assert_raise(TypeError) { @client.execute(nil) }
155 assert_equal [], @client.execute('').each
156 end
157
2a8b8e94 » metaskills
2010-09-30 Slippery slope while handling "messages" from the server that are err…
158 should 'throw an error when you execute another query with other results pending' do
159 result1 = @client.execute(@query1)
160 action = lambda { @client.execute(@query1) }
161 assert_raise_tinytds_error(action) do |e|
162 assert_match %r|with results pending|i, e.message
163 assert_equal 7, e.severity
164 assert_equal 20019, e.db_error_number
165 end
166 end
167
9a8100ba » metaskills
2010-09-30 Testing more errors and handle a bit differently.
168 should 'error gracefully with bad table name' do
169 assert_raise_tinytds_error(lambda{ @client.execute('SELECT * FROM [foobar]') }) do |e|
170 assert_match %r|invalid object name.*foobar|i, e.message
171 assert_equal 16, e.severity
172 assert_equal 208, e.db_error_number
173 end
174 assert_followup_query
2a8b8e94 » metaskills
2010-09-30 Slippery slope while handling "messages" from the server that are err…
175 end
9a8100ba » metaskills
2010-09-30 Testing more errors and handle a bit differently.
176
177 should 'error gracefully with invalid syntax' do
178 assert_raise_tinytds_error(lambda{ @client.execute('this will not work') }) do |e|
179 assert_match %r|incorrect syntax|i, e.message
180 assert_equal 15, e.severity
181 assert_equal 156, e.db_error_number
182 end
183 assert_followup_query
2a8b8e94 » metaskills
2010-09-30 Slippery slope while handling "messages" from the server that are err…
184 end
531f204b » metaskills
2010-09-29 Starting to do value conversions of raw row data.
185
186 end
12ea5cef » metaskills
2010-09-26 A lot more built out for the result structure and a basic #execute fo…
187
fe270f5f » metaskills
2010-09-29 Enough code to return an array of one result set, no row value castin…
188 end
189
190
9a8100ba » metaskills
2010-09-30 Testing more errors and handle a bit differently.
191 protected
192
193 def assert_followup_query
194 assert_nothing_raised do
195 result = @client.execute(@query1)
ce7216cd » metaskills
2010-10-04 Testing under 1.8.6, forcing a version of activesupport that is frien…
196 assert_equal 1, result.each.first['one']
9a8100ba » metaskills
2010-09-30 Testing more errors and handle a bit differently.
197 end
198 end
199
12ea5cef » metaskills
2010-09-26 A lot more built out for the result structure and a basic #execute fo…
200 end
201
Something went wrong with that request. Please try again.