Skip to content

Commit

Permalink
Add unit tests for fortnight calcs
Browse files Browse the repository at this point in the history
  • Loading branch information
Benrnz committed Dec 31, 2024
1 parent fe19fbd commit 86a2b79
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
20 changes: 20 additions & 0 deletions BudgetAnalyser.Engine.UnitTest/AssertExtension.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand Down

0 comments on commit 86a2b79

Please sign in to comment.