Skip to content

Commit 545ae35

Browse files
committed
Fixed various styling errors
1 parent c3eef39 commit 545ae35

File tree

4 files changed

+564
-563
lines changed

4 files changed

+564
-563
lines changed
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
namespace MyTested.AspNetCore.Mvc
2-
{
1+
namespace MyTested.AspNetCore.Mvc
2+
{
33
using System.Collections.Generic;
4-
54
using Microsoft.Extensions.Configuration;
65

7-
public static class ConfigurationBuilderExtensions
8-
{
9-
/// <summary>
10-
/// Adds the provided key-value pair to the configuration builder.
11-
/// </summary>
12-
/// <param name="configurationBuilder">The <see cref="Microsoft.Extensions.Configuration.IConfigurationBuilder" /> to add to.</param>
13-
/// <param name="key">The configuration key to add.</param>
14-
/// <param name="value">The configuration value to add.</param>
15-
/// <returns>The same <see cref="Microsoft.Extensions.Configuration.IConfigurationBuilder" />.</returns>
16-
public static IConfigurationBuilder Add(
17-
this IConfigurationBuilder configurationBuilder,
18-
string key,
19-
string value)
20-
=> configurationBuilder.AddInMemoryCollection(new[]
21-
{
22-
new KeyValuePair<string, string>(key, value)
23-
});
24-
}
25-
}
6+
public static class ConfigurationBuilderExtensions
7+
{
8+
/// <summary>
9+
/// Adds the provided key-value pair to the configuration builder.
10+
/// </summary>
11+
/// <param name="configurationBuilder">The <see cref="Microsoft.Extensions.Configuration.IConfigurationBuilder" /> to add to.</param>
12+
/// <param name="key">The configuration key to add.</param>
13+
/// <param name="value">The configuration value to add.</param>
14+
/// <returns>The same <see cref="Microsoft.Extensions.Configuration.IConfigurationBuilder" />.</returns>
15+
public static IConfigurationBuilder Add(
16+
this IConfigurationBuilder configurationBuilder,
17+
string key,
18+
string value)
19+
=> configurationBuilder.AddInMemoryCollection(new[]
20+
{
21+
new KeyValuePair<string, string>(key, value)
22+
});
23+
}
24+
}

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Internal/EntityFrameworkCore/ScopedInMemoryOptionsExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
#pragma warning disable EF1001 // Internal EF Core API usage.
99
public class ScopedInMemoryOptionsExtension : InMemoryOptionsExtension
10-
//#pragma warning restore EF1001 // Internal EF Core API usage.
1110
{
1211
public override void ApplyServices(IServiceCollection services)
1312
=> services
1413
.AddScoped<IMigrator, MigratorMock>()
1514
.ReplaceLifetime<IInMemorySingletonOptions>(ServiceLifetime.Scoped)
1615
.ReplaceLifetime<IInMemoryStoreCache>(ServiceLifetime.Scoped)
1716
.ReplaceLifetime<IInMemoryTableFactory>(ServiceLifetime.Scoped);
18-
}
17+
}
18+
#pragma warning restore EF1001 // Internal EF Core API usage.
1919
}
Lines changed: 116 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,118 @@
1-
namespace MyTested.AspNetCore.Mvc.Test
2-
{
3-
using System.Linq;
4-
using Internal.EntityFrameworkCore;
5-
using Microsoft.EntityFrameworkCore;
6-
using Microsoft.EntityFrameworkCore.Infrastructure;
7-
using Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal;
8-
using Microsoft.Extensions.DependencyInjection;
9-
using Setups;
10-
using Setups.Common;
1+
namespace MyTested.AspNetCore.Mvc.Test
2+
{
3+
using System.Linq;
4+
using Internal.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.EntityFrameworkCore.Infrastructure;
7+
using Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Setups;
10+
using Setups.Common;
1111
using Xunit;
1212

13-
#pragma warning disable EF1001 // Internal EF Core API usage.
14-
public class ServicesTests
15-
{
16-
[Fact]
17-
public void ReplaceDbContextShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOne()
18-
{
19-
var services = new ServiceCollection();
20-
21-
this.AddDbContextWithSqlServer(services);
22-
23-
services.ReplaceDbContext();
24-
25-
this.AssertCorrectDbContextAndOptions(services);
26-
}
27-
28-
[Fact]
29-
public void ReplaceDbContextShouldReplaceInMemoryDatabaseWithInMemoryScopedOne()
30-
{
31-
var services = new ServiceCollection();
32-
33-
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName));
34-
35-
services.ReplaceDbContext();
36-
37-
this.AssertCorrectDbContextAndOptions(services);
38-
}
39-
40-
[Fact]
41-
public void ReplaceDbContextShouldNotAddDbContextIfMissing()
42-
{
43-
var services = new ServiceCollection();
44-
45-
services.ReplaceDbContext();
46-
47-
var serviceProvider = services.BuildServiceProvider();
48-
49-
Assert.Null(serviceProvider.GetService<DbContext>());
50-
}
51-
52-
[Fact]
53-
public void ReplaceDbContextShouldReplaceMultipleDbContextTypes()
54-
{
55-
var services = new ServiceCollection();
56-
57-
this.AddDbContextWithSqlServer<CustomDbContext>(services);
58-
this.AddDbContextWithSqlServer<AnotherDbContext>(services);
59-
60-
services.ReplaceDbContext();
61-
62-
this.AssertCorrectDbContextAndOptions<CustomDbContext>(services);
63-
this.AssertCorrectDbContextAndOptions<AnotherDbContext>(services);
64-
}
65-
66-
[Fact]
67-
public void CallingMigrateShouldNotThrowExceptionWithInMemoryDatabase()
68-
{
69-
var services = new ServiceCollection();
70-
71-
this.AddDbContextWithSqlServer(services);
72-
73-
services.ReplaceDbContext();
74-
75-
services.BuildServiceProvider().GetRequiredService<CustomDbContext>().Database.Migrate();
76-
}
77-
78-
private void AddDbContextWithSqlServer(IServiceCollection services)
79-
=> this.AddDbContextWithSqlServer<CustomDbContext>(services);
80-
81-
private void AddDbContextWithSqlServer<TDbContext>(IServiceCollection services)
82-
where TDbContext : DbContext
83-
=> services.AddDbContext<TDbContext>(options =>
84-
options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"));
85-
86-
private void AssertCorrectDbContextAndOptions(IServiceCollection services)
87-
=> this.AssertCorrectDbContextAndOptions<CustomDbContext>(services);
88-
89-
private void AssertCorrectDbContextAndOptions<TDbContext>(IServiceCollection services)
90-
where TDbContext : DbContext
91-
{
92-
var serviceProvider = services.BuildServiceProvider();
93-
94-
var dbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(TDbContext));
95-
96-
Assert.NotNull(dbContextService);
97-
Assert.Equal(ServiceLifetime.Scoped, dbContextService.Lifetime);
98-
99-
var customDbContext = serviceProvider.GetService<TDbContext>();
100-
101-
Assert.NotNull(customDbContext);
102-
103-
var dbContextOptions = serviceProvider.GetService<DbContextOptions<TDbContext>>();
104-
105-
Assert.NotNull(dbContextOptions);
106-
Assert.Equal(3, dbContextOptions.Extensions.Count());
107-
108-
var coreOptionsExtension = dbContextOptions.FindExtension<CoreOptionsExtension>();
109-
var inMemoryOptionsExtension = dbContextOptions.FindExtension<InMemoryOptionsExtension>();
110-
var scopedInMemoryOptionsExtension = dbContextOptions.FindExtension<ScopedInMemoryOptionsExtension>();
111-
112-
Assert.NotNull(coreOptionsExtension);
113-
Assert.NotNull(inMemoryOptionsExtension);
114-
Assert.NotNull(scopedInMemoryOptionsExtension);
115-
}
116-
}
117-
}
13+
public class ServicesTests
14+
{
15+
[Fact]
16+
public void ReplaceDbContextShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOne()
17+
{
18+
var services = new ServiceCollection();
19+
20+
this.AddDbContextWithSqlServer(services);
21+
22+
services.ReplaceDbContext();
23+
24+
this.AssertCorrectDbContextAndOptions(services);
25+
}
26+
27+
[Fact]
28+
public void ReplaceDbContextShouldReplaceInMemoryDatabaseWithInMemoryScopedOne()
29+
{
30+
var services = new ServiceCollection();
31+
32+
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName));
33+
34+
services.ReplaceDbContext();
35+
36+
this.AssertCorrectDbContextAndOptions(services);
37+
}
38+
39+
[Fact]
40+
public void ReplaceDbContextShouldNotAddDbContextIfMissing()
41+
{
42+
var services = new ServiceCollection();
43+
44+
services.ReplaceDbContext();
45+
46+
var serviceProvider = services.BuildServiceProvider();
47+
48+
Assert.Null(serviceProvider.GetService<DbContext>());
49+
}
50+
51+
[Fact]
52+
public void ReplaceDbContextShouldReplaceMultipleDbContextTypes()
53+
{
54+
var services = new ServiceCollection();
55+
56+
this.AddDbContextWithSqlServer<CustomDbContext>(services);
57+
this.AddDbContextWithSqlServer<AnotherDbContext>(services);
58+
59+
services.ReplaceDbContext();
60+
61+
this.AssertCorrectDbContextAndOptions<CustomDbContext>(services);
62+
this.AssertCorrectDbContextAndOptions<AnotherDbContext>(services);
63+
}
64+
65+
[Fact]
66+
public void CallingMigrateShouldNotThrowExceptionWithInMemoryDatabase()
67+
{
68+
var services = new ServiceCollection();
69+
70+
this.AddDbContextWithSqlServer(services);
71+
72+
services.ReplaceDbContext();
73+
74+
services.BuildServiceProvider().GetRequiredService<CustomDbContext>().Database.Migrate();
75+
}
76+
77+
private void AddDbContextWithSqlServer(IServiceCollection services)
78+
=> this.AddDbContextWithSqlServer<CustomDbContext>(services);
79+
80+
private void AddDbContextWithSqlServer<TDbContext>(IServiceCollection services)
81+
where TDbContext : DbContext
82+
=> services.AddDbContext<TDbContext>(options =>
83+
options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"));
84+
85+
private void AssertCorrectDbContextAndOptions(IServiceCollection services)
86+
=> this.AssertCorrectDbContextAndOptions<CustomDbContext>(services);
87+
88+
private void AssertCorrectDbContextAndOptions<TDbContext>(IServiceCollection services)
89+
where TDbContext : DbContext
90+
{
91+
var serviceProvider = services.BuildServiceProvider();
92+
93+
var dbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(TDbContext));
94+
95+
Assert.NotNull(dbContextService);
96+
Assert.Equal(ServiceLifetime.Scoped, dbContextService.Lifetime);
97+
98+
var customDbContext = serviceProvider.GetService<TDbContext>();
99+
100+
Assert.NotNull(customDbContext);
101+
102+
var dbContextOptions = serviceProvider.GetService<DbContextOptions<TDbContext>>();
103+
104+
Assert.NotNull(dbContextOptions);
105+
Assert.Equal(3, dbContextOptions.Extensions.Count());
106+
107+
#pragma warning disable EF1001 // Internal EF Core API usage.
108+
var coreOptionsExtension = dbContextOptions.FindExtension<CoreOptionsExtension>();
109+
var inMemoryOptionsExtension = dbContextOptions.FindExtension<InMemoryOptionsExtension>();
110+
var scopedInMemoryOptionsExtension = dbContextOptions.FindExtension<ScopedInMemoryOptionsExtension>();
111+
#pragma warning restore EF1001 // Internal EF Core API usage.
112+
113+
Assert.NotNull(coreOptionsExtension);
114+
Assert.NotNull(inMemoryOptionsExtension);
115+
Assert.NotNull(scopedInMemoryOptionsExtension);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)