Skip to content

Commit 4a3ce62

Browse files
committed
Add tests - EF6 Repository write actions.
1 parent 57108fc commit 4a3ce62

2 files changed

Lines changed: 182 additions & 0 deletions

File tree

tests/Ardalis.Specification.EntityFramework6.Tests/FixtureNew/IntegrationTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections;
2+
using System.Data.Entity;
3+
using System.Data.Entity.Infrastructure;
24
using System.IO;
35

46
namespace Tests.FixtureNew;
@@ -32,6 +34,14 @@ public static string GetQueryString<T>(TestDbContext dbContext, IQueryable<T> qu
3234
return string.Join(Environment.NewLine, filteredLines).Trim();
3335
}
3436

37+
public void ClearChangeTracker()
38+
{
39+
foreach (var entry in DbContext.ChangeTracker.Entries())
40+
{
41+
entry.State = EntityState.Detached;
42+
}
43+
}
44+
3545
public Task InitializeAsync()
3646
{
3747
DbContext = new TestDbContext(_testFactory.ConnectionString);
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
using System.Data.Entity;
2+
using Tests.FixtureNew;
3+
4+
namespace Tests.Repositories;
5+
6+
[Collection("SharedCollection")]
7+
public class Repository_WriteTests(TestFactory factory) : IntegrationTest(factory)
8+
{
9+
[Fact]
10+
public async Task AddAsync_ShouldAddEntity()
11+
{
12+
var repo = new Repository<Country>(DbContext);
13+
var country = new Country
14+
{
15+
Name = Guid.NewGuid().ToString(),
16+
};
17+
18+
await repo.AddAsync(country);
19+
ClearChangeTracker();
20+
21+
var countriesInDb = await DbContext.Countries.ToListAsync();
22+
countriesInDb.Should().ContainSingle();
23+
countriesInDb.First().Name.Should().Be(country.Name);
24+
}
25+
26+
[Fact]
27+
public async Task AddAsync_ShouldAddMultipleEntities()
28+
{
29+
var repo = new Repository<Country>(DbContext);
30+
var countries = new[]
31+
{
32+
new Country { Name = Guid.NewGuid().ToString() },
33+
new Country { Name = Guid.NewGuid().ToString() },
34+
new Country { Name = Guid.NewGuid().ToString() },
35+
};
36+
37+
await repo.AddRangeAsync(countries);
38+
ClearChangeTracker();
39+
40+
var countriesInDb = await DbContext.Countries.ToListAsync();
41+
countriesInDb.Should().HaveCount(3);
42+
countriesInDb.Select(x => x.Name).Should().BeEquivalentTo(countries.Select(x => x.Name));
43+
}
44+
45+
[Fact]
46+
public async Task UpdateAsync_ShouldUpdateEntity()
47+
{
48+
var repo = new Repository<Country>(DbContext);
49+
var country = new Country
50+
{
51+
Name = Guid.NewGuid().ToString(),
52+
};
53+
await SeedAsync(country);
54+
55+
country = await DbContext.Countries.FirstAsync();
56+
country.Name = Guid.NewGuid().ToString();
57+
await repo.UpdateAsync(country);
58+
ClearChangeTracker();
59+
60+
var countriesInDb = await DbContext.Countries.ToListAsync();
61+
countriesInDb.Should().NotBeNull();
62+
countriesInDb.Should().ContainSingle();
63+
countriesInDb.First().Name.Should().Be(country.Name);
64+
}
65+
66+
[Fact]
67+
public async Task UpdateRangeAsync_ShouldUpdateEntities()
68+
{
69+
var repo = new Repository<Country>(DbContext);
70+
var countries = new[]
71+
{
72+
new Country { Name = Guid.NewGuid().ToString() },
73+
new Country { Name = Guid.NewGuid().ToString() },
74+
new Country { Name = Guid.NewGuid().ToString() },
75+
};
76+
await SeedRangeAsync(countries);
77+
78+
countries = await DbContext.Countries.ToArrayAsync();
79+
foreach (var country in countries)
80+
{
81+
country.Name = Guid.NewGuid().ToString();
82+
}
83+
await repo.UpdateRangeAsync(countries);
84+
ClearChangeTracker();
85+
86+
var countriesInDb = await DbContext.Countries.ToListAsync();
87+
countriesInDb.Should().NotBeNull();
88+
countriesInDb[0].Name.Should().Be(countries[0].Name);
89+
countriesInDb[1].Name.Should().Be(countries[1].Name);
90+
countriesInDb[2].Name.Should().Be(countries[2].Name);
91+
}
92+
93+
[Fact]
94+
public async Task DeleteAsync_ShouldDeleteEntity()
95+
{
96+
var repo = new Repository<Country>(DbContext);
97+
var country = new Country
98+
{
99+
Name = Guid.NewGuid().ToString(),
100+
};
101+
await SeedAsync(country);
102+
103+
var countryInDb = await DbContext.Countries.FirstAsync();
104+
await repo.DeleteAsync(countryInDb);
105+
ClearChangeTracker();
106+
107+
var countriesInDb = await DbContext.Countries.ToListAsync();
108+
countriesInDb.Should().BeEmpty();
109+
}
110+
111+
[Fact]
112+
public async Task DeleteRangeAsync_ShouldDeleteEntities()
113+
{
114+
var repo = new Repository<Country>(DbContext);
115+
var countries = new[]
116+
{
117+
new Country { Name = Guid.NewGuid().ToString() },
118+
new Country { Name = Guid.NewGuid().ToString() },
119+
new Country { Name = Guid.NewGuid().ToString() },
120+
};
121+
await SeedRangeAsync(countries);
122+
123+
var countriesInDb = await DbContext.Countries.ToListAsync();
124+
await repo.DeleteRangeAsync(countriesInDb);
125+
ClearChangeTracker();
126+
127+
countriesInDb = await DbContext.Countries.ToListAsync();
128+
countriesInDb.Should().BeEmpty();
129+
}
130+
131+
[Fact]
132+
public async Task DeleteRangeAsync_ShouldDeleteEntitiesBySpec()
133+
{
134+
var guid = Guid.NewGuid().ToString();
135+
var repo = new Repository<Country>(DbContext);
136+
var countries = new[]
137+
{
138+
new Country { Name = guid },
139+
new Country { Name = guid },
140+
new Country { Name = guid },
141+
};
142+
await SeedRangeAsync(countries);
143+
144+
var spec = new Specification<Country>();
145+
spec.Query.Where(x => x.Name == guid);
146+
await repo.DeleteRangeAsync(spec);
147+
ClearChangeTracker();
148+
149+
var countriesInDb = await DbContext.Countries.ToListAsync();
150+
countriesInDb.Should().BeEmpty();
151+
}
152+
153+
[Fact]
154+
public async Task DeleteAsync_ShouldDeleteMultipleEntities()
155+
{
156+
var repo = new Repository<Country>(DbContext);
157+
var countries = new[]
158+
{
159+
new Country { Name = Guid.NewGuid().ToString() },
160+
new Country { Name = Guid.NewGuid().ToString() },
161+
new Country { Name = Guid.NewGuid().ToString() },
162+
};
163+
await SeedRangeAsync(countries);
164+
165+
var countriesInDb = await DbContext.Countries.ToListAsync();
166+
await repo.DeleteRangeAsync(countriesInDb);
167+
ClearChangeTracker();
168+
169+
countriesInDb = await DbContext.Countries.ToListAsync();
170+
countriesInDb.Should().BeEmpty();
171+
}
172+
}

0 commit comments

Comments
 (0)