This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
/
OrmLiteSelectTests.cs
352 lines (266 loc) · 8.9 KB
/
OrmLiteSelectTests.cs
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using System.Collections.Generic;
using NUnit.Framework;
using ServiceStack.Common;
using ServiceStack.Common.Tests.Models;
using ServiceStack.Text;
using ServiceStack.DataAnnotations;
namespace ServiceStack.OrmLite.FirebirdTests
{
[TestFixture]
public class OrmLiteSelectTests
: OrmLiteTestBase
{
[Test]
public void Can_GetById_int_from_ModelWithFieldsOfDifferentTypes_table()
{
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithFieldsOfDifferentTypes>(true);
var rowIds = new List<int>(new[] { 1, 2, 3 });
rowIds.ForEach(x => db.Insert(ModelWithFieldsOfDifferentTypes.Create(x)));
var row = db.GetById<ModelWithFieldsOfDifferentTypes>(1);
Assert.That(row.Id, Is.EqualTo(1));
}
}
[Test]
public void Can_GetById_string_from_ModelWithOnlyStringFields_table()
{
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithOnlyStringFields>(true);
var rowIds = new List<string>(new[] { "id-1", "id-2", "id-3" });
rowIds.ForEach(x => db.Insert(ModelWithOnlyStringFields.Create(x)));
var row = db.GetById<ModelWithOnlyStringFields>("id-1");
Assert.That(row.Id, Is.EqualTo("id-1"));
}
}
[Test]
public void Can_GetByIds_int_from_ModelWithFieldsOfDifferentTypes_table()
{
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithFieldsOfDifferentTypes>(true);
var rowIds = new List<int>(new[] { 1, 2, 3 });
rowIds.ForEach(x => db.Insert(ModelWithFieldsOfDifferentTypes.Create(x)));
var rows = db.GetByIds<ModelWithFieldsOfDifferentTypes>(rowIds);
var dbRowIds = rows.ConvertAll(x => x.Id);
Assert.That(dbRowIds, Is.EquivalentTo(rowIds));
}
}
[Test]
public void Can_GetByIds_string_from_ModelWithOnlyStringFields_table()
{
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithOnlyStringFields>(true);
var rowIds = new List<string>(new[] { "id-1", "id-2", "id-3" });
rowIds.ForEach(x => db.Insert(ModelWithOnlyStringFields.Create(x)));
var rows = db.GetByIds<ModelWithOnlyStringFields>(rowIds);
var dbRowIds = rows.ConvertAll(x => x.Id);
Assert.That(dbRowIds, Is.EquivalentTo(rowIds));
}
}
[Test]
public void Can_select_with_filter_from_ModelWithOnlyStringFields_table()
{
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithOnlyStringFields>(true);
var rowIds = new List<string>(new[] { "id-1", "id-2", "id-3" });
rowIds.ForEach(x => db.Insert(ModelWithOnlyStringFields.Create(x)));
var filterRow = ModelWithOnlyStringFields.Create("id-4");
filterRow.AlbumName = "FilteredName";
db.Insert(filterRow);
var rows = db.Select<ModelWithOnlyStringFields>("AlbumName = {0}", filterRow.AlbumName);
var dbRowIds = rows.ConvertAll(x => x.Id);
Assert.That(dbRowIds, Has.Count.EqualTo(1));
Assert.That(dbRowIds[0], Is.EqualTo(filterRow.Id));
}
}
[Test]
public void Can_select_scalar_value()
{
const int n = 5;
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithIdAndName>(true);
db.DeleteAll<ModelWithIdAndName>();
n.Times(x => db.Insert(ModelWithIdAndName.Create(0)));
var count = db.GetScalar<int>("SELECT COUNT(*) FROM ModelWIN");
Assert.That(count, Is.EqualTo(n));
}
}
[Test]
public void Can_loop_each_string_from_ModelWithOnlyStringFields_table()
{
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithOnlyStringFields>(true);
var rowIds = new List<string>(new[] { "id-1", "id-2", "id-3" });
rowIds.ForEach(x => db.Insert(ModelWithOnlyStringFields.Create(x)));
var dbRowIds = new List<string>();
foreach (var row in db.Each<ModelWithOnlyStringFields>())
{
dbRowIds.Add(row.Id);
}
Assert.That(dbRowIds, Is.EquivalentTo(rowIds));
}
}
[Test]
public void Can_loop_each_with_filter_from_ModelWithOnlyStringFields_table()
{
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithOnlyStringFields>(true);
var rowIds = new List<string>(new[] { "id-1", "id-2", "id-3" });
rowIds.ForEach(x => db.Insert(ModelWithOnlyStringFields.Create(x)));
var filterRow = ModelWithOnlyStringFields.Create("id-4");
filterRow.AlbumName = "FilteredName";
db.Insert(filterRow);
var dbRowIds = new List<string>();
var rows = db.Each<ModelWithOnlyStringFields>("AlbumName = {0}", filterRow.AlbumName);
foreach (var row in rows)
{
dbRowIds.Add(row.Id);
}
Assert.That(dbRowIds, Has.Count.EqualTo(1));
Assert.That(dbRowIds[0], Is.EqualTo(filterRow.Id));
}
}
[Test]
public void Can_GetFirstColumn()
{
const int n = 5;
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithIdAndName>(true);
db.DeleteAll<ModelWithIdAndName>();
n.Times(x => db.Insert(ModelWithIdAndName.Create(0)));
var ids = db.GetFirstColumn<int>("SELECT Id FROM ModelWIN");
Assert.That(ids.Count, Is.EqualTo(n));
}
}
[Test]
public void Can_GetFirstColumnDistinct()
{
const int n = 5;
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithIdAndName>(true);
db.DeleteAll<ModelWithIdAndName>();
n.Times(x => db.Insert(ModelWithIdAndName.Create(0)));
var ids = db.GetFirstColumnDistinct<int>("SELECT Id FROM ModelWIN");
Assert.That(ids.Count, Is.EqualTo(n));
}
}
[Test]
public void Can_GetLookup()
{
const int n = 5;
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithIdAndName>(true);
db.DeleteAll<ModelWithIdAndName>();
n.Times(x => {
var row = ModelWithIdAndName.Create(0);
row.Name = x % 2 == 0 ? "OddGroup" : "EvenGroup";
db.Insert(row);
});
var lookup = db.GetLookup<string, int>("SELECT Name, Id FROM ModelWIN");
Assert.That(lookup, Has.Count.EqualTo(2));
Assert.That(lookup["OddGroup"], Has.Count.EqualTo(3));
Assert.That(lookup["EvenGroup"], Has.Count.EqualTo(2));
}
}
[Test]
public void Can_GetDictionary()
{
const int n = 5;
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithIdAndName>(true);
db.DeleteAll<ModelWithIdAndName>();
n.Times(x => db.Insert(ModelWithIdAndName.Create(0)));
var dictionary = db.GetDictionary<int, string>("SELECT Id, Name FROM ModelWIN");
Assert.That(dictionary, Has.Count.EqualTo(5));
//Console.Write(dictionary.Dump());
}
}
[Test]
public void Can_Select_subset_ModelWithIdAndName_from_ModelWithFieldsOfDifferentTypes_table()
{
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithFieldsOfDifferentTypes>(true);
var rowIds = new List<int>(new[] { 1, 2, 3 });
rowIds.ForEach(x => db.Insert(ModelWithFieldsOfDifferentTypes.Create(x)));
var rows = db.Select<ModelWithIdAndName>("SELECT Id, Name FROM ModelWFDT");
var dbRowIds = rows.ConvertAll(x => x.Id);
Assert.That(dbRowIds, Is.EquivalentTo(rowIds));
}
}
[Test]
public void Can_Select_Into_ModelWithIdAndName_from_ModelWithFieldsOfDifferentTypes_table()
{
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithFieldsOfDifferentTypes>(true);
var rowIds = new List<int>(new[] { 1, 2, 3 });
rowIds.ForEach(x => db.Insert(ModelWithFieldsOfDifferentTypes.Create(x)));
var rows = db.Select<ModelWithIdAndName>(typeof(ModelWithFieldsOfDifferentTypes));
var dbRowIds = rows.ConvertAll(x => x.Id);
Assert.That(dbRowIds, Is.EquivalentTo(rowIds));
}
}
[Test]
public void Can_Select_In_for_string_value()
{
const int n = 5;
using (var db = ConnectionString.OpenDbConnection())
{
db.CreateTable<ModelWithIdAndName>(true);
db.DeleteAll<ModelWithIdAndName>();
for(int i=1; i<=n; i++){
ModelWithIdAndName m = new ModelWithIdAndName(){
Name= "Name"+i.ToString()
};
db.Insert(m);
}
var selectInNames = new[] {"Name1", "Name2"};
var rows = db.Select<ModelWithIdAndName>("Name IN ({0})", selectInNames.SqlInValues());
Assert.That(rows.Count, Is.EqualTo(selectInNames.Length));
}
}
//[Alias("RDB$DATABASE")]
public class PocoFlag
{
public string Name { get; set; }
public bool Flag { get; set; }
}
[Test]
public void Can_populate_PocoFlag()
{
using (var db = ConnectionString.OpenDbConnection())
{
var rows = db.Select<PocoFlag>("SELECT 1 as Flag FROM RDB$DATABASE");
Assert.That(rows[0].Flag);
}
}
public class PocoFlagWithId
{
public int Id { get; set; }
public bool Flag { get; set; }
}
[Test]
public void Can_populate_PocoFlagWithId()
{
using (var db = ConnectionString.OpenDbConnection())
{
var rows = db.Select<PocoFlagWithId>("SELECT 1 as Id, 1 as Flag FROM RDB$DATABASE");
Assert.That(rows[0].Id, Is.EqualTo(1));
Assert.That(rows[0].Flag);
}
}
}
}