Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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