forked from coronabytes/dotnet-arangodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinqTest.cs
324 lines (268 loc) · 10.1 KB
/
LinqTest.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core.Arango.Protocol;
using Core.Arango.Linq;
using Core.Arango.Tests.Core;
using Newtonsoft.Json;
using Xunit;
using Xunit.Abstractions;
namespace Core.Arango.Tests
{
public class LinqTest : TestBase
{
private const string D = "test";
private readonly ITestOutputHelper _output;
public LinqTest(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public async Task SelectDocument()
{
var q = Arango.Query<Project>("test")
.Where(x => x.Name == "Project A")
.Select(x => new
{
x.Key,
x.Name,
ClientName = Aql.Document<Client>("Client", x.ClientKey).Name
});
_output.WriteLine(q.ToAql().aql);
_output.WriteLine("");
_output.WriteLine(JsonConvert.SerializeObject(await q.ToListAsync(), Formatting.Indented));
}
[Fact]
public async Task FirstOrDefault()
{
var p = await Arango.Query<Project>("test")
.FirstOrDefaultAsync(x => x.Name == "Project A");
Assert.Equal("Project A", p.Name);
}
[Fact]
public async Task SingleOrDefault()
{
var p = await Arango.Query<Project>("test")
.SingleOrDefaultAsync(x => x.Name == "Project A");
Assert.Equal("Project A", p.Name);
}
[Fact]
public async Task SingleException()
{
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
await Arango.Query<Project>("test")
.SingleAsync(x => x.Name == "Nope");
});
}
[Fact]
public Task MultiFilter()
{
var q = Arango.Query<Project>("test")
.Where(x => x.Name == "Project A")
.OrderBy(x => x.Name)
.Where(x => x.Name == "Project B")
.OrderByDescending(x => x.Name)
.Skip(0).Take(1);
Assert.Equal("FOR `x` IN `Project` FILTER ( `x`.`Name` == @P1 ) SORT `x`.`Name` ASC FILTER ( `x`.`Name` == @P2 ) SORT `x`.`Name` DESC LIMIT @P3 , @P4 RETURN `x`", q.ToAql().aql.Trim());
return Task.CompletedTask;
}
[Fact]
public Task FilterOrder()
{
var q = Arango.Query<Project>("test")
.Where(x => (x.Name == "Project A" || x.Name == "Project B") && x.Budget <= 0);
Assert.Equal("FOR `x` IN `Project` FILTER ( ( ( `x`.`Name` == @P1 ) OR ( `x`.`Name` == @P2 ) ) AND ( `x`.`Budget` <= @P3 ) ) RETURN `x`", q.ToAql().aql.Trim());
return Task.CompletedTask;
}
[Fact]
public Task Options()
{
var q = Arango.Query<Project>("test")
.Where(x => x.Name == "Project A")
.Options(() => new { indexHint = "byName" });
Assert.Equal("FOR `x` IN `Project` FILTER ( `x`.`Name` == @P1 ) OPTIONS { `indexHint` : @P2 } RETURN `x`", q.ToAql().aql.Trim());
return Task.CompletedTask;
}
[Fact]
public async Task GroupBy()
{
var q = Arango.Query<Project>("test")
.GroupBy(y => y.ParentKey)
.Select(x => new
{
Parent = x.Key,
Max = x.Max(y => y.Budget)
});
_output.WriteLine(q.ToAql().aql);
_output.WriteLine("");
await Task.CompletedTask;
//_output.WriteLine(JsonConvert.SerializeObject(await q.ToListAsync(), Formatting.Indented));
}
[Fact]
public async Task MathAbs()
{
var q = Arango.Query<Project>("test")
.Select(x => new
{
x.Key,
Budget = Math.Abs(x.Budget)
});
_output.WriteLine(q.ToAql().aql);
_output.WriteLine("");
await Task.CompletedTask;
//_output.WriteLine(JsonConvert.SerializeObject(await q.ToListAsync(), Formatting.Indented));
}
[Fact]
public async Task Ternary()
{
var q = Arango.Query<Project>("test")
.Select(x => new
{
x.Key,
Name = x.Name == "Test" ? "-" : x.Name
});
_output.WriteLine(q.ToAql().aql);
_output.WriteLine("");
await Task.CompletedTask;
//_output.WriteLine(JsonConvert.SerializeObject(await q.ToListAsync(), Formatting.Indented));
}
[Fact]
public async Task Update()
{
var q = Arango.Query<Project>("test")
.Where(x => x.Name == "Project A")
.Update(x => new
{
x.Key,
Name = Aql.Concat(x.Name, "2")
}, x => x.Key);
_output.WriteLine(q.ToAql().aql);
_output.WriteLine("");
_output.WriteLine(JsonConvert.SerializeObject(await q.ToListAsync(), Formatting.Indented));
}
[Fact]
public async Task Remove()
{
var q = Arango.Query<Project>("test")
.Where(x => x.Name == "Project A")
.Remove().In<Project>().Select(x => x.Key);
_output.WriteLine(q.ToAql().aql);
await Task.CompletedTask;
}
[Fact]
public async Task Let()
{
var q =
from p in Arango.Query<Project>("test")
let clients = (from c in Arango.Query<Client>() select c.Key)
select new { p.Name, Clients = Aql.As<string[]>(clients) };
_output.WriteLine(q.ToAql().aql);
_output.WriteLine("");
_output.WriteLine(JsonConvert.SerializeObject(await q.ToListAsync(), Formatting.Indented));
}
[Fact]
public async Task ListContains()
{
var list = new List<string> { "CB" }.ToArray();
var q = Arango.Query<Project>("test")
.Where(x => list.Contains(x.ClientKey));
//var q = Arango.Query<Project>("test")
// .Where(x => Aql.Position(list, x.ClientKey));
_output.WriteLine(q.ToAql().aql);
_output.WriteLine("");
var output = await q.ToListAsync();
_output.WriteLine(JsonConvert.SerializeObject(output, Formatting.Indented));
Assert.Single(output);
}
[Fact]
public async Task QueryableContains()
{
var q = Arango.Query<Project>("test")
.Select(x => x.ClientKey)
.Contains("CB");
Assert.True(q);
await Task.CompletedTask;
}
public override async Task InitializeAsync()
{
await base.InitializeAsync();
Arango = new ArangoContext(UniqueTestRealm());
await Arango.Database.CreateAsync(D);
await Arango.Collection.CreateAsync(D, nameof(Client), ArangoCollectionType.Document);
await Arango.Collection.CreateAsync(D, nameof(Project), ArangoCollectionType.Document);
await Arango.Collection.CreateAsync(D, nameof(Activity), ArangoCollectionType.Document);
await Arango.Document.CreateManyAsync(D, nameof(Client), new List<Client>
{
new()
{
Key = "CA",
Name = "Client A"
},
new()
{
Key = "CB",
Name = "Client B"
}
});
await Arango.Document.CreateManyAsync(D, nameof(Project), new List<Project>
{
new ()
{
Key = "PA",
Name = "Project A",
ClientKey = "CA"
},
new ()
{
Key = "PB",
Name = "Project B",
ClientKey = "CB"
}
});
}
[Fact]
public async Task Count_SubQuery()
{
var q = Arango.Query<Client>("test")
.Select(c => Arango.Query<Project>().Where(p => p.ClientKey == c.Key).Count());
_output.WriteLine(q.ToAql().aql);
var clientWithProjects = await q.ToListAsync();
Assert.Equal(new List<int> { 1, 1 }, clientWithProjects);
}
/*[Fact]
public async Task Except_SubQuery()
{
var list = await Arango.Query<Project>("test")
.Take(1)
.ToListAsync();
var q = Arango.Query<Client>("test")
.Select(c => Arango
.Query<Project>()
.Where(p => p.ClientKey == c.Key)
.Except(list) // TODO : Order of operations is not working correctly ('length' is being called before 'minus')
.Count()
);
PrintQuery(q, _output);
var p = await q.ToListAsync();
Assert.Equal(new List<int> { 0, 1 }, p);
}*/
[Fact]
public async Task StringContains()
{
var q = Arango.Query<Project>("test").Where(x => x.Name.Contains("abc"));
var aql = q.ToAql().aql.Trim();
Assert.Equal("FOR `x` IN `Project` FILTER CONTAINS( `x`.`Name` , @P1 ) RETURN `x`", aql);
await Task.CompletedTask;
}
[Fact]
public async Task StringConcat()
{
var q = Arango.Query<Project>("test").Where(x => string.Concat(x.Name, "Suffix") == "TestSuffix");
var aql = q.ToAql().aql.Trim();
Assert.Equal("FOR `x` IN `Project` FILTER ( CONCAT( `x`.`Name` , @P1 ) == @P2 ) RETURN `x`", aql);
await Task.CompletedTask;
}
}
}