Skip to content

Commit

Permalink
use .env in local tess
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Dec 13, 2024
1 parent 7c81913 commit 3489f95
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 19 deletions.
3 changes: 3 additions & 0 deletions MicroOrm.Dapper.Repositories.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E409731D-9AB4-4389-B611-53A3AF8324A4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{94F86B38-C3FB-4FBB-802E-7786C457050D}"
ProjectSection(SolutionItems) = preProject
tests\.env = tests\.env
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroOrm.Dapper.Repositories", "src\MicroOrm.Dapper.Repositories.csproj", "{A8258060-EDF7-4713-9428-088ADD6FE503}"
EndProject
Expand Down
1 change: 1 addition & 0 deletions tests/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST_DB_PASS=Password12!
29 changes: 29 additions & 0 deletions tests/Repositories.Base/DotEnv.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.IO;

namespace Repositories.Base;

public static class DotEnv
{
private const string _fileName = ".env";

static DotEnv()
{
if (!File.Exists(_fileName))
return;

foreach (var line in File.ReadAllLines(_fileName))
{
var parts = line.Split(
'=',
StringSplitOptions.RemoveEmptyEntries);

if (parts.Length != 2)
continue;

Environment.SetEnvironmentVariable(parts[0].Trim(), parts[1].Trim());
}
}

public static string GetTestDbPass() => Environment.GetEnvironmentVariable("TEST_DB_PASS");
}
3 changes: 3 additions & 0 deletions tests/Repositories.Base/Repositories.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
<ItemGroup>
<ProjectReference Include="..\TestClasses\TestClasses.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="../.env" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion tests/Repositories.MSSQL.Tests/MicrosoftDatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class MicrosoftDatabaseFixture : DatabaseFixture
{
public MicrosoftDatabaseFixture()
: base(new TestDbContext(
new SqlConnection("Server=localhost;Database=master;User ID=sa;Password=Password12!;Trust Server Certificate=true"), SqlProvider.MSSQL))
new SqlConnection($"Server=localhost;Database=master;User ID=sa;Password={DotEnv.GetTestDbPass()};Trust Server Certificate=true"), SqlProvider.MSSQL))
{
}
}
2 changes: 1 addition & 1 deletion tests/Repositories.MSSQL.Tests/SystemDatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Repositories.MSSQL.Tests;
public class SystemDatabaseFixture : DatabaseFixture
{
public SystemDatabaseFixture()
: base(new TestDbContext(new SqlConnection("Server=localhost;Database=master;User ID=sa;Password=Password12!"), SqlProvider.MSSQL))
: base(new TestDbContext(new SqlConnection($"Server=localhost;Database=master;User ID=sa;Password={DotEnv.GetTestDbPass()}"), SqlProvider.MSSQL))
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Repositories.MySql.Tests;
public class MySqlClientDatabaseFixture : DatabaseFixture
{
public MySqlClientDatabaseFixture()
: base( new TestDbContext(new MySqlConnection("Server=localhost;Uid=root;Pwd=Password12!"), SqlProvider.MySQL))
: base( new TestDbContext(new MySqlConnection($"Server=localhost;Uid=root;Pwd={DotEnv.GetTestDbPass()}"), SqlProvider.MySQL))
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Repositories.MySql.Tests;
public class MySqlConnectorDatabaseFixture : DatabaseFixture
{
public MySqlConnectorDatabaseFixture()
: base(new TestDbContext(new MySqlConnection("Server=localhost;Uid=root;Pwd=Password12!"), SqlProvider.MySQL))
: base(new TestDbContext(new MySqlConnection($"Server=localhost;Uid=root;Pwd={DotEnv.GetTestDbPass()}"), SqlProvider.MySQL))
{
}
}
2 changes: 1 addition & 1 deletion tests/Repositories.Oracle.Tests/DatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public DatabaseFixture()
{
Db = new TestDbContext(
new OracleConnection(
"DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XEPDB1)));User Id=system;Password=Password12!"),
$"DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XEPDB1)));User Id=system;Password={DotEnv.GetTestDbPass()}"),
SqlProvider.Oracle);
ClearDb();
InitDb();
Expand Down
2 changes: 1 addition & 1 deletion tests/Repositories.PostgreSQL.Tests/DatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Repositories.PostgreSQL.Tests;
public class DatabaseFixture : IDisposable
{
private readonly string _dbName = "test_" + RandomGenerator.String();
private const string _masterConnString = "Host=localhost;Username=postgres;Password=Password12!";
private readonly string _masterConnString = $"Host=localhost;Username=postgres;Password={DotEnv.GetTestDbPass()}";

public DatabaseFixture()
{
Expand Down
19 changes: 6 additions & 13 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
services:
mysql:
image: mysql:8.0-debian
environment:
MYSQL_ROOT_PASSWORD: ${TEST_DB_PASS}
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: "Password12!"

mssql:
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "Password12!"
SA_PASSWORD: ${TEST_DB_PASS}
ports:
- "1433:1433"
restart: always

oracle:
image: gvenzl/oracle-xe:21-slim-faststart
environment:
ORACLE_PASSWORD: ${TEST_DB_PASS}
ports:
- "1521:1521"
restart: always
environment:
ORACLE_PASSWORD: "Password12!"

postgres:
image: postgres:17.2-alpine
ports:
- "5432:5432"
restart: always
environment:
POSTGRES_PASSWORD: "Password12!"
POSTGRES_PASSWORD: ${TEST_DB_PASS}

0 comments on commit 3489f95

Please sign in to comment.