/
QueryTests.cs
436 lines (367 loc) · 17.6 KB
/
QueryTests.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using System;
using System.Linq;
using Norm.BSON;
using Norm.Responses;
using Xunit;
using Norm.Collections;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Norm.Commands.Modifiers;
namespace Norm.Tests
{
public class QueryTests : IDisposable
{
private readonly Mongo _server;
private BuildInfoResponse _buildInfo = null;
private readonly MongoCollection<Person> _collection;
public QueryTests()
{
var admin = new MongoAdmin("mongodb://localhost/admin?pooling=false&strict=true");
_server = Mongo.Create("mongodb://localhost/NormTests?pooling=false");
_collection = _server.GetCollection<Person>("People");
_buildInfo = admin.BuildInfo();
//cause the collection to exist on the server by inserting, then deleting some things.
_collection.Insert(new Person());
_collection.Delete(new { });
}
public void Dispose()
{
_server.Database.DropCollection("People");
using (var admin = new MongoAdmin("mongodb://localhost/NormTests?pooling=false"))
{
admin.DropDatabase();
}
_server.Dispose();
}
[Fact]
public void FindUsesLimit()
{
_collection.Insert(new Person { Name = "BBB" });
_collection.Insert(new Person { Name = "CCC" });
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "DDD" });
var result = _collection.Find(new { }, 3).ToArray();
Assert.Equal(3, result.Length);
}
[Fact(Skip="broken")]
public void MongoCollection_Supports_LINQ()
{
_collection.Insert(new Person { Name = "BBB" });
_collection.Insert(new Person { Name = "CCC" });
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "DDD" });
//var result = _collection.Where(y => y.Name == "AAA").ToArray();
//Assert.Equal(1, result.Length);
}
[Fact]
public void Count_Works()
{
_collection.Insert(new Person { Name = "BBB" });
_collection.Insert(new Person { Name = "CCC" });
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "DDD" });
var result = _collection.Count();
Assert.Equal(4, result);
}
[Fact]
public void Count_With_Filter_Works()
{
_collection.Insert(new Person { Name = "BBB" });
_collection.Insert(new Person { Name = "CCC" });
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "DDD" });
var result = _collection.Count(new { Name = "AAA" });
Assert.Equal(1, result);
}
[Fact]
public void DateTime_GreaterThan_Qualifier_Works()
{
_collection.Insert(new Person { Birthday = new DateTime(1910, 1, 1) });
_collection.Insert(new Person { Birthday = new DateTime(1920, 1, 1) });
_collection.Insert(new Person { Birthday = new DateTime(1930, 1, 1) });
var find = _collection.Find(new { Birthday = Q.GreaterThan(new DateTime(1920, 1, 1)) });
Assert.Equal(1, find.Count());
}
[Fact]
public void Element_Match_Matches()
{
using (var db = Mongo.Create(TestHelper.ConnectionString()))
{
var coll = db.GetCollection<Post>();
coll.Delete(new { });
coll.Insert(new Post
{
Comments = new Comment[] {
new Comment { Text = "xabc" },
new Comment { Text = "abc" }
}
},
new Post { Tags = new String[] { "hello", "world" } },
new Post
{
Comments = new Comment[] {
new Comment { Text = "xyz" },
new Comment { Text = "abc" }
}
});
Assert.Equal(1, coll.Find(new { Comments = Q.ElementMatch(new { Text = "xyz" }) }).Count());
Assert.Equal(2, coll.Find(new { Comments = Q.ElementMatch(new { Text = Q.Matches("^x") }) }).Count());
}
}
[Fact]
public void Where_Qualifier_Works()
{
_collection.Insert(new Person { Name = "Gnomey" });
_collection.Insert(new Person { Name = "kde" });
_collection.Insert(new Person { Name = "Elfy" });
var find = _collection.Find(Q.Where("this.Name === 'Elfy';"));
Assert.Equal(1, find.Count());
}
[Fact]
public void Find_Uses_Limit_Orderby_And_Skip()
{
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "BBB" });
_collection.Insert(new Person { Name = "CCC" });
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "DDD" });
var result = _collection.Find(new { Name = Q.NotEqual(new int?()) }, new { Name = OrderBy.Descending }, 3, 1).ToArray();
Assert.Equal(3, result.Length);
Assert.Equal("CCC", result[0].Name);
Assert.Equal("BBB", result[1].Name);
Assert.Equal("AAA", result[2].Name);
}
[Fact]
public void Find_Uses_Query_And_Orderby()
{
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "BBB" });
_collection.Insert(new Person { Name = "CCC" });
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "DDD" });
var result = _collection.Find(new { Name = Q.NotEqual("AAA") }, new { Name = OrderBy.Descending }).ToArray();
Assert.Equal(3, result.Length);
Assert.Equal("DDD", result[0].Name);
Assert.Equal("CCC", result[1].Name);
Assert.Equal("BBB", result[2].Name);
}
[Fact]
public void Find_Uses_Query_And_Orderby_And_Limit()
{
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "BBB" });
_collection.Insert(new Person { Name = "CCC" });
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "DDD" });
var result = _collection.Find(new { Name = Q.NotEqual("DDD") }, new { Name = OrderBy.Descending }, 2, 0).ToArray();
Assert.Equal(2, result.Length);
Assert.Equal("CCC", result[0].Name);
Assert.Equal("BBB", result[1].Name);
}
[Fact]
public void Find_Uses_Null_Qualifier()
{
_collection.Insert(new Person { Name = null });
_collection.Insert(new Person { Name = "BBB" });
_collection.Insert(new Person { Name = "CCC" });
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "DDD" });
var result = _collection.Find(new { Name = Q.IsNull() }, new { Name = OrderBy.Descending }, 2, 0).ToArray();
Assert.Equal(1, result.Length);
Assert.Equal(null, result[0].Name);
result = _collection.Find(new { Name = Q.IsNotNull() }, new { Name = OrderBy.Descending }).ToArray();
Assert.Equal(4, result.Length);
Assert.Equal("DDD", result[0].Name);
}
[Fact]
public void FindUsesLimitAndSkip()
{
_collection.Insert(new Person { Name = "BBB" });
_collection.Insert(new Person { Name = "CCC" });
_collection.Insert(new Person { Name = "AAA" });
_collection.Insert(new Person { Name = "DDD" });
var result = _collection.Find(new { }, 1, 1).ToArray();
Assert.Equal(1, result.Length);
Assert.Equal("CCC", result[0].Name);
}
[Fact]
public void FindCanQueryEmbeddedArray()
{
_collection.Delete(new { });
var person1 = new Person
{
Name = "Joe Cool",
Address =
{
Street = "123 Main St",
City = "Anytown",
State = "CO",
Zip = "45123"
}
};
var person2 = new Person
{
Name = "Sam Cool",
Address =
{
Street = "300 Main St",
City = "Anytown",
State = "CO",
Zip = "45123"
},
Relatives = new List<string>() { "Emma", "Bruce", "Charlie" }
};
_collection.Insert(person1);
_collection.Insert(person2);
var elem = new Expando();
elem["Relatives"] = "Charlie";
var a = _collection.Find(elem).ToArray();
Assert.Equal(1, a.Length);
}
[Fact]
public void BasicQueryUsingProperty()
{
_collection.Insert(new Person { Name = "Lisa Cool", Address = { Street = "300 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
_collection.Insert(new Person { Name = "Joe Cool", Address = { Street = "123 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
_collection.Insert(new Person { Name = "Sam Cool", Address = { Street = "300 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
var matchRegex = new Regex("^.{4}Cool$");
var results = _collection.Find(new { Name = matchRegex }).ToArray();
Assert.Equal(2, results.Length);
Assert.True(results.All(y => matchRegex.IsMatch(y.Name)));
}
[Fact]
public void BasicQueryWithSort()
{
//remove everything from the collection.
_collection.Delete(new { });
_collection.Insert(new Person { Name = "Third", LastContact = new DateTime(2010, 1, 1) });
_collection.Insert(new Person { Name = "First", LastContact = new DateTime(2000, 1, 1) });
_collection.Insert(new Person { Name = "Second", LastContact = new DateTime(2005, 1, 1) });
var people = _collection.Find(new { }, new { LastContact = 1 }).ToArray();
Assert.Equal(3, people.Length);
Assert.Equal("First", people[0].Name);
Assert.Equal("Second", people[1].Name);
Assert.Equal("Third", people[2].Name);
}
[Fact]
public void BasicQueryWithMultiSortOrdering()
{
//remove everything from the collection.
_collection.Delete(new { });
_collection.Insert(new Person { Name = "Third", LastContact = new DateTime(2010, 1, 1) });
_collection.Insert(new Person { Name = "First", LastContact = new DateTime(2005, 1, 1) });
_collection.Insert(new Person { Name = "Second", LastContact = new DateTime(2005, 1, 1) });
var people = _collection.Find(new { }, new { LastContact = -1, Name = 1 }).ToArray();
Assert.Equal(3, people.Length);
Assert.Equal("Third", people[0].Name);
Assert.Equal("First", people[1].Name);
Assert.Equal("Second", people[2].Name);
}
[Fact]
public void BasicQueryUsingChildProperty()
{
_collection.Insert(new Person { Name = "Joe Cool", Address = { Street = "123 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
_collection.Insert(new Person { Name = "Sam Cool", Address = { Street = "300 Main St", City = "Anytown", State = "CO", Zip = "45123" } });
var query = new Expando();
query["Address.City"] = Q.Equals<string>("Anytown");
var results = _collection.Find(query);
Assert.Equal(2, results.Count());
}
[Fact]
public void QueryWithinEmbeddedArray()
{
var post1 = new Person { Name = "First", Relatives = new List<String> { "comment1", "comment2" } };
var post2 = new Person { Name = "Second", Relatives = new List<String> { "commentA", "commentB" } };
_collection.Insert(post1);
_collection.Insert(post2);
var results = _collection.Find(new { Relatives = "commentA" });
Assert.Equal("Second", results.First().Name);
}
[Fact]
public void Distinct_On_Collection_Should_Return_Arrays_As_Value_If_Earlier_Than_1_5_0()
{
var isLessThan150 = Regex.IsMatch(_buildInfo.Version, "^([01][.][01234])");
// Any version earlier than MongoDB 1.5.0
if (isLessThan150)
{
_collection.Insert(new Person { Name = "Joe Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
_collection.Insert(new Person { Name = "Sam Cool", Relatives = new List<string>(new[] { "Joe Cool", "Jay Cool" }) });
_collection.Insert(new Person { Name = "Ted Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
_collection.Insert(new Person { Name = "Jay Cool", Relatives = new List<string>(new[] { "Sam Cool" }) });
var results = _collection.Distinct<string[]>("Relatives");
Assert.Equal(3, results.Count());
}
}
[Fact]
public void Distinct_On_Collection_Should_Return_Array_Values_In_1_5_0_Or_Later()
{
var isLessThan150 = Regex.IsMatch(_buildInfo.Version, "^([01][.][01234])");
// Any version MongoDB 1.5.0 +
if (!isLessThan150)
{
_collection.Insert(new Person { Name = "Joe Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
_collection.Insert(new Person { Name = "Sam Cool", Relatives = new List<string>(new[] { "Joe Cool", "Jay Cool" }) });
_collection.Insert(new Person { Name = "Ted Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
_collection.Insert(new Person { Name = "Jay Cool", Relatives = new List<string>(new[] { "Sam Cool" }) });
var results = _collection.Distinct<string>("Relatives");
Assert.Equal(4, results.Count());
}
}
[Fact]
public void DistinctOnSimpleProperty()
{
_collection.Insert(new Person { Name = "Joe Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
_collection.Insert(new Person { Name = "Sam Cool", Relatives = new List<string>(new[] { "Joe Cool", "Jay Cool" }) });
_collection.Insert(new Person { Name = "Ted Cool", Relatives = new List<string>(new[] { "Tom Cool", "Sam Cool" }) });
_collection.Insert(new Person { Name = "Jay Cool", Relatives = new List<string>(new[] { "Sam Cool" }) });
var results = _collection.Distinct<string>("Name");
Assert.Equal(4, results.Count());
}
[Fact]
public void DistinctOnComplexProperty()
{
_collection.Insert(new Person { Name = "Joe Cool", Address = new Address { State = "CA" } });
_collection.Insert(new Person { Name = "Sam Cool", Address = new Address { State = "CA" } });
_collection.Insert(new Person { Name = "Ted Cool", Address = new Address { State = "CA", Zip = "90010" } });
_collection.Insert(new Person { Name = "Jay Cool", Address = new Address { State = "NY" } });
var results = _collection.Distinct<Address>("Address");
Assert.Equal(3, results.Count());
}
[Fact]
public void FindAndModify()
{
_collection.Insert(new Person { Name = "Joe Cool", Age = 10 });
var update = new Expando();
update["$inc"] = new { Age = 1 };
var result = _collection.FindAndModify(new { Name = "Joe Cool" }, update);
Assert.Equal(10, result.Age);
var result2 = _collection.Find(new { Name = "Joe Cool" }).FirstOrDefault();
Assert.Equal(11, result2.Age);
}
[Fact]
public void FindAndModifyWithSort()
{
_collection.Insert(new Person { Name = "Joe Cool", Age = 10 });
_collection.Insert(new Person { Name = "Joe Cool", Age = 15 });
var update = new Expando();
update["$inc"] = new { Age = 1 };
var result = _collection.FindAndModify(new { Name = "Joe Cool" }, update, new { Age = Norm.OrderBy.Descending });
Assert.Equal(15, result.Age);
var result2 = _collection.Find(new { Name = "Joe Cool" }).OrderByDescending(x=>x.Age).ToList();
Assert.Equal(16, result2[0].Age);
Assert.Equal(10, result2[1].Age);
}
[Fact]
public void FindAndModifyReturnsNullWhenQueryNotFound()
{
_collection.Insert(new Person { Name = "Joe Cool", Age = 10 });
_collection.Insert(new Person { Name = "Joe Cool", Age = 15 });
var update = new Expando();
update["$inc"] = new { Age = 1 };
var result = _collection.FindAndModify(new { Name = "Joe Cool1" }, update, new { Age = Norm.OrderBy.Descending });
Assert.Null(result);
var result2 = _collection.Find(new { Age = 15 }).ToList();
Assert.Equal(1, result2.Count);
}
}
}