-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialise CRUD Device Controller (#27)
- Loading branch information
Showing
14 changed files
with
817 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Mortein.Tests/Controllers/DeviceController/DeviceControllerCreateDeviceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Mortein.Tests/Controllers/DeviceController/DeviceControllerDeleteDeviceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
Mortein.Tests/Controllers/DeviceController/DeviceControllerGetAllDevicesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
Mortein.Tests/Controllers/DeviceController/DeviceControllerGetDeviceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Mortein.Tests/Controllers/DeviceController/DeviceControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
47 changes: 47 additions & 0 deletions
47
Mortein.Tests/Controllers/DeviceController/DeviceControllerUpdateDeviceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.