Skip to content

Commit

Permalink
Merge pull request #4 from The-Poolz/issue#3
Browse files Browse the repository at this point in the history
Issue #3
  • Loading branch information
Lomet authored Jun 13, 2023
2 parents 5d39f8e + c95b7cc commit 7c8c80e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
18 changes: 18 additions & 0 deletions ConfiguredSqlConnection/ConfiguredSqlConnection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,26 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>5405e2de-7a05-4014-aea5-3ee845eb7fee</UserSecretsId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>ConfiguredSqlConnection</Title>
<Version>1.0.0</Version>
<Authors>Senia,ArdenHide,Lomet</Authors>
<Company>The Poolz</Company>
<PackageProjectUrl>https://github.com/The-Poolz/ConfiguredSqlConnection</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>https://github.com/The-Poolz/ConfiguredSqlConnection</RepositoryType>
<PackageTags>Database SQL EF</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>The NuGet package is a collection of utilities for working with SQL Server database connections using environment settings and secure connection strings.</Description>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\RPCToolkit-DB\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
Expand Down
4 changes: 2 additions & 2 deletions ConfiguredSqlConnection/DataBaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var actionConnection = EnvManager.GetEnvironmentValue<string>("ACTION_CONNECTION");
var actionConnection = EnvManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_ACTION_CONNECTION");
if (!string.IsNullOrEmpty(actionConnection))
{
optionsBuilder.UseSqlServer(actionConnection);
}
if (!optionsBuilder.IsConfigured)
{
var secretValue = EnvManager.GetEnvironmentValue<string>("SECRET_NAME_OF_CONNECTION", true);
var secretValue = EnvManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_SECRET_NAME_OF_CONNECTION", true);

var connectionString = new SecretManager().GetSecretValue(secretValue, "connectionString");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public DbContextEnvironmentFactory()
public DbContextEnvironmentFactory(DbContextOptionsBuilderFactory<DataBaseContext> optionsBuilderFactory)
: base(optionsBuilderFactory)
{
dbMode = EnvManager.GetEnvironmentValue<string>("DB_MODE", true);
dbName = EnvManager.GetEnvironmentValue<string>("DB_NAME");
dbMode = EnvManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_DB_MODE", true);
dbName = EnvManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_DB_NAME");
}

public virtual DataBaseContext CreateFromEnvironment()
{
if (!Enum.TryParse(typeof(ContextOption), dbMode, true, out var optionObj) || !(optionObj is ContextOption option))
if (!Enum.TryParse(typeof(ContextOption), dbMode, true, out var optionObj) || optionObj is not 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public virtual DbContextOptionsBuilder<TContext> Create(ContextOption option, st

protected virtual void ConfigureProdContext()
{
var secretValue = EnvManager.GetEnvironmentValue<string>("SECRET_NAME_OF_CONNECTION", true);
var secretValue = EnvManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_SECRET_NAME_OF_CONNECTION", true);

var connectionString = new SecretManager().GetSecretValue(secretValue, "connectionString");

Expand Down
34 changes: 13 additions & 21 deletions ConfiguredSqlConnectionTests/ExtensionsTests/FactoriesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ public class FactoriesTests
public FactoriesTests()
{
contextOption = ContextOption.Prod;

optionsBuilder = new DbContextOptionsBuilder<DataBaseContext>()
.UseInMemoryDatabase("dbName");

optionsBuilder = new DbContextOptionsBuilder<DataBaseContext>().UseInMemoryDatabase("dbName");
var mockOptionsBuilderFactory = new Mock<DbContextOptionsBuilderFactory<DataBaseContext>>();
mockOptionsBuilderFactory.Setup(x => x.Create(contextOption, null))
.Returns(optionsBuilder);

mockOptionsBuilderFactory.Setup(x => x.Create(contextOption, null)).Returns(optionsBuilder);
optionsBuilderFactory = mockOptionsBuilderFactory.Object;
}

Expand All @@ -41,8 +36,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<DbContextEnvironmentFactory>(optionsBuilderFactory);
factory.Setup(x => x.Create(contextOption, null)).CallBase();
factory.Setup(x => x.CreateFromEnvironment()).CallBase();
Expand All @@ -55,34 +50,31 @@ public void CreateFromEnvironment()
[Fact]
public void CreateFromEnvironment_OptionNotSet_ThrowException()
{
Environment.SetEnvironmentVariable("DB_MODE", $"");
Environment.SetEnvironmentVariable("DB_NAME", $"");
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<DbContextEnvironmentFactory>(optionsBuilderFactory);
factory.Setup(x => x.Create(contextOption, null)).CallBase();
factory.Setup(x => x.CreateFromEnvironment()).CallBase();

Action testCode = () => factory.Object.CreateFromEnvironment();
var exception = Assert.Throws<TargetInvocationException>(() => factory.Object.CreateFromEnvironment());

var exception = Assert.Throws<TargetInvocationException>(testCode);
var expectedExceptionMessage = $"Environment variable 'DB_MODE' is null or empty.";
Assert.Equal(expectedExceptionMessage, exception?.InnerException?.Message);
}

[Fact]
public void CreateFromEnvironment_InvaledOption_ThrowException()
{
var invalidOption = "Production";
Environment.SetEnvironmentVariable("DB_MODE", $"{invalidOption}");
Environment.SetEnvironmentVariable("DB_NAME", $"");
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<DbContextEnvironmentFactory>(optionsBuilderFactory);
factory.Setup(x => x.Create(contextOption, null)).CallBase();
factory.Setup(x => x.CreateFromEnvironment()).CallBase();

Action testCode = () => factory.Object.CreateFromEnvironment();

var exception = Assert.Throws<ArgumentException>(testCode);
var expectedExceptionMessage = $"Invalid value for environment variable 'DB_MODE': {invalidOption}";
var exception = Assert.Throws<ArgumentException>(() => factory.Object.CreateFromEnvironment());

Assert.Equal(expectedExceptionMessage, exception.Message);
}

}

0 comments on commit 7c8c80e

Please sign in to comment.