From d7451179401a7afc013eeef768715fe3d18dc077 Mon Sep 17 00:00:00 2001 From: Sergey988 Date: Mon, 12 Jun 2023 20:06:51 +0300 Subject: [PATCH 1/3] Issue #3 --- ConfiguredSqlConnection/DataBaseContext.cs | 4 ++-- .../Extensions/DbContextEnvironmentFactory.cs | 6 +++--- .../Extensions/DbContextOptionsBuilderFactory.cs | 2 +- .../ExtensionsTests/FactoriesTests.cs | 16 ++++++++-------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/ConfiguredSqlConnection/DataBaseContext.cs b/ConfiguredSqlConnection/DataBaseContext.cs index 715afe3..dde97e8 100644 --- a/ConfiguredSqlConnection/DataBaseContext.cs +++ b/ConfiguredSqlConnection/DataBaseContext.cs @@ -11,14 +11,14 @@ public DataBaseContext(DbContextOptions options) : base(options protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - var actionConnection = EnvManager.GetEnvironmentValue("ACTION_CONNECTION"); + var actionConnection = EnvManager.GetEnvironmentValue("CONFIGUREDSQLCONNECTION_ACTION_CONNECTION"); if (!string.IsNullOrEmpty(actionConnection)) { optionsBuilder.UseSqlServer(actionConnection); } if (!optionsBuilder.IsConfigured) { - var secretValue = EnvManager.GetEnvironmentValue("SECRET_NAME_OF_CONNECTION", true); + var secretValue = EnvManager.GetEnvironmentValue("CONFIGUREDSQLCONNECTION_SECRET_NAME_OF_CONNECTION", true); var connectionString = new SecretManager().GetSecretValue(secretValue, "connectionString"); diff --git a/ConfiguredSqlConnection/Extensions/DbContextEnvironmentFactory.cs b/ConfiguredSqlConnection/Extensions/DbContextEnvironmentFactory.cs index 9b67ae8..818e195 100644 --- a/ConfiguredSqlConnection/Extensions/DbContextEnvironmentFactory.cs +++ b/ConfiguredSqlConnection/Extensions/DbContextEnvironmentFactory.cs @@ -14,15 +14,15 @@ public DbContextEnvironmentFactory() public DbContextEnvironmentFactory(DbContextOptionsBuilderFactory optionsBuilderFactory) : base(optionsBuilderFactory) { - dbMode = EnvManager.GetEnvironmentValue("DB_MODE", true); - dbName = EnvManager.GetEnvironmentValue("DB_NAME"); + dbMode = EnvManager.GetEnvironmentValue("CONFIGUREDSQLCONNECTION_DB_MODE", true); + dbName = EnvManager.GetEnvironmentValue("CONFIGUREDSQLCONNECTION_DB_NAME"); } public virtual DataBaseContext CreateFromEnvironment() { if (!Enum.TryParse(typeof(ContextOption), dbMode, true, out var optionObj) || !(optionObj is ContextOption option)) { - throw new ArgumentException($"Invalid value for environment variable 'DB_MODE': {dbMode}"); + throw new ArgumentException($"Invalid value for environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE': {dbMode}"); } return Create(option, dbName); diff --git a/ConfiguredSqlConnection/Extensions/DbContextOptionsBuilderFactory.cs b/ConfiguredSqlConnection/Extensions/DbContextOptionsBuilderFactory.cs index 07fe58f..7674085 100644 --- a/ConfiguredSqlConnection/Extensions/DbContextOptionsBuilderFactory.cs +++ b/ConfiguredSqlConnection/Extensions/DbContextOptionsBuilderFactory.cs @@ -37,7 +37,7 @@ public virtual DbContextOptionsBuilder Create(ContextOption option, st protected virtual void ConfigureProdContext() { - var secretValue = EnvManager.GetEnvironmentValue("SECRET_NAME_OF_CONNECTION", true); + var secretValue = EnvManager.GetEnvironmentValue("CONFIGUREDSQLCONNECTION_SECRET_NAME_OF_CONNECTION", true); var connectionString = new SecretManager().GetSecretValue(secretValue, "connectionString"); diff --git a/ConfiguredSqlConnectionTests/ExtensionsTests/FactoriesTests.cs b/ConfiguredSqlConnectionTests/ExtensionsTests/FactoriesTests.cs index b468522..147f0ab 100644 --- a/ConfiguredSqlConnectionTests/ExtensionsTests/FactoriesTests.cs +++ b/ConfiguredSqlConnectionTests/ExtensionsTests/FactoriesTests.cs @@ -41,8 +41,8 @@ public void Create() [Fact] public void CreateFromEnvironment() { - Environment.SetEnvironmentVariable("DB_MODE", $"{contextOption}"); - Environment.SetEnvironmentVariable("DB_NAME", ""); + Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_MODE", $"{contextOption}"); + Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_NAME", ""); var factory = new Mock(optionsBuilderFactory); factory.Setup(x => x.Create(contextOption, null)).CallBase(); factory.Setup(x => x.CreateFromEnvironment()).CallBase(); @@ -55,8 +55,8 @@ public void CreateFromEnvironment() [Fact] public void CreateFromEnvironment_OptionNotSet_ThrowException() { - Environment.SetEnvironmentVariable("DB_MODE", $""); - Environment.SetEnvironmentVariable("DB_NAME", $""); + Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_MODE", $""); + Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_NAME", $""); var factory = new Mock(optionsBuilderFactory); factory.Setup(x => x.Create(contextOption, null)).CallBase(); factory.Setup(x => x.CreateFromEnvironment()).CallBase(); @@ -64,7 +64,7 @@ public void CreateFromEnvironment_OptionNotSet_ThrowException() Action testCode = () => factory.Object.CreateFromEnvironment(); var exception = Assert.Throws(testCode); - var expectedExceptionMessage = $"Environment variable 'DB_MODE' is null or empty."; + var expectedExceptionMessage = $"Environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE' is null or empty."; Assert.Equal(expectedExceptionMessage, exception?.InnerException?.Message); } @@ -72,8 +72,8 @@ public void CreateFromEnvironment_OptionNotSet_ThrowException() public void CreateFromEnvironment_InvaledOption_ThrowException() { var invalidOption = "Production"; - Environment.SetEnvironmentVariable("DB_MODE", $"{invalidOption}"); - Environment.SetEnvironmentVariable("DB_NAME", $""); + Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_MODE", $"{invalidOption}"); + Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_NAME", $""); var factory = new Mock(optionsBuilderFactory); factory.Setup(x => x.Create(contextOption, null)).CallBase(); factory.Setup(x => x.CreateFromEnvironment()).CallBase(); @@ -81,7 +81,7 @@ public void CreateFromEnvironment_InvaledOption_ThrowException() Action testCode = () => factory.Object.CreateFromEnvironment(); var exception = Assert.Throws(testCode); - var expectedExceptionMessage = $"Invalid value for environment variable 'DB_MODE': {invalidOption}"; + var expectedExceptionMessage = $"Invalid value for environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE': {invalidOption}"; Assert.Equal(expectedExceptionMessage, exception.Message); } From 2e07c9757bf83179a27d9ec5896f265b1103a12c Mon Sep 17 00:00:00 2001 From: Sergey988 Date: Tue, 13 Jun 2023 11:11:08 +0300 Subject: [PATCH 2/3] cleaning --- .../Extensions/DbContextEnvironmentFactory.cs | 2 +- .../ExtensionsTests/FactoriesTests.cs | 22 ++++++------------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/ConfiguredSqlConnection/Extensions/DbContextEnvironmentFactory.cs b/ConfiguredSqlConnection/Extensions/DbContextEnvironmentFactory.cs index 818e195..acfee50 100644 --- a/ConfiguredSqlConnection/Extensions/DbContextEnvironmentFactory.cs +++ b/ConfiguredSqlConnection/Extensions/DbContextEnvironmentFactory.cs @@ -20,7 +20,7 @@ public DbContextEnvironmentFactory(DbContextOptionsBuilderFactory() - .UseInMemoryDatabase("dbName"); - + optionsBuilder = new DbContextOptionsBuilder().UseInMemoryDatabase("dbName"); var mockOptionsBuilderFactory = new Mock>(); - mockOptionsBuilderFactory.Setup(x => x.Create(contextOption, null)) - .Returns(optionsBuilder); - + mockOptionsBuilderFactory.Setup(x => x.Create(contextOption, null)).Returns(optionsBuilder); optionsBuilderFactory = mockOptionsBuilderFactory.Object; } @@ -55,16 +50,15 @@ public void CreateFromEnvironment() [Fact] public void CreateFromEnvironment_OptionNotSet_ThrowException() { + var expectedExceptionMessage = $"Environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE' is null or empty."; Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_MODE", $""); Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_NAME", $""); var factory = new Mock(optionsBuilderFactory); factory.Setup(x => x.Create(contextOption, null)).CallBase(); factory.Setup(x => x.CreateFromEnvironment()).CallBase(); - Action testCode = () => factory.Object.CreateFromEnvironment(); + var exception = Assert.Throws(() => factory.Object.CreateFromEnvironment()); - var exception = Assert.Throws(testCode); - var expectedExceptionMessage = $"Environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE' is null or empty."; Assert.Equal(expectedExceptionMessage, exception?.InnerException?.Message); } @@ -72,17 +66,15 @@ public void CreateFromEnvironment_OptionNotSet_ThrowException() public void CreateFromEnvironment_InvaledOption_ThrowException() { var invalidOption = "Production"; + var expectedExceptionMessage = $"Invalid value for environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE': {invalidOption}"; Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_MODE", $"{invalidOption}"); Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_NAME", $""); var factory = new Mock(optionsBuilderFactory); factory.Setup(x => x.Create(contextOption, null)).CallBase(); factory.Setup(x => x.CreateFromEnvironment()).CallBase(); - Action testCode = () => factory.Object.CreateFromEnvironment(); - - var exception = Assert.Throws(testCode); - var expectedExceptionMessage = $"Invalid value for environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE': {invalidOption}"; + var exception = Assert.Throws(() => factory.Object.CreateFromEnvironment()); + Assert.Equal(expectedExceptionMessage, exception.Message); } - } \ No newline at end of file From c95b7cc8025687b9d780837b26c14c9f3ee6f9f9 Mon Sep 17 00:00:00 2001 From: Sergey988 Date: Tue, 13 Jun 2023 12:08:39 +0300 Subject: [PATCH 3/3] update .csproj --- .../ConfiguredSqlConnection.csproj | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ConfiguredSqlConnection/ConfiguredSqlConnection.csproj b/ConfiguredSqlConnection/ConfiguredSqlConnection.csproj index afd5ff0..49837a5 100644 --- a/ConfiguredSqlConnection/ConfiguredSqlConnection.csproj +++ b/ConfiguredSqlConnection/ConfiguredSqlConnection.csproj @@ -5,8 +5,26 @@ enable enable 5405e2de-7a05-4014-aea5-3ee845eb7fee + True + ConfiguredSqlConnection + 1.0.0 + Senia,ArdenHide,Lomet + The Poolz + https://github.com/The-Poolz/ConfiguredSqlConnection + README.md + https://github.com/The-Poolz/ConfiguredSqlConnection + Database SQL EF + MIT + The NuGet package is a collection of utilities for working with SQL Server database connections using environment settings and secure connection strings. + + + True + \ + + +