From 805f782c83b2ba65ff1188ad7c3b3f91a890015d Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Tue, 13 Feb 2024 12:38:37 +0100 Subject: [PATCH] Tests: Add tests for documents --- .gitea/workflows/test.yaml | 4 ++- Elwig/Documents/DeliveryNote.cs | 4 +-- Elwig/Helpers/Printing/Html.cs | 4 +-- Elwig/Helpers/Printing/Pdf.cs | 17 ++++++++---- Tests/DocumentTests/DeliveryNoteTest.cs | 31 +++++++++++++++++++++ Tests/DocumentTests/Setup.cs | 37 +++++++++++++++++++++++++ Tests/DocumentTests/Utils.cs | 20 +++++++++++++ Tests/Resources/Sql/DocumentDelete.sql | 5 ++++ Tests/Resources/Sql/DocumentInsert.sql | 20 +++++++++++++ Tests/Tests.csproj | 7 +++-- 10 files changed, 136 insertions(+), 13 deletions(-) create mode 100644 Tests/DocumentTests/DeliveryNoteTest.cs create mode 100644 Tests/DocumentTests/Setup.cs create mode 100644 Tests/DocumentTests/Utils.cs create mode 100644 Tests/Resources/Sql/DocumentDelete.sql create mode 100644 Tests/Resources/Sql/DocumentInsert.sql diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 264544d..ef5bbe0 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -24,4 +24,6 @@ jobs: run: $(& dotnet build Tests; $a=$lastexitcode) | findstr x*; exit $a - name: Run Tests shell: powershell - run: $(& dotnet test Tests; $a=$lastexitcode) | findstr x*; exit $a + run: | + $env:PATH += ";$(pwd)\Installer\Files" + $(& dotnet test Tests; $a=$lastexitcode) | findstr x*; exit $a diff --git a/Elwig/Documents/DeliveryNote.cs b/Elwig/Documents/DeliveryNote.cs index 8c069c9..3f9463f 100644 --- a/Elwig/Documents/DeliveryNote.cs +++ b/Elwig/Documents/DeliveryNote.cs @@ -15,7 +15,7 @@ namespace Elwig.Documents { // 3 - full public int DisplayStats = App.Client.ModeDeliveryNoteStats; - public DeliveryNote(Delivery d, AppDbContext ctx) : base($"Traubenübernahmeschein Nr. {d.LsNr}", d.Member) { + public DeliveryNote(Delivery d, AppDbContext? ctx = null) : base($"Traubenübernahmeschein Nr. {d.LsNr}", d.Member) { UseBillingAddress = true; ShowDateAndLocation = true; Delivery = d; @@ -27,7 +27,7 @@ namespace Elwig.Documents { $""; Text = App.Client.TextDeliveryNote; DocumentId = d.LsNr; - MemberBuckets = ctx.GetMemberBuckets(d.Year, d.Member.MgNr).GetAwaiter().GetResult(); + MemberBuckets = ctx?.GetMemberBuckets(d.Year, d.Member.MgNr).GetAwaiter().GetResult() ?? []; } } } diff --git a/Elwig/Helpers/Printing/Html.cs b/Elwig/Helpers/Printing/Html.cs index d578637..779b8ff 100644 --- a/Elwig/Helpers/Printing/Html.cs +++ b/Elwig/Helpers/Printing/Html.cs @@ -8,7 +8,7 @@ namespace Elwig.Helpers.Printing { private static RazorLightEngine? Engine = null; public static bool IsReady => Engine != null; - public static async Task Init(Action evtHandler) { + public static async Task Init(Action? evtHandler = null) { var e = new RazorLightEngineBuilder() .UseFileSystemProject(App.DataPath + "resources") .UseMemoryCachingProvider() @@ -24,7 +24,7 @@ namespace Elwig.Helpers.Printing { await e.CompileTemplateAsync("DeliveryConfirmation"); Engine = e; - evtHandler(); + evtHandler?.Invoke(); } public static async Task CompileRenderAsync(string key, object model) { diff --git a/Elwig/Helpers/Printing/Pdf.cs b/Elwig/Helpers/Printing/Pdf.cs index 103405a..d52bfe1 100644 --- a/Elwig/Helpers/Printing/Pdf.cs +++ b/Elwig/Helpers/Printing/Pdf.cs @@ -11,13 +11,20 @@ using System.Linq; namespace Elwig.Helpers.Printing { public static class Pdf { - private static readonly string PdfToPrinter = App.ExePath + "PDFtoPrinter.exe"; - private static readonly string WinziPrint = App.ExePath + "WinziPrint.exe"; + private static readonly string PdfToPrinter = new string[] { App.ExePath } + .Union(Environment.GetEnvironmentVariable("PATH")?.Split(';') ?? []) + .Select(x => Path.Combine(x, "PDFtoPrinter.exe")) + .Where(x => File.Exists(x)) + .FirstOrDefault() ?? throw new FileNotFoundException("PDFtoPrinter executable not found"); + private static readonly string WinziPrint = new string[] { App.ExePath } + .Union(Environment.GetEnvironmentVariable("PATH")?.Split(';') ?? []) + .Select(x => Path.Combine(x, "WinziPrint.exe")) + .Where(x => File.Exists(x)) + .FirstOrDefault() ?? throw new FileNotFoundException("WiniPrint executable not found"); private static Process? WinziPrintProc; public static bool IsReady => WinziPrintProc != null; - - public static async Task Init(Action evtHandler) { + public static async Task Init(Action? evtHandler = null) { var p = new Process() { StartInfo = new() { FileName = WinziPrint, CreateNoWindow = true, @@ -33,7 +40,7 @@ namespace Elwig.Helpers.Printing { p.StartInfo.ArgumentList.Add("-"); p.Start(); WinziPrintProc = p; - evtHandler(); + evtHandler?.Invoke(); } public static async Task> Convert(string htmlPath, string pdfPath, bool doubleSided = false, IProgress? progress = null) { diff --git a/Tests/DocumentTests/DeliveryNoteTest.cs b/Tests/DocumentTests/DeliveryNoteTest.cs new file mode 100644 index 0000000..3c7cb14 --- /dev/null +++ b/Tests/DocumentTests/DeliveryNoteTest.cs @@ -0,0 +1,31 @@ +using Elwig.Documents; +using Elwig.Helpers; + +namespace Tests.DocumentTests { + [TestFixture] + public class DeliveryNoteTest { + + private readonly AppDbContext Context = new(); + + [Test] + public async Task Test_01_OneDeliveryPart() { + var d = await Context.Deliveries.FindAsync(2020, 1); + using var doc = new DeliveryNote(d!, Context); + var text = await Utils.GeneratePdfText(doc); + Console.Write(text); + Assert.Multiple(() => { + Assert.That(text, Contains.Substring(""" + MUSTERMANN Max + Winzerstraße 1 + 2223 Hohenruppersdorf + """)); + Assert.That(text, Contains.Substring("1472583")); // Betriebsnummer + Assert.That(text, Contains.Substring("pauschaliert")); + Assert.That(text, Contains.Substring($"Wolkersdorf, am {DateTime.Now:dd.MM.yyyy}")); + Assert.That(text, Contains.Substring("Traubenübernahmeschein Nr. 20201001X001")); + // TODO + Assert.That(text, Contains.Substring("Ich bin der Text, der auf einem Traubenübernahmeschein steht!")); + }); + } + } +} diff --git a/Tests/DocumentTests/Setup.cs b/Tests/DocumentTests/Setup.cs new file mode 100644 index 0000000..0720460 --- /dev/null +++ b/Tests/DocumentTests/Setup.cs @@ -0,0 +1,37 @@ +using Elwig.Helpers; +using System.Reflection; +using Microsoft.Data.Sqlite; +using Elwig.Helpers.Printing; + +namespace Tests.DocumentTests { + [SetUpFixture] + public class Setup { + + private SqliteConnection? Connection; + + [OneTimeSetUp] + public async Task SetupDatabase() { + Connection = await AppDbContext.ConnectAsync(); + await AppDbContext.ExecuteEmbeddedScript(Connection, Assembly.GetExecutingAssembly(), "Tests.Resources.Sql.DocumentInsert.sql"); + } + + [OneTimeTearDown] + public async Task TeardownDatabase() { + if (Connection == null) return; + await AppDbContext.ExecuteEmbeddedScript(Connection, Assembly.GetExecutingAssembly(), "Tests.Resources.Sql.DocumentDelete.sql"); + await Connection.DisposeAsync(); + Connection = null; + } + + [OneTimeSetUp] + public async Task SetupPrinting() { + await Html.Init(); + await Pdf.Init(); + } + + [OneTimeTearDown] + public void TeardownPrinting() { + // no teardown needed yet + } + } +} diff --git a/Tests/DocumentTests/Utils.cs b/Tests/DocumentTests/Utils.cs new file mode 100644 index 0000000..89e6dac --- /dev/null +++ b/Tests/DocumentTests/Utils.cs @@ -0,0 +1,20 @@ +using Elwig.Documents; +using NReco.PdfRenderer; + +namespace Tests.DocumentTests { + public static class Utils { + + private static readonly string FileName = Path.Combine(Path.GetTempPath(), "test_document.pdf"); + + public static async Task GeneratePdfText(Document doc) { + await doc.Generate(); + try { + doc.SaveTo(FileName); + var conv = new PdfToTextConverter { CustomArgs = "-raw " }; + return conv.GenerateText(FileName); + } finally { + File.Delete(FileName); + } + } + } +} diff --git a/Tests/Resources/Sql/DocumentDelete.sql b/Tests/Resources/Sql/DocumentDelete.sql new file mode 100644 index 0000000..c74c86a --- /dev/null +++ b/Tests/Resources/Sql/DocumentDelete.sql @@ -0,0 +1,5 @@ +-- deletes for DocumentTests + +DELETE FROM delivery; +DELETE FROM season; +DELETE FROM wine_attribute; diff --git a/Tests/Resources/Sql/DocumentInsert.sql b/Tests/Resources/Sql/DocumentInsert.sql new file mode 100644 index 0000000..167bffa --- /dev/null +++ b/Tests/Resources/Sql/DocumentInsert.sql @@ -0,0 +1,20 @@ +-- inserts for DocumentTests + +INSERT INTO wine_attribute (attrid, name, active, max_kg_per_ha, strict, fill_lower) VALUES +('B', 'Bio', TRUE, NULL, FALSE, 0), +('K', 'Kabinett', TRUE, NULL, FALSE, 0); + +INSERT INTO season (year, currency, min_kg_per_bs, max_kg_per_bs, penalty_per_kg, penalty_amount, penalty_none, start_date, end_date) VALUES +(2020, 'EUR', 1000, 2000, NULL, NULL, NULL, NULL, NULL); + +INSERT INTO modifier (year, modid, ordering, name, abs, rel, standard, quick_select) VALUES +(2020, 'S', 0, 'Geschädigte Trauben', NULL, -0.1, FALSE, FALSE), +(2020, 'A', 0, 'Keine Voranmeldung', -1000, NULL, FALSE, FALSE); + +INSERT INTO delivery (mgnr, year, did, date, time, zwstid, lnr) VALUES +(101, 2020, 1, '2020-10-01', NULL, 'X', 1), +(101, 2020, 2, '2020-10-01', NULL, 'X', 2), +(101, 2020, 3, '2020-10-01', NULL, 'X', 3); + +INSERT INTO delivery_part (year, did, dpnr, sortid, attrid, weight, kmw, qualid, hkid, kgnr, gerebelt, manual_weighing, spl_check, scale_id, weighing_id, weighing_reason) VALUES +(2020, 1, 1, 'GV', NULL, 4000, 17, 'KAB', 'WLNO', 06109, TRUE, FALSE, FALSE, NULL, NULL, NULL); diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 5567fb8..849befc 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -4,9 +4,9 @@ net8.0-windows enable enable - false true + true @@ -19,10 +19,11 @@ - + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive