forked from DapperLib/Dapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryMultipleTests.cs
More file actions
270 lines (239 loc) · 9.57 KB
/
QueryMultipleTests.cs
File metadata and controls
270 lines (239 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Xunit;
namespace Dapper.Tests
{
public class QueryMultipleTests : TestBase
{
[Fact]
public void TestQueryMultipleBuffered()
{
using (var grid = connection.QueryMultiple("select 1; select 2; select @x; select 4", new { x = 3 }))
{
var a = grid.Read<int>();
var b = grid.Read<int>();
var c = grid.Read<int>();
var d = grid.Read<int>();
a.Single().Equals(1);
b.Single().Equals(2);
c.Single().Equals(3);
d.Single().Equals(4);
}
}
[Fact]
public void TestQueryMultipleNonBufferedIncorrectOrder()
{
using (var grid = connection.QueryMultiple("select 1; select 2; select @x; select 4", new { x = 3 }))
{
var a = grid.Read<int>(false);
try
{
var b = grid.Read<int>(false);
throw new InvalidOperationException(); // should have thrown
}
catch (InvalidOperationException)
{
// that's expected
}
}
}
[Fact]
public void TestQueryMultipleNonBufferedCorrectOrder()
{
using (var grid = connection.QueryMultiple("select 1; select 2; select @x; select 4", new { x = 3 }))
{
var a = grid.Read<int>(false).Single();
var b = grid.Read<int>(false).Single();
var c = grid.Read<int>(false).Single();
var d = grid.Read<int>(false).Single();
a.Equals(1);
b.Equals(2);
c.Equals(3);
d.Equals(4);
}
}
[Fact]
public void TestMultiReaderBasic()
{
const string sql = "select 1 as Id union all select 2 as Id select 'abc' as name select 1 as Id union all select 2 as Id";
int i, j;
string s;
using (var multi = connection.QueryMultiple(sql))
{
i = multi.Read<int>().First();
s = multi.Read<string>().Single();
j = multi.Read<int>().Sum();
}
Assert.Equal(1, i);
Assert.Equal("abc", s);
Assert.Equal(3, j);
}
[Fact]
public void TestReadDynamicWithGridReader()
{
const string createSql = @"
create table #Users (Id int, Name varchar(20))
create table #Posts (Id int, OwnerId int, Content varchar(20))
insert #Users values(99, 'Sam')
insert #Users values(2, 'I am')
insert #Posts values(1, 99, 'Sams Post1')
insert #Posts values(2, 99, 'Sams Post2')
insert #Posts values(3, null, 'no ones post')";
try
{
connection.Execute(createSql);
const string sql = @"SELECT * FROM #Users ORDER BY Id
SELECT * FROM #Posts ORDER BY Id DESC";
var grid = connection.QueryMultiple(sql);
var users = grid.Read().ToList();
var posts = grid.Read().ToList();
Assert.Equal(2, users.Count);
Assert.Equal(3, posts.Count);
Assert.Equal(2, (int)users[0].Id);
Assert.Equal(3, (int)posts[0].Id);
}
finally
{
connection.Execute("drop table #Users drop table #Posts");
}
}
[Fact]
public void Issue268_ReturnQueryMultiple()
{
connection.Execute(@"create proc #TestProc268 (@a int, @b int, @c int)as
begin
select @a;
select @b
return @c;
end");
var p = new DynamicParameters(new { a = 1, b = 2, c = 3 });
p.Add("RetVal", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
using (var reader = connection.QueryMultiple("#TestProc268", p, commandType: CommandType.StoredProcedure))
{
reader.Read();
}
var retVal = p.Get<int>("RetVal");
Assert.Equal(3, retVal);
}
[Fact]
public void Issue524_QueryMultiple_Cast()
{
// aka: Read<int> should work even if the data is a <long>
// using regular API
Assert.Equal(42, connection.Query<int>("select cast(42 as bigint)").Single());
Assert.Equal(42, connection.QuerySingle<int>("select cast(42 as bigint)"));
// using multi-reader API
using (var reader = connection.QueryMultiple("select cast(42 as bigint); select cast(42 as bigint)"))
{
Assert.Equal(42, reader.Read<int>().Single());
Assert.Equal(42, reader.ReadSingle<int>());
}
}
[Fact]
public void QueryMultipleFromClosed()
{
using (var conn = GetClosedConnection())
{
using (var multi = conn.QueryMultiple("select 1; select 'abc';"))
{
Assert.Equal(1, multi.Read<int>().Single());
Assert.Equal("abc", multi.Read<string>().Single());
}
Assert.Equal(ConnectionState.Closed, conn.State);
}
}
[Fact]
public void QueryMultiple2FromClosed()
{
using (var conn = GetClosedConnection())
{
Assert.Equal(ConnectionState.Closed, conn.State);
using (var multi = conn.QueryMultiple("select 1 select 2 select 3"))
{
Assert.Equal(1, multi.Read<int>().Single());
Assert.Equal(2, multi.Read<int>().Single());
// not reading 3 is intentional here
}
Assert.Equal(ConnectionState.Closed, conn.State);
}
}
[Fact]
public void SO35554284_QueryMultipleUntilConsumed()
{
using (var reader = connection.QueryMultiple("select 1 as Id; select 2 as Id; select 3 as Id;"))
{
var items = new List<HazNameId>();
while (!reader.IsConsumed)
{
items.AddRange(reader.Read<HazNameId>());
}
Assert.Equal(3, items.Count);
Assert.Equal(1, items[0].Id);
Assert.Equal(2, items[1].Id);
Assert.Equal(3, items[2].Id);
}
}
[Fact]
public void QueryMultipleInvalidFromClosed()
{
using (var conn = GetClosedConnection())
{
Assert.ThrowsAny<Exception>(() => conn.QueryMultiple("select gibberish"));
Assert.Equal(ConnectionState.Closed, conn.State);
}
}
[Fact]
public void TestMultiSelectWithSomeEmptyGridsUnbuffered() => TestMultiSelectWithSomeEmptyGrids(false);
[Fact]
public void TestMultiSelectWithSomeEmptyGridsBuffered() => TestMultiSelectWithSomeEmptyGrids(true);
private void TestMultiSelectWithSomeEmptyGrids(bool buffered)
{
using (var reader = connection.QueryMultiple("select 1; select 2 where 1 = 0; select 3 where 1 = 0; select 4;"))
{
var one = reader.Read<int>(buffered: buffered).ToArray();
var two = reader.Read<int>(buffered: buffered).ToArray();
var three = reader.Read<int>(buffered: buffered).ToArray();
var four = reader.Read<int>(buffered: buffered).ToArray();
try
{ // only returned four grids; expect a fifth read to fail
reader.Read<int>(buffered: buffered);
throw new InvalidOperationException("this should not have worked!");
}
catch (ObjectDisposedException ex)
{ // expected; success
Assert.Equal("The reader has been disposed; this can happen after all data has been consumed\r\nObject name: 'Dapper.SqlMapper+GridReader'.", ex.Message);
}
Assert.Single(one);
Assert.Equal(1, one[0]);
Assert.Empty(two);
Assert.Empty(three);
Assert.Single(four);
Assert.Equal(4, four[0]);
}
}
[Fact]
public void TypeBasedViaTypeMulti()
{
Type type = Common.GetSomeType();
dynamic first, second;
using (var multi = connection.QueryMultiple("select @A as [A], @B as [B]; select @C as [A], @D as [B]",
new { A = 123, B = "abc", C = 456, D = "def" }))
{
first = multi.Read(type).Single();
second = multi.Read(type).Single();
}
Assert.Equal(((object)first).GetType(), type);
int a = first.A;
string b = first.B;
Assert.Equal(123, a);
Assert.Equal("abc", b);
Assert.Equal(((object)second).GetType(), type);
a = second.A;
b = second.B;
Assert.Equal(456, a);
Assert.Equal("def", b);
}
}
}