-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: or-2451 add werkingsgebieden to powerbi export
- Loading branch information
Showing
9 changed files
with
257 additions
and
27 deletions.
There are no files selected for viewing
137 changes: 110 additions & 27 deletions
137
...ciationRegistry.Admin.ProjectionHost/Projections/PowerBiExport/PowerBiExportProjection.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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
7 changes: 7 additions & 0 deletions
7
src/AssociationRegistry.Admin.Schema/PowerBiExport/Werkingsgebied.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,7 @@ | ||
namespace AssociationRegistry.Admin.Schema.PowerBiExport; | ||
|
||
public record Werkingsgebied | ||
{ | ||
public string Code { get; init; } = null!; | ||
public string Naam { get; init; } = null!; | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/AssociationRegistry.PowerBi.ExportHost/Records/WerkgebiedenRecord.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,8 @@ | ||
namespace AssociationRegistry.PowerBi.ExportHost.Records; | ||
|
||
using CsvHelper.Configuration.Attributes; | ||
|
||
public record WerkgebiedenRecord( | ||
[property: Name("code"), Index(0)] string Code, | ||
[property: Name("naam"), Index(1)] string Naam, | ||
[property: Name("vcode"), Index(2)] string VCode); |
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
26 changes: 26 additions & 0 deletions
26
src/AssociationRegistry.PowerBi.ExportHost/Writers/WerkgebiedenRecordWriter.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,26 @@ | ||
namespace AssociationRegistry.PowerBi.ExportHost.Writers; | ||
|
||
using Admin.Schema.PowerBiExport; | ||
using Records; | ||
using CsvHelper; | ||
|
||
public class WerkgebiedenRecordWriter : IRecordWriter | ||
{ | ||
public async Task Write(IEnumerable<PowerBiExportDocument> docs, IWriter csvWriter) | ||
{ | ||
csvWriter.WriteHeader<WerkgebiedenRecord>(); | ||
await csvWriter.NextRecordAsync(); | ||
|
||
foreach (var vereniging in docs) | ||
{ | ||
foreach (var werkgebied in vereniging.Werkingsgebieden) | ||
{ | ||
csvWriter.WriteRecord(new WerkgebiedenRecord( | ||
werkgebied.Code, werkgebied.Naam, | ||
vereniging.VCode)); | ||
|
||
await csvWriter.NextRecordAsync(); | ||
} | ||
} | ||
} | ||
} |
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
100 changes: 100 additions & 0 deletions
100
test/AssociationRegistry.Test.PowerBi.ExportHost/WerkgebiedenExportTests.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,100 @@ | ||
namespace AssociationRegistry.Test.PowerBi.ExportHost; | ||
|
||
using Admin.Schema.PowerBiExport; | ||
using Amazon.S3; | ||
using Amazon.S3.Model; | ||
using AssociationRegistry.PowerBi.ExportHost; | ||
using AssociationRegistry.PowerBi.ExportHost.Writers; | ||
using AutoFixture; | ||
using Common.AutoFixture; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Moq; | ||
using System.Net; | ||
using System.Text; | ||
using Xunit; | ||
|
||
public class WerkgebiedenRecordWriterTests | ||
{ | ||
private Stream _resultStream; | ||
private readonly Fixture _fixture; | ||
private readonly Mock<IAmazonS3> _s3ClientMock; | ||
|
||
public WerkgebiedenRecordWriterTests() | ||
{ | ||
_fixture = new Fixture().CustomizeDomain(); | ||
_s3ClientMock = SetupS3Client(); | ||
} | ||
|
||
[Fact] | ||
public async Task WithNoDocuments_ThenCsvExportsOnlyHeaders() | ||
{ | ||
var docs = Array.Empty<PowerBiExportDocument>(); | ||
|
||
await Export(docs); | ||
|
||
var actualResult = await GetActualResult(); | ||
actualResult.Should().BeEquivalentTo("code,naam,vcode\r\n"); | ||
} | ||
|
||
[Fact] | ||
public async Task WithMultipleDocuments_ThenCsvExportShouldExport() | ||
{ | ||
var docs = _fixture.CreateMany<PowerBiExportDocument>(); | ||
|
||
await Export(docs); | ||
|
||
var actualResult = await GetActualResult(); | ||
var expectedResult = GetExpectedResult(docs); | ||
|
||
actualResult.Should().BeEquivalentTo(expectedResult); | ||
} | ||
|
||
private async Task<string> GetActualResult() | ||
{ | ||
using var reader = new StreamReader(_resultStream, Encoding.UTF8); | ||
|
||
var content = await reader.ReadToEndAsync(); | ||
|
||
return content; | ||
} | ||
|
||
private static string GetExpectedResult(IEnumerable<PowerBiExportDocument> docs) | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
stringBuilder.Append("code,naam,vcode\r\n"); | ||
|
||
foreach (var doc in docs) | ||
{ | ||
foreach (var werkgebied in doc.Werkingsgebieden) | ||
{ | ||
stringBuilder.Append( | ||
$"{werkgebied.Code},{werkgebied.Naam},{doc.VCode}\r\n"); | ||
} | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
|
||
private Mock<IAmazonS3> SetupS3Client() | ||
{ | ||
var s3ClientMock = new Mock<IAmazonS3>(); | ||
|
||
s3ClientMock.Setup(x => x.PutObjectAsync(It.IsAny<PutObjectRequest>(), default)) | ||
.Callback<PutObjectRequest, CancellationToken>((request, _) => _resultStream = request.InputStream) | ||
.ReturnsAsync(new PutObjectResponse { HttpStatusCode = HttpStatusCode.OK }); | ||
|
||
return s3ClientMock; | ||
} | ||
|
||
private async Task Export(IEnumerable<PowerBiExportDocument> docs) | ||
{ | ||
var exporter = new Exporter(WellKnownFileNames.Werkgebieden, | ||
bucketName: "something", | ||
new WerkgebiedenRecordWriter(), | ||
_s3ClientMock.Object, | ||
new NullLogger<Exporter>()); | ||
|
||
await exporter.Export(docs); | ||
} | ||
} |