Skip to content

Commit

Permalink
test: or-2354 refactors test for power bi export
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Lesage authored and janlesage committed Sep 4, 2024
1 parent aadb583 commit d9d334b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using System.Net;
using System.Text;
using Xunit;

public class BasisgegevensExportTests
{
private Stream _resultStream = null;
private Stream _resultStream;
private readonly Fixture _fixture;
private readonly Mock<IAmazonS3> _s3ClientMock;

public BasisgegevensExportTests()
{
_fixture = new Fixture().CustomizeDomain();
_s3ClientMock = SetupS3Client();
}

[Fact]
public async Task WithMultipleDocuments_ThenCsvExportShouldExport()
{
var fixture = new Fixture().CustomizeDomain();
var s3ClientMock = SetupS3Client();
var docs = fixture.CreateMany<PowerBiExportDocument>();
var docs = _fixture.CreateMany<PowerBiExportDocument>();

await Export(docs, s3ClientMock.Object);
await Export(docs);

var actualResult = await GetActualResult();
var expectedResult = GetExpectedResult(docs);
Expand All @@ -44,12 +51,14 @@ private async Task<string> GetActualResult()
private static string GetExpectedResult(IEnumerable<PowerBiExportDocument> docs)
{
var stringBuilder = new StringBuilder();
stringBuilder.Append("bron,doelgroep.maximumleeftijd,doelgroep.minimumleeftijd,einddatum,isUitgeschrevenUitPubliekeDatastroom,korteBeschrijving,korteNaam,naam,roepnaam,startdatum,status,vCode,verenigingstype.code,verenigingstype.naam,kboNummer,corresponderendeVCodes,aantalVertegenwoordigers,datumLaatsteAanpassing\r\n");

stringBuilder.Append(
"bron,doelgroep.maximumleeftijd,doelgroep.minimumleeftijd,einddatum,isUitgeschrevenUitPubliekeDatastroom,korteBeschrijving,korteNaam,naam,roepnaam,startdatum,status,vCode,verenigingstype.code,verenigingstype.naam,kboNummer,corresponderendeVCodes,aantalVertegenwoordigers,datumLaatsteAanpassing\r\n");

foreach (var doc in docs)
{
stringBuilder.Append(
$"{doc.Bron},{doc.Doelgroep.Maximumleeftijd},{doc.Doelgroep.Minimumleeftijd},{doc.Einddatum},{doc.IsUitgeschrevenUitPubliekeDatastroom},{doc.KorteBeschrijving},{doc.KorteNaam},{doc.Naam},{doc.Roepnaam},{doc.Startdatum},{doc.Status},{doc.VCode},{doc.Verenigingstype.Code},{doc.Verenigingstype.Naam},{doc.KboNummer},\"{string.Join(", ", doc.CorresponderendeVCodes)}\",{doc.AantalVertegenwoordigers},{doc.DatumLaatsteAanpassing}\r\n");
$"{doc.Bron},{doc.Doelgroep.Maximumleeftijd},{doc.Doelgroep.Minimumleeftijd},{doc.Einddatum},{doc.IsUitgeschrevenUitPubliekeDatastroom},{doc.KorteBeschrijving},{doc.KorteNaam},{doc.Naam},{doc.Roepnaam},{doc.Startdatum},{doc.Status},{doc.VCode},{doc.Verenigingstype.Code},{doc.Verenigingstype.Naam},{doc.KboNummer},\"{string.Join(separator: ", ", doc.CorresponderendeVCodes)}\",{doc.AantalVertegenwoordigers},{doc.DatumLaatsteAanpassing}\r\n");
}

return stringBuilder.ToString();
Expand All @@ -61,17 +70,17 @@ private Mock<IAmazonS3> SetupS3Client()

s3ClientMock.Setup(x => x.PutObjectAsync(It.IsAny<PutObjectRequest>(), default))
.Callback<PutObjectRequest, CancellationToken>((request, _) => _resultStream = request.InputStream)
.ReturnsAsync(new PutObjectResponse { HttpStatusCode = System.Net.HttpStatusCode.OK });
.ReturnsAsync(new PutObjectResponse { HttpStatusCode = HttpStatusCode.OK });

return s3ClientMock;
}

private static async Task Export(IEnumerable<PowerBiExportDocument> docs, IAmazonS3 s3Client)
private async Task Export(IEnumerable<PowerBiExportDocument> docs)
{
var exporter = new Exporter(WellKnownFileNames.Basisgegevens,
"something",
bucketName: "something",
new BasisgegevensRecordWriter(),
s3Client,
_s3ClientMock.Object,
new NullLogger<Exporter>());

await exporter.Export(docs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using System.Net;
using System.Text;
using Xunit;

public class ContactgegevensExportTests
{
private Stream _resultStream = null;
private Stream _resultStream;
private readonly Fixture _fixture;
private readonly Mock<IAmazonS3> _s3ClientMock;

public ContactgegevensExportTests()
{
_fixture = new Fixture().CustomizeDomain();
_s3ClientMock = SetupS3Client();
}

[Fact]
public async Task WithMultipleDocuments_ThenCsvExportShouldExport()
{
var fixture = new Fixture().CustomizeDomain();
var s3ClientMock = SetupS3Client();
var docs = fixture.CreateMany<PowerBiExportDocument>();
var docs = _fixture.CreateMany<PowerBiExportDocument>();

await Export(docs, s3ClientMock.Object);
await Export(docs);

var actualResult = await GetActualResult();
var expectedResult = GetExpectedResult(docs);
Expand All @@ -50,7 +57,8 @@ private static string GetExpectedResult(IEnumerable<PowerBiExportDocument> docs)
{
foreach (var contactgegeven in doc.Contactgegevens)
{
stringBuilder.Append($"{contactgegeven.Beschrijving},{contactgegeven.Bron},{contactgegeven.ContactgegevenId},{contactgegeven.Contactgegeventype},{contactgegeven.IsPrimair},{doc.VCode},{contactgegeven.Waarde}\r\n");
stringBuilder.Append(
$"{contactgegeven.Beschrijving},{contactgegeven.Bron},{contactgegeven.ContactgegevenId},{contactgegeven.Contactgegeventype},{contactgegeven.IsPrimair},{doc.VCode},{contactgegeven.Waarde}\r\n");
}
}

Expand All @@ -63,17 +71,17 @@ private Mock<IAmazonS3> SetupS3Client()

s3ClientMock.Setup(x => x.PutObjectAsync(It.IsAny<PutObjectRequest>(), default))
.Callback<PutObjectRequest, CancellationToken>((request, _) => _resultStream = request.InputStream)
.ReturnsAsync(new PutObjectResponse { HttpStatusCode = System.Net.HttpStatusCode.OK });
.ReturnsAsync(new PutObjectResponse { HttpStatusCode = HttpStatusCode.OK });

return s3ClientMock;
}

private static async Task Export(IEnumerable<PowerBiExportDocument> docs, IAmazonS3 s3Client)
private async Task Export(IEnumerable<PowerBiExportDocument> docs)
{
var exporter = new Exporter(WellKnownFileNames.Contactgegevens,
"something",
bucketName: "something",
new ContactgegevensRecordWriter(),
s3Client,
_s3ClientMock.Object,
new NullLogger<Exporter>());

await exporter.Export(docs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using System.Net;
using System.Text;
using Xunit;

public class HoofdactiviteitenRecordWriterTests
{
private Stream _resultStream = null;

private Stream _resultStream;
private readonly Fixture _fixture;
private readonly Mock<IAmazonS3> _s3ClientMock;

public HoofdactiviteitenRecordWriterTests()
{
_fixture = new Fixture().CustomizeDomain();
_s3ClientMock = SetupS3Client();
_fixture = new Fixture().CustomizeDomain();
_s3ClientMock = SetupS3Client();
}

[Fact]
Expand All @@ -34,7 +34,7 @@ public async Task WithNoDocuments_ThenCsvExportsOnlyHeaders()
await Export(docs);

var actualResult = await GetActualResult();
actualResult.Should().BeEquivalentTo($"code,naam,vcode\r\n");
actualResult.Should().BeEquivalentTo("code,naam,vcode\r\n");
}

[Fact]
Expand Down Expand Up @@ -82,100 +82,19 @@ private Mock<IAmazonS3> SetupS3Client()

s3ClientMock.Setup(x => x.PutObjectAsync(It.IsAny<PutObjectRequest>(), default))
.Callback<PutObjectRequest, CancellationToken>((request, _) => _resultStream = request.InputStream)
.ReturnsAsync(new PutObjectResponse { HttpStatusCode = System.Net.HttpStatusCode.OK });
.ReturnsAsync(new PutObjectResponse { HttpStatusCode = HttpStatusCode.OK });

return s3ClientMock;
}

private async Task Export(IEnumerable<PowerBiExportDocument> docs)
{
var exporter = new Exporter(WellKnownFileNames.Hoofdactiviteiten,
"something",
bucketName: "something",
new HoofdactiviteitenRecordWriter(),
_s3ClientMock.Object,
new NullLogger<Exporter>());

await exporter.Export(docs);
}
}

// namespace AssociationRegistry.Test.PowerBi.ExportHost;
//
// using Admin.Schema.PowerBiExport;
// using AssociationRegistry.PowerBi.ExportHost;
// using AssociationRegistry.PowerBi.ExportHost.Writers;
// using FluentAssertions;
// using System.Text;
// using Xunit;
//
// public class HoofdactiviteitenRecordWriterTests
// {
// [Fact]
// public async Task WithNoDocuments_ThenCsvExportsOnlyHeaders()
// {
// var docs = Array.Empty<PowerBiExportDocument>();
//
// var content = await GenerateCsv(docs);
// content.Should().BeEquivalentTo($"code,naam,vcode\r\n");
// }
//
// [Fact]
// public async Task WithOneDocument_ThenCsvExportShouldExport()
// {
// var docs = new List<PowerBiExportDocument>
// {
// new()
// {
// VCode = "V0001001",
// HoofdactiviteitenVerenigingsloket = [
// new HoofdactiviteitVerenigingsloket(){ Naam = "hoofd", Code = "activiteit"},
// ]
// }
// };
//
// var content = await GenerateCsv(docs);
// content.Should().BeEquivalentTo($"code,naam,vcode\r\nactiviteit,hoofd,V0001001\r\n");
// }
//
// [Fact]
// public async Task WithMultipleDocuments_ThenCsvExportShouldExport()
// {
// var docs = new List<PowerBiExportDocument>
// {
// new()
// {
// VCode = "V0001001",
// HoofdactiviteitenVerenigingsloket = [
// new HoofdactiviteitVerenigingsloket(){ Naam = "hoofd", Code = "activiteit"},
// ]
// },
// new()
// {
// VCode = "V0001002",
// HoofdactiviteitenVerenigingsloket = [
// new HoofdactiviteitVerenigingsloket(){ Naam = "hoofd1", Code = "activiteit1"},
// new HoofdactiviteitVerenigingsloket(){ Naam = "hoofd2", Code = "activiteit2"},
// new HoofdactiviteitVerenigingsloket(){ Naam = "hoofd3", Code = "activiteit3"},
// ]
// }
// };
//
// var content = await GenerateCsv(docs);
// content.Should().BeEquivalentTo($"code,naam,vcode\r\nactiviteit,hoofd,V0001001\r\nactiviteit1,hoofd1,V0001002\r\nactiviteit2,hoofd2,V0001002\r\nactiviteit3,hoofd3,V0001002\r\n");
// }
//
// private static async Task<string> GenerateCsv(IEnumerable<PowerBiExportDocument> docs)
// {
// var exporter = new PowerBiDocumentExporter();
//
// var exportStream = await exporter.Export(docs, new HoofdactiviteitenRecordWriter());
//
// using var reader = new StreamReader(exportStream, Encoding.UTF8);
//
// var content = await reader.ReadToEndAsync();
//
// return content;
// }
// }
//
//
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@

public class LocatiesExportTests
{
private Stream _resultStream = null;
private Stream _resultStream;
private readonly Fixture _fixture;
private readonly Mock<IAmazonS3> _s3ClientMock;

public LocatiesExportTests()
{
_fixture = new Fixture().CustomizeDomain();
_s3ClientMock = SetupS3Client();
}

[Fact]
public async Task WithMultipleDocuments_ThenCsvExportShouldExport()
{
var fixture = new Fixture().CustomizeDomain();
var s3ClientMock = SetupS3Client();
var docs = fixture.CreateMany<PowerBiExportDocument>();
var docs = _fixture.CreateMany<PowerBiExportDocument>();

await Export(docs, s3ClientMock.Object);
await Export(docs);

var actualResult = await GetActualResult();
var expectedResult = GetExpectedResult(docs);
Expand All @@ -45,7 +51,9 @@ private async Task<string> GetActualResult()
private static string GetExpectedResult(IEnumerable<PowerBiExportDocument> docs)
{
var stringBuilder = new StringBuilder();
stringBuilder.Append("adresId.broncode,adresId.bronwaarde,adresvoorstelling,bron,busnummer,gemeente,huisnummer,isPrimair,land,locatieId,locatieType,naam,postcode,straatnaam,vCode\r\n");

stringBuilder.Append(
"adresId.broncode,adresId.bronwaarde,adresvoorstelling,bron,busnummer,gemeente,huisnummer,isPrimair,land,locatieId,locatieType,naam,postcode,straatnaam,vCode\r\n");

foreach (var doc in docs)
{
Expand All @@ -70,12 +78,12 @@ private Mock<IAmazonS3> SetupS3Client()
return s3ClientMock;
}

private static async Task Export(IEnumerable<PowerBiExportDocument> docs, IAmazonS3 s3Client)
private async Task Export(IEnumerable<PowerBiExportDocument> docs)
{
var exporter = new Exporter(WellKnownFileNames.Locaties,
"something",
bucketName: "something",
new LocatiesRecordWriter(),
s3Client,
_s3ClientMock.Object,
new NullLogger<Exporter>());

await exporter.Export(docs);
Expand Down

0 comments on commit d9d334b

Please sign in to comment.