Skip to content

Commit 6dfd013

Browse files
committed
Added options to the DbContext by provided entity set type (#125)
1 parent 4ccc00b commit 6dfd013

File tree

12 files changed

+348
-35
lines changed

12 files changed

+348
-35
lines changed

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IDbContextBuilder.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@
99
public interface IDbContextBuilder
1010
{
1111
/// <summary>
12-
/// Sets initial values to the provided <see cref="DbContext"/> service.
12+
/// Sets initial values to the provided <see cref="DbContext"/>.
1313
/// </summary>
1414
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/> to set up.</typeparam>
1515
/// <param name="dbContextSetup">Action setting the <see cref="DbContext"/>.</param>
16-
void WithSetup<TDbContext>(Action<TDbContext> dbContextSetup)
16+
void WithEntities<TDbContext>(Action<TDbContext> dbContextSetup)
1717
where TDbContext : DbContext;
18+
19+
/// <summary>
20+
/// Sets initial values to the provided <see cref="DbContext"/> entity.
21+
/// </summary>
22+
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/> to set up.</typeparam>
23+
/// <typeparam name="TEntity">Type of entity to set up.</typeparam>
24+
/// <param name="entitySetup">Action setting the <see cref="DbContext"/> entity.</param>
25+
void WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>> entitySetup)
26+
where TDbContext : DbContext
27+
where TEntity : class;
1828
}
1929
}

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IDbContextTestBuilder.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,39 @@
99
public interface IDbContextTestBuilder
1010
{
1111
/// <summary>
12-
/// Tests whether <see cref="DbContext"/> entries pass the given assertions.
12+
/// Tests whether <see cref="DbContext"/> entities pass the given assertions.
1313
/// </summary>
1414
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/>.</typeparam>
1515
/// <param name="assertions">Action containing all assertions on the <see cref="DbContext"/> entities.</param>
16-
void WithEntries<TDbContext>(Action<TDbContext> assertions)
16+
void WithEntities<TDbContext>(Action<TDbContext> assertions)
1717
where TDbContext : DbContext;
1818

1919
/// <summary>
20-
/// Tests whether <see cref="DbContext"/> entries pass the given predicate.
20+
/// Tests whether <see cref="DbContext"/> entities pass the given predicate.
2121
/// </summary>
2222
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/>.</typeparam>
2323
/// <param name="predicate">Predicate testing the <see cref="DbContext"/> entities.</param>
24-
void WithEntries<TDbContext>(Func<TDbContext, bool> predicate)
24+
void WithEntities<TDbContext>(Func<TDbContext, bool> predicate)
2525
where TDbContext : DbContext;
26+
27+
/// <summary>
28+
/// Tests whether <see cref="DbContext"/> entity <see cref="DbSet{TEntity}"/> passes the given assertions.
29+
/// </summary>
30+
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/>.</typeparam>
31+
/// <typeparam name="TEntity">Type of entity set.</typeparam>
32+
/// <param name="assertions">Action containing all assertions on the <see cref="DbContext"/> entity set.</param>
33+
void WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>> assertions)
34+
where TDbContext : DbContext
35+
where TEntity : class;
36+
37+
/// <summary>
38+
/// Tests whether <see cref="DbContext"/> entity <see cref="DbSet{TEntity}"/> passes the given predicate.
39+
/// </summary>
40+
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/>.</typeparam>
41+
/// <typeparam name="TEntity">Type of entity set.</typeparam>
42+
/// <param name="predicate">Predicate testing the <see cref="DbContext"/> entity set.</param>
43+
void WithSet<TDbContext, TEntity>(Func<DbSet<TEntity>, bool> predicate)
44+
where TDbContext : DbContext
45+
where TEntity : class;
2646
}
2747
}

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/DbContextBuilder.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.EntityFrameworkCore;
88
using Microsoft.Extensions.DependencyInjection;
99
using Utilities.Validators;
10-
10+
using Internal;
1111
/// <summary>
1212
/// Used for building <see cref="DbContext"/>.
1313
/// </summary>
@@ -23,19 +23,26 @@ public DbContextBuilder(HttpTestContext testContext)
2323
}
2424

2525
/// <inheritdoc />
26-
public void WithSetup<TDbContext>(Action<TDbContext> dbContextSetup)
26+
public void WithEntities<TDbContext>(Action<TDbContext> dbContextSetup)
2727
where TDbContext : DbContext
2828
{
2929
CommonValidator.CheckForNullReference(dbContextSetup, nameof(dbContextSetup));
30-
ServiceValidator.ValidateScopedServiceLifetime<TDbContext>(nameof(WithSetup));
31-
32-
var dbContext = this.TestContext
33-
.HttpContext
34-
.RequestServices
35-
.GetRequiredService<TDbContext>();
3630

31+
var dbContext = this.TestContext.GetDbContext<TDbContext>();
3732
dbContextSetup(dbContext);
3833
dbContext.SaveChanges();
3934
}
35+
36+
/// <inheritdoc />
37+
public void WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>> entitySetup)
38+
where TDbContext : DbContext
39+
where TEntity : class
40+
{
41+
CommonValidator.CheckForNullReference(entitySetup, nameof(entitySetup));
42+
43+
var dbContext = this.TestContext.GetDbContext<TDbContext>();
44+
entitySetup(dbContext.Set<TEntity>());
45+
dbContext.SaveChanges();
46+
}
4047
}
4148
}

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/DbContextTestBuilder.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using Base;
55
using Contracts.Data;
66
using Exceptions;
7+
using Internal;
78
using Internal.TestContexts;
89
using Microsoft.EntityFrameworkCore;
9-
using Microsoft.Extensions.DependencyInjection;
1010
using Utilities;
1111
using Utilities.Extensions;
1212
using Utilities.Validators;
@@ -26,36 +26,54 @@ public DbContextTestBuilder(ControllerTestContext testContext)
2626
}
2727

2828
/// <inheritdoc />
29-
public void WithEntries<TDbContext>(Func<TDbContext, bool> predicate) where TDbContext : DbContext
29+
public void WithEntities<TDbContext>(Func<TDbContext, bool> predicate) where TDbContext : DbContext
3030
{
3131
CommonValidator.CheckForNullReference(predicate, nameof(predicate));
3232

33-
if (!predicate(this.GetDbContext<TDbContext>()))
33+
if (!predicate(this.TestContext.GetDbContext<TDbContext>()))
3434
{
3535
throw new DataProviderAssertionException(string.Format(
36-
"When calling {0} action in {1} expected the {2} entries to pass the given predicate, but it failed.",
36+
"When calling {0} action in {1} expected the {2} entities to pass the given predicate, but it failed.",
3737
this.TestContext.ActionName,
3838
this.TestContext.Controller.GetName(),
3939
typeof(TDbContext).ToFriendlyTypeName()));
4040
}
4141
}
4242

4343
/// <inheritdoc />
44-
public void WithEntries<TDbContext>(Action<TDbContext> assertions) where TDbContext : DbContext
44+
public void WithEntities<TDbContext>(Action<TDbContext> assertions) where TDbContext : DbContext
4545
{
4646
CommonValidator.CheckForNullReference(assertions, nameof(assertions));
4747

48-
assertions(this.GetDbContext<TDbContext>());
48+
assertions(this.TestContext.GetDbContext<TDbContext>());
4949
}
5050

51-
private TDbContext GetDbContext<TDbContext>()
51+
/// <inheritdoc />
52+
public void WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>> assertions)
53+
where TDbContext : DbContext
54+
where TEntity : class
5255
{
53-
ServiceValidator.ValidateScopedServiceLifetime<TDbContext>(nameof(WithEntries));
56+
CommonValidator.CheckForNullReference(assertions, nameof(assertions));
57+
58+
assertions(this.TestContext.GetDbContext<TDbContext>().Set<TEntity>());
59+
}
5460

55-
return this.TestContext
56-
.HttpContext
57-
.RequestServices
58-
.GetRequiredService<TDbContext>();
61+
/// <inheritdoc />
62+
public void WithSet<TDbContext, TEntity>(Func<DbSet<TEntity>, bool> predicate)
63+
where TDbContext : DbContext
64+
where TEntity : class
65+
{
66+
CommonValidator.CheckForNullReference(predicate, nameof(predicate));
67+
68+
if (!predicate(this.TestContext.GetDbContext<TDbContext>().Set<TEntity>()))
69+
{
70+
throw new DataProviderAssertionException(string.Format(
71+
"When calling {0} action in {1} expected the {2} set of {3} to pass the given predicate, but it failed.",
72+
this.TestContext.ActionName,
73+
this.TestContext.Controller.GetName(),
74+
typeof(TDbContext).ToFriendlyTypeName(),
75+
typeof(TEntity).ToFriendlyTypeName()));
76+
}
5977
}
6078
}
6179
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal
2+
{
3+
using Microsoft.Extensions.DependencyInjection;
4+
using TestContexts;
5+
using Utilities.Validators;
6+
7+
public static class HttpTestContextEntityFrameworkExtensions
8+
{
9+
public static TDbContext GetDbContext<TDbContext>(this HttpTestContext httpTestContext)
10+
where TDbContext : class
11+
{
12+
ServiceValidator.ValidateScopedServiceLifetime<TDbContext>(nameof(GetDbContext));
13+
14+
return httpTestContext
15+
.HttpContext
16+
.RequestServices
17+
.GetRequiredService<TDbContext>();
18+
}
19+
}
20+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionsTests.ShouldHaveTests
2+
{
3+
using Exceptions;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.EntityFrameworkCore;
6+
using Setups;
7+
using Setups.Common;
8+
using Xunit;
9+
10+
public class ShouldHaveDbContextTests
11+
{
12+
[Fact]
13+
public void DbContextShouldNotThrowExceptionWithCorrectAssertions()
14+
{
15+
MyMvc
16+
.IsUsingDefaultConfiguration()
17+
.WithServices(services =>
18+
{
19+
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase());
20+
});
21+
22+
MyMvc
23+
.Controller<DbContextController>()
24+
.Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" }))
25+
.ShouldHave()
26+
.DbContext(dbContext => dbContext
27+
.WithEntities<CustomDbContext>(db =>
28+
{
29+
Assert.NotNull(db.Models.FirstOrDefaultAsync(m => m.Id == 1));
30+
}))
31+
.AndAlso()
32+
.ShouldReturn()
33+
.Ok();
34+
35+
MyMvc.IsUsingDefaultConfiguration();
36+
}
37+
38+
[Fact]
39+
public void DbContextShouldNotThrowExceptionWithCorrectPredicate()
40+
{
41+
MyMvc
42+
.IsUsingDefaultConfiguration()
43+
.WithServices(services =>
44+
{
45+
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase());
46+
});
47+
48+
MyMvc
49+
.Controller<DbContextController>()
50+
.Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" }))
51+
.ShouldHave()
52+
.DbContext(dbContext => dbContext
53+
.WithEntities<CustomDbContext>(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null))
54+
.AndAlso()
55+
.ShouldReturn()
56+
.Ok();
57+
58+
MyMvc.IsUsingDefaultConfiguration();
59+
}
60+
61+
[Fact]
62+
public void DbContextShouldThrowExceptionWithIncorrectPredicate()
63+
{
64+
MyMvc
65+
.IsUsingDefaultConfiguration()
66+
.WithServices(services =>
67+
{
68+
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase());
69+
});
70+
71+
MyMvc
72+
.Controller<DbContextController>()
73+
.Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" }))
74+
.ShouldHave()
75+
.DbContext(dbContext => dbContext
76+
.WithEntities<CustomDbContext>(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null))
77+
.AndAlso()
78+
.ShouldReturn()
79+
.Ok();
80+
81+
Test.AssertException<DataProviderAssertionException>(() =>
82+
{
83+
MyMvc
84+
.Controller<DbContextController>()
85+
.Calling(c => c.Create(new CustomModel { Id = 2, Name = "Test" }))
86+
.ShouldHave()
87+
.DbContext(dbContext => dbContext
88+
.WithEntities<CustomDbContext>(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) == null))
89+
.AndAlso()
90+
.ShouldReturn()
91+
.Ok();
92+
},
93+
"When calling Create action in DbContextController expected the CustomDbContext entities to pass the given predicate, but it failed.");
94+
95+
MyMvc.IsUsingDefaultConfiguration();
96+
}
97+
98+
[Fact]
99+
public void DbContextWithSetShouldNotThrowExceptionWithCorrectAssertions()
100+
{
101+
MyMvc
102+
.IsUsingDefaultConfiguration()
103+
.WithServices(services =>
104+
{
105+
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase());
106+
});
107+
108+
MyMvc
109+
.Controller<DbContextController>()
110+
.Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" }))
111+
.ShouldHave()
112+
.DbContext(dbContext => dbContext
113+
.WithSet<CustomDbContext, CustomModel>(set =>
114+
{
115+
Assert.NotNull(set.FirstOrDefaultAsync(m => m.Id == 1));
116+
}))
117+
.AndAlso()
118+
.ShouldReturn()
119+
.Ok();
120+
121+
MyMvc.IsUsingDefaultConfiguration();
122+
}
123+
124+
[Fact]
125+
public void DbContextWithSetShouldNotThrowExceptionWithCorrectPredicate()
126+
{
127+
MyMvc
128+
.IsUsingDefaultConfiguration()
129+
.WithServices(services =>
130+
{
131+
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase());
132+
});
133+
134+
MyMvc
135+
.Controller<DbContextController>()
136+
.Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" }))
137+
.ShouldHave()
138+
.DbContext(dbContext => dbContext
139+
.WithSet<CustomDbContext, CustomModel>(set => set.FirstOrDefaultAsync(m => m.Id == 1) != null))
140+
.AndAlso()
141+
.ShouldReturn()
142+
.Ok();
143+
144+
MyMvc.IsUsingDefaultConfiguration();
145+
}
146+
147+
[Fact]
148+
public void DbContextWithSetShouldThrowExceptionWithIncorrectPredicate()
149+
{
150+
MyMvc
151+
.IsUsingDefaultConfiguration()
152+
.WithServices(services =>
153+
{
154+
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase());
155+
});
156+
157+
MyMvc
158+
.Controller<DbContextController>()
159+
.Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" }))
160+
.ShouldHave()
161+
.DbContext(dbContext => dbContext
162+
.WithSet<CustomDbContext, CustomModel>(set => set.FirstOrDefaultAsync(m => m.Id == 1) != null))
163+
.AndAlso()
164+
.ShouldReturn()
165+
.Ok();
166+
167+
Test.AssertException<DataProviderAssertionException>(() =>
168+
{
169+
MyMvc
170+
.Controller<DbContextController>()
171+
.Calling(c => c.Create(new CustomModel { Id = 2, Name = "Test" }))
172+
.ShouldHave()
173+
.DbContext(dbContext => dbContext
174+
.WithSet<CustomDbContext, CustomModel>(set => set.FirstOrDefaultAsync(m => m.Id == 1) == null))
175+
.AndAlso()
176+
.ShouldReturn()
177+
.Ok();
178+
},
179+
"When calling Create action in DbContextController expected the CustomDbContext set of CustomModel to pass the given predicate, but it failed.");
180+
181+
MyMvc.IsUsingDefaultConfiguration();
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)