-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathEntitiesTests.Select.cs
More file actions
178 lines (146 loc) · 5.18 KB
/
EntitiesTests.Select.cs
File metadata and controls
178 lines (146 loc) · 5.18 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
using System.Collections;
using System.Linq.Dynamic.Core.Exceptions;
using FluentAssertions;
using Newtonsoft.Json;
#if EFCORE
using Microsoft.EntityFrameworkCore;
#else
using System.Data.Entity;
#endif
using Xunit;
namespace System.Linq.Dynamic.Core.Tests;
public partial class EntitiesTests
{
[Fact]
public void Entities_Select_SingleColumn_NullCoalescing()
{
var expected1 = _context.Blogs.Select(x => (int?)(x.NullableInt ?? 10)).ToArray();
var expected2 = _context.Blogs.Select(x => (int?)(x.NullableInt ?? 9 + x.BlogId)).ToArray();
// Act
var test1 = _context.Blogs.Select<int?>("NullableInt ?? 10").ToArray();
var test2 = _context.Blogs.Select<int?>("NullableInt ?? 9 + BlogId").ToArray();
// Assert
Assert.Equal(expected1, test1);
Assert.Equal(expected2, test2);
}
[Fact]
public void Entities_Select_SingleColumn()
{
//Arrange
var expected = _context.Blogs.Select(x => x.BlogId).ToArray();
//Act
var test = _context.Blogs.Select<int>("BlogId").ToArray();
//Assert
Assert.Equal<ICollection>(expected, test);
}
[Fact]
public void Entities_Select_EmptyObject()
{
// Arrange
ParsingConfig config = ParsingConfig.Default;
config.EvaluateGroupByAtDatabase = true;
var expected = _context.Blogs.Select(x => new { }).ToList();
// Act
var test = _context.Blogs.GroupBy(config, "BlogId", "new()").Select<object>("new()").ToList();
// Assert
Assert.Equal(JsonConvert.SerializeObject(expected), JsonConvert.SerializeObject(test));
}
[Fact]
public void Entities_Select_BrokenObject()
{
ParsingConfig config = ParsingConfig.Default;
config.DisableMemberAccessToIndexAccessorFallback = false;
// Silently creates something that will later fail on materialization
var test = _context.Blogs.Select(config, "new(~.BlogId)");
test = test.Select(config, "new(nonexistentproperty as howcanthiswork)");
// Will fail when creating the expression
config.DisableMemberAccessToIndexAccessorFallback = true;
Assert.ThrowsAny<ParseException>(() =>
{
test = test.Select(config, "new(nonexistentproperty as howcanthiswork)");
});
}
[Fact]
public void Entities_Select_MultipleColumn()
{
//Arrange
var expected = _context.Blogs.Select(x => new { X = "x", x.BlogId, x.Name }).ToArray();
//Act
var test = _context.Blogs.Select("new (\"x\" as X, BlogId, Name)").ToDynamicArray();
//Assert
Assert.Equal(
expected,
test.Select(x => new { X = "x", BlogId = (int)x.BlogId, Name = (string)x.Name }).ToArray() // Convert to same anonymous type used by expected, so they can be found equal.
);
}
[Fact]
public void Entities_Select_BlogPosts()
{
//Arrange
var expected = _context.Blogs.Where(x => x.BlogId == 1).SelectMany(x => x.Posts).Select(x => x.PostId).ToArray();
//Act
var test = _context.Blogs.Where(x => x.BlogId == 1).SelectMany("Posts").Select<int>("PostId").ToArray();
//Assert
Assert.Equal(expected, test);
}
// fixed : EF issue !!! https://github.com/aspnet/EntityFramework/issues/4968
[Fact]
public void Entities_Select_BlogAndPosts()
{
//Arrange
var expected = _context.Blogs.Select(x => new { x.BlogId, x.Name, x.Posts }).ToArray();
//Act
var test = _context.Blogs.Select("new (BlogId, Name, Posts)").ToDynamicArray();
//Assert
Assert.Equal(expected.Length, test.Length);
for (int i = 0; i < expected.Length; i++)
{
var expectedRow = expected[i];
var testRow = test[i];
Assert.Equal(expectedRow.BlogId, testRow.BlogId);
Assert.Equal(expectedRow.Name, testRow.Name);
Assert.True(expectedRow.Posts != null);
Assert.Equal(expectedRow.Posts.ToList(), testRow.Posts);
}
}
/// <summary>
/// #593
/// </summary>
[Fact]
public void Entities_Select_DynamicClass_And_Call_Any()
{
// Act
var result = _context.Blogs
.Select("new (BlogId, Name)")
.Any("Name == \"Blog2\"");
// Assert
Assert.True(result);
}
/// <summary>
/// #907
/// </summary>
[Fact]
public void Entities_Select_DynamicClass_And_Select_DynamicClass()
{
// Act
var dynamicData = _context.Blogs
.Take(2)
.Select("new (BlogId as I, Name as N)")
.ToDynamicArray();
// Assert
var dynamicResult = dynamicData
.AsQueryable()
.Select("I")
.ToDynamicArray();
dynamicResult.Should().BeEquivalentTo([1000, 1001]);
}
[Fact]
public void Entities_Select_ClassWithItemProperty()
{
// Act
var result = _context.Posts.Select(x => new { x.Item, x.BlogId }).ToArray();
var resultDynamic = _context.Posts.Select("new (Item, BlogId)").ToDynamicArray();
// Assert
resultDynamic.Should().BeEquivalentTo(result);
}
}