Skip to content

Commit

Permalink
Testing github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ckam03 committed Sep 13, 2023
1 parent dcc51ae commit 5c6e363
Show file tree
Hide file tree
Showing 49 changed files with 287 additions and 274 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/github-actions-demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
18 changes: 0 additions & 18 deletions .idea/gradle.xml

This file was deleted.

4 changes: 0 additions & 4 deletions .idea/misc.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/vcs.xml

This file was deleted.

65 changes: 0 additions & 65 deletions .idea/workspace.xml

This file was deleted.

1 change: 1 addition & 0 deletions src/Growth.Tests/Growth.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
70 changes: 70 additions & 0 deletions src/Growth.Tests/GrowthApiFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Data.Common;
using growth.Backend.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using Respawn;
using Testcontainers.PostgreSql;

namespace Growth.Tests;

public class GrowthApiFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
private readonly PostgreSqlContainer _dbContainer = new PostgreSqlBuilder()
.WithImage("postgres:latest")
.WithDatabase("growth_db")
.WithUsername("postgres")
.WithPassword("postgres")
.Build();

private DbConnection _dbConnection = default!;
private Respawner _respawner = default!;

public HttpClient HttpClient { get; private set; } = default!;

protected void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
var descriptor = services.SingleOrDefault(s => s.ServiceType == typeof(DbContextOptions<GrowthDbContext>));

if (descriptor is not null)
{
services.Remove(descriptor);
}

services.AddDbContext<GrowthDbContext>(options =>
{
options.UseNpgsql(_dbContainer.GetConnectionString());
});
});

}

public async Task InitializeAsync()
{
await _dbContainer.StartAsync();
_dbConnection = new NpgsqlConnection(_dbContainer.GetConnectionString());

HttpClient = CreateClient();
await _dbConnection.OpenAsync();
_respawner = await Respawner.CreateAsync(_dbConnection, new RespawnerOptions
{
DbAdapter = DbAdapter.Postgres,
SchemasToInclude = new[] { "public" }
});
}

public async Task ResetDatabaseAsync()
{
await _respawner.ResetAsync(_dbConnection);
}

public async Task DisposeAsync()
{
await _dbContainer.StopAsync();
}
}
10 changes: 10 additions & 0 deletions src/Growth.Tests/JournalEntryTests/JournalEntryRoutesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ public async Task GetJournalEntriesAsync_Returns200()
//Act

}

[Fact]
public async Task CreateEntry_Async()
{
//Arrange
var client = _webApplicationFactory.CreateClient();

//Act

}
}
22 changes: 11 additions & 11 deletions src/Growth.Tests/JournalTests/JournalRouteTests.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
using System.Net.Http.Json;
using growth.Backend.Features.Journals;
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;

namespace Growth.Tests.JournalTests;

public class JournalRouteTests : IClassFixture<WebApplicationFactory<Program>>
public class JournalRouteTests : IAsyncLifetime
{
private readonly WebApplicationFactory<Program> _webApplicationFactory;
private readonly HttpClient _httpClient;
private readonly Func<Task> _resetDatabase;

public JournalRouteTests(WebApplicationFactory<Program> webApplicationFactory)
public JournalRouteTests(GrowthApiFactory growthApiFactory)
{
_webApplicationFactory = webApplicationFactory;
_httpClient = growthApiFactory.HttpClient;
_resetDatabase = growthApiFactory.ResetDatabaseAsync;
}

[Fact]
public async Task GetJournalsAsync_Returns200()
{
//Arrange
var client = _webApplicationFactory.CreateClient();

//Act
var response = await client.GetAsync("/journal");
var response = await _httpClient.GetAsync("/journal");

//Assert
Assert.True(response.IsSuccessStatusCode);
Expand All @@ -31,11 +30,10 @@ public async Task GetJournalsAsync_Returns200()
public async Task CreateJournal_Returns200()
{
//Arrange
var client = _webApplicationFactory.CreateClient();
const string journalName = "JournalTest";

//Act
var response = await client.PostAsJsonAsync("/journal", journalName);
var response = await _httpClient.PostAsJsonAsync("/journal", journalName);
var created = await response.Content.ReadFromJsonAsync<Journal>();

//Assert
Expand All @@ -47,9 +45,11 @@ public async Task CreateJournal_Returns200()
public async Task DeleteJournalAsync_Returns200()
{
//Arrange

//Act

//Assert
}

public Task InitializeAsync() => Task.CompletedTask;
public Task DisposeAsync() => _resetDatabase();
}
8 changes: 6 additions & 2 deletions src/growth.Backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 5000
EXPOSE 5001

ENV ASPNETCORE_URLS=http://+:5000

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
ARG configuration=Release
WORKDIR /src
COPY ["growth.Backend.csproj", "./"]
RUN dotnet restore "growth.Backend.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "/src/growth.Backend/growth.Backend.csprojgrowth.Backend.csproj" -c $configuration -o /app/build
RUN dotnet build "growth.Backend.csproj" -c $configuration -o /app/build

FROM build AS publish
ARG configuration=Release
Expand Down
34 changes: 10 additions & 24 deletions src/growth.Backend/Extensions/EndpointExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
using growth.Backend.Features.Audio;
using growth.Backend.Features.Entry;
using growth.Backend.Features.Entry.Endpoints;
using growth.Backend.Features.Journals.Endpoints;
using growth.Backend.Features.Motivation;
using growth.Backend.Features.Unsplash;
using growth.Backend.Shared;

namespace growth.Backend.Extensions;

public static class EndpointExtensions
{
public static WebApplication MapEndpoints(this WebApplication app)
{
app.MapGroup("/journal").WithTags("Journal")
.MapGetJournal()
.MapGetJournals()
.MapCreateJournal()
.MapDeleteJournal();

app.MapGroup("/entry").WithTags("JournalEntry")
.MapGetJournalEntry()
.MapGetJournalEntries()
.MapCreateJournalEntry()
.MapUpdateJournalEntry()
.MapDeleteJournalEntry();
var endpoints = typeof(Program).Assembly
.GetTypes()
.Where(t => t.IsAssignableTo(typeof(IEndpoint)) && !t.IsAbstract && !t.IsInterface)
.Select(Activator.CreateInstance)
.Cast<IEndpoint>();

app.MapGroup("/audio").MapGetAudioFiles();
app.MapGroup("/photos").MapGetPhotos().WithTags("Photos");
app.MapGroup("/quote").MapGetQuote();
foreach (var endpoint in endpoints)
{
endpoint.Map(app);
}

app.MapGroup("/image").WithTags("Image")
.MapGetPhoto()
.MapSelectPhoto();
return app;
}
}
17 changes: 17 additions & 0 deletions src/growth.Backend/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using growth.Backend.Data;
using Microsoft.EntityFrameworkCore;

namespace growth.Backend.Extensions;

public static class ServiceExtensions
{
public static void ApplyMigrations(this IApplicationBuilder app)
{
using var serviceScope = app.ApplicationServices
.GetRequiredService<IServiceScopeFactory>()
.CreateScope();

using var context = serviceScope.ServiceProvider.GetService<GrowthDbContext>();
context?.Database.Migrate();
}
}
10 changes: 5 additions & 5 deletions src/growth.Backend/Features/Audio/GetAudioFiles.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using growth.Backend.Shared;
using Microsoft.AspNetCore.Mvc;

namespace growth.Backend.Features.Audio;

public static class GetAudioFiles
public class GetAudioFiles : IEndpoint
{
public static RouteGroupBuilder MapGetAudioFiles(this RouteGroupBuilder app)
public void Map(WebApplication app)
{
app.MapGet("/", HandleAsync);
return app;
}

public static async Task<IResult> HandleAsync([FromServices]IAudioService audioService)
public async Task<IResult> HandleAsync([FromServices]IAudioService audioService)
{
var audio = await audioService.CreateBucket();

Expand Down
Loading

0 comments on commit 5c6e363

Please sign in to comment.