-
-
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.
use vertical slices for import as well
- Loading branch information
Showing
17 changed files
with
322 additions
and
200 deletions.
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
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
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
85 changes: 69 additions & 16 deletions
85
CleanAspCore.Api.Tests/Features/Import/ImportControllerTests.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 |
---|---|---|
@@ -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) | ||
{ | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
CleanAspCore.Api.Tests/Helpers/FileProviderMockExtensions.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,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; | ||
} | ||
} |
Oops, something went wrong.