Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 202 lines (165 sloc) 6.821 kB
2c52e21 @metaskills Add a Result#do alias to #cancel which is just syntactic for things l…
metaskills authored
1 # encoding: utf-8
12ea5ce @metaskills A lot more built out for the result structure and a basic #execute fo…
metaskills authored
2 require 'test_helper'
3
4 class ResultTest < TinyTds::TestCase
5
fe270f5 @metaskills Enough code to return an array of one result set, no row value castin…
metaskills authored
6 context 'Basic query and result' do
12ea5ce @metaskills A lot more built out for the result structure and a basic #execute fo…
metaskills authored
7
fe270f5 @metaskills Enough code to return an array of one result set, no row value castin…
metaskills authored
8 setup do
2c52e21 @metaskills Add a Result#do alias to #cancel which is just syntactic for things l…
metaskills authored
9 @@current_schema_loaded ||= load_current_schema
fe270f5 @metaskills Enough code to return an array of one result set, no row value castin…
metaskills authored
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
b95fac8 @metaskills Successive calls to enumerate result always returns the rows.
metaskills authored
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
7580b0b @metaskills Testing query options for array/keys. Add a #fields accessor to the r…
metaskills authored
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
5b7eff0 @metaskills Allow :cache_rows option. Future AR performance feature. Allows raw c…
metaskills authored
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
a551efd @metaskills Allow a :first option to be used with the query options to load only …
metaskills authored
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
4d56584 @metaskills Make sure :first query option handles no results properly.
metaskills authored
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
2c52e21 @metaskills Add a Result#do alias to #cancel which is just syntactic for things l…
metaskills authored
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
7580b0b @metaskills Testing query options for array/keys. Add a #fields accessor to the r…
metaskills authored
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
6ca40dd @metaskills Add a TinyTds::Result#cancel method and a few tests that show the imp…
metaskills authored
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
efe8d7b @metaskills New TODO. Test shared object id for keys in result.
metaskills authored
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
531f204 @metaskills Starting to do value conversions of raw row data.
metaskills authored
136
137 context 'when casting to native ruby values' do
2a8b8e9 @metaskills Slippery slope while handling "messages" from the server that are err…
metaskills authored
138
531f204 @metaskills Starting to do value conversions of raw row data.
metaskills authored
139 should 'return fixnum for 1' do
ce7216c @metaskills Testing under 1.8.6, forcing a version of activesupport that is frien…
metaskills authored
140 value = @client.execute('SELECT 1 AS [fixnum]').each.first['fixnum']
531f204 @metaskills Starting to do value conversions of raw row data.
metaskills authored
141 assert_equal 1, value
142 end
b4eb145 @metaskills Account for NULL to nil type cast.
metaskills authored
143
144 should 'return nil for NULL' do
ce7216c @metaskills Testing under 1.8.6, forcing a version of activesupport that is frien…
metaskills authored
145 value = @client.execute('SELECT NULL AS [null]').each.first['null']
b4eb145 @metaskills Account for NULL to nil type cast.
metaskills authored
146 assert_equal nil, value
147 end
2a8b8e9 @metaskills Slippery slope while handling "messages" from the server that are err…
metaskills authored
148
149 end
150
151 context 'when shit happens' do
9a8100b @metaskills Testing more errors and handle a bit differently.
metaskills authored
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
2a8b8e9 @metaskills Slippery slope while handling "messages" from the server that are err…
metaskills authored
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
9a8100b @metaskills Testing more errors and handle a bit differently.
metaskills authored
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
2a8b8e9 @metaskills Slippery slope while handling "messages" from the server that are err…
metaskills authored
175 end
9a8100b @metaskills Testing more errors and handle a bit differently.
metaskills authored
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
2a8b8e9 @metaskills Slippery slope while handling "messages" from the server that are err…
metaskills authored
184 end
531f204 @metaskills Starting to do value conversions of raw row data.
metaskills authored
185
186 end
12ea5ce @metaskills A lot more built out for the result structure and a basic #execute fo…
metaskills authored
187
fe270f5 @metaskills Enough code to return an array of one result set, no row value castin…
metaskills authored
188 end
189
190
9a8100b @metaskills Testing more errors and handle a bit differently.
metaskills authored
191 protected
192
193 def assert_followup_query
194 assert_nothing_raised do
195 result = @client.execute(@query1)
ce7216c @metaskills Testing under 1.8.6, forcing a version of activesupport that is frien…
metaskills authored
196 assert_equal 1, result.each.first['one']
9a8100b @metaskills Testing more errors and handle a bit differently.
metaskills authored
197 end
198 end
199
12ea5ce @metaskills A lot more built out for the result structure and a basic #execute fo…
metaskills authored
200 end
201
Something went wrong with that request. Please try again.