diff --git a/.github/workflows/tests-workflow.yml b/.github/workflows/tests-workflow.yml new file mode 100644 index 00000000..8632a362 --- /dev/null +++ b/.github/workflows/tests-workflow.yml @@ -0,0 +1,24 @@ +# Builds and runs tests + +name: Run tests + +on: push + +jobs: + test: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '7.0.x' + include-prerelease: true + + - name: Run tests with dotnet + run: | + cd FU.API + dotnet test + shell: pwsh diff --git a/FU.API/FU.API.Tests/FU.API.Tests.csproj b/FU.API/FU.API.Tests/FU.API.Tests.csproj new file mode 100644 index 00000000..e3e0cd26 --- /dev/null +++ b/FU.API/FU.API.Tests/FU.API.Tests.csproj @@ -0,0 +1,30 @@ + + + + net7.0 + enable + enable + + false + true + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/FU.API/FU.API.Tests/GameServiceTests.cs b/FU.API/FU.API.Tests/GameServiceTests.cs new file mode 100644 index 00000000..05b70996 --- /dev/null +++ b/FU.API/FU.API.Tests/GameServiceTests.cs @@ -0,0 +1,45 @@ +namespace FU.API.Tests; + +using FU.API.Data; +using FU.API.Services; +using Microsoft.EntityFrameworkCore; + +public class GameServiceTests +{ + private readonly DbContextOptions _contextOptions; + + // adapted from https://github.com/dotnet/EntityFramework.Docs/blob/main/samples/core/Testing/TestingWithoutTheDatabase/InMemoryBloggingControllerTest.cs + public GameServiceTests() + { + _contextOptions = new DbContextOptionsBuilder() + .UseInMemoryDatabase("GameServiceTest") + .Options; + + using var context = new AppDbContext(_contextOptions); + + context.Database.EnsureDeleted(); + context.Database.EnsureCreated(); + + context.SaveChanges(); + } + + AppDbContext CreateContext() => new(_contextOptions); + + [Theory] + [InlineData("")] + [InlineData("Title1")] + [InlineData("Title with space")] + [InlineData("Title with symbols :-={}<>.")] + public async void CreateGame_WithValidString_ReturnsGame(string gameName) + { + // Arrange + using var context = CreateContext(); + var gameService = new GameService(context); + + // Act + var game = await gameService.CreateGame(gameName); + + // Assert + Assert.Equal(game.Name, gameName); + } +} diff --git a/FU.API/FU.API.Tests/GlobalUsings.cs b/FU.API/FU.API.Tests/GlobalUsings.cs new file mode 100644 index 00000000..8c927eb7 --- /dev/null +++ b/FU.API/FU.API.Tests/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/FU.API/FU.API.sln b/FU.API/FU.API.sln index a245d5f5..fc891663 100644 --- a/FU.API/FU.API.sln +++ b/FU.API/FU.API.sln @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .editorconfig = .editorconfig EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FU.API.Tests", "FU.API.Tests\FU.API.Tests.csproj", "{EECF8E74-3D21-449F-92A9-B3A77B784ADD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -20,6 +22,10 @@ Global {3126D604-A505-4516-8204-9805A7807DB0}.Debug|Any CPU.Build.0 = Debug|Any CPU {3126D604-A505-4516-8204-9805A7807DB0}.Release|Any CPU.ActiveCfg = Release|Any CPU {3126D604-A505-4516-8204-9805A7807DB0}.Release|Any CPU.Build.0 = Release|Any CPU + {EECF8E74-3D21-449F-92A9-B3A77B784ADD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EECF8E74-3D21-449F-92A9-B3A77B784ADD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EECF8E74-3D21-449F-92A9-B3A77B784ADD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EECF8E74-3D21-449F-92A9-B3A77B784ADD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/README.md b/README.md index b78598ca..ad196e2a 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ dotnet ef database update ### Connect to Postgres Set the `CONNECTION_STRING` environment variable. ``` -CONNECTION_STRING="Host=localhost; Database=ForcesUnite; Username=dev; Password=dev" +CONNECTION_STRING="Host=localhost; Database=fu_dev; Username=dev; Password=dev" ``` ### SPA Environment Setup