-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathGearsOfWarQueryNpgsqlTest.cs
More file actions
278 lines (237 loc) · 8.83 KB
/
GearsOfWarQueryNpgsqlTest.cs
File metadata and controls
278 lines (237 loc) · 8.83 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
271
272
273
274
275
276
277
278
using Microsoft.EntityFrameworkCore.TestModels.GearsOfWarModel;
namespace Microsoft.EntityFrameworkCore.Query;
// ReSharper disable once UnusedMember.Global
public class GearsOfWarQueryNpgsqlTest : GearsOfWarQueryRelationalTestBase<GearsOfWarQueryNpgsqlFixture>
{
// ReSharper disable once UnusedParameter.Local
public GearsOfWarQueryNpgsqlTest(GearsOfWarQueryNpgsqlFixture fixture, ITestOutputHelper testOutputHelper)
: base(fixture)
{
Fixture.TestSqlLoggerFactory.Clear();
Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
}
#region Byte array
public override async Task Byte_array_filter_by_length_literal_does_not_cast_on_varbinary_n(bool async)
{
await base.Byte_array_filter_by_length_literal_does_not_cast_on_varbinary_n(async);
AssertSql(
"""
SELECT s."Id", s."Banner", s."Banner5", s."InternalNumber", s."Name"
FROM "Squads" AS s
WHERE length(s."Banner5") = 5
""");
}
#endregion Byte array
#region DateTimeOffset
// Not supported by design: we support getting a local DateTime via DateTime.Now (based on PG TimeZone), but there's no way to get a
// non-UTC DateTimeOffset.
public override Task DateTimeOffsetNow_minus_timespan(bool async)
=> Assert.ThrowsAsync<InvalidOperationException>(() => base.DateTimeOffsetNow_minus_timespan(async));
public override async Task DateTimeOffset_Date_returns_datetime(bool async)
{
var dateTimeOffset = new DateTimeOffset(2, 3, 1, 8, 0, 0, new TimeSpan(-5, 0, 0));
await AssertQuery(
async,
ss => ss.Set<Mission>().Where(m => m.Timeline.Date.ToLocalTime() >= dateTimeOffset.Date));
AssertSql(
"""
@dateTimeOffset_Date='0002-03-01T00:00:00.0000000'
SELECT m."Id", m."CodeName", m."Date", m."Difficulty", m."Duration", m."Rating", m."Time", m."Timeline"
FROM "Missions" AS m
WHERE date_trunc('day', m."Timeline" AT TIME ZONE 'UTC')::timestamp >= @dateTimeOffset_Date
""");
}
public override async Task DateTimeOffset_Contains_Less_than_Greater_than(bool async)
{
var dto = new DateTimeOffset(599898024001234567, new TimeSpan(0));
var start = dto.AddDays(-1);
var end = dto.AddDays(1);
var dates = new[] { dto };
await AssertQuery(
async,
ss => ss.Set<Mission>().Where(
m => start <= m.Timeline.Date && m.Timeline < end && dates.Contains(m.Timeline)),
assertEmpty: true); // TODO: Look into this
AssertSql(
"""
@start='1902-01-01T10:00:00.1234567+00:00' (DbType = DateTime)
@end='1902-01-03T10:00:00.1234567+00:00' (DbType = DateTime)
@dates={ '1902-01-02T10:00:00.1234567+00:00' } (DbType = Object)
SELECT m."Id", m."CodeName", m."Date", m."Difficulty", m."Duration", m."Rating", m."Time", m."Timeline"
FROM "Missions" AS m
WHERE @start <= date_trunc('day', m."Timeline" AT TIME ZONE 'UTC')::timestamptz AND m."Timeline" < @end AND m."Timeline" = ANY (@dates)
""");
}
// Base implementation uses DateTimeOffset.Now, which we don't translate by design. Use DateTimeOffset.UtcNow instead.
public override async Task Select_datetimeoffset_comparison_in_projection(bool async)
{
await AssertQueryScalar(
async,
ss => ss.Set<Mission>().Select(m => m.Timeline > DateTimeOffset.UtcNow));
AssertSql(
"""
SELECT m."Timeline" > now()
FROM "Missions" AS m
""");
}
#endregion DateTimeOffset
#region DateTime
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_datetime_subtraction(bool async)
=> AssertQuery(
async,
ss => ss.Set<Mission>().Where(
m =>
new DateTimeOffset(2, 3, 2, 8, 0, 0, new TimeSpan(-5, 0, 0)) - m.Timeline > TimeSpan.FromDays(3)),
assertEmpty: true); // TODO: Look into this
#endregion DateTime
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Where_TimeSpan_TotalDays(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Mission>().Where(m => m.Duration.TotalDays < 0.042));
AssertSql(
"""
SELECT m."Id", m."CodeName", m."Date", m."Difficulty", m."Duration", m."Rating", m."Time", m."Timeline"
FROM "Missions" AS m
WHERE date_part('epoch', m."Duration") / 86400.0 < 0.042000000000000003
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Where_TimeSpan_TotalHours(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Mission>().Where(m => m.Duration.TotalHours < 1.02));
AssertSql(
"""
SELECT m."Id", m."CodeName", m."Date", m."Difficulty", m."Duration", m."Rating", m."Time", m."Timeline"
FROM "Missions" AS m
WHERE date_part('epoch', m."Duration") / 3600.0 < 1.02
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Where_TimeSpan_TotalMinutes(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Mission>().Where(m => m.Duration.TotalMinutes < 61));
AssertSql(
"""
SELECT m."Id", m."CodeName", m."Date", m."Difficulty", m."Duration", m."Rating", m."Time", m."Timeline"
FROM "Missions" AS m
WHERE date_part('epoch', m."Duration") / 60.0 < 61.0
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Where_TimeSpan_TotalSeconds(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Mission>().Where(m => m.Duration.TotalSeconds < 3700));
AssertSql(
"""
SELECT m."Id", m."CodeName", m."Date", m."Difficulty", m."Duration", m."Rating", m."Time", m."Timeline"
FROM "Missions" AS m
WHERE date_part('epoch', m."Duration") < 3700.0
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Where_TimeSpan_TotalMilliseconds(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Mission>().Where(m => m.Duration.TotalMilliseconds < 3700000));
AssertSql(
"""
SELECT m."Id", m."CodeName", m."Date", m."Difficulty", m."Duration", m."Rating", m."Time", m."Timeline"
FROM "Missions" AS m
WHERE date_part('epoch', m."Duration") / 0.001 < 3700000.0
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task GroupBy_Property_Select_Sum_over_TimeSpan(bool async)
{
await AssertQueryScalar(
async,
ss => ss.Set<Mission>()
.GroupBy(o => o.Id)
.Select(g => EF.Functions.Sum(g.Select(o => o.Duration))),
ss => ss.Set<Mission>()
.GroupBy(o => o.Id)
.Select(g => (TimeSpan?)new TimeSpan(g.Sum(o => o.Duration.Ticks))));
AssertSql(
"""
SELECT sum(m."Duration")
FROM "Missions" AS m
GROUP BY m."Id"
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task GroupBy_Property_Select_Average_over_TimeSpan(bool async)
{
await AssertQueryScalar(
async,
ss => ss.Set<Mission>()
.GroupBy(o => o.Id)
.Select(g => EF.Functions.Average(g.Select(o => o.Duration))),
ss => ss.Set<Mission>()
.GroupBy(o => o.Id)
.Select(g => (TimeSpan?)new TimeSpan((long)g.Average(o => o.Duration.Ticks))));
AssertSql(
"""
SELECT avg(m."Duration")
FROM "Missions" AS m
GROUP BY m."Id"
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task GroupBy_Property_Select_BoolAnd_over_Bool(bool async)
{
await AssertQueryScalar(
async,
ss => ss.Set<Mission>()
.GroupBy(o => o.CodeName)
.Select(g => EF.Functions.BoolAnd(g.Select(o => o.Id == 1))),
ss => ss.Set<Mission>()
.GroupBy(o => o.CodeName)
.Select(g => g.All(o => o.Id == 1)));
AssertSql(
"""
SELECT bool_and(m."Id" = 1)
FROM "Missions" AS m
GROUP BY m."CodeName"
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task GroupBy_Property_Select_BoolOr_over_Bool(bool async)
{
await AssertQueryScalar(
async,
ss => ss.Set<Mission>()
.GroupBy(o => o.CodeName)
.Select(g => EF.Functions.BoolOr(g.Select(o => o.Id == 1))),
ss => ss.Set<Mission>()
.GroupBy(o => o.CodeName)
.Select(g => g.Any(o => o.Id == 1)));
AssertSql(
"""
SELECT bool_or(m."Id" = 1)
FROM "Missions" AS m
GROUP BY m."CodeName"
""");
}
private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}