Skip to content

Commit

Permalink
Refactor enum conversion (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArdenHide authored Jun 15, 2023
1 parent 00431f7 commit 0b45b43
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ConfiguredSqlConnection/ConfiguredSqlConnection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="SecretsManager" Version="1.0.0" />
<PackageReference Include="TestableDbContext.Mock" Version="1.0.0" />
<PackageReference Include="Utils.EnvironmentManager" Version="1.0.0" />
<PackageReference Include="Utils.EnvironmentManager" Version="1.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ namespace ConfiguredSqlConnection.Extensions;

public static class ConnectionStringFromEnvironmentFactory
{
private static string DbMode =>
EnvManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_DB_MODE", true);
private static ContextOption DbMode =>
EnvManager.GetEnvironmentValue<ContextOption>("CONFIGUREDSQLCONNECTION_DB_MODE", true);
private static string DbName =>
EnvManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_DB_NAME");

public static string GetConnectionFromEnvironment()
{
if (!Enum.TryParse(typeof(ContextOption), DbMode, true, out var optionObj) || optionObj is not ContextOption option)
{
throw new ArgumentException($"Invalid value for environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE': {DbMode}");
}

return ConnectionStringFactory.GetConnection(option, DbName);
}
public static string GetConnectionFromEnvironment() =>
ConnectionStringFactory.GetConnection(DbMode, DbName);
}
15 changes: 4 additions & 11 deletions ConfiguredSqlConnection/Extensions/DbContextEnvironmentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ConfiguredSqlConnection.Extensions;

public class DbContextEnvironmentFactory<TContext> : DbContextFactory<TContext> where TContext : DbContext
{
private readonly string dbMode;
private readonly ContextOption dbMode;
private readonly string dbName;

public DbContextEnvironmentFactory()
Expand All @@ -15,17 +15,10 @@ public DbContextEnvironmentFactory()
public DbContextEnvironmentFactory(DbContextOptionsBuilderFactory<TContext> optionsBuilderFactory)
: base(optionsBuilderFactory)
{
dbMode = EnvManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_DB_MODE", true);
dbMode = EnvManager.GetEnvironmentValue<ContextOption>("CONFIGUREDSQLCONNECTION_DB_MODE", true);
dbName = EnvManager.GetEnvironmentValue<string>("CONFIGUREDSQLCONNECTION_DB_NAME");
}

public virtual TContext CreateFromEnvironment()
{
if (!Enum.TryParse(typeof(ContextOption), dbMode, true, out var optionObj) || optionObj is not ContextOption option)
{
throw new ArgumentException($"Invalid value for environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE': {dbMode}");
}

return Create(option, dbName);
}
public virtual TContext CreateFromEnvironment() =>
Create(dbMode, dbName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ public void CreateFromEnvironment_OptionNotSet_ThrowException()
public void CreateFromEnvironment_InvaledOption_ThrowException()
{
var invalidOption = "Production";
var expectedExceptionMessage = $"Invalid value for environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE': {invalidOption}";
var expectedExceptionMessage = $"Failed to convert environment variable 'CONFIGUREDSQLCONNECTION_DB_MODE' to type '{typeof(ContextOption).FullName}'.";
Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_MODE", $"{invalidOption}");
Environment.SetEnvironmentVariable("CONFIGUREDSQLCONNECTION_DB_NAME", $"");
var factory = new Mock<DbContextEnvironmentFactory<DbContext>>(optionsBuilderFactory);
factory.Setup(x => x.Create(contextOption, null)).CallBase();
factory.Setup(x => x.CreateFromEnvironment()).CallBase();

var exception = Assert.Throws<ArgumentException>(() => factory.Object.CreateFromEnvironment());

Assert.Equal(expectedExceptionMessage, exception.Message);
var exception = Assert.Throws<TargetInvocationException>(() => factory.Object.CreateFromEnvironment());

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

0 comments on commit 0b45b43

Please sign in to comment.