-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from intcooper/feature/mvvm
Feature/mvvm
- Loading branch information
Showing
29 changed files
with
642 additions
and
169 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0-windows10.0.22000.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" /> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="NUnit" Version="3.13.3" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MMExNotifier\MMExNotifier.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
|
||
using MMExNotifier.Database; | ||
using MMExNotifier.DataModel; | ||
using MMExNotifier.Helpers; | ||
using MMExNotifier.ViewModels; | ||
using Moq; | ||
|
||
namespace MMExNotifier.Tests | ||
{ | ||
public class MainViewModelTests | ||
{ | ||
private Mock<IAppConfiguration> mockAppConfiguration; | ||
private Mock<IDatabaseService> mockDatabaseService; | ||
private Mock<INotificationService> mockNotificationService; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
mockAppConfiguration = new Mock<IAppConfiguration>(); | ||
mockAppConfiguration.Setup(x => x.MMExDatabasePath).Returns("C:\\mydatabase.mmb"); | ||
mockAppConfiguration.Setup(x => x.RunAtLogon).Returns(false); | ||
mockAppConfiguration.Setup(x => x.DaysAhead).Returns(7); | ||
|
||
mockDatabaseService = new Mock<IDatabaseService>(); | ||
|
||
mockNotificationService = new Mock<INotificationService>(); | ||
} | ||
|
||
[Test] | ||
public void WhenDatabasePathIsNotConfigured_ShouldNotAttemptOpenDatabase() | ||
{ | ||
mockAppConfiguration.Setup(x => x.MMExDatabasePath).Returns(string.Empty); | ||
mockDatabaseService.Setup(x => x.ExpiringBills).Returns(() => null); | ||
|
||
var mainViewModel = new MainViewModel(mockAppConfiguration.Object, mockNotificationService.Object, mockDatabaseService.Object); | ||
mainViewModel.Activate(); | ||
|
||
Assert.That(mockDatabaseService.Invocations, Is.Empty); | ||
} | ||
|
||
[Test] | ||
public void WhenNoExpiringBills_ShouldRaiseCloseEvent() | ||
{ | ||
mockDatabaseService.Setup(x => x.ExpiringBills).Returns(() => null); | ||
|
||
var mainViewModel = new MainViewModel(mockAppConfiguration.Object, mockNotificationService.Object, mockDatabaseService.Object); | ||
var closeInvoked = false; | ||
mainViewModel.OnClose += (s, e) => { closeInvoked = true; }; | ||
mainViewModel.Activate(); | ||
|
||
Assert.That(closeInvoked, Is.True); | ||
} | ||
|
||
[Test] | ||
public void WhenAtLeastOneExpiringBill_ShouldShowToastNotification() | ||
{ | ||
mockDatabaseService.Setup(x => x.ExpiringBills).Returns( | ||
new List<ExpiringBill> | ||
{ | ||
new() | ||
{ | ||
BillId=1, | ||
CategoryName="testCategory", | ||
PayeeName="TestPayee", | ||
NextOccurrenceDate=new DateTime(2024,6,1) | ||
} | ||
}); | ||
|
||
mockNotificationService.Setup( | ||
x => x.ShowToastNotification("viewTransactions", It.IsAny<int>(), "MMExNotifier", "One ore more recurring transaction are about to expire.", It.IsAny<Action>()) | ||
); | ||
|
||
var mainViewModel = new MainViewModel(mockAppConfiguration.Object, mockNotificationService.Object, mockDatabaseService.Object); | ||
mainViewModel.Activate(); | ||
|
||
mockNotificationService.Verify(); | ||
} | ||
|
||
[Test] | ||
public void WhenNotificationActivated_ShouldRaiseOpen() | ||
{ | ||
mockDatabaseService.Setup(x => x.ExpiringBills).Returns( | ||
new List<ExpiringBill> | ||
{ | ||
new() | ||
{ | ||
BillId=1, | ||
CategoryName="testCategory", | ||
PayeeName="TestPayee", | ||
NextOccurrenceDate=new DateTime(2024,6,1) | ||
} | ||
}); | ||
|
||
var mockToastNotification = new Mock<IToastNotification>(); | ||
mockToastNotification.Setup( | ||
x => x.Show(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>()) | ||
); | ||
|
||
var mainViewModel = new MainViewModel(mockAppConfiguration.Object, new NotificationService(mockToastNotification.Object), mockDatabaseService.Object); | ||
var openInvoked = false; | ||
mainViewModel.OnOpen += (s, e) => { openInvoked = true; }; | ||
mainViewModel.Activate(); | ||
|
||
mockToastNotification.Raise(x => x.OnActivated += null, new EventArgs()); | ||
|
||
Assert.That(openInvoked, Is.True); | ||
} | ||
|
||
[Test] | ||
public void OnDatabaseError_ShouldShowErrorMessage() | ||
{ | ||
mockDatabaseService.Setup(x => x.ExpiringBills).Throws<InvalidOperationException>(); | ||
|
||
mockNotificationService.Setup( | ||
x => x.ShowErrorNotification(It.IsAny<string>()) | ||
); | ||
|
||
var mainViewModel = new MainViewModel(mockAppConfiguration.Object, mockNotificationService.Object, mockDatabaseService.Object); | ||
mainViewModel.Activate(); | ||
|
||
mockNotificationService.Verify(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
MMExNotifier/Entities/Account.cs → MMExNotifier/DataModel/Account.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Microsoft.Win32.TaskScheduler; | ||
using System; | ||
using System.Linq; | ||
using System.Security.Principal; | ||
|
||
namespace MMExNotifier.DataModel | ||
{ | ||
internal class AppConfiguration : IAppConfiguration | ||
{ | ||
public string? MMExDatabasePath { get; set; } | ||
public int DaysAhead { get; set; } | ||
public bool RunAtLogon { get; set; } | ||
|
||
public AppConfiguration() | ||
{ | ||
MMExDatabasePath = Properties.Settings.Default.MMExDatabasePath; | ||
DaysAhead = Properties.Settings.Default.DaysAhead; | ||
|
||
using TaskService taskService = new(); | ||
RunAtLogon = taskService.RootFolder.Tasks.Any(t => t.Name == "MMExNotifier"); | ||
} | ||
|
||
public void Save() | ||
{ | ||
Properties.Settings.Default.DaysAhead = DaysAhead; | ||
Properties.Settings.Default.MMExDatabasePath = MMExDatabasePath; | ||
|
||
if (RunAtLogon) | ||
{ | ||
EnableSchedulerTask(); | ||
} | ||
else | ||
{ | ||
DisableSchedulerTask(); | ||
} | ||
|
||
Properties.Settings.Default.Save(); | ||
} | ||
|
||
private static void EnableSchedulerTask() | ||
{ | ||
using TaskService taskService = new(); | ||
|
||
if (taskService.RootFolder.Tasks.Any(t => t.Name == "MMExNotifier")) | ||
return; | ||
|
||
TaskDefinition taskDefinition = taskService.NewTask(); | ||
|
||
// Set the task settings | ||
taskDefinition.RegistrationInfo.Description = "MMExNotifier"; | ||
var userId = WindowsIdentity.GetCurrent().Name; | ||
|
||
// Set the trigger to run on logon | ||
LogonTrigger logonTrigger = new() { UserId = userId }; | ||
taskDefinition.Triggers.Add(logonTrigger); | ||
|
||
// Set the action to run the executable that creates the task | ||
string executablePath = Environment.ProcessPath ?? ""; | ||
taskDefinition.Actions.Add(new ExecAction(executablePath)); | ||
|
||
// Register the task in the Windows Task Scheduler | ||
taskService.RootFolder.RegisterTaskDefinition("MMExNotifier", taskDefinition); | ||
} | ||
|
||
private static void DisableSchedulerTask() | ||
{ | ||
using TaskService taskService = new(); | ||
taskService.RootFolder.DeleteTask("MMExNotifier", false); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
MMExNotifier/Entities/BillDeposit.cs → MMExNotifier/DataModel/BillDeposit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
MMExNotifier/Entities/Category.cs → MMExNotifier/DataModel/Category.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
MMExNotifier/Entities/ExpiringBill.cs → MMExNotifier/DataModel/ExpiringBill.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
using System; | ||
|
||
namespace MMExNotifier.Entities | ||
namespace MMExNotifier.DataModel | ||
{ | ||
public class ExpiringBill | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace MMExNotifier.DataModel | ||
{ | ||
internal interface IAppConfiguration | ||
{ | ||
int DaysAhead { get; set; } | ||
string? MMExDatabasePath { get; set; } | ||
bool RunAtLogon { get; set; } | ||
|
||
void Save(); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
MMExNotifier/Entities/Payee.cs → MMExNotifier/DataModel/Payee.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.