From 34178105a76dbcf43982384c7bb18f8b972303f3 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Mon, 8 Jul 2024 13:05:21 +0200 Subject: [PATCH] E2ETests: Use ElwigTestDB.sqlite3 instead of default --- .gitignore | 1 + Elwig/App.xaml.cs | 7 ++++- Elwig/Helpers/AppDbContext.cs | 8 +++--- Elwig/Helpers/AppDbUpdater.cs | 2 +- Elwig/Helpers/Config.cs | 4 +-- Tests/E2ETests/AppSession.cs | 4 ++- Tests/E2ETests/MainWindowTest.cs | 2 +- Tests/E2ETests/MemberAdminWindowTest.cs | 38 ++++++++++++------------- Tests/E2ETests/Setup.cs | 21 +++++++++++++- Tests/E2ETests/Utils.cs | 4 ++- Tests/config.test.ini | 7 +++++ 11 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 Tests/config.test.ini diff --git a/.gitignore b/.gitignore index d05c040..4af30d2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ bin/ Tests/Resources/Sql/Create.sql *.exe !WinziPrint.exe +*.sqlite3 diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index 846cd2d..343712f 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -31,8 +31,8 @@ namespace Elwig { public static readonly string DataPath = @"C:\ProgramData\Elwig\"; public static readonly string ExePath = @"C:\Program Files\Elwig\"; public static readonly string TempPath = Path.Combine(Path.GetTempPath(), "Elwig"); - public static readonly Config Config = new(DataPath + "config.ini"); + public static Config Config { get; private set; } = new(Path.Combine(DataPath, "config.ini")); public static int VersionMajor { get; private set; } public static int VersionMinor { get; private set; } public static int VersionPatch { get; private set; } @@ -74,6 +74,11 @@ namespace Elwig { CurrentApp = this; OverrideCulture(); + var args = Environment.GetCommandLineArgs(); + if (args.Length >= 2) { + Config = new(Path.GetFullPath(args[1])); + } + ContextTimer.Tick += (object? sender, EventArgs evt) => { if (CurrentLastWrite > LastChanged) { LastChanged = CurrentLastWrite; diff --git a/Elwig/Helpers/AppDbContext.cs b/Elwig/Helpers/AppDbContext.cs index 449e418..217ab76 100644 --- a/Elwig/Helpers/AppDbContext.cs +++ b/Elwig/Helpers/AppDbContext.cs @@ -98,15 +98,15 @@ namespace Elwig.Helpers { SavedChanges += OnSavedChanges; } - public static SqliteConnection Connect() { - var cnx = new SqliteConnection(ConnectionString); + public static SqliteConnection Connect(string? connectionString = null) { + var cnx = new SqliteConnection(connectionString ?? ConnectionString); cnx.CreateFunction("REGEXP", (pattern, value) => value == null ? null : Regex.Match(value, pattern).Success, true); cnx.Open(); return cnx; } - public static async Task ConnectAsync() { - var cnx = new SqliteConnection(ConnectionString); + public static async Task ConnectAsync(string? connectionString = null) { + var cnx = new SqliteConnection(connectionString ?? ConnectionString); cnx.CreateFunction("REGEXP", (pattern, value) => value == null ? null : Regex.Match(value, pattern).Success, true); await cnx.OpenAsync(); return cnx; diff --git a/Elwig/Helpers/AppDbUpdater.cs b/Elwig/Helpers/AppDbUpdater.cs index 184348c..4a57078 100644 --- a/Elwig/Helpers/AppDbUpdater.cs +++ b/Elwig/Helpers/AppDbUpdater.cs @@ -17,7 +17,7 @@ namespace Elwig.Helpers { using var cnx = AppDbContext.Connect(); var applId = (long?)await AppDbContext.ExecuteScalar(cnx, "PRAGMA application_id") ?? 0; - if (applId != 0x454C5747) throw new Exception("Invalid application_id of database"); + if (applId != 0x454C5747) throw new Exception($"Invalid application_id in database (0x{applId:X08})"); var schemaVers = (long?)await AppDbContext.ExecuteScalar(cnx, "PRAGMA schema_version") ?? 0; VersionOffset = (int)(schemaVers % 100); diff --git a/Elwig/Helpers/Config.cs b/Elwig/Helpers/Config.cs index ba732bd..aeae8f6 100644 --- a/Elwig/Helpers/Config.cs +++ b/Elwig/Helpers/Config.cs @@ -69,9 +69,9 @@ namespace Elwig.Helpers { public void Read() { var config = new ConfigurationBuilder().AddIniFile(FileName).Build(); - DatabaseFile = Path.Combine(App.DataPath, config["database:file"] ?? "database.sqlite3"); + DatabaseFile = Path.Combine(Path.GetDirectoryName(FileName) ?? App.DataPath, config["database:file"] ?? "database.sqlite3"); var log = config["database:log"]; - DatabaseLog = log != null ? Path.Combine(App.DataPath, log) : null; + DatabaseLog = log != null ? Path.Combine(Path.GetDirectoryName(FileName) ?? App.DataPath, log) : null; Branch = config["general:branch"]; Debug = TrueValues.Contains(config["general:debug"]?.ToLower()); UpdateUrl = config["update:url"]; diff --git a/Tests/E2ETests/AppSession.cs b/Tests/E2ETests/AppSession.cs index 5bbc622..0940c25 100644 --- a/Tests/E2ETests/AppSession.cs +++ b/Tests/E2ETests/AppSession.cs @@ -9,10 +9,12 @@ namespace Tests.E2ETests { public readonly WindowsDriver App; public readonly WindowsDriver Desktop; - public AppSession(string appPath, string winAppDriverUrl) { + public AppSession(string appPath, string? appArgs, string winAppDriverUrl) { WinAppDriverUrl = winAppDriverUrl; var appiumOptions = new AppiumOptions(); appiumOptions.AddAdditionalCapability("app", appPath); + if (appArgs != null) + appiumOptions.AddAdditionalCapability("appArguments", appArgs); appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC"); appiumOptions.AddAdditionalCapability("ms:waitForAppLaunch", WaitForAppLaunch); App = new WindowsDriver(new Uri(WinAppDriverUrl), appiumOptions); diff --git a/Tests/E2ETests/MainWindowTest.cs b/Tests/E2ETests/MainWindowTest.cs index ea0b23b..cfa5aa0 100644 --- a/Tests/E2ETests/MainWindowTest.cs +++ b/Tests/E2ETests/MainWindowTest.cs @@ -6,7 +6,7 @@ [OneTimeSetUp] public void Setup() { - Session = new(Utils.ApplicationPath, WinAppDriver.WinAppDriverUrl); + Session = new(Utils.ApplicationPath, Utils.ConfigPath, WinAppDriver.WinAppDriverUrl); } [OneTimeTearDown] diff --git a/Tests/E2ETests/MemberAdminWindowTest.cs b/Tests/E2ETests/MemberAdminWindowTest.cs index ec4494c..b225939 100644 --- a/Tests/E2ETests/MemberAdminWindowTest.cs +++ b/Tests/E2ETests/MemberAdminWindowTest.cs @@ -9,7 +9,7 @@ namespace Tests.E2ETests { [OneTimeSetUp] public void WindowSetup() { - Session = new(Utils.ApplicationPath, WinAppDriver.WinAppDriverUrl); + Session = new(Utils.ApplicationPath, Utils.ConfigPath, WinAppDriver.WinAppDriverUrl); Session.App.FindElementByName("Mitglieder").Click(); Thread.Sleep(1000); Window = Session.CreateWindowDriver("MemberAdminWindow"); @@ -33,10 +33,10 @@ namespace Tests.E2ETests { Window!.FindById("NewMemberButton").Click(); Window.FindById("MgNrInput").Clear(); - Window.FindById("MgNrInput").SendKeys("10003"); + Window.FindById("MgNrInput").SendKeys("9999"); - Window.FindById("GivenNameInput").SendKeys("Max"); - Window.FindById("FamilyNameInput").SendKeys("Mustermann"); + Window.FindById("GivenNameInput").SendKeys("Norbert"); + Window.FindById("FamilyNameInput").SendKeys("Neuling"); Window.FindById("PrefixInput").SendKeys("Ing."); Window.FindById("SuffixInput").SendKeys("jun."); Window.FindById("BirthdayInput").SendKeys("1987"); @@ -45,8 +45,8 @@ namespace Tests.E2ETests { Window.FindById("PlzInput").SendKeys("2120"); Window.SelectComboBoxItemByCount("OrtInput", 1); - Window.FindById("EmailAddress1Input").SendKeys("max.mustermann@aon.at"); - Window.FindById("EmailAddress2Input").SendKeys("erika.mustermann@aon.at"); + Window.FindById("EmailAddress1Input").SendKeys("norbert.neuling@aon.at"); + Window.FindById("EmailAddress2Input").SendKeys("nathalie.neuling@aon.at"); Window.SelectComboBoxItemByCount("PhoneNr1TypeInput", 1); Window.FindById("PhoneNr1Input").SendKeys("012345678"); @@ -57,13 +57,13 @@ namespace Tests.E2ETests { Window.FindById("IbanInput").SendKeys("AT611904300234573201"); Window.FindById("BicInput").SendKeys("RLNWATWWWDF"); - Window.FindById("UstIdNrInput").SendKeys("ATU66192906"); //TODO: Testdaten? - Window.FindById("LfbisNrInput").SendKeys("1251074"); //TODO: Testdaten? + Window.FindById("UstIdNrInput").SendKeys("ATU66192906"); // TODO: Testdaten? + Window.FindById("LfbisNrInput").SendKeys("1251074"); // TODO: Testdaten? Window.FindById("BuchführendInput").Click(); Window.FindById("OrganicInput").Click(); - Window.FindById("BillingNameInput").SendKeys("Mustermann KG"); + Window.FindById("BillingNameInput").SendKeys("Neuling KG"); Window.FindById("BillingAddressInput").SendKeys("Betriebsstraße 1"); Window.FindById("BillingPlzInput").SendKeys("2120"); Window.SelectComboBoxItemByCount("BillingOrtInput", 2); @@ -80,20 +80,20 @@ namespace Tests.E2ETests { Window.FindById("SaveButton").Click(); - Window.FindById("SearchInput").SendKeys("10003 Max Mustermann"); + Window.FindById("SearchInput").SendKeys("9999"); Thread.Sleep(500); var memberListRow = Window.FindById("MemberList").FindElementByClassName("DataGridRow"); Assert.Multiple(() => { Assert.That(memberListRow, Is.Not.Null); - Assert.That(memberListRow.FindElementByName("10003 "), Is.Not.Null); - Assert.That(memberListRow.FindElementByName("Max"), Is.Not.Null); - Assert.That(memberListRow.FindElementByName("Mustermann"), Is.Not.Null); + Assert.That(memberListRow.FindElementByName("9999 "), Is.Not.Null); + Assert.That(memberListRow.FindElementByName("Norbert"), Is.Not.Null); + Assert.That(memberListRow.FindElementByName("Neuling"), Is.Not.Null); }); } [Test] public void Test_2_EditMember() { - Window!.FindById("SearchInput").SendKeys("10003 Max Mustermann"); + Window!.FindById("SearchInput").SendKeys("9999"); Thread.Sleep(500); var memberList = Window.FindById("MemberList"); Assert.That(memberList, Is.Not.Null); @@ -118,7 +118,7 @@ namespace Tests.E2ETests { [Test] public void Test_3_DeleteMember() { - Window!.FindById("SearchInput").SendKeys("10003 Max Mustermann"); + Window!.FindById("SearchInput").SendKeys("9999"); Thread.Sleep(500); var memberList = Window.FindById("MemberList"); Assert.That(memberList, Is.Not.Null); @@ -129,14 +129,14 @@ namespace Tests.E2ETests { var memberListRow = memberListRows.First(); Assert.Multiple(() => { Assert.That(memberListRow, Is.Not.Null); - Assert.That(memberListRow.FindElementByName("10003 "), Is.Not.Null); - Assert.That(memberListRow.FindElementByName("Max"), Is.Not.Null); - Assert.That(memberListRow.FindElementByName("Mustermann"), Is.Not.Null); + Assert.That(memberListRow.FindElementByName("9999 "), Is.Not.Null); + Assert.That(memberListRow.FindElementByName("Norbert"), Is.Not.Null); + Assert.That(memberListRow.FindElementByName("Neuling"), Is.Not.Null); }); Window.FindById("DeleteMemberButton").Click(); var dialog = Session.CreateWindowDriver("DeleteMemberDialog"); - dialog.FindById("NameInput").SendKeys("10003 Ing. Max Mustermann jun."); + dialog.FindById("NameInput").SendKeys("9999 Ing. Norbert Neuling jun."); dialog.FindById("ConfirmButton").Click(); memberListRows = memberList.FindElementsByClassName("DataGridRow"); diff --git a/Tests/E2ETests/Setup.cs b/Tests/E2ETests/Setup.cs index 5504789..0adf3a6 100644 --- a/Tests/E2ETests/Setup.cs +++ b/Tests/E2ETests/Setup.cs @@ -1,4 +1,7 @@ -namespace Tests.E2ETests { +using Elwig.Helpers; +using System.Reflection; + +namespace Tests.E2ETests { [SetUpFixture] public static class Setup { @@ -9,9 +12,25 @@ Driver = new(); } + [OneTimeSetUp] + public static async Task SetupDatabase() { + if (File.Exists(Utils.TestDatabasePath)) File.Delete(Utils.TestDatabasePath); + using var cnx = await AppDbContext.ConnectAsync($"Data Source=\"{Utils.TestDatabasePath}\"; Mode=ReadWriteCreate; Foreign Keys=True; Cache=Default"); + await AppDbContext.ExecuteEmbeddedScript(cnx, Assembly.GetExecutingAssembly(), "Tests.Resources.Sql.Create.sql"); + await AppDbContext.ExecuteEmbeddedScript(cnx, Assembly.GetExecutingAssembly(), "Tests.Resources.Sql.Insert.sql"); + } + [OneTimeTearDown] public static void TeardownWinAppDriver() { Driver?.Dispose(); } + + [OneTimeTearDown] + public static void TeardownDatabase() { + try { + // FIXME not working - other process using file + File.Delete(Utils.TestDatabasePath); + } catch { } + } } } diff --git a/Tests/E2ETests/Utils.cs b/Tests/E2ETests/Utils.cs index 5ae127b..184eff0 100644 --- a/Tests/E2ETests/Utils.cs +++ b/Tests/E2ETests/Utils.cs @@ -4,7 +4,9 @@ using OpenQA.Selenium.Appium.Windows; namespace Tests.E2ETests { public static class Utils { - public const string ApplicationPath = @"..\..\..\..\Elwig\bin\Debug\net8.0-windows\Elwig.exe"; + public static readonly string ApplicationPath = Path.GetFullPath(@"..\..\..\..\Elwig\bin\Debug\net8.0-windows\Elwig.exe"); + public static readonly string ConfigPath = Path.GetFullPath(@"..\..\..\..\Tests\config.test.ini"); + public static readonly string TestDatabasePath = Path.GetFullPath(@"..\..\..\..\Tests\ElwigTestDB.sqlite3"); public static WindowsElement FindById(this WindowsDriver session, string accessibilityId) { return session.FindElementByAccessibilityId(accessibilityId); diff --git a/Tests/config.test.ini b/Tests/config.test.ini new file mode 100644 index 0000000..eb7ea1a --- /dev/null +++ b/Tests/config.test.ini @@ -0,0 +1,7 @@ + +[general] +debug = true + +[database] +file = ElwigTestDB.sqlite3 +