From ec249104d326359aad4968f83e0a6cca34fe18a2 Mon Sep 17 00:00:00 2001 From: Alexander Kastler Date: Sun, 4 Feb 2024 13:09:41 +0100 Subject: [PATCH] Add tests for DocumentModelFactory #237 --- .../CreateAcceptanceModel.cs | 122 ++++++++++- .../CreateProductLabelModel.cs | 167 +++++++++++++++ .../CreateSaleModel.cs | 199 ++++++++++++++++++ 3 files changed, 484 insertions(+), 4 deletions(-) create mode 100644 test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateProductLabelModel.cs create mode 100644 test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateSaleModel.cs diff --git a/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateAcceptanceModel.cs b/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateAcceptanceModel.cs index 38bfada..5f13b92 100644 --- a/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateAcceptanceModel.cs +++ b/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateAcceptanceModel.cs @@ -5,7 +5,20 @@ namespace BraunauMobil.VeloBasar.Tests.BusinessLogic.DocumentModelFactoryTests; public class CreateAcceptanceModel : TestBase { -#warning @todo NEXT: With status link, and throw exception + [Fact] + public void NoSellerThrowsInvalidOperationException() + { + // Arrange + TransactionEntity acceptance = Fixture.BuildTransaction() + .Without(transaction => transaction.Seller) + .Create(); + + // Act + Action act = () => Sut.CreateAcceptanceModel(acceptance); + + // Assert + act.Should().Throw().WithMessage("Cannot create Acceptance Document without Seller"); + } [Fact] public void NoStatusLink() @@ -32,7 +45,7 @@ public void NoStatusLink() .With(acceptance => acceptance.Seller, seller) .With(acceptance => acceptance.TimeStamp, X.FirstContactDay.AddHours(2)) .Create(); - + ProductTypeEntity rennradType = Fixture.BuildProductType() .With(type => type.Name, "Rennrad") .Create(); @@ -69,14 +82,17 @@ public void NoStatusLink() AcceptanceDocumentModel result = Sut.CreateAcceptanceModel(acceptance); // Assert - AcceptanceDocumentModel expectedModel = new ( + AcceptanceDocumentModel expectedModel = new( Settings.Acceptance.TitleFormat, "Wasserau, 05.04.2063", "VeloBasar_PageNumberFromOverall", " - powered by https://github.com/braunau-mobil/velo-basar", Settings.PageMargins, Settings.Acceptance.SubTitle, - "Hamfast Gamdschie\r\nBeutelsend 4\r\nA457857 Hobbingen\r\n", + @"Hamfast Gamdschie +Beutelsend 4 +A457857 Hobbingen +", "VeloBasar_SellerIdShort_0", false, string.Empty, @@ -103,4 +119,102 @@ public void NoStatusLink() ); result.Should().BeEquivalentTo(expectedModel); } + + [Fact] + public void WithStatusLink() + { + // Arrange + Settings.Acceptance.SignatureText = "Unterschrift"; + Settings.Acceptance.StatusLinkFormat = "StatusLinkFormat_{0}"; + + BasarEntity basar = Fixture.BuildBasar() + .With(basar => basar.Name, "4. Basar") + .With(basar => basar.Location, "Wasserau") + .With(basar => basar.Date, X.FirstContactDay) + .Create(); + + SellerEntity seller = Fixture.BuildSeller() + .With(seller => seller.City, "Hobbingen") + .With(seller => seller.FirstName, "Hamfast") + .With(seller => seller.LastName, "Gamdschie") + .With(seller => seller.Street, "Beutelsend 4") + .With(seller => seller.ZIP, "A457857") + .With(seller => seller.Token, "X1234") + .Create(); + + TransactionEntity acceptance = Fixture.BuildTransaction(basar) + .With(acceptance => acceptance.Seller, seller) + .With(acceptance => acceptance.TimeStamp, X.FirstContactDay.AddHours(2)) + .Create(); + + ProductTypeEntity rennradType = Fixture.BuildProductType() + .With(type => type.Name, "Rennrad") + .Create(); + ProductEntity rennrad = Fixture.BuildProduct() + .With(product => product.Id, 1) + .With(product => product.Brand, "Harlond Räder") + .With(product => product.Color, "blau") + .With(product => product.Description, "Mein tolles Rennrad") + .With(product => product.DonateIfNotSold, false) + .With(product => product.FrameNumber, "123456") + .With(product => product.Price, 12.34m) + .With(product => product.TireSize, "29") + .With(product => product.Type, rennradType) + .Create(); + acceptance.Products.Add(new ProductToTransactionEntity { Product = rennrad, Transaction = acceptance }); + + ProductTypeEntity cityBikeType = Fixture.BuildProductType() + .With(type => type.Name, "City Bike") + .Create(); + ProductEntity cityBike = Fixture.BuildProduct() + .With(product => product.Id, 2) + .With(product => product.Brand, "GCB") + .With(product => product.Color, "grün") + .With(product => product.Description, "Das schönste City Bike") + .With(product => product.DonateIfNotSold, true) + .With(product => product.FrameNumber, "XYZ") + .With(product => product.Price, 154.23m) + .With(product => product.TireSize, "28") + .With(product => product.Type, cityBikeType) + .Create(); + acceptance.Products.Add(new ProductToTransactionEntity { Product = cityBike, Transaction = acceptance }); + + // Act + AcceptanceDocumentModel result = Sut.CreateAcceptanceModel(acceptance); + + // Assert + AcceptanceDocumentModel expectedModel = new( + Settings.Acceptance.TitleFormat, + "Wasserau, 05.04.2063", + "VeloBasar_PageNumberFromOverall", + " - powered by https://github.com/braunau-mobil/velo-basar", + Settings.PageMargins, + Settings.Acceptance.SubTitle, + "Hamfast Gamdschie\r\nBeutelsend 4\r\nA457857 Hobbingen\r\n", + "VeloBasar_SellerIdShort_0", + true, + "StatusLinkFormat_X1234", + Settings.Acceptance.TokenTitle, + seller.Token, + "Unterschrift ______________________________", + "VeloBasar_AtLocationAndDateAndTime_Wasserau_05.04.2063 13:22:33", + Settings.QrCodeLengthMillimeters, + new ProductsTableDocumentModel( + "VeloBasar_Id", + "VeloBasar_ProductDescription", + "VeloBasar_Size", + "VeloBasar_Price", + "VeloBasar_Sum:", + "VeloBasar_ProductCounter_2", + "€ 166,57", + null, + new[] + { + new ProductTableRowDocumentModel("1", "Harlond Räder - Rennrad\r\nMein tolles Rennrad\r\n blau 123456", "29", "€ 12,34", null), + new ProductTableRowDocumentModel("2", "GCB - City Bike\r\nDas schönste City Bike\r\n grün XYZ\r\nVeloBasar_DonateIfNotSoldOnProductTable\r\n", "28", "€ 154,23", null), + } + ) + ); + result.Should().BeEquivalentTo(expectedModel); + } } diff --git a/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateProductLabelModel.cs b/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateProductLabelModel.cs new file mode 100644 index 0000000..7658763 --- /dev/null +++ b/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateProductLabelModel.cs @@ -0,0 +1,167 @@ +using BraunauMobil.VeloBasar.Models.Documents; + +namespace BraunauMobil.VeloBasar.Tests.BusinessLogic.DocumentModelFactoryTests; + +public class CreateProductLabelModel + : TestBase +{ + [Fact] + public void NoFrameNumberNoTireSize() + { + // Arrange + Settings.Label.TitleFormat = "TitleFormat_{0}"; + Settings.Label.MaxDescriptionLength = 30; + + ProductTypeEntity productType = Fixture.BuildProductType() + .With(productType => productType.Name, "City-Bike") + .Create(); + + ProductEntity product = Fixture.BuildProduct() + .With(product => product.Id, 666) + .With(product => product.Brand, "Cube") + .With(product => product.Description, "This is a really great Bike. I like it a lot, but unfortunatley the description is way to long to fit on the tiny label that gets printed out.") + .With(product => product.Type, productType) + .With(product => product.Price, 123.45m) + .Without(product => product.FrameNumber) + .Without(product => product.TireSize) + .Create(); + product.Session.Basar.Name = "1. Basar"; + + // Act + ProductLabelDocumentModel result = Sut.CreateProductLabelModel(product); + + // Assert + ProductLabelDocumentModel expectedModel = new( + "TitleFormat_1. Basar", + "Cube - City-Bike", + "This is a really great Bike. I", + product.Color, + null, + null, + "666", + "€ 123,45" + ); + + result.Should().BeEquivalentTo(expectedModel); + } + + [Fact] + public void WithFrameNumberNoTireSize() + { + // Arrange + Settings.Label.TitleFormat = "TitleFormat_{0}"; + Settings.Label.MaxDescriptionLength = 30; + + ProductTypeEntity productType = Fixture.BuildProductType() + .With(productType => productType.Name, "City-Bike") + .Create(); + + ProductEntity product = Fixture.BuildProduct() + .With(product => product.Id, 666) + .With(product => product.Brand, "Cube") + .With(product => product.Description, "This is a really great Bike. I like it a lot, but unfortunatley the description is way to long to fit on the tiny label that gets printed out.") + .With(product => product.Type, productType) + .With(product => product.Price, 123.45m) + .With(product => product.FrameNumber, "1234567890") + .Without(product => product.TireSize) + .Create(); + product.Session.Basar.Name = "1. Basar"; + + // Act + ProductLabelDocumentModel result = Sut.CreateProductLabelModel(product); + + // Assert + ProductLabelDocumentModel expectedModel = new( + "TitleFormat_1. Basar", + "Cube - City-Bike", + "This is a really great Bike. I", + product.Color, + "VeloBasar_FrameNumberLabel_1234567890", + null, + "666", + "€ 123,45" + ); + + result.Should().BeEquivalentTo(expectedModel); + } + + [Fact] + public void NoFrameNumberWithTireSize() + { + // Arrange + Settings.Label.TitleFormat = "TitleFormat_{0}"; + Settings.Label.MaxDescriptionLength = 30; + + ProductTypeEntity productType = Fixture.BuildProductType() + .With(productType => productType.Name, "City-Bike") + .Create(); + + ProductEntity product = Fixture.BuildProduct() + .With(product => product.Id, 666) + .With(product => product.Brand, "Cube") + .With(product => product.Description, "This is a really great Bike. I like it a lot, but unfortunatley the description is way to long to fit on the tiny label that gets printed out.") + .With(product => product.Type, productType) + .With(product => product.Price, 123.45m) + .Without(product => product.FrameNumber) + .With(product => product.TireSize, "28") + .Create(); + product.Session.Basar.Name = "1. Basar"; + + // Act + ProductLabelDocumentModel result = Sut.CreateProductLabelModel(product); + + // Assert + ProductLabelDocumentModel expectedModel = new( + "TitleFormat_1. Basar", + "Cube - City-Bike", + "This is a really great Bike. I", + product.Color, + null, + "VeloBasar_TireSizeLabel_28", + "666", + "€ 123,45" + ); + + result.Should().BeEquivalentTo(expectedModel); + } + + [Fact] + public void WithFrameNumberAndTireSize() + { + // Arrange + Settings.Label.TitleFormat = "TitleFormat_{0}"; + Settings.Label.MaxDescriptionLength = 30; + + ProductTypeEntity productType = Fixture.BuildProductType() + .With(productType => productType.Name, "City-Bike") + .Create(); + + ProductEntity product = Fixture.BuildProduct() + .With(product => product.Id, 666) + .With(product => product.Brand, "Cube") + .With(product => product.Description, "This is a really great Bike. I like it a lot, but unfortunatley the description is way to long to fit on the tiny label that gets printed out.") + .With(product => product.Type, productType) + .With(product => product.FrameNumber, "ABCD") + .With(product => product.TireSize, "29") + .With(product => product.Price, 123.45m) + .Create(); + product.Session.Basar.Name = "1. Basar"; + + // Act + ProductLabelDocumentModel result = Sut.CreateProductLabelModel(product); + + // Assert + ProductLabelDocumentModel expectedModel = new( + "TitleFormat_1. Basar", + "Cube - City-Bike", + "This is a really great Bike. I", + product.Color, + "VeloBasar_FrameNumberLabel_ABCD", + "VeloBasar_TireSizeLabel_29", + "666", + "€ 123,45" + ); + + result.Should().BeEquivalentTo(expectedModel); + } +} diff --git a/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateSaleModel.cs b/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateSaleModel.cs new file mode 100644 index 0000000..78af50d --- /dev/null +++ b/test/BraunauMobil.VeloBasar.Tests/BusinessLogic/DocumentModelFactoryTests/CreateSaleModel.cs @@ -0,0 +1,199 @@ +using BraunauMobil.VeloBasar.Models.Documents; + +namespace BraunauMobil.VeloBasar.Tests.BusinessLogic.DocumentModelFactoryTests; + +public class CreateSaleModel + : TestBase +{ + [Fact] + public void NoBanner() + { + // Arrange + Settings.Acceptance.SignatureText = "Unterschrift"; + Settings.Acceptance.StatusLinkFormat = null; + + BasarEntity basar = Fixture.BuildBasar() + .With(basar => basar.Name, "4. Basar") + .With(basar => basar.Location, "Wasserau") + .With(basar => basar.Date, X.FirstContactDay) + .Create(); + + SellerEntity seller = Fixture.BuildSeller() + .With(seller => seller.City, "Hobbingen") + .With(seller => seller.FirstName, "Hamfast") + .With(seller => seller.LastName, "Gamdschie") + .With(seller => seller.Street, "Beutelsend 4") + .With(seller => seller.ZIP, "A457857") + .Create(); + + TransactionEntity acceptance = Fixture.BuildTransaction(basar) + .With(acceptance => acceptance.Seller, seller) + .With(acceptance => acceptance.TimeStamp, X.FirstContactDay.AddHours(2)) + .Create(); + + ProductTypeEntity rennradType = Fixture.BuildProductType() + .With(type => type.Name, "Rennrad") + .Create(); + ProductEntity rennrad = Fixture.BuildProduct() + .With(product => product.Id, 1) + .With(product => product.Brand, "Harlond Räder") + .With(product => product.Color, "blau") + .With(product => product.Description, "Mein tolles Rennrad") + .With(product => product.DonateIfNotSold, false) + .With(product => product.FrameNumber, "123456") + .With(product => product.Price, 12.34m) + .With(product => product.TireSize, "29") + .With(product => product.Type, rennradType) + .Create(); + acceptance.Products.Add(new ProductToTransactionEntity { Product = rennrad, Transaction = acceptance }); + + ProductTypeEntity cityBikeType = Fixture.BuildProductType() + .With(type => type.Name, "City Bike") + .Create(); + ProductEntity cityBike = Fixture.BuildProduct() + .With(product => product.Id, 2) + .With(product => product.Brand, "GCB") + .With(product => product.Color, "grün") + .With(product => product.Description, "Das schönste City Bike") + .With(product => product.DonateIfNotSold, true) + .With(product => product.FrameNumber, "XYZ") + .With(product => product.Price, 154.23m) + .With(product => product.TireSize, "28") + .With(product => product.Type, cityBikeType) + .Create(); + acceptance.Products.Add(new ProductToTransactionEntity { Product = cityBike, Transaction = acceptance }); + + // Act + SaleDocumentModel result = Sut.CreateSaleModel(acceptance); + + // Assert + SaleDocumentModel expectedModel = new( + Settings.Acceptance.TitleFormat, + "Wasserau, 05.04.2063", + "VeloBasar_PageNumberFromOverall", + " - powered by https://github.com/braunau-mobil/velo-basar", + Settings.PageMargins, + Settings.Acceptance.SubTitle, + false, + string.Empty, + string.Empty, + "website", + "hinttext", + "footertext", + new ProductsTableDocumentModel( + "VeloBasar_Id", + "VeloBasar_ProductDescription", + "VeloBasar_Size", + "VeloBasar_Price", + "VeloBasar_Sum:", + "VeloBasar_ProductCounter_2", + "€ 166,57", + null, + new[] + { + new ProductTableRowDocumentModel("1", "Harlond Räder - Rennrad\r\nMein tolles Rennrad\r\n blau 123456", "29", "€ 12,34", null), + new ProductTableRowDocumentModel("2", "GCB - City Bike\r\nDas schönste City Bike\r\n grün XYZ\r\nVeloBasar_DonateIfNotSoldOnProductTable\r\n", "28", "€ 154,23", null), + } + ) + ); + result.Should().BeEquivalentTo(expectedModel); + } + + [Fact] + public void WithBanner() + { + // Arrange + Settings.Acceptance.SignatureText = "Unterschrift"; + Settings.Acceptance.StatusLinkFormat = "StatusLinkFormat_{0}"; + + BasarEntity basar = Fixture.BuildBasar() + .With(basar => basar.Name, "4. Basar") + .With(basar => basar.Location, "Wasserau") + .With(basar => basar.Date, X.FirstContactDay) + .Create(); + + SellerEntity seller = Fixture.BuildSeller() + .With(seller => seller.City, "Hobbingen") + .With(seller => seller.FirstName, "Hamfast") + .With(seller => seller.LastName, "Gamdschie") + .With(seller => seller.Street, "Beutelsend 4") + .With(seller => seller.ZIP, "A457857") + .With(seller => seller.Token, "X1234") + .Create(); + + TransactionEntity acceptance = Fixture.BuildTransaction(basar) + .With(acceptance => acceptance.Seller, seller) + .With(acceptance => acceptance.TimeStamp, X.FirstContactDay.AddHours(2)) + .Create(); + + ProductTypeEntity rennradType = Fixture.BuildProductType() + .With(type => type.Name, "Rennrad") + .Create(); + ProductEntity rennrad = Fixture.BuildProduct() + .With(product => product.Id, 1) + .With(product => product.Brand, "Harlond Räder") + .With(product => product.Color, "blau") + .With(product => product.Description, "Mein tolles Rennrad") + .With(product => product.DonateIfNotSold, false) + .With(product => product.FrameNumber, "123456") + .With(product => product.Price, 12.34m) + .With(product => product.TireSize, "29") + .With(product => product.Type, rennradType) + .Create(); + acceptance.Products.Add(new ProductToTransactionEntity { Product = rennrad, Transaction = acceptance }); + + ProductTypeEntity cityBikeType = Fixture.BuildProductType() + .With(type => type.Name, "City Bike") + .Create(); + ProductEntity cityBike = Fixture.BuildProduct() + .With(product => product.Id, 2) + .With(product => product.Brand, "GCB") + .With(product => product.Color, "grün") + .With(product => product.Description, "Das schönste City Bike") + .With(product => product.DonateIfNotSold, true) + .With(product => product.FrameNumber, "XYZ") + .With(product => product.Price, 154.23m) + .With(product => product.TireSize, "28") + .With(product => product.Type, cityBikeType) + .Create(); + acceptance.Products.Add(new ProductToTransactionEntity { Product = cityBike, Transaction = acceptance }); + + // Act + AcceptanceDocumentModel result = Sut.CreateAcceptanceModel(acceptance); + + // Assert + AcceptanceDocumentModel expectedModel = new( + Settings.Acceptance.TitleFormat, + "Wasserau, 05.04.2063", + "VeloBasar_PageNumberFromOverall", + " - powered by https://github.com/braunau-mobil/velo-basar", + Settings.PageMargins, + Settings.Acceptance.SubTitle, + "Hamfast Gamdschie\r\nBeutelsend 4\r\nA457857 Hobbingen\r\n", + "VeloBasar_SellerIdShort_0", + true, + "StatusLinkFormat_X1234", + Settings.Acceptance.TokenTitle, + seller.Token, + "Unterschrift ______________________________", + "VeloBasar_AtLocationAndDateAndTime_Wasserau_05.04.2063 13:22:33", + Settings.QrCodeLengthMillimeters, + new ProductsTableDocumentModel( + "VeloBasar_Id", + "VeloBasar_ProductDescription", + "VeloBasar_Size", + "VeloBasar_Price", + "VeloBasar_Sum:", + "VeloBasar_ProductCounter_2", + "€ 166,57", + null, + new[] + { + new ProductTableRowDocumentModel("1", "Harlond Räder - Rennrad\r\nMein tolles Rennrad\r\n blau 123456", "29", "€ 12,34", null), + new ProductTableRowDocumentModel("2", "GCB - City Bike\r\nDas schönste City Bike\r\n grün XYZ\r\nVeloBasar_DonateIfNotSoldOnProductTable\r\n", "28", "€ 154,23", null), + } + ) + ); + result.Should().BeEquivalentTo(expectedModel); + } +}