Skip to content

Commit

Permalink
feat: or-2451 add werkingsgebieden to powerbi export
Browse files Browse the repository at this point in the history
  • Loading branch information
emalfroy committed Oct 24, 2024
1 parent d28b9ca commit afae7bc
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 27 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public record PowerBiExportDocument : IVCode
public HoofdactiviteitVerenigingsloket[] HoofdactiviteitenVerenigingsloket { get; set; } =
Array.Empty<HoofdactiviteitVerenigingsloket>();

public Werkingsgebied[] Werkingsgebieden { get; set; } =
Array.Empty<Werkingsgebied>();

public bool IsUitgeschrevenUitPubliekeDatastroom { get; set; }
public string Bron { get; set; } = null!;
[Identity] public string VCode { get; init; } = null!;
Expand Down
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!;
}
1 change: 1 addition & 0 deletions src/AssociationRegistry.PowerBi.ExportHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private static void ConfigureServices(HostBuilderContext context, IServiceCollec
new Exporter(WellKnownFileNames.Basisgegevens, powerBiExportOptions.BucketName, new BasisgegevensRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Contactgegevens, powerBiExportOptions.BucketName, new ContactgegevensRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Hoofdactiviteiten, powerBiExportOptions.BucketName, new HoofdactiviteitenRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Werkgebieden, powerBiExportOptions.BucketName, new WerkgebiedenRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Locaties, powerBiExportOptions.BucketName, new LocatiesRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Historiek, powerBiExportOptions.BucketName, new HistoriekRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
});
Expand Down
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);
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class WellKnownFileNames
public const string Locaties = "Locaties.csv";
public const string Contactgegevens = "Contactgegevens.csv";
public const string Historiek = "Historiek.csv";
public const string Werkgebieden = "Hoofdactiviteiten.csv";
}
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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace AssociationRegistry.Test.Common.AutoFixture;
using Vereniging.TelefoonNummers;
using Vereniging.Websites;
using HoofdactiviteitVerenigingsloket = Vereniging.HoofdactiviteitVerenigingsloket;
using Werkingsgebied = Vereniging.Werkingsgebied;

public static class AutoFixtureCustomizations
{
Expand Down
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);
}
}

0 comments on commit afae7bc

Please sign in to comment.