Skip to content

Commit

Permalink
use vertical slices for import as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed Jun 29, 2023
1 parent 3d79592 commit f25a118
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 200 deletions.
1 change: 1 addition & 0 deletions CleanAspCore.Api.Tests/CleanAspCore.Api.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Npgsql" Version="7.0.4" />
<PackageReference Include="Testcontainers" Version="3.2.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.2.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Net.Http.Json;
using CleanAspCore.Api.Tests.Helpers;
using CleanAspCore.Domain.Department;
using FluentAssertions;
using CleanAspCore.Domain.Department;

namespace CleanAspCore.Api.Tests.Features.Departments;

Expand All @@ -11,17 +8,19 @@ public class DepartmentControllerTests : IntegrationTestBase
public async Task SearchDepartments_ReturnsExpectedDepartments()
{
//Arrange
Context.Departments.Add(new Department()
await using var api = CreateApi();
api.SeedData(context =>
{
Id = 0,
Name = "Foo",
City = "bar",
context.Departments.Add(new Department()
{
Id = 0,
Name = "Foo",
City = "bar",
});
});

await Context.SaveChangesAsync();

//Act
var result = await Client.GetFromJsonAsync<DepartmentDto[]>("Department");
var result = await api.CreateClient().GetFromJsonAsync<DepartmentDto[]>("Department");

//Assert
result.Should().BeEquivalentTo(new[]
Expand Down
121 changes: 64 additions & 57 deletions CleanAspCore.Api.Tests/Features/Employees/EmployeeControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Net.Http.Json;
using CleanAspCore.Api.Tests.Helpers;
using CleanAspCore.Domain.Department;
using CleanAspCore.Domain.Department;
using CleanAspCore.Domain.Employee;
using CleanAspCore.Domain.Job;
using FluentAssertions;

namespace CleanAspCore.Api.Tests.Features.Employees;

Expand All @@ -13,34 +10,36 @@ public class EmployeeControllerTests : IntegrationTestBase
public async Task SearchEmployee_ReturnsExpectedJobs()
{
//Arrange
Context.Employees.Add(new Employee()
await using var api = CreateApi();
api.SeedData(context =>
{
Id = 0,
FirstName = "Foo",
LastName = "Bar",
Email = "email",
Gender = "Weird",
DepartmentId = 1,
JobId = 2
});
context.Employees.Add(new Employee()
{
Id = 0,
FirstName = "Foo",
LastName = "Bar",
Email = "email",
Gender = "Weird",
DepartmentId = 1,
JobId = 2
});
Context.Departments.Add(new Department()
{
Id = 1,
Name = "Foo",
City = "Bar"
});
context.Departments.Add(new Department()
{
Id = 1,
Name = "Foo",
City = "Bar"
});
Context.Jobs.Add(new Job
{
Id = 2,
Name = "Foo",
context.Jobs.Add(new Job
{
Id = 2,
Name = "Foo",
});
});

await Context.SaveChangesAsync();

//Act
var result = await Client.GetFromJsonAsync<EmployeeDto[]>("Employee");
var result = await api.CreateClient().GetFromJsonAsync<EmployeeDto[]>("Employee");

//Assert
result.Should().BeEquivalentTo(new[]
Expand All @@ -62,21 +61,23 @@ public async Task SearchEmployee_ReturnsExpectedJobs()
public async Task AddEmployee_IsAdded()
{
//Arrange
Context.Departments.Add(new Department()
await using var api = CreateApi();
api.SeedData(context =>
{
Id = 1,
Name = "Foo",
City = "Bar"
});
context.Departments.Add(new Department()
{
Id = 1,
Name = "Foo",
City = "Bar"
});
Context.Jobs.Add(new Job()
{
Id = 2,
Name = "Foo",
context.Jobs.Add(new Job()
{
Id = 2,
Name = "Foo",
});
});

await Context.SaveChangesAsync();

var newEmployee = new EmployeeDto()
{
Id = 0,
Expand All @@ -89,33 +90,39 @@ public async Task AddEmployee_IsAdded()
};

//Act
var result = await Client.PostAsJsonAsync("Employee", newEmployee);
var result = await api.CreateClient().PostAsJsonAsync("Employee", newEmployee);
result.EnsureSuccessStatusCode();

//Assert
Context.Employees.Should().BeEquivalentTo(new[]
api.AssertDatabase(context =>
{
new Employee()
context.Employees
.Include(x => x.Department)
.Include(x => x.Job)
.Should().BeEquivalentTo(new[]
{
Id = 0,
FirstName = "Foo",
LastName = "Bar",
Email = "email",
Gender = "Weird",
DepartmentId = 1,
Department = new Department()
new Employee()
{
Id = 1,
Name = "Foo",
City = "Bar"
},
JobId = 2,
Job = new Job()
{
Id = 2,
Name = "Foo",
Id = 0,
FirstName = "Foo",
LastName = "Bar",
Email = "email",
Gender = "Weird",
DepartmentId = 1,
Department = new Department()
{
Id = 1,
Name = "Foo",
City = "Bar"
},
JobId = 2,
Job = new Job()
{
Id = 2,
Name = "Foo",
}
}
}
});
});
}

Expand Down
85 changes: 69 additions & 16 deletions CleanAspCore.Api.Tests/Features/Import/ImportControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,79 @@
using System.Text.Json;
using CleanAspCore.Domain.Department;
using CleanAspCore.Domain.Employee;
using CleanAspCore.Domain.Job;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;

namespace CleanAspCore.Api.Tests.Features.Import;

public class ImportControllerTests
public class ImportControllerTests : IntegrationTestBase
{
[Fact]
public async Task foo()
public async Task Import_SingleNewEmployee_IsImported()
{
var foo =
"""
[
await using var api = CreateApi().ConfigureServices(services =>
{
"id": 1,
"firstname": "Neal",
"lastname": "Collopy",
"email": "ncollopy0@slate.com",
"gender": "Male",
"Department": 1,
"Job": 2
}]
""";
var result = JsonSerializer.Deserialize<EmployeeDto[]>(foo);
var fileProviderMock = new Mock<IFileProvider>()
.SetupJsonFileMock("TestData/Employee.json", new[]
{
new Employee
{
Id = 1,
FirstName = "Foo",
LastName = "Bar",
Email = "email",
Gender = "Weird",
JobId = 2,
DepartmentId = 3
}
})
.SetupJsonFileMock("TestData/Job.json", Array.Empty<JobDto>())
.SetupJsonFileMock("TestData/Department.json", Array.Empty<DepartmentDto>());
services.Replace(new ServiceDescriptor(typeof(IFileProvider), fileProviderMock.Object));
});

api.SeedData(context =>
{
context.Jobs.Add(new Job
{
Id = 2,
Name = "Foo",
});
context.Departments.Add(new Department
{
Id = 3,
Name = "Bar",
City = "Foo"
});
});

//Act
var result = await api.CreateClient().PutAsync("Import", null);
result.EnsureSuccessStatusCode();

//Assert
api.AssertDatabase(context =>
{
context.Employees.Should().BeEquivalentTo(new []
{
new Employee
{
Id = 1,
FirstName = "Foo",
LastName = "Bar",
Email = "email",
Gender = "Weird",
JobId = 2,
DepartmentId = 3
}
});
});
}

public ImportControllerTests(PostgreSqlLifetime fixture) : base(fixture)
{
}
}
17 changes: 8 additions & 9 deletions CleanAspCore.Api.Tests/Features/Jobs/JobControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Net.Http.Json;
using CleanAspCore.Api.Tests.Helpers;
using CleanAspCore.Domain.Job;
using FluentAssertions;

namespace CleanAspCore.Api.Tests.Features.Jobs;

Expand All @@ -11,16 +8,18 @@ public class JobControllerTests : IntegrationTestBase
public async Task SearchJobs_ReturnsExpectedJobs()
{
//Arrange
Context.Jobs.Add(new Job()
await using var api = CreateApi();
api.SeedData(context =>
{
Id = 0,
Name = "Foo",
context.Jobs.Add(new Job()
{
Id = 0,
Name = "Foo",
});
});

await Context.SaveChangesAsync();

//Act
var result = await Client.GetFromJsonAsync<JobDto[]>("Job");
var result = await api.CreateClient().GetFromJsonAsync<JobDto[]>("Job");

//Assert
result.Should().BeEquivalentTo(new[]
Expand Down
23 changes: 23 additions & 0 deletions CleanAspCore.Api.Tests/Helpers/FileProviderMockExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.FileProviders;

namespace CleanAspCore.Api.Tests.Helpers;

public static class FileProviderMockExtensions
{
public static Mock<IFileProvider> SetupJsonFileMock(this Mock<IFileProvider> mock, string path, object obj)
{
mock.SetupFileMock(path, Encoding.UTF8.GetBytes(JsonSerializer.Serialize(obj)));
return mock;
}

public static Mock<IFileProvider> SetupFileMock(this Mock<IFileProvider> mock, string path, byte[] content)
{
var fileInfoMock = new Mock<IFileInfo>();
fileInfoMock.Setup(x => x.CreateReadStream()).Returns(new MemoryStream(content));

mock.Setup(x => x.GetFileInfo(path)).Returns(fileInfoMock.Object);
return mock;
}
}
Loading

0 comments on commit f25a118

Please sign in to comment.