diff --git a/BudgetAnalyser.Engine.UnitTest/AssertExtension.cs b/BudgetAnalyser.Engine.UnitTest/AssertExtension.cs new file mode 100644 index 00000000..93f55d14 --- /dev/null +++ b/BudgetAnalyser.Engine.UnitTest/AssertExtension.cs @@ -0,0 +1,20 @@ +namespace BudgetAnalyser.Engine.UnitTest; + +public static class AssertExtensions +{ + public static void AreEqualWithTolerance(decimal expected, decimal actual, decimal tolerance = 0.01M, string message = "") + { + if (Math.Abs(expected - actual) >= tolerance) + { + throw new AssertFailedException($"Expected: {expected}, Actual: {actual}. {message}"); + } + } + + public static void AreEqualWithTolerance(double expected, double actual, double tolerance = 0.01, string message = "") + { + if (Math.Abs(expected - actual) >= tolerance) + { + throw new AssertFailedException($"Expected: {expected}, Actual: {actual}. {message}"); + } + } +} diff --git a/BudgetAnalyser.Engine.UnitTest/Reports/OverallPerformanceBudgetAnalyserTest.cs b/BudgetAnalyser.Engine.UnitTest/Reports/OverallPerformanceBudgetAnalyserTest.cs index 6c3229d8..c07d87b4 100644 --- a/BudgetAnalyser.Engine.UnitTest/Reports/OverallPerformanceBudgetAnalyserTest.cs +++ b/BudgetAnalyser.Engine.UnitTest/Reports/OverallPerformanceBudgetAnalyserTest.cs @@ -11,9 +11,9 @@ namespace BudgetAnalyser.Engine.UnitTest.Reports; public class OverallPerformanceBudgetAnalyserTest { private readonly IBudgetBucketRepository bucketRepository = new BucketBucketRepoAlwaysFind(); - private readonly BudgetCollection budgetsTestData = BudgetModelTestData.CreateCollectionWith2And5(); private readonly StatementModel statementTestData = StatementModelTestData.TestData6(); private OverallPerformanceBudgetAnalyser? analyser; + private BudgetCollection budgetsTestData = BudgetModelTestData.CreateCollectionWith2And5(); private GlobalFilterCriteria dateCriteria = new() { BeginDate = new DateTime(2013, 1, 1), EndDate = new DateTime(2014, 1, 1) }; [TestMethod] @@ -53,6 +53,17 @@ public void Analyse_ShouldRecogniseSingleBudget() Assert.IsFalse(result.UsesMultipleBudgets); } + + [TestMethod] + public void Analyse_ShouldRecogniseSingleBudget_Fortnight() + { + this.budgetsTestData[0].BudgetCycle = BudgetCycle.Fortnightly; + this.budgetsTestData[1].BudgetCycle = BudgetCycle.Fortnightly; + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + + Assert.IsFalse(result.UsesMultipleBudgets); + } + [TestMethod] public void Analyse_ShouldReturnCorrectAverageSpend() { @@ -61,6 +72,17 @@ public void Analyse_ShouldReturnCorrectAverageSpend() Assert.AreEqual(-1000, result.AverageSpend); } + [TestMethod] + public void Analyse_ShouldReturnCorrectAverageSpend_Fortnight() + { + this.budgetsTestData = new BudgetCollection { BudgetModelTestData.CreateTestData5() }; + this.budgetsTestData.Single().BudgetCycle = BudgetCycle.Fortnightly; + + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + + AssertExtensions.AreEqualWithTolerance(-461.54M, result.AverageSpend); + } + [TestMethod] public void Analyse_ShouldReturnCorrectAverageSurplus() { @@ -69,6 +91,28 @@ public void Analyse_ShouldReturnCorrectAverageSurplus() Assert.AreEqual(-500, result.AverageSurplus); } + [TestMethod] + public void Analyse_ShouldReturnCorrectAverageSurplus_Fortnight() + { + this.budgetsTestData = new BudgetCollection { BudgetModelTestData.CreateTestData5() }; + this.budgetsTestData.Single().BudgetCycle = BudgetCycle.Fortnightly; + + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + + AssertExtensions.AreEqualWithTolerance(-230.77M, result.AverageSurplus); + } + + [TestMethod] + public void Analyse_ShouldReturnCorrectNumberOfFortnights() + { + this.budgetsTestData = new BudgetCollection { BudgetModelTestData.CreateTestData5() }; + this.budgetsTestData.Single().BudgetCycle = BudgetCycle.Fortnightly; + + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + + Assert.AreEqual(26, result.DurationInPeriods); + } + [TestMethod] public void Analyse_ShouldReturnCorrectNumberOfMonths() { @@ -85,6 +129,17 @@ public void Analyse_ShouldReturnCorrectOverallPerformanceRating() Assert.AreEqual(-2760, result.OverallPerformance); } + [TestMethod] + public void Analyse_ShouldReturnCorrectOverallPerformanceRating_Fortnight() + { + this.budgetsTestData = new BudgetCollection { BudgetModelTestData.CreateTestData5() }; + this.budgetsTestData.Single().BudgetCycle = BudgetCycle.Fortnightly; + + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + + AssertExtensions.AreEqualWithTolerance(8020M, result.OverallPerformance); + } + [TestMethod] public void Analyse_ShouldReturnResultCorrectAvgForCarMtc() { @@ -93,6 +148,18 @@ public void Analyse_ShouldReturnResultCorrectAvgForCarMtc() Assert.AreEqual(200, result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.CarMtcBucket.Code).AverageSpend); } + [TestMethod] + public void Analyse_ShouldReturnResultCorrectAvgForCarMtc_Fortnight() + { + this.budgetsTestData = new BudgetCollection { BudgetModelTestData.CreateTestData5() }; + this.budgetsTestData.Single().BudgetCycle = BudgetCycle.Fortnightly; + + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + var avgCarMtc = result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.CarMtcBucket.Code).AverageSpend; + + AssertExtensions.AreEqualWithTolerance(92.31M, avgCarMtc); + } + [TestMethod] public void Analyse_ShouldReturnResultCorrectAvgForHair() { @@ -101,6 +168,18 @@ public void Analyse_ShouldReturnResultCorrectAvgForHair() Assert.AreEqual(300, result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.HairBucket.Code).AverageSpend); } + [TestMethod] + public void Analyse_ShouldReturnResultCorrectAvgForHair_Fortnight() + { + this.budgetsTestData = new BudgetCollection { BudgetModelTestData.CreateTestData5() }; + this.budgetsTestData.Single().BudgetCycle = BudgetCycle.Fortnightly; + + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + var avgHair = result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.HairBucket.Code).AverageSpend; + + AssertExtensions.AreEqualWithTolerance(138.46M, avgHair); + } + [TestMethod] public void Analyse_ShouldReturnResultCorrectAvgForPhone() { @@ -109,6 +188,18 @@ public void Analyse_ShouldReturnResultCorrectAvgForPhone() Assert.AreEqual(100, result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.PhoneBucket.Code).AverageSpend); } + [TestMethod] + public void Analyse_ShouldReturnResultCorrectAvgForPhone_Fortnight() + { + this.budgetsTestData = new BudgetCollection { BudgetModelTestData.CreateTestData5() }; + this.budgetsTestData.Single().BudgetCycle = BudgetCycle.Fortnightly; + + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + var avgPhone = result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.PhoneBucket.Code).AverageSpend; + + AssertExtensions.AreEqualWithTolerance(46.15M, avgPhone); + } + [TestMethod] public void Analyse_ShouldReturnResultCorrectAvgForPower() { @@ -117,6 +208,18 @@ public void Analyse_ShouldReturnResultCorrectAvgForPower() Assert.AreEqual(400, result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.PowerBucket.Code).AverageSpend); } + [TestMethod] + public void Analyse_ShouldReturnResultCorrectAvgForPower_Fortnight() + { + this.budgetsTestData = new BudgetCollection { BudgetModelTestData.CreateTestData5() }; + this.budgetsTestData.Single().BudgetCycle = BudgetCycle.Fortnightly; + + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + var avgPower = result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.PowerBucket.Code).AverageSpend; + + AssertExtensions.AreEqualWithTolerance(184.62M, avgPower); + } + [TestMethod] public void Analyse_ShouldReturnResultCorrectAvgForSurplus() { @@ -125,6 +228,18 @@ public void Analyse_ShouldReturnResultCorrectAvgForSurplus() Assert.AreEqual(500, result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.SurplusBucket.Code).AverageSpend); } + [TestMethod] + public void Analyse_ShouldReturnResultCorrectAvgForSurplus_Fortnight() + { + this.budgetsTestData = new BudgetCollection { BudgetModelTestData.CreateTestData5() }; + this.budgetsTestData.Single().BudgetCycle = BudgetCycle.Fortnightly; + + var result = this.analyser!.Analyse(this.statementTestData, this.budgetsTestData, this.dateCriteria); + var avgSurplus = result.Analyses.Single(b => b.Bucket.Code == StatementModelTestData.SurplusBucket.Code).AverageSpend; + + AssertExtensions.AreEqualWithTolerance(230.77M, avgSurplus); + } + [TestMethod] public void Analyse_ShouldReturnResultWithSingleBudget() {