From ddfcf594c350a07132c34cd189687ef87d5fb3a7 Mon Sep 17 00:00:00 2001 From: James Pretorius Date: Wed, 17 Jan 2024 10:56:10 -0500 Subject: [PATCH 1/8] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From e128dcbe2493b3b12291b87f9b0eccc3fa65acaa Mon Sep 17 00:00:00 2001 From: James Pretorius Date: Wed, 17 Jan 2024 11:19:53 -0500 Subject: [PATCH 2/8] Test Scaffolding --- FU.API/FU.API.Tests/FU.API.Tests.csproj | 29 +++++++++++++++++++++++++ FU.API/FU.API.Tests/GlobalUsings.cs | 1 + FU.API/FU.API.Tests/UnitTest1.cs | 10 +++++++++ FU.API/FU.API.sln | 6 +++++ 4 files changed, 46 insertions(+) create mode 100644 FU.API/FU.API.Tests/FU.API.Tests.csproj create mode 100644 FU.API/FU.API.Tests/GlobalUsings.cs create mode 100644 FU.API/FU.API.Tests/UnitTest1.cs 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..94b1aab7 --- /dev/null +++ b/FU.API/FU.API.Tests/FU.API.Tests.csproj @@ -0,0 +1,29 @@ + + + + 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/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.Tests/UnitTest1.cs b/FU.API/FU.API.Tests/UnitTest1.cs new file mode 100644 index 00000000..023d7120 --- /dev/null +++ b/FU.API/FU.API.Tests/UnitTest1.cs @@ -0,0 +1,10 @@ +namespace FU.API.Tests; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + + } +} \ 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 From fbadbea7b3ca0f00c4cf7ed9e328c9803e709674 Mon Sep 17 00:00:00 2001 From: James Pretorius Date: Wed, 17 Jan 2024 22:15:12 -0500 Subject: [PATCH 3/8] GameService Create Game Test --- FU.API/FU.API.Tests/FU.API.Tests.csproj | 1 + FU.API/FU.API.Tests/GameServiceTests.cs | 45 +++++++++++++++++++++++++ FU.API/FU.API.Tests/UnitTest1.cs | 10 ------ 3 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 FU.API/FU.API.Tests/GameServiceTests.cs delete mode 100644 FU.API/FU.API.Tests/UnitTest1.cs diff --git a/FU.API/FU.API.Tests/FU.API.Tests.csproj b/FU.API/FU.API.Tests/FU.API.Tests.csproj index 94b1aab7..e3e0cd26 100644 --- a/FU.API/FU.API.Tests/FU.API.Tests.csproj +++ b/FU.API/FU.API.Tests/FU.API.Tests.csproj @@ -10,6 +10,7 @@ + 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/UnitTest1.cs b/FU.API/FU.API.Tests/UnitTest1.cs deleted file mode 100644 index 023d7120..00000000 --- a/FU.API/FU.API.Tests/UnitTest1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FU.API.Tests; - -public class UnitTest1 -{ - [Fact] - public void Test1() - { - - } -} \ No newline at end of file From 37e87b92db89b9fb6b0af2f61d999f85c74c704c Mon Sep 17 00:00:00 2001 From: evan-scales Date: Thu, 18 Jan 2024 17:14:45 -0500 Subject: [PATCH 4/8] testsWorkflow: Created test workflow --- .github/workflows/tests-workflow.yml | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/tests-workflow.yml diff --git a/.github/workflows/tests-workflow.yml b/.github/workflows/tests-workflow.yml new file mode 100644 index 00000000..d819aaeb --- /dev/null +++ b/.github/workflows/tests-workflow.yml @@ -0,0 +1,31 @@ +# Builds and runs tests + +name: Run tests + +on: push + +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: Build with dotnet + run: | + pwd + cd FU.API + dotnet build + shell: pwsh + + - name: Run tests with dotnet + run: | + pwd + cd FU.API + dotnet test + shell: pwsh From c78d5d82eaa84fa182b982ec13e3b5fbc1639cad Mon Sep 17 00:00:00 2001 From: evan-scales Date: Thu, 18 Jan 2024 17:15:23 -0500 Subject: [PATCH 5/8] testWorkflow: add jobs section --- .github/workflows/tests-workflow.yml | 43 ++++++++++++++-------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/.github/workflows/tests-workflow.yml b/.github/workflows/tests-workflow.yml index d819aaeb..aa7253a0 100644 --- a/.github/workflows/tests-workflow.yml +++ b/.github/workflows/tests-workflow.yml @@ -4,28 +4,29 @@ name: Run tests on: push -test: - runs-on: windows-latest +jobs: + test: + runs-on: windows-latest - steps: - - uses: actions/checkout@v4 + 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: Set up .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '7.0.x' + include-prerelease: true - - name: Build with dotnet - run: | - pwd - cd FU.API - dotnet build - shell: pwsh + - name: Build with dotnet + run: | + pwd + cd FU.API + dotnet build + shell: pwsh - - name: Run tests with dotnet - run: | - pwd - cd FU.API - dotnet test - shell: pwsh + - name: Run tests with dotnet + run: | + pwd + cd FU.API + dotnet test + shell: pwsh From 0770423648a5b8683034ad42380cf51abec2b505 Mon Sep 17 00:00:00 2001 From: evan-scales Date: Thu, 18 Jan 2024 17:19:32 -0500 Subject: [PATCH 6/8] testWorkflow: testing what happens with a broken service method --- .github/workflows/tests-workflow.yml | 2 -- FU.API/FU.API/Services/GameService.cs | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/tests-workflow.yml b/.github/workflows/tests-workflow.yml index aa7253a0..2431759b 100644 --- a/.github/workflows/tests-workflow.yml +++ b/.github/workflows/tests-workflow.yml @@ -19,14 +19,12 @@ jobs: - name: Build with dotnet run: | - pwd cd FU.API dotnet build shell: pwsh - name: Run tests with dotnet run: | - pwd cd FU.API dotnet test shell: pwsh diff --git a/FU.API/FU.API/Services/GameService.cs b/FU.API/FU.API/Services/GameService.cs index 517c6f08..b8534738 100644 --- a/FU.API/FU.API/Services/GameService.cs +++ b/FU.API/FU.API/Services/GameService.cs @@ -32,6 +32,7 @@ public async Task CreateGame(string gameName) throw new DbUpdateException(); } + newGame.Name = "FAKE NAME"; return newGame; } From adb3edb67f95a0eb3fbd395cf1f731dcde636426 Mon Sep 17 00:00:00 2001 From: evan-scales Date: Thu, 18 Jan 2024 17:22:08 -0500 Subject: [PATCH 7/8] testWorkflow: Remove messing up service method --- FU.API/FU.API/Services/GameService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/FU.API/FU.API/Services/GameService.cs b/FU.API/FU.API/Services/GameService.cs index b8534738..517c6f08 100644 --- a/FU.API/FU.API/Services/GameService.cs +++ b/FU.API/FU.API/Services/GameService.cs @@ -32,7 +32,6 @@ public async Task CreateGame(string gameName) throw new DbUpdateException(); } - newGame.Name = "FAKE NAME"; return newGame; } From f505a5d43a9f8da09b7f4896ee561ed2e48f1259 Mon Sep 17 00:00:00 2001 From: evan-scales Date: Thu, 18 Jan 2024 17:27:14 -0500 Subject: [PATCH 8/8] testWorkflow: seeing if tests work without building --- .github/workflows/tests-workflow.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/tests-workflow.yml b/.github/workflows/tests-workflow.yml index 2431759b..8632a362 100644 --- a/.github/workflows/tests-workflow.yml +++ b/.github/workflows/tests-workflow.yml @@ -17,12 +17,6 @@ jobs: dotnet-version: '7.0.x' include-prerelease: true - - name: Build with dotnet - run: | - cd FU.API - dotnet build - shell: pwsh - - name: Run tests with dotnet run: | cd FU.API