diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 250bb53..2acfe09 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -12,42 +12,7 @@ on: jobs: tests: runs-on: ubuntu-latest - - # Service containers to run with `container-job` - services: - # Label used to access the service container - postgres: - # Docker Hub image - image: postgres - # Provide the password for postgres - env: - POSTGRES_PASSWORD: postgres - # Set health checks to wait until postgres has started - options: >- - --health-cmd pg_isready - --health-interval 10s - --health-timeout 5s - --health-retries 5 - steps: - - name: Start PostgreSQL on Ubuntu - run: | - sudo systemctl start postgresql.service - pg_isready - - - name: Create additional user - run: | - sudo -u postgres psql --command="CREATE USER testuser PASSWORD 'password'" --command="\du" - - - name: Create additional database - run: | - sudo -u postgres createdb TestDatabase --owner=testuser - PGPASSWORD=password - - - name: Enable uuid-ossp extension - run: | - echo "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";" | sudo -u postgres psql TestDatabase - - name: Checkout uses: actions/checkout@v4 @@ -55,19 +20,9 @@ jobs: uses: actions/setup-dotnet@v4 with: dotnet-version: 8.0.x - - - name: Create database - env: - REMOTE_TESTING: "true" - BotaniqueDb: Host=localhost;Database=TestDatabase;Username=testuser;Password=password - run: cd api && - dotnet run & - (sleep 90 && kill -9 $(lsof -t -i:5141)) - name: Run tests - env: - BotaniqueDb: Host=localhost;Database=TestDatabase;Username=testuser;Password=password - run: cd Tests && dotnet test --no-build + run: cd Tests && dotnet test diff --git a/Tests/GlobalTestSetup.cs b/Tests/GlobalTestSetup.cs index d6b8107..9e0c3c5 100644 --- a/Tests/GlobalTestSetup.cs +++ b/Tests/GlobalTestSetup.cs @@ -8,6 +8,6 @@ public class GlobalTestSetup [OneTimeSetUp] public async Task StartApi() { - await Startup.StartApi(["ENVIRONMENT=Development"]); + await Startup.StartApi(["Testing=true", "--db-init"]); } } \ No newline at end of file diff --git a/api/Program.cs b/api/Program.cs index 4935a01..64793b3 100644 --- a/api/Program.cs +++ b/api/Program.cs @@ -14,6 +14,7 @@ using Shared.Dtos.FromClient; using Shared.Models; using Shared.Models.Exceptions; +using Testcontainers.PostgreSql; namespace api; @@ -41,21 +42,43 @@ public static async Task StartApi(string[] args) var builder = WebApplication.CreateBuilder(args); - var connectionString = builder.Configuration.GetConnectionString("BotaniqueDb"); - builder.Services.AddDbContextFactory(options => + if (args.Contains("Testing=true")) { - connectionString ??= Environment.GetEnvironmentVariable("BotaniqueDb"); - options.UseNpgsql(connectionString ?? throw new Exception("Connection string cannot be null")); - }); + var dbContainer = + new PostgreSqlBuilder() + .WithDatabase("botanique") + .WithUsername("root") + .WithPassword("password") + .Build(); + await dbContainer.StartAsync(); + + var connectionString = dbContainer.GetConnectionString(); + builder.Services.AddDbContextFactory(options => + { + options.UseNpgsql(connectionString ?? throw new Exception("Connection string cannot be null")); + }); + } + + else + { + var connectionString = builder.Configuration.GetConnectionString("BotaniqueDb"); + builder.Services.AddDbContextFactory(options => + { + connectionString ??= Environment.GetEnvironmentVariable("BotaniqueDb"); + options.UseNpgsql(connectionString ?? throw new Exception("Connection string cannot be null")); + }); + } + builder.Services.Configure(builder.Configuration.GetSection("JWT")); builder.Services.Configure(builder.Configuration.GetSection("MQTT")); builder.Services.AddServicesAndRepositories(); - + var services = builder.FindAndInjectClientEventHandlers(Assembly.GetExecutingAssembly()); + var app = builder.Build(); - if (args.Contains("--db-init") || Environment.GetEnvironmentVariable("REMOTE_TESTING") is not null) + if (args.Contains("--db-init")) { var scope = app.Services.CreateScope(); var db = app.Services.GetRequiredService>().CreateDbContext(); @@ -72,10 +95,9 @@ await userRepository.CreateUser(new RegisterUserDto }); } - // builder.WebHost.UseUrls("http://*:9999"); - var port = Environment.GetEnvironmentVariable("PORT") ?? "8181"; var wsServer = new WebSocketServer($"ws://0.0.0.0:{port}"); + // builder.WebHost.UseUrls("http://*:9999"); wsServer.Start(socket => { diff --git a/api/api.csproj b/api/api.csproj index 69978d0..4c28d60 100644 --- a/api/api.csproj +++ b/api/api.csproj @@ -14,6 +14,7 @@ +