|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using Shouldly; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace Volo.Abp.EntityFrameworkCore; |
| 6 | + |
| 7 | +public class EfCoreDatabaseProviderHelper_Tests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void Should_Detect_SqlServer_From_Real_Assembly() |
| 11 | + { |
| 12 | + var builder = new DbContextOptionsBuilder<EmptyDbContext>(); |
| 13 | + Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(builder, "Server=localhost;Database=test"); |
| 14 | + using var context = new EmptyDbContext(builder.Options); |
| 15 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) |
| 16 | + .ShouldBe(EfCoreDatabaseProvider.SqlServer); |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void Should_Detect_PostgreSql_From_Real_Assembly() |
| 21 | + { |
| 22 | + var builder = new DbContextOptionsBuilder<EmptyDbContext>(); |
| 23 | + Microsoft.EntityFrameworkCore.NpgsqlDbContextOptionsBuilderExtensions.UseNpgsql(builder, "Host=localhost;Database=test"); |
| 24 | + using var context = new EmptyDbContext(builder.Options); |
| 25 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) |
| 26 | + .ShouldBe(EfCoreDatabaseProvider.PostgreSql); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void Should_Detect_MySql_Pomelo_From_Assembly_Name() |
| 31 | + { |
| 32 | + var providerName = typeof(Microsoft.EntityFrameworkCore.MySqlDbContextOptionsBuilderExtensions).Assembly.GetName().Name; |
| 33 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(providerName) |
| 34 | + .ShouldBe(EfCoreDatabaseProvider.MySql); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void Should_Detect_MySql_Oracle_From_Real_Assembly() |
| 39 | + { |
| 40 | + var builder = new DbContextOptionsBuilder<EmptyDbContext>(); |
| 41 | + Microsoft.EntityFrameworkCore.MySQLDbContextOptionsExtensions.UseMySQL(builder, "Server=localhost;Database=test"); |
| 42 | + using var context = new EmptyDbContext(builder.Options); |
| 43 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) |
| 44 | + .ShouldBe(EfCoreDatabaseProvider.MySql); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void Should_Detect_Oracle_From_Real_Assembly() |
| 49 | + { |
| 50 | + var builder = new DbContextOptionsBuilder<EmptyDbContext>(); |
| 51 | + Microsoft.EntityFrameworkCore.OracleDbContextOptionsExtensions.UseOracle(builder, "Data Source=localhost/XE"); |
| 52 | + using var context = new EmptyDbContext(builder.Options); |
| 53 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) |
| 54 | + .ShouldBe(EfCoreDatabaseProvider.Oracle); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Should_Detect_Sqlite_From_Real_Assembly() |
| 59 | + { |
| 60 | + var builder = new DbContextOptionsBuilder<EmptyDbContext>(); |
| 61 | + Microsoft.EntityFrameworkCore.SqliteDbContextOptionsBuilderExtensions.UseSqlite(builder, "Data Source=:memory:"); |
| 62 | + using var context = new EmptyDbContext(builder.Options); |
| 63 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) |
| 64 | + .ShouldBe(EfCoreDatabaseProvider.Sqlite); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void Should_Detect_InMemory_From_Real_Assembly() |
| 69 | + { |
| 70 | + var builder = new DbContextOptionsBuilder<EmptyDbContext>(); |
| 71 | + Microsoft.EntityFrameworkCore.InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(builder, "test"); |
| 72 | + using var context = new EmptyDbContext(builder.Options); |
| 73 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) |
| 74 | + .ShouldBe(EfCoreDatabaseProvider.InMemory); |
| 75 | + } |
| 76 | + |
| 77 | + [Theory] |
| 78 | + [InlineData("Devart.Data.Oracle.Entity.EFCore", EfCoreDatabaseProvider.Oracle)] |
| 79 | + [InlineData("FirebirdSql.EntityFrameworkCore.Firebird", EfCoreDatabaseProvider.Firebird)] |
| 80 | + [InlineData("Microsoft.EntityFrameworkCore.Cosmos", EfCoreDatabaseProvider.Cosmos)] |
| 81 | + public void Should_Detect_Providers_Without_Package_Reference(string providerName, EfCoreDatabaseProvider expected) |
| 82 | + { |
| 83 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(providerName).ShouldBe(expected); |
| 84 | + } |
| 85 | + |
| 86 | + [Theory] |
| 87 | + [InlineData("microsoft.entityframeworkcore.sqlserver", EfCoreDatabaseProvider.SqlServer)] |
| 88 | + [InlineData("POMELO.ENTITYFRAMEWORKCORE.MYSQL", EfCoreDatabaseProvider.MySql)] |
| 89 | + [InlineData("npgsql.entityframeworkcore.postgresql", EfCoreDatabaseProvider.PostgreSql)] |
| 90 | + public void Should_Detect_Providers_Case_Insensitively(string providerName, EfCoreDatabaseProvider expected) |
| 91 | + { |
| 92 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(providerName).ShouldBe(expected); |
| 93 | + } |
| 94 | + |
| 95 | + [Theory] |
| 96 | + [InlineData(null)] |
| 97 | + [InlineData("")] |
| 98 | + [InlineData(" ")] |
| 99 | + [InlineData("Some.Unknown.Provider")] |
| 100 | + public void Should_Return_Null_For_Unknown_Or_Empty(string? providerName) |
| 101 | + { |
| 102 | + EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(providerName).ShouldBeNull(); |
| 103 | + } |
| 104 | + |
| 105 | + private class EmptyDbContext : DbContext |
| 106 | + { |
| 107 | + public EmptyDbContext(DbContextOptions<EmptyDbContext> options) : base(options) |
| 108 | + { |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments