-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainTest.cs
164 lines (146 loc) · 4.87 KB
/
MainTest.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
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using ReusableEfCoreIncludes.ExampleProject;
using ReusableEfCoreIncludes.Tests.Util;
using static ReusableEfCoreIncludes.Tests.Util.Assertions;
namespace ReusableEfCoreIncludes.Tests;
public class MainTest : DatabaseTest
{
[SetUp]
public override async Task SetupAsync()
{
await base.SetupAsync();
await Fixtures.Make(_context!);
}
[Test]
public async Task TestIncludeCompany()
{
var company = await _context!.Companies
// including a generic needs a helper if the types are specified directly
.BeginInclude().IncludeCompany().AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
AssertCompany(company);
// can execute it again without issues
company = await _context!.Companies
// including a generic needs a helper if the types are specified directly
.BeginInclude().IncludeCompany().AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
AssertCompany(company);
Assert.IsNotEmpty(company!.Departments);
}
[Test]
public async Task TestIncludeCompanyExclusion()
{
var company = await _context!.Companies
.BeginInclude()
.IncludeCompany(new CompanyIncludeOptions {ExcludeDepartments = true})
.AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
Assert.NotNull(company);
Assert.IsEmpty(company!.Departments);
}
[Test]
public async Task TestIncludeDepartments()
{
var department = await _context!.Departments
.BeginInclude()
.IncludeDepartment()
.AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
AssertDepartment(department);
Assert.IsNotEmpty(department!.Users);
}
[Test]
public async Task TestIncludeDepartmentsDownCast()
{
var company = await _context!.Companies
.BeginInclude()
.IncludeDepartmentTestDownCast(q => q.IncludeMany(c => c.Departments), true)
.AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
Assert.IsNotEmpty(company!.Departments);
Assert.IsNotEmpty(company!.Departments[0].Users);
Assert.NotNull(company!.Departments[0].LeadUser);
Assert.NotNull(company!.Departments[0].LeadUser!.Role);
}
[Test]
public async Task TestThenIncludeIf()
{
var company = await _context!.Companies
.BeginInclude()
.IncludeDepartmentTestDownCast(q => q.IncludeMany(c => c.Departments), false)
.AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
Assert.IsNull(company!.Departments[0].LeadUser!.Role);
}
[Test]
public async Task TestIncludeUser()
{
var user = await _context!.Users
.BeginInclude()
.IncludeUser()
.AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
AssertUser(user);
}
[Test]
public async Task TestIncludeBase()
{
var department = await _context!.Departments
.AsQueryable()
.BeginInclude()
.IncludeMany(d => d.Users)
.AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
Assert.NotNull(department);
Assert.IsNotEmpty(department!.Users);
var company = await _context.Companies
.BeginInclude()
.IncludeMany(c => c.Departments.Where(d => d.Name.Contains("f")))
.ThenIncludeMany(d => d.Users)
.AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
Assert.NotNull(company);
Assert.IsEmpty(company!.Departments);
var user = await _context.Users
.BeginInclude()
.Include(u => u.Department)
.ThenInclude(u => u!.Company)
.AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
Assert.NotNull(user);
Assert.NotNull(user!.Department);
Assert.NotNull(user.Department!.Company);
department = await _context!.Departments
.AsQueryable()
.BeginInclude()
.IncludeLeadUser(Including.FromBase)
.AsQueryable()
.AsNoTracking()
.FirstOrDefaultAsync();
Assert.NotNull(department);
Assert.NotNull(department!.LeadUser);
}
[Test]
public async Task TestIncludeUserThenIncludeBase()
{
var user = await _context!.Users
.BeginInclude()
.Include(u => u.Department)
.AsQueryable()
.FirstOrDefaultAsync();
Assert.NotNull(user);
}
}