-
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
14 changed files
with
398 additions
and
29 deletions.
There are no files selected for viewing
146 changes: 119 additions & 27 deletions
146
...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/WerkingsgebiedenRecord.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 WerkingsgebiedenRecord( | ||
[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/WerkingsgebiedenRecordWriter.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 WerkingsgebiedenRecordWriter : IRecordWriter | ||
{ | ||
public async Task Write(IEnumerable<PowerBiExportDocument> docs, IWriter csvWriter) | ||
{ | ||
csvWriter.WriteHeader<WerkingsgebiedenRecord>(); | ||
await csvWriter.NextRecordAsync(); | ||
|
||
foreach (var vereniging in docs) | ||
{ | ||
foreach (var werkgebied in vereniging.Werkingsgebieden) | ||
{ | ||
csvWriter.WriteRecord(new WerkingsgebiedenRecord( | ||
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
67 changes: 67 additions & 0 deletions
67
...stry.Test.Admin.Api/Projections/V2/PowerBiExport/Given_WerkingsgebiedenWerdenGewijzigd.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,67 @@ | ||
namespace AssociationRegistry.Test.Admin.Api.Projections.V2.PowerBiExport; | ||
|
||
using AssociationRegistry.Admin.Schema.PowerBiExport; | ||
using FluentAssertions; | ||
using KellermanSoftware.CompareNetObjects; | ||
using Marten; | ||
using Projections.PowerBiExport; | ||
using ScenarioClassFixtures; | ||
using Xunit; | ||
|
||
[Collection(nameof(PowerBiExportContext))] | ||
public class Given_WerkingsgebiedenWerdenGewijzigd : IClassFixture<WerkingsgebiedenWerdenGewijzigdScenario> | ||
{ | ||
private readonly PowerBiExportContext _context; | ||
private readonly WerkingsgebiedenWerdenGewijzigdScenario _scenario; | ||
|
||
public Given_WerkingsgebiedenWerdenGewijzigd( | ||
PowerBiExportContext context, | ||
WerkingsgebiedenWerdenGewijzigdScenario scenario) | ||
{ | ||
_context = context; | ||
_scenario = scenario; | ||
} | ||
|
||
[Fact] | ||
public async Task ARecordIsStored_With_Hoofdactiviteiten() | ||
{ | ||
await using var documentSession = _context | ||
.Session; | ||
|
||
var powerBiExportDocument = | ||
await documentSession | ||
.Query<PowerBiExportDocument>() | ||
.Where(w => w.VCode == _scenario.VerenigingWerdGeregistreerd.VCode) | ||
.SingleAsync(); | ||
|
||
var expectedHoofdactiviteiten = | ||
_scenario | ||
.WerkingsgebiedenWerdenGewijzigd | ||
.Werkingsgebieden | ||
.Select(x => new Werkingsgebied() | ||
{ | ||
Naam = x.Naam, | ||
Code = x.Code, | ||
}) | ||
.ToArray(); | ||
|
||
powerBiExportDocument.Werkingsgebieden.ShouldCompare(expectedHoofdactiviteiten); | ||
} | ||
|
||
[Fact] | ||
public async Task ARecordIsStored_With_Historiek() | ||
{ | ||
await using var documentSession = _context | ||
.Session; | ||
|
||
var powerBiExportDocument = | ||
await documentSession | ||
.Query<PowerBiExportDocument>() | ||
.SingleAsync(w => w.VCode == _scenario.VerenigingWerdGeregistreerd.VCode); | ||
|
||
powerBiExportDocument.VCode.Should().Be(_scenario.VerenigingWerdGeregistreerd.VCode); | ||
powerBiExportDocument.Historiek.Should().NotBeEmpty(); | ||
powerBiExportDocument.Historiek.Should() | ||
.ContainSingle(x => x.EventType == "WerkingsgebiedenWerdenGewijzigd"); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ections/V2/PowerBiExport/ScenarioClassFixtures/WerkingsgebiedenWerdenGewijzigdScenario.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,36 @@ | ||
namespace AssociationRegistry.Test.Admin.Api.Projections.V2.PowerBiExport.ScenarioClassFixtures; | ||
|
||
using AssociationRegistry.Events; | ||
using AssociationRegistry.Test.Admin.Api.Projections.V2.PowerBiExport; | ||
using AutoFixture; | ||
using Projections.PowerBiExport; | ||
|
||
public class WerkingsgebiedenWerdenGewijzigdScenario : ProjectionScenarioFixture<PowerBiExportContext> | ||
{ | ||
public FeitelijkeVerenigingWerdGeregistreerd VerenigingWerdGeregistreerd { get; } | ||
public WerkingsgebiedenWerdenGewijzigd WerkingsgebiedenWerdenGewijzigd { get; set; } | ||
|
||
public WerkingsgebiedenWerdenGewijzigdScenario(PowerBiExportContext context): base(context) | ||
{ | ||
VerenigingWerdGeregistreerd = AutoFixture.Create<FeitelijkeVerenigingWerdGeregistreerd>(); | ||
|
||
WerkingsgebiedenWerdenGewijzigd = AutoFixture.Create<WerkingsgebiedenWerdenGewijzigd>(); | ||
} | ||
|
||
public override async Task Given() | ||
{ | ||
await using var session = await Context.DocumentSession(); | ||
|
||
session.Events.Append(VerenigingWerdGeregistreerd.VCode, | ||
VerenigingWerdGeregistreerd); | ||
await session.SaveChangesAsync(); | ||
await using var session2 = await Context.DocumentSession(); | ||
|
||
session2.Events.Append(VerenigingWerdGeregistreerd.VCode, | ||
WerkingsgebiedenWerdenGewijzigd); | ||
|
||
await session2.SaveChangesAsync(); | ||
|
||
await Context.WaitForNonStaleProjectionDataAsync(); | ||
} | ||
} |
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
100 changes: 100 additions & 0 deletions
100
test/AssociationRegistry.Test.PowerBi.ExportHost/WerkingsgebiedenExportTests.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 WerkingsgebiedenRecordWriterTests | ||
{ | ||
private Stream _resultStream; | ||
private readonly Fixture _fixture; | ||
private readonly Mock<IAmazonS3> _s3ClientMock; | ||
|
||
public WerkingsgebiedenRecordWriterTests() | ||
{ | ||
_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.Werkingsgebieden, | ||
bucketName: "something", | ||
new WerkingsgebiedenRecordWriter(), | ||
_s3ClientMock.Object, | ||
new NullLogger<Exporter>()); | ||
|
||
await exporter.Export(docs); | ||
} | ||
} |
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