Skip to content

Commit

Permalink
UITest/BookLib: add LoadCorruptDatabaseTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 30, 2024
1 parent 201c275 commit 13d2eff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
28 changes: 28 additions & 0 deletions src/Samples.UITest/BookLibrary.Test/Tests/GeneralTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,32 @@ public void AboutTest() => Run(() =>
dataMenu.Click();
dataMenu.ExitMenuItem.Click();
});

[Fact]
public void LoadCorruptDatabaseTest() => Run(() =>

Check failure on line 34 in src/Samples.UITest/BookLibrary.Test/Tests/GeneralTest.cs

View workflow job for this annotation

GitHub Actions / 🛠️ Build and test

UITest.BookLibrary.Tests.GeneralTest.LoadCorruptDatabaseTest

System.IO.DirectoryNotFoundException : Could not find a part of the path 'C:\Users\runneradmin\AppData\Roaming\jbe2277\Waf Book Library\Resources\BookLibrary.db'.
{
if (File.Exists(AppInfo.DatabaseFile)) File.Delete(AppInfo.DatabaseFile);
File.AppendAllText(AppInfo.DatabaseFile, "42");

Launch(resetDatabase: false);
var window = GetShellWindow();
var bookListView = window.TabControl.BookLibraryTabItem.BookListView;
Assert.Equal(0, bookListView.BookDataGrid.RowCount);

var messageBox = window.FirstModalWindow().As<MessageBox>();
Assert.Equal("Could not load the Persons from the database.", messageBox.Message);
messageBox.Buttons[0].Click();

messageBox = window.FirstModalWindow().As<MessageBox>();
Assert.Equal("Could not load the Books from the database.", messageBox.Message);
messageBox.Buttons[0].Click();

Assert.Equal(0, bookListView.BookDataGrid.RowCount);

window.TabControl.AddressBookTabItem.Select();
var personListView = window.TabControl.AddressBookTabItem.PersonListView;
Assert.Equal(0, personListView.PersonDataGrid.RowCount);

window.Close();
});
}
30 changes: 19 additions & 11 deletions src/Samples.UITest/BookLibrary.Test/UITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@

namespace UITest.BookLibrary;

public abstract class UITest(ITestOutputHelper log) : UITestBase(log, "BookLibrary.exe",
public record AppInfo(string CompanyName, string ProductName, string SettingsFile, string DatabaseFile);

public abstract class UITest : UITestBase
{
protected UITest(ITestOutputHelper log) : base(log, "BookLibrary.exe",
Environment.GetEnvironmentVariable("UITestExePath") ?? "out/BookLibrary/Release/net8.0-windows/",
Environment.GetEnvironmentVariable("UITestOutputPath") ?? "out/Samples.UITest/BookLibrary/")
{
{
var companyName = FileVersionInfo.GetVersionInfo(Executable).CompanyName ?? throw new InvalidOperationException("Could not read the CompanyName from the exe.");
var productName = FileVersionInfo.GetVersionInfo(Executable).ProductName ?? throw new InvalidOperationException("Could not read the ProductName from the exe.");
AppInfo = new(companyName, productName,
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), productName, "Settings", "Settings.xml"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), companyName, productName, "Resources", "BookLibrary.db"));
}

protected AppInfo AppInfo { get; }

public Application Launch(LaunchArguments? arguments = null, bool resetSettings = true, bool resetDatabase = true)
{
Log.WriteLine("");
if (resetSettings)
{
var productName = FileVersionInfo.GetVersionInfo(Executable).ProductName ?? throw new InvalidOperationException("Could not read the ProductName from the exe.");
var settingsFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), productName, "Settings", "Settings.xml");
if (File.Exists(settingsFile)) File.Delete(settingsFile);
Log.WriteLine($"Delete settings: {settingsFile}");
if (File.Exists(AppInfo.SettingsFile)) File.Delete(AppInfo.SettingsFile);
Log.WriteLine($"Delete settings: {AppInfo.SettingsFile}");
}
if (resetDatabase)
{
var companyName = FileVersionInfo.GetVersionInfo(Executable).CompanyName ?? throw new InvalidOperationException("Could not read the CompanyName from the exe.");
var productName = FileVersionInfo.GetVersionInfo(Executable).ProductName ?? throw new InvalidOperationException("Could not read the ProductName from the exe.");
var dbFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), companyName, productName, "Resources", "BookLibrary.db");
if (File.Exists(dbFile)) File.Delete(dbFile);
Log.WriteLine($"Delete database: {dbFile}");
if (File.Exists(AppInfo.DatabaseFile)) File.Delete(AppInfo.DatabaseFile);
Log.WriteLine($"Delete database: {AppInfo.DatabaseFile}");
}
var args = (arguments ?? new LaunchArguments()).ToArguments();
Log.WriteLine($"Launch: {args}");
Expand Down

0 comments on commit 13d2eff

Please sign in to comment.