Skip to content

Commit

Permalink
UITest/BookLib: add LoadCorruptDatabaseTest (#60)
Browse files Browse the repository at this point in the history
* UITest/BookLib: add LoadCorruptDatabaseTest

* UITest/BookLib: improve LoadCorruptDatabaseTest
  • Loading branch information
jbe2277 authored Nov 30, 2024
1 parent 201c275 commit 20c5778
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
29 changes: 29 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,33 @@ public void AboutTest() => Run(() =>
dataMenu.Click();
dataMenu.ExitMenuItem.Click();
});

[Fact]
public void LoadCorruptDatabaseTest() => Run(() =>
{
if (File.Exists(AppInfo.DatabaseFile)) File.Delete(AppInfo.DatabaseFile);
Directory.CreateDirectory(Path.GetDirectoryName(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 Books from the database.", messageBox.Message);
messageBox.Buttons[0].Click();

messageBox = window.FirstModalWindow().As<MessageBox>();
Assert.Equal("Could not load the Persons 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 20c5778

Please sign in to comment.