Skip to content

Commit 19ad16d

Browse files
committed
Refactoring testing libreria
1 parent 56961bb commit 19ad16d

File tree

5 files changed

+90
-54
lines changed

5 files changed

+90
-54
lines changed

src/ClassLibrary.EFCore.Tests/ClassLibrary.EFCore.Tests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
@@ -9,8 +9,9 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="Bogus" Version="35.6.1" />
1213
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
1415
<PackageReference Include="xunit" Version="2.9.2" />
1516
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1617
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1-
using ClassLibrary.EFCore.Tests.Entities;
1+
using Bogus;
2+
using ClassLibrary.EFCore.Tests.Entities;
23
using Microsoft.EntityFrameworkCore;
4+
using Persone = ClassLibrary.EFCore.Tests.Entities.Person;
35

46
namespace ClassLibrary.EFCore.Tests.DatabaseContext;
57

68
public class UnitTestDbContext(DbContextOptions<UnitTestDbContext> options) : DbContext(options)
79
{
8-
public DbSet<Person> People { get; set; }
10+
public DbSet<Persone> Persone { get; set; }
11+
public DbSet<Address> Indirizzi { get; set; }
912

1013
protected override void OnModelCreating(ModelBuilder modelBuilder)
1114
{
12-
modelBuilder.Entity<Person>()
13-
.HasData(new Person { Id = 1, Cognome = "Cognome1", Nome = "Nome1" },
14-
new Person { Id = 2, Cognome = "Cognome2", Nome = "Nome2" },
15-
new Person { Id = 3, Cognome = "Cognome3", Nome = "Nome3" },
16-
new Person { Id = 4, Cognome = "Cognome4", Nome = "Nome4" },
17-
new Person { Id = 5, Cognome = "Cognome5", Nome = "Nome5" },
18-
new Person { Id = 6, Cognome = "Cognome6", Nome = "Nome6" },
19-
new Person { Id = 7, Cognome = "Cognome7", Nome = "Nome7" },
20-
new Person { Id = 8, Cognome = "Cognome8", Nome = "Nome8" },
21-
new Person { Id = 9, Cognome = "Cognome9", Nome = "Nome9" },
22-
new Person { Id = 10, Cognome = "Cognome10", Nome = "Nome10" }
23-
);
15+
var fakerAddresses = new Faker<Address>("it")
16+
.RuleFor(x => x.Id, f => f.IndexFaker + 1)
17+
.RuleFor(x => x.Citta, f => f.Address.City())
18+
.RuleFor(x => x.Cap, f => f.Address.ZipCode())
19+
.RuleFor(x => x.Provincia, f => f.Address.State());
20+
21+
var indirizzi = fakerAddresses.Generate(10);
22+
modelBuilder.Entity<Address>().HasData(indirizzi);
23+
24+
var fakerPeople = new Faker<Persone>("it")
25+
.RuleFor(x => x.Id, f => f.IndexFaker + 1)
26+
.RuleFor(x => x.Cognome, f => f.Person.LastName)
27+
.RuleFor(x => x.Nome, f => f.Person.FirstName)
28+
.RuleFor(d => d.IndirizzoId, f => f.PickRandom(indirizzi).Id);
29+
30+
var persone = fakerPeople.Generate(10);
31+
modelBuilder.Entity<Persone>().HasData(persone);
2432
}
2533
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ClassLibrary.EFCore.Tests.Entities;
2+
3+
public class Address
4+
{
5+
public int Id { get; set; }
6+
public string Citta { get; set; } = null!;
7+
public string Cap { get; set; } = null!;
8+
public string Provincia { get; set; } = null!;
9+
}

src/ClassLibrary.EFCore.Tests/Entities/Person.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace ClassLibrary.EFCore.Tests.Entities;
55
public class Person : IEntity<int>
66
{
77
public int Id { get; set; }
8-
public string Cognome { get; set; } = string.Empty;
9-
public string Nome { get; set; } = string.Empty;
8+
public string Cognome { get; set; } = null!;
9+
public string Nome { get; set; } = null!;
10+
public int IndirizzoId { get; set; }
11+
public Address Indirizzo { get; set; } = default!;
1012
}

src/ClassLibrary.EFCore.Tests/Tests.cs

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
using Bogus;
12
using ClassLibrary.EFCore.Tests.DatabaseContext;
2-
using ClassLibrary.EFCore.Tests.Entities;
3+
using Microsoft.EntityFrameworkCore;
34
using Xunit;
5+
using Persone = ClassLibrary.EFCore.Tests.Entities.Person;
46

57
namespace ClassLibrary.EFCore.Tests;
68

79
public class Tests : InMemoryDbContext
810
{
911
[Fact]
10-
public async Task GetAllEntities()
12+
public async Task GetAllEntitiesAsync()
1113
{
1214
using var dbContext = GetDbContext();
13-
var repository = new Repository<Person, int>(dbContext);
15+
var repository = new Repository<Persone, int>(dbContext);
1416

1517
await dbContext.Database.EnsureDeletedAsync();
1618
await dbContext.Database.EnsureCreatedAsync();
@@ -24,7 +26,7 @@ public async Task GetAllEntities()
2426
public async Task GetAllEntitiesWithPredicateAsync()
2527
{
2628
using var dbContext = GetDbContext();
27-
var repository = new Repository<Person, int>(dbContext);
29+
var repository = new Repository<Persone, int>(dbContext);
2830

2931
await dbContext.Database.EnsureDeletedAsync();
3032
await dbContext.Database.EnsureCreatedAsync();
@@ -39,24 +41,24 @@ public async Task GetAllEntitiesWithPredicateAsync()
3941
public async Task GetEntityByIdAsync()
4042
{
4143
using var dbContext = GetDbContext();
42-
var repository = new Repository<Person, int>(dbContext);
44+
var repository = new Repository<Persone, int>(dbContext);
4345

4446
await dbContext.Database.EnsureDeletedAsync();
4547
await dbContext.Database.EnsureCreatedAsync();
4648

4749
var entity = await repository.GetByIdAsync(2);
4850

4951
Assert.NotNull(entity);
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);
52+
Assert.Equal(2, Assert.IsType<Persone>(entity).Id);
53+
Assert.Equal(entity.Nome, Assert.IsType<Persone>(entity).Nome);
54+
Assert.Equal(entity.Cognome, Assert.IsType<Persone>(entity).Cognome);
5355
}
5456

5557
[Fact]
5658
public async Task GetEntityByIdNotFoundAsync()
5759
{
5860
using var dbContext = GetDbContext();
59-
var repository = new Repository<Person, int>(dbContext);
61+
var repository = new Repository<Persone, int>(dbContext);
6062

6163
await dbContext.Database.EnsureDeletedAsync();
6264
await dbContext.Database.EnsureCreatedAsync();
@@ -70,52 +72,66 @@ public async Task GetEntityByIdNotFoundAsync()
7072
public async Task CreateEntityAsync()
7173
{
7274
using var dbContext = GetDbContext();
73-
var repository = new Repository<Person, int>(dbContext);
75+
var repository = new Repository<Persone, int>(dbContext);
7476

7577
await dbContext.Database.EnsureDeletedAsync();
7678
await dbContext.Database.EnsureCreatedAsync();
7779

78-
var entity = new Person
79-
{
80-
Nome = "Nome11",
81-
Cognome = "Cognome11"
82-
};
80+
var personFaker = new Faker<Persone>("it")
81+
.RuleFor(p => p.Id, f => f.IndexFaker + f.Random.Number(11, 100))
82+
.RuleFor(p => p.Cognome, f => f.Person.LastName)
83+
.RuleFor(p => p.Nome, f => f.Person.FirstName)
84+
.RuleFor(p => p.IndirizzoId, f => f.Random.Int(1, 10));
85+
86+
var entity = personFaker.Generate();
8387

84-
await repository.CreateAsync(entity);
88+
await repository.CreateAsync(personFaker);
8589

8690
Assert.NotNull(entity);
87-
Assert.Equal(11, entity.Id);
88-
Assert.Equal("Nome11", entity.Nome);
89-
Assert.Equal("Cognome11", entity.Cognome);
91+
Assert.True(entity.Id > 0);
92+
Assert.True(entity.Id <= 100);
93+
Assert.False(string.IsNullOrEmpty(entity.Nome));
94+
Assert.False(string.IsNullOrEmpty(entity.Cognome));
9095
}
9196

9297
[Fact]
9398
public async Task UpdateEntityAsync()
9499
{
95100
using var dbContext = GetDbContext();
96-
var repository = new Repository<Person, int>(dbContext);
101+
var repository = new Repository<Persone, int>(dbContext);
97102

98103
await dbContext.Database.EnsureDeletedAsync();
99104
await dbContext.Database.EnsureCreatedAsync();
100105

101106
var entity = await repository.GetByIdAsync(2);
102107

103-
entity.Nome = "Nome2-bis";
104-
entity.Cognome = "Cognome2-bis";
108+
if (entity == null)
109+
{
110+
return;
111+
}
112+
113+
var personFaker = new Faker<Persone>("it")
114+
.RuleFor(p => p.Cognome, f => f.Person.LastName)
115+
.RuleFor(p => p.Nome, f => f.Person.FirstName);
116+
117+
var newEntity = personFaker.Generate();
118+
119+
entity.Nome = newEntity.Nome;
120+
entity.Cognome = newEntity.Cognome;
105121

106122
await repository.UpdateAsync(entity);
107123

108124
Assert.NotNull(entity);
109-
Assert.Equal(2, entity.Id);
110-
Assert.Equal("Nome2-bis", entity.Nome);
111-
Assert.Equal("Cognome2-bis", entity.Cognome);
125+
Assert.Equal(2, Assert.IsType<Persone>(entity).Id);
126+
Assert.Equal(newEntity.Nome, Assert.IsType<Persone>(entity).Nome);
127+
Assert.Equal(newEntity.Cognome, Assert.IsType<Persone>(entity).Cognome);
112128
}
113129

114130
[Fact]
115131
public async Task DeleteEntityAsync()
116132
{
117133
using var dbContext = GetDbContext();
118-
var repository = new Repository<Person, int>(dbContext);
134+
var repository = new Repository<Persone, int>(dbContext);
119135

120136
await dbContext.Database.EnsureDeletedAsync();
121137
await dbContext.Database.EnsureCreatedAsync();
@@ -133,7 +149,7 @@ public async Task DeleteEntityAsync()
133149
public async Task DeleteByIdEntityAsync()
134150
{
135151
using var dbContext = GetDbContext();
136-
var repository = new Repository<Person, int>(dbContext);
152+
var repository = new Repository<Persone, int>(dbContext);
137153

138154
await dbContext.Database.EnsureDeletedAsync();
139155
await dbContext.Database.EnsureCreatedAsync();
@@ -150,12 +166,12 @@ public async Task DeleteByIdEntityAsync()
150166
public async Task GetPaginatedEntitiesAsync()
151167
{
152168
using var dbContext = GetDbContext();
153-
var repository = new Repository<Person, int>(dbContext);
169+
var repository = new Repository<Persone, int>(dbContext);
154170

155171
await dbContext.Database.EnsureDeletedAsync();
156172
await dbContext.Database.EnsureCreatedAsync();
157173

158-
var entities = await repository.GetPaginatedAsync(null!, x => x.Id <= 10, x => x.Id, true, 2, 5);
174+
var entities = await repository.GetPaginatedAsync(includes: query => query.Include(p => p.Indirizzo), x => x.Id <= 10, x => x.Id, ascending: true, pageIndex: 2, pageSize: 5);
159175

160176
Assert.NotNull(entities);
161177
Assert.Equal(5, entities.Count);
@@ -166,12 +182,12 @@ public async Task GetPaginatedEntitiesAsync()
166182
public async Task GetPaginatedEntitiesWithoutIncludeAsync()
167183
{
168184
using var dbContext = GetDbContext();
169-
var repository = new Repository<Person, int>(dbContext);
185+
var repository = new Repository<Persone, int>(dbContext);
170186

171187
await dbContext.Database.EnsureDeletedAsync();
172188
await dbContext.Database.EnsureCreatedAsync();
173189

174-
var entities = await repository.GetPaginatedAsync(null!, x => x.Id <= 10, x => x.Id, true, 2, 5);
190+
var entities = await repository.GetPaginatedAsync(includes: null!, x => x.Id <= 10, x => x.Id, ascending: true, pageIndex: 2, pageSize: 5);
175191

176192
Assert.NotNull(entities);
177193
Assert.Equal(5, entities.Count);
@@ -182,12 +198,12 @@ public async Task GetPaginatedEntitiesWithoutIncludeAsync()
182198
public async Task GetPaginatedEntitiesWithoutWhereAsync()
183199
{
184200
using var dbContext = GetDbContext();
185-
var repository = new Repository<Person, int>(dbContext);
201+
var repository = new Repository<Persone, int>(dbContext);
186202

187203
await dbContext.Database.EnsureDeletedAsync();
188204
await dbContext.Database.EnsureCreatedAsync();
189205

190-
var entities = await repository.GetPaginatedAsync(null!, null!, x => x.Id, true, 1, 5);
206+
var entities = await repository.GetPaginatedAsync(includes: query => query.Include(p => p.Indirizzo), conditionWhere: null!, x => x.Id, ascending: true, pageIndex: 1, pageSize: 5);
191207

192208
Assert.NotNull(entities);
193209
Assert.Equal(5, entities.Count);
@@ -198,16 +214,16 @@ public async Task GetPaginatedEntitiesWithoutWhereAsync()
198214
public async Task GetPaginatedEntitiesDescendingOrderTypeAsync()
199215
{
200216
using var dbContext = GetDbContext();
201-
var repository = new Repository<Person, int>(dbContext);
217+
var repository = new Repository<Persone, int>(dbContext);
202218

203219
await dbContext.Database.EnsureDeletedAsync();
204220
await dbContext.Database.EnsureCreatedAsync();
205221

206-
var entities = await repository.GetPaginatedAsync(null!, null!, x => x.Id, false, 1, 5);
222+
var entities = await repository.GetPaginatedAsync(includes: query => query.Include(p => p.Indirizzo), conditionWhere: null!, x => x.Id, ascending: false, pageIndex: 1, pageSize: 5);
207223

208224
Assert.NotNull(entities);
209225
Assert.Equal(5, entities.Count);
210-
Assert.Equal(10, Assert.IsType<Person>(entities.First()).Id);
226+
Assert.Equal(10, Assert.IsType<Persone>(entities.First()).Id);
211227
Assert.Contains(entities, x => x.Id == 8);
212228
}
213229
}

0 commit comments

Comments
 (0)