Skip to content

Commit

Permalink
Initialise CRUD Device Controller (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
willsawyerrrr authored Sep 23, 2024
2 parents edb1b1b + 5f04a4b commit ab62ce6
Show file tree
Hide file tree
Showing 14 changed files with 817 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .github/workflows/status-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,20 @@ jobs:
- run: dotnet tool restore

- run: dotnet format --verify-no-changes

dotnet-test:
environment: testing
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Create .env file from `testing` Environment
run: |
echo '${{ toJson(secrets) }}' \
| jq -r 'to_entries[] | "\(.key)=\(.value)"' > .devcontainer/.env
echo '${{ toJson(vars) }}' \
| jq -r 'to_entries[] | "\(.key)=\(.value)"' >> .devcontainer/.env
echo 'GITHUB_ACTIONS=true' >> .devcontainer/.env
- name: .NET Tests
uses: devcontainers/ci@v0.3.1900000349
with:
runCmd: dotnet test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Faker;
using Microsoft.AspNetCore.Mvc;
using Mortein.Types;

namespace Mortein.Tests.Controllers.DeviceController;

public partial class DeviceControllerTests
{
[Fact]
public async void CreateDeviceAddsToDatabase()
{
var existingDevices = controller.GetAllDevices();
var result = await controller.CreateDevice(Lorem.Sentence());
var resultingDevices = controller.GetAllDevices();

var createdAtAction = Assert.IsType<CreatedAtActionResult>(result.Result);
var device = Assert.IsType<Device>(createdAtAction.Value);

Assert.NotNull(device);
Assert.DoesNotContain(device, existingDevices);
Assert.Contains(device, resultingDevices);

await controller.DeleteDevice(device.Id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Faker;
using Microsoft.AspNetCore.Mvc;
using Mortein.Types;

namespace Mortein.Tests.Controllers.DeviceController;

public partial class DeviceControllerTests
{
[Fact]
public async Task DeleteWithNoDevicesRespondsWith404Async()
{
var result = await controller.DeleteDevice(Guid.NewGuid());

Assert.IsType<NotFoundResult>(result);
}

[Fact]
public async void DeleteWithDifferentDeviceRespondsWith404()
{
var createResult = await controller.CreateDevice(Lorem.Sentence());
var createAction = Assert.IsType<CreatedAtActionResult>(createResult.Result);
var device = Assert.IsType<Device>(createAction.Value);

var deleteResult = await controller.DeleteDevice(Guid.NewGuid());
Assert.IsType<NotFoundResult>(deleteResult);

await controller.DeleteDevice(device.Id);
}

[Fact]
public async void DeleteDeletes()
{
var createResult = await controller.CreateDevice(Lorem.Sentence());
var createAction = Assert.IsType<CreatedAtActionResult>(createResult.Result);
var device = Assert.IsType<Device>(createAction.Value);

var deleteResult = await controller.DeleteDevice(device.Id);
Assert.IsType<NoContentResult>(deleteResult);

var getResult = await controller.GetDevice(Guid.NewGuid());
Assert.IsType<NotFoundResult>(getResult.Result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Faker;
using Microsoft.AspNetCore.Mvc;
using Mortein.Types;

namespace Mortein.Tests.Controllers.DeviceController;

[Collection("Sequential")]
public partial class DeviceControllerTests
{
[Fact]
public void GetAllWithNoDevices()
{
var devices = controller.GetAllDevices();

Assert.Empty(devices);
}

[Fact]
public async void GetAllGetsOne()
{
var createResult = await controller.CreateDevice(Lorem.Sentence());
var createAction = Assert.IsType<CreatedAtActionResult>(createResult.Result);
var device = Assert.IsType<Device>(createAction.Value);

var devices = controller.GetAllDevices();

Assert.Single(devices);

Assert.Equal(device, devices.First());

await controller.DeleteDevice(device.Id);
}

[Fact]
public async void GetAllGetsMany()
{
var expectedDevices = new List<Device>();

for (int i = 0; i < 5; i++)
{
var createResult = await controller.CreateDevice(Lorem.Sentence());
var createAction = Assert.IsType<CreatedAtActionResult>(createResult.Result);
var device = Assert.IsType<Device>(createAction.Value);
expectedDevices.Add(device);
}

var actualDevices = controller.GetAllDevices();

Assert.Equal(expectedDevices, actualDevices);

foreach (var actualDevice in actualDevices)
{
await controller.DeleteDevice(actualDevice.Id);
}

foreach (var expectedDevice in expectedDevices)
{
await controller.DeleteDevice(expectedDevice.Id);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Faker;
using Microsoft.AspNetCore.Mvc;
using Mortein.Types;

namespace Mortein.Tests.Controllers.DeviceController;

public partial class DeviceControllerTests
{
[Fact]
public async void GetWithNoDevicesRespondsWith404()
{
var result = await controller.GetDevice(Guid.NewGuid());

Assert.IsType<NotFoundResult>(result.Result);
}

[Fact]
public async void GetWithDifferentDeviceRespondsWith404()
{
var createResult = await controller.CreateDevice(Lorem.Sentence());
var createAction = Assert.IsType<CreatedAtActionResult>(createResult.Result);
var device = Assert.IsType<Device>(createAction.Value);

var getResult = await controller.GetDevice(Guid.NewGuid());

Assert.IsType<NotFoundResult>(getResult.Result);

await controller.DeleteDevice(device.Id);
}

[Fact]
public async void GetGets()
{
var createResult = await controller.CreateDevice(Lorem.Sentence());
var createAction = Assert.IsType<CreatedAtActionResult>(createResult.Result);
var device = Assert.IsType<Device>(createAction.Value);

var getResult = await controller.GetDevice(device.Id);

var retrievedDevice = Assert.IsType<Device>(getResult.Value);
Assert.Equal(device, retrievedDevice);

await controller.DeleteDevice(device.Id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Mortein.Tests.Fixtures;

namespace Mortein.Tests.Controllers.DeviceController;

public partial class DeviceControllerTests(DatabaseContextFixture databaseContextFixture)
: IClassFixture<DatabaseContextFixture>
{
private readonly Mortein.Controllers.DeviceController controller = new(databaseContextFixture.databaseContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Faker;
using Microsoft.AspNetCore.Mvc;
using Mortein.Types;

namespace Mortein.Tests.Controllers.DeviceController;

public partial class DeviceControllerTests
{
[Fact]
public async void UpdateWithNoDevicesRespondsWith404()
{
var updateResult = await controller.UpdateDevice(Guid.NewGuid(), Lorem.Sentence());

Assert.IsType<NotFoundResult>(updateResult.Result);
}

[Fact]
public async void UpdateWithDifferentDeviceRespondsWith404()
{
var createResult = await controller.CreateDevice(Lorem.Sentence());
var createAction = Assert.IsType<CreatedAtActionResult>(createResult.Result);
var device = Assert.IsType<Device>(createAction.Value);

var updateResult = await controller.UpdateDevice(Guid.NewGuid(), Lorem.Sentence());

Assert.IsType<NotFoundResult>(updateResult.Result);

await controller.DeleteDevice(device.Id);
}

[Fact]
public async void UpdateChangesName()
{
var createResult = await controller.CreateDevice(Lorem.Sentence());
var createAction = Assert.IsType<CreatedAtActionResult>(createResult.Result);
var device = Assert.IsType<Device>(createAction.Value);

var newName = Lorem.Sentence();
var updateResult = await controller.UpdateDevice(device.Id, newName);

var updatedDevice = Assert.IsType<Device>(updateResult.Value);
Assert.Equal(device.Id, updatedDevice.Id);
Assert.Equal(newName, updatedDevice.Name);

await controller.DeleteDevice(device.Id);
}
}
21 changes: 21 additions & 0 deletions Mortein.Tests/Fixtures/DatabaseContextFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore;

namespace Mortein.Tests.Fixtures;

public class DatabaseContextFixture : IDisposable
{
public DatabaseContext databaseContext;

public DatabaseContextFixture()
{
DbContextOptions<DatabaseContext> options = new DbContextOptionsBuilder<DatabaseContext>().Options;
databaseContext = new(options);

databaseContext.Database.EnsureCreated();
}

public void Dispose()
{
GC.SuppressFinalize(this);
}
}
27 changes: 27 additions & 0 deletions Mortein.Tests/Mortein.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Faker.Net" Version="2.0.163" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../Mortein/Mortein.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>
Loading

0 comments on commit ab62ce6

Please sign in to comment.