Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 120 lines (95 sloc) 3.705 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]'
10 end
11
12 should 'have included Enumerable' do
13 assert TinyTds::Result.ancestors.include?(Enumerable)
14 end
15
16 should 'respond to #each' do
17 result = @client.execute(@query1)
18 assert result.respond_to?(:each)
19 end
20
21 should 'return all results for #each with no block' do
22 result = @client.execute(@query1)
23 data = result.each
24 row = data.first
25 assert_instance_of Array, data
26 assert_equal 1, data.size
27 assert_instance_of Hash, row, 'hash is the default query option'
28 end
29
30 should 'return all results for #each with a block yielding a row at a time' do
31 result = @client.execute(@query1)
32 data = result.each do |row|
33 assert_instance_of Hash, row, 'hash is the default query option'
34 end
35 assert_instance_of Array, data
36 end
b95fac8 @metaskills Successive calls to enumerate result always returns the rows.
metaskills authored
37
38 should 'allow successive calls to each returning the same data' do
39 result = @client.execute(@query1)
40 data = result.each
41 assert_nothing_raised() { result.each }
42 assert_equal data.object_id, result.each.object_id
43 assert_equal data.first.object_id, result.each.first.object_id
44 end
7580b0b @metaskills Testing query options for array/keys. Add a #fields accessor to the r…
metaskills authored
45
46 should 'return hashes with string keys' do
47 result = @client.execute(@query1)
48 row = result.each(:as => :hash, :symbolize_keys => false).first
49 assert_instance_of Hash, row
50 assert_equal ['one'], row.keys
51 assert_equal ['one'], result.fields
52 end
53
54 should 'return hashes with symbol keys' do
55 result = @client.execute(@query1)
56 row = result.each(:as => :hash, :symbolize_keys => true).first
57 assert_instance_of Hash, row
58 assert_equal [:one], row.keys
59 assert_equal [:one], result.fields
60 end
61
62 should 'return arrays with string fields' do
63 result = @client.execute(@query1)
64 row = result.each(:as => :array, :symbolize_keys => false).first
65 assert_instance_of Array, row
66 assert_equal ['one'], result.fields
67 end
68
69 should 'return arrays with symbol fields' do
70 result = @client.execute(@query1)
71 row = result.each(:as => :array, :symbolize_keys => true).first
72 assert_instance_of Array, row
73 assert_equal [:one], result.fields
74 end
75
76 should 'have a #fields accessor with logic default and valid outcome' do
77 result = @client.execute(@query1)
78 assert_nil result.fields
79 result.each
80 assert_instance_of Array, result.fields
81 end
82
6ca40dd @metaskills Add a TinyTds::Result#cancel method and a few tests that show the imp…
metaskills authored
83 should 'allow the result to be canceled before reading' do
84 result = @client.execute(@query1)
85 result.cancel
86 assert_nothing_raised() { @client.execute(@query1).each }
87 end
88
89 should 'throw an error when you execute another query with other results pending' do
90 result1 = @client.execute(@query1)
91 action = lambda { @client.execute(@query1) }
92 assert_raise_tinytds_error(action) do |e|
93 assert_match %r|with results pending|i, e.message
94 assert_equal 7, e.severity
95 assert_equal 20019, e.db_error_number
96 end
97 end
98
7580b0b @metaskills Testing query options for array/keys. Add a #fields accessor to the r…
metaskills authored
99 should_eventually 'use same string object for hash keys'
531f204 @metaskills Starting to do value conversions of raw row data.
metaskills authored
100
101 context 'when casting to native ruby values' do
102
103 should 'return fixnum for 1' do
104 value = @client.execute('SELECT 1 AS [fixnum]').first['fixnum']
105 assert_equal 1, value
106 end
b4eb145 @metaskills Account for NULL to nil type cast.
metaskills authored
107
108 should 'return nil for NULL' do
109 value = @client.execute('SELECT NULL AS [null]').first['null']
110 assert_equal nil, value
111 end
531f204 @metaskills Starting to do value conversions of raw row data.
metaskills authored
112
113 end
12ea5ce @metaskills A lot more built out for the result structure and a basic #execute fo…
metaskills authored
114
fe270f5 @metaskills Enough code to return an array of one result set, no row value castin…
metaskills authored
115 end
116
117
12ea5ce @metaskills A lot more built out for the result structure and a basic #execute fo…
metaskills authored
118 end
119
Something went wrong with that request. Please try again.