Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using TransactionProcessor.Mobile.BusinessLogic.Database;
using Shouldly;

namespace TransactionProcessor.Mobile.BusinessLogic.Tests.DatabaseTests;

public class DatabaseContextTests
{
[Fact]
public async Task DatabaseContext_SaveApplicationOption_OptionCanBeReadBack()
{
Func<LogLevel> logLevelFunc = () => LogLevel.Debug;
IDatabaseContext databaseContext = new DatabaseContext(":memory:", logLevelFunc);

await databaseContext.InitialiseDatabase();
await databaseContext.SaveApplicationOption("DarkThemeEnabled", "True");

String? optionValue = await databaseContext.GetApplicationOption("DarkThemeEnabled");

optionValue.ShouldBe("True");
}

[Fact]
public async Task DatabaseContext_SaveApplicationOption_ExistingValueIsUpdated()
{
Func<LogLevel> logLevelFunc = () => LogLevel.Debug;
IDatabaseContext databaseContext = new DatabaseContext(":memory:", logLevelFunc);

await databaseContext.InitialiseDatabase();
await databaseContext.SaveApplicationOption("DarkThemeEnabled", "True");
await databaseContext.SaveApplicationOption("DarkThemeEnabled", "False");

String? optionValue = await databaseContext.GetApplicationOption("DarkThemeEnabled");

optionValue.ShouldBe("False");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class MyAccountPageViewModelTests
private readonly Mock<IDialogService> DialogService;

private readonly Mock<IMediator> Mediator;
private readonly Mock<IApplicationThemeService> ApplicationThemeService;

private readonly MyAccountPageViewModel ViewModel;

Expand All @@ -37,24 +38,35 @@ public MyAccountPageViewModelTests() {
this.DialogService = new Mock<IDialogService>();
this.DeviceService = new Mock<IDeviceService>();
this.Mediator = new Mock<IMediator>();
this.ApplicationThemeService = new Mock<IApplicationThemeService>();
this.ViewModel = new MyAccountPageViewModel(this.NavigationService.Object, this.ApplicationCache.Object,
this.DialogService.Object, this.DeviceService.Object,
this.Mediator.Object,
this.NavigationParameterService.Object);
this.DialogService.Object, this.DeviceService.Object,
this.ApplicationThemeService.Object,
this.Mediator.Object,
this.NavigationParameterService.Object);
}

[Fact]
public async Task MyAccountPageViewModel_Initialise_IsInitialised() {

this.ApplicationThemeService.Setup(s => s.GetDarkThemeEnabled()).ReturnsAsync(true);
this.Mediator.Setup(m => m.Send(It.IsAny<GetMerchantDetailsRequest>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success( TestData.MerchantDetailsModel));

await this.ViewModel.Initialise(CancellationToken.None);

this.ViewModel.MerchantName.ShouldBe(TestData.MerchantDetailsModel.MerchantName);
this.ViewModel.LastLogin.ShouldBe(DateTime.Now, TimeSpan.FromSeconds(30));
this.ViewModel.IsDarkThemeEnabled.ShouldBeTrue();
this.ApplicationCache.Verify(a => a.SetMerchantDetails(It.IsAny<MerchantDetailsModel>(), It.IsAny<MemoryCacheEntryOptions>()), Times.Once);
}

[Fact]
public async Task MyAccountPageViewModel_SetDarkTheme_ThemeStoredAndPropertyUpdated() {
await this.ViewModel.SetDarkTheme(true);

this.ViewModel.IsDarkThemeEnabled.ShouldBeTrue();
this.ApplicationThemeService.Verify(s => s.SetDarkTheme(true), Times.Once);
}

[Fact]
public void MyAccountPageViewModel_OptionSelectedCommand_AccountInfo_Execute_IsExecuted() {
this.ViewModel.OptionSelectedCommand.Execute(this.CreateItemSelected(MyAccountPageViewModel.AccountOptions.AccountInfo));
Expand Down Expand Up @@ -117,4 +129,4 @@ private ItemSelected<ListViewItem> CreateItemSelected(MyAccountPageViewModel.Acc
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,18 @@ public class ReportsSalesAnalysisPageViewModelTests{
private readonly Mock<IApplicationCache> ApplicationCache;
private readonly Mock<IDialogService> DialogService;
private readonly Mock<IDeviceService> DeviceService;
private readonly Mock<IMediator> Mediator;

public ReportsSalesAnalysisPageViewModelTests(){
this.NavigationService = new Mock<INavigationService>();
this.NavigationParameterService = new Mock<INavigationParameterService>();
this.ApplicationCache = new Mock<IApplicationCache>();
this.DialogService = new Mock<IDialogService>();
this.DeviceService = new Mock<IDeviceService>();
this.Mediator = new Mock<IMediator>();

this.ViewModel = new ReportsSalesAnalysisPageViewModel(this.NavigationService.Object,
this.ApplicationCache.Object,
this.DialogService.Object,
this.DeviceService.Object,
this.Mediator.Object,
this.NavigationParameterService.Object);
}

Expand All @@ -124,6 +121,15 @@ public async Task ReportsSalesAnalysisPageViewModel_ComparisonDatePickerSelected
this.ViewModel.SalesAnalysisList.Count.ShouldBe(2);
}

[Fact]
public void ReportsSalesAnalysisPageViewModel_ComparisonDatePickerSelectedIndexChangedCommand_WithoutSelectedItem_ReturnsEmptyList()
{
this.ViewModel.ComparisonDatePickerSelectedIndexChangedCommand.Execute(null);

this.ViewModel.SalesAnalysisList.ShouldNotBeNull();
this.ViewModel.SalesAnalysisList.ShouldBeEmpty();
}

[Fact]
public async Task ReportsSalesAnalysisPageViewModel_BackButtonCommand_HomePageIsShown()
{
Expand All @@ -141,7 +147,6 @@ public class ReportsBalanceAnalysisPageViewModelTests
private Mock<IApplicationCache> ApplicationCache;
private Mock<IDialogService> DialogService;
private readonly Mock<IDeviceService> DeviceService;
private Mock<IMediator> Mediator;

public ReportsBalanceAnalysisPageViewModelTests()
{
Expand All @@ -150,13 +155,11 @@ public ReportsBalanceAnalysisPageViewModelTests()
this.ApplicationCache = new Mock<IApplicationCache>();
this.DialogService = new Mock<IDialogService>();
this.DeviceService = new Mock<IDeviceService>();
this.Mediator = new Mock<IMediator>();

this.ViewModel = new ReportsBalanceAnalysisPageViewModel(this.NavigationService.Object,
this.ApplicationCache.Object,
this.DialogService.Object,
this.DeviceService.Object,
this.Mediator.Object,
this.NavigationParameterService.Object);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Diagnostics.CodeAnalysis;
using SQLite;

namespace TransactionProcessor.Mobile.BusinessLogic.Database
{
[ExcludeFromCodeCoverage]
public class ApplicationOption
{
[PrimaryKey]
public String OptionName { get; set; } = String.Empty;

public String OptionValue { get; set; } = String.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public async Task<Int64> CreateTransaction(TransactionRecord transactionRecord)
return SQLite3.LastInsertRowid(this.Connection.Handle);
}

public Task<String?> GetApplicationOption(String optionName) {
ApplicationOption option = this.Connection.Find<ApplicationOption>(optionName);

return Task.FromResult(option?.OptionValue);
}

public async Task<List<LogMessage>> GetLogMessages(Int32 batchSize, Boolean isTrainingMode) {
if (this.Connection == null)
return new List<LogMessage>();
Expand All @@ -55,6 +61,7 @@ public async Task<List<TransactionRecord>> GetTransactions(Boolean isTrainingMod
}

public async Task InitialiseDatabase() {
this.Connection.CreateTable<ApplicationOption>();
this.Connection.CreateTable<TransactionRecord>();
this.Connection.CreateTable<LogMessage>();
}
Expand Down Expand Up @@ -83,10 +90,20 @@ public async Task RemoveUploadedMessages(List<LogMessage> logMessagesToRemove) {
}
}

public Task SaveApplicationOption(String optionName,
String optionValue) {
this.Connection.InsertOrReplace(new ApplicationOption {
OptionName = optionName,
OptionValue = optionValue
});

return Task.CompletedTask;
}

public async Task UpdateTransaction(TransactionRecord transactionRecord) {
this.Connection.Update(transactionRecord);
}

#endregion
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public interface IDatabaseContext
{
Task InitialiseDatabase();

Task<String?> GetApplicationOption(String optionName);

Task SaveApplicationOption(String optionName, String optionValue);

Task<Int64> CreateTransaction(TransactionRecord transactionRecord);

Task UpdateTransaction(TransactionRecord transactionRecord);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TransactionProcessor.Mobile.BusinessLogic.UIServices;

public interface IApplicationThemeService
{
Task ApplyConfiguredTheme();

Task<Boolean> GetDarkThemeEnabled();

Task SetDarkTheme(Boolean isEnabled);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public partial class MyAccountPageViewModel : ExtendedBaseViewModel
private DateTime lastLogin;

private readonly IMediator Mediator;
private readonly IApplicationThemeService ApplicationThemeService;

private String merchantName;
private Boolean isDarkThemeEnabled;

#endregion

Expand All @@ -32,8 +34,10 @@ public MyAccountPageViewModel(INavigationService navigationService,
IApplicationCache applicationCache,
IDialogService dialogService,
IDeviceService deviceService,
IApplicationThemeService applicationThemeService,
IMediator mediator,
INavigationParameterService navigationParameterService) : base(applicationCache, dialogService, navigationService, deviceService,navigationParameterService) {
this.ApplicationThemeService = applicationThemeService;
this.Mediator = mediator;
this.Title = "My Account";
}
Expand All @@ -47,6 +51,11 @@ public DateTime LastLogin {
set => this.SetProperty(ref this.lastLogin, value);
}

public Boolean IsDarkThemeEnabled {
get => this.isDarkThemeEnabled;
set => this.SetProperty(ref this.isDarkThemeEnabled, value);
}

public String MerchantName {
get => this.merchantName;
set => this.SetProperty(ref this.merchantName, value);
Expand Down Expand Up @@ -93,6 +102,13 @@ public async Task Initialise(CancellationToken cancellationToken) {
this.ApplicationCache.SetMerchantDetails(merchantDetailsResult.Data, cacheEntryOptions);

this.LastLogin = DateTime.Now; // TODO: might cache this in the application
this.IsDarkThemeEnabled = await this.ApplicationThemeService.GetDarkThemeEnabled();
}

public async Task SetDarkTheme(Boolean isEnabled) {
this.IsDarkThemeEnabled = isEnabled;

await this.ApplicationThemeService.SetDarkTheme(isEnabled);
}

private async Task LogoutCommandExecute() {
Expand Down Expand Up @@ -136,4 +152,4 @@ public enum AccountOptions

#endregion
}
}
}
Loading
Loading