Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve infra for unit tests #431

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,40 @@ on:
- "main"
workflow_dispatch:

env:
TEST_DB_PASS: "Password12!"

jobs:
build:
name: Build & Test
name: Build, Test & Pack
runs-on: ubuntu-24.04
services:
mysql:
image: mysql:8.0-debian
env:
MYSQL_ROOT_PASSWORD: ${{ env.TEST_DB_PASS }}
ports:
- "3306:3306"
mssql:
image: mcr.microsoft.com/mssql/server:2022-latest
env:
ACCEPT_EULA: "Y"
SA_PASSWORD: ${{ env.TEST_DB_PASS }}
ports:
- "1433:1433"
oracle:
image: gvenzl/oracle-xe:21-slim-faststart
env:
ORACLE_PASSWORD: ${{ env.TEST_DB_PASS }}
ports:
- "1521:1521"
postgres:
image: postgres:17.2-alpine
ports:
- "5432:5432"
env:
POSTGRES_PASSWORD: ${{ env.TEST_DB_PASS }}

steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -27,13 +57,6 @@ jobs:
- name: Build
run: dotnet build --no-restore -c Release -p:ContinuousIntegrationBuild=true

- name: Run containers
working-directory: tests
run: docker compose up -d

- name: Wait for containers
run: sleep 40s

- name: Run tests
run: dotnet test --no-build -c Release

Expand Down
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>
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}