|
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; |
11 | 11 | using Xunit;
|
12 | 12 |
|
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