Tests: Add tests for documents
This commit is contained in:
31
Tests/DocumentTests/DeliveryNoteTest.cs
Normal file
31
Tests/DocumentTests/DeliveryNoteTest.cs
Normal file
@ -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!"));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
37
Tests/DocumentTests/Setup.cs
Normal file
37
Tests/DocumentTests/Setup.cs
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
20
Tests/DocumentTests/Utils.cs
Normal file
20
Tests/DocumentTests/Utils.cs
Normal file
@ -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<string> GeneratePdfText(Document doc) {
|
||||
await doc.Generate();
|
||||
try {
|
||||
doc.SaveTo(FileName);
|
||||
var conv = new PdfToTextConverter { CustomArgs = "-raw " };
|
||||
return conv.GenerateText(FileName);
|
||||
} finally {
|
||||
File.Delete(FileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user