Skip to content

Commit 23778bd

Browse files
committed
Refactoring UnitTest
1 parent 1d22afe commit 23778bd

File tree

1 file changed

+81
-81
lines changed

1 file changed

+81
-81
lines changed

src/ClassLibrary.EFCore.Tests/Tests.cs

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,81 +7,81 @@ namespace ClassLibrary.EFCore.Tests;
77
public class Tests : InMemoryDbContext
88
{
99
[Fact]
10-
public void GetAllEntities()
10+
public async Task GetAllEntities()
1111
{
1212
using var dbContext = GetDbContext();
13-
Repository<Person, int> repository = new(dbContext);
13+
var repository = new Repository<Person, int>(dbContext);
1414

15-
dbContext.Database.EnsureDeletedAsync();
16-
dbContext.Database.EnsureCreatedAsync();
15+
await dbContext.Database.EnsureDeletedAsync();
16+
await dbContext.Database.EnsureCreatedAsync();
1717

18-
var entities = repository.GetAllAsync();
18+
var entities = await repository.GetAllAsync();
1919

2020
Assert.NotNull(entities);
2121
}
2222

2323
[Fact]
24-
public void GetAllEntitiesWithPredicate()
24+
public async Task GetAllEntitiesWithPredicateAsync()
2525
{
2626
using var dbContext = GetDbContext();
27-
Repository<Person, int> repository = new(dbContext);
27+
var repository = new Repository<Person, int>(dbContext);
2828

29-
dbContext.Database.EnsureDeletedAsync();
30-
dbContext.Database.EnsureCreatedAsync();
29+
await dbContext.Database.EnsureDeletedAsync();
30+
await dbContext.Database.EnsureCreatedAsync();
3131

32-
var entities = repository.GetAllEntitiesAsync(predicate: x => x.Id is >= 3 and <= 8);
32+
var entities = await repository.GetAllEntitiesAsync(predicate: x => x.Id is >= 3 and <= 8);
3333

34-
Assert.Equal(6, entities?.Result.Count);
34+
Assert.Equal(6, entities.Count);
3535
Assert.NotNull(entities);
3636
}
3737

3838
[Fact]
39-
public void GetEntityById()
39+
public async Task GetEntityByIdAsync()
4040
{
4141
using var dbContext = GetDbContext();
42-
Repository<Person, int> repository = new(dbContext);
42+
var repository = new Repository<Person, int>(dbContext);
4343

44-
dbContext.Database.EnsureDeletedAsync();
45-
dbContext.Database.EnsureCreatedAsync();
44+
await dbContext.Database.EnsureDeletedAsync();
45+
await dbContext.Database.EnsureCreatedAsync();
4646

47-
var entity = repository.GetByIdAsync(2);
47+
var entity = await repository.GetByIdAsync(2);
4848

4949
Assert.NotNull(entity);
50-
Assert.Equal(2, Assert.IsType<Person>(entity?.Result).Id);
51-
Assert.Equal("Nome2", Assert.IsType<Person>(entity?.Result).Nome);
52-
Assert.Equal("Cognome2", Assert.IsType<Person>(entity?.Result).Cognome);
50+
Assert.Equal(2, Assert.IsType<Person>(entity).Id);
51+
Assert.Equal("Nome2", Assert.IsType<Person>(entity).Nome);
52+
Assert.Equal("Cognome2", Assert.IsType<Person>(entity).Cognome);
5353
}
5454

5555
[Fact]
56-
public void GetEntityByIdNotFound()
56+
public async Task GetEntityByIdNotFoundAsync()
5757
{
5858
using var dbContext = GetDbContext();
59-
Repository<Person, int> repository = new(dbContext);
59+
var repository = new Repository<Person, int>(dbContext);
6060

61-
dbContext.Database.EnsureDeletedAsync();
62-
dbContext.Database.EnsureCreatedAsync();
61+
await dbContext.Database.EnsureDeletedAsync();
62+
await dbContext.Database.EnsureCreatedAsync();
6363

64-
var entity = repository.GetByIdAsync(30)?.Result;
64+
var entity = await repository.GetByIdAsync(30);
6565

6666
Assert.Null(entity);
6767
}
6868

6969
[Fact]
70-
public void CreateEntity()
70+
public async Task CreateEntityAsync()
7171
{
7272
using var dbContext = GetDbContext();
73-
Repository<Person, int> repository = new(dbContext);
73+
var repository = new Repository<Person, int>(dbContext);
7474

75-
dbContext.Database.EnsureDeletedAsync();
76-
dbContext.Database.EnsureCreatedAsync();
75+
await dbContext.Database.EnsureDeletedAsync();
76+
await dbContext.Database.EnsureCreatedAsync();
7777

7878
var entity = new Person
7979
{
8080
Nome = "Nome11",
8181
Cognome = "Cognome11"
8282
};
8383

84-
repository?.CreateAsync(entity);
84+
await repository.CreateAsync(entity);
8585

8686
Assert.NotNull(entity);
8787
Assert.Equal(11, entity.Id);
@@ -90,20 +90,20 @@ public void CreateEntity()
9090
}
9191

9292
[Fact]
93-
public void UpdateEntity()
93+
public async Task UpdateEntityAsync()
9494
{
9595
using var dbContext = GetDbContext();
96-
Repository<Person, int> repository = new(dbContext);
96+
var repository = new Repository<Person, int>(dbContext);
9797

98-
dbContext.Database.EnsureDeletedAsync();
99-
dbContext.Database.EnsureCreatedAsync();
98+
await dbContext.Database.EnsureDeletedAsync();
99+
await dbContext.Database.EnsureCreatedAsync();
100100

101-
var entity = repository.GetByIdAsync(2)?.Result;
101+
var entity = await repository.GetByIdAsync(2);
102102

103-
entity!.Nome = "Nome2-bis";
104-
entity!.Cognome = "Cognome2-bis";
103+
entity.Nome = "Nome2-bis";
104+
entity.Cognome = "Cognome2-bis";
105105

106-
repository?.UpdateAsync(entity);
106+
await repository.UpdateAsync(entity);
107107

108108
Assert.NotNull(entity);
109109
Assert.Equal(2, entity.Id);
@@ -112,102 +112,102 @@ public void UpdateEntity()
112112
}
113113

114114
[Fact]
115-
public void DeleteEntity()
115+
public async Task DeleteEntityAsync()
116116
{
117117
using var dbContext = GetDbContext();
118-
Repository<Person, int> repository = new(dbContext);
118+
var repository = new Repository<Person, int>(dbContext);
119119

120-
dbContext.Database.EnsureDeletedAsync();
121-
dbContext.Database.EnsureCreatedAsync();
120+
await dbContext.Database.EnsureDeletedAsync();
121+
await dbContext.Database.EnsureCreatedAsync();
122122

123-
var entity = repository.GetByIdAsync(4)?.Result;
124-
repository?.DeleteAsync(entity!);
123+
var entity = await repository.GetByIdAsync(4);
124+
await repository.DeleteAsync(entity!);
125125

126-
var entities = repository?.GetAllAsync();
126+
var entities = await repository.GetAllAsync();
127127

128128
Assert.NotNull(entities);
129-
Assert.Equal(9, entities?.Result.Count);
129+
Assert.Equal(9, entities.Count);
130130
}
131131

132132
[Fact]
133-
public void DeleteByIdEntity()
133+
public async Task DeleteByIdEntityAsync()
134134
{
135135
using var dbContext = GetDbContext();
136-
Repository<Person, int> repository = new(dbContext);
136+
var repository = new Repository<Person, int>(dbContext);
137137

138-
dbContext.Database.EnsureDeletedAsync();
139-
dbContext.Database.EnsureCreatedAsync();
138+
await dbContext.Database.EnsureDeletedAsync();
139+
await dbContext.Database.EnsureCreatedAsync();
140140

141-
repository?.DeleteByIdAsync(4);
141+
await repository.DeleteByIdAsync(4);
142142

143-
var entities = repository?.GetAllAsync();
143+
var entities = await repository.GetAllAsync();
144144

145145
Assert.NotNull(entities);
146-
Assert.Equal(9, entities?.Result.Count);
146+
Assert.Equal(9, entities.Count);
147147
}
148148

149149
[Fact]
150-
public void GetPaginatedEntities()
150+
public async Task GetPaginatedEntitiesAsync()
151151
{
152152
using var dbContext = GetDbContext();
153-
Repository<Person, int> repository = new(dbContext);
153+
var repository = new Repository<Person, int>(dbContext);
154154

155-
dbContext.Database.EnsureDeletedAsync();
156-
dbContext.Database.EnsureCreatedAsync();
155+
await dbContext.Database.EnsureDeletedAsync();
156+
await dbContext.Database.EnsureCreatedAsync();
157157

158-
var entities = repository.GetPaginatedAsync(null!, x => x.Id <= 10, x => x.Id, true, 2, 5);
158+
var entities = await repository.GetPaginatedAsync(null!, x => x.Id <= 10, x => x.Id, true, 2, 5);
159159

160160
Assert.NotNull(entities);
161-
Assert.Equal(5, entities?.Result.Count);
162-
Assert.Contains(entities!.Result, x => x.Id == 8);
161+
Assert.Equal(5, entities.Count);
162+
Assert.Contains(entities, x => x.Id == 8);
163163
}
164164

165165
[Fact]
166-
public void GetPaginatedEntitiesWithoutInclude()
166+
public async Task GetPaginatedEntitiesWithoutIncludeAsync()
167167
{
168168
using var dbContext = GetDbContext();
169-
Repository<Person, int> repository = new(dbContext);
169+
var repository = new Repository<Person, int>(dbContext);
170170

171-
dbContext.Database.EnsureDeletedAsync();
172-
dbContext.Database.EnsureCreatedAsync();
171+
await dbContext.Database.EnsureDeletedAsync();
172+
await dbContext.Database.EnsureCreatedAsync();
173173

174-
var entities = repository.GetPaginatedAsync(null!, x => x.Id <= 10, x => x.Id, true, 2, 5);
174+
var entities = await repository.GetPaginatedAsync(null!, x => x.Id <= 10, x => x.Id, true, 2, 5);
175175

176176
Assert.NotNull(entities);
177-
Assert.Equal(5, entities?.Result.Count);
178-
Assert.Contains(entities!.Result, x => x.Id == 8);
177+
Assert.Equal(5, entities.Count);
178+
Assert.Contains(entities, x => x.Id == 8);
179179
}
180180

181181
[Fact]
182-
public void GetPaginatedEntitiesWithoutWhere()
182+
public async Task GetPaginatedEntitiesWithoutWhereAsync()
183183
{
184184
using var dbContext = GetDbContext();
185-
Repository<Person, int> repository = new(dbContext);
185+
var repository = new Repository<Person, int>(dbContext);
186186

187-
dbContext.Database.EnsureDeletedAsync();
188-
dbContext.Database.EnsureCreatedAsync();
187+
await dbContext.Database.EnsureDeletedAsync();
188+
await dbContext.Database.EnsureCreatedAsync();
189189

190-
var entities = repository.GetPaginatedAsync(null!, null!, x => x.Id, true, 1, 5);
190+
var entities = await repository.GetPaginatedAsync(null!, null!, x => x.Id, true, 1, 5);
191191

192192
Assert.NotNull(entities);
193-
Assert.Equal(5, entities?.Result.Count);
194-
Assert.Contains(entities!.Result, x => x.Id == 3);
193+
Assert.Equal(5, entities.Count);
194+
Assert.Contains(entities, x => x.Id == 3);
195195
}
196196

197197
[Fact]
198-
public void GetPaginatedEntitiesDescendingOrderType()
198+
public async Task GetPaginatedEntitiesDescendingOrderTypeAsync()
199199
{
200200
using var dbContext = GetDbContext();
201-
Repository<Person, int> repository = new(dbContext);
201+
var repository = new Repository<Person, int>(dbContext);
202202

203-
dbContext.Database.EnsureDeletedAsync();
204-
dbContext.Database.EnsureCreatedAsync();
203+
await dbContext.Database.EnsureDeletedAsync();
204+
await dbContext.Database.EnsureCreatedAsync();
205205

206-
var entities = repository.GetPaginatedAsync(null!, null!, x => x.Id, false, 1, 5);
206+
var entities = await repository.GetPaginatedAsync(null!, null!, x => x.Id, false, 1, 5);
207207

208208
Assert.NotNull(entities);
209-
Assert.Equal(5, entities?.Result.Count);
210-
Assert.Equal(10, Assert.IsType<Person>(entities?.Result.First()).Id);
211-
Assert.Contains(entities!.Result, x => x.Id == 8);
209+
Assert.Equal(5, entities.Count);
210+
Assert.Equal(10, Assert.IsType<Person>(entities.First()).Id);
211+
Assert.Contains(entities, x => x.Id == 8);
212212
}
213213
}

0 commit comments

Comments
 (0)