Skip to content

Commit

Permalink
Add six unit tests for models, covering edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
maacpiash committed Jul 15, 2020
1 parent d39a54c commit 0846e27
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
11 changes: 10 additions & 1 deletion tests/ModelTests/FAHP.test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public void Can_GiveEqualWeightsForAllOnes()
Assert.Equal(0.1666667, compMat[i], 6);
}

[Fact]
public void Can_SetGet_CriteriaWeights()
{
int[] values = new int[] { 1, 1, 1, 1, 1 };
var FAHP = new KonSchool.Models.FAHP(ComparisonMatrix(values));
var weights = new double[] { 0.166667, 0.166667, 0.166667, 0.166667, 0.166667, 0.166667 };
FAHP.CriteriaWeights = weights;
Assert.Equal(weights, FAHP.CriteriaWeights);
}

[Fact]
public void WontFAHP_forInvalidDiagonalTuple()
{
Expand All @@ -27,7 +37,6 @@ public void WontFAHP_forInvalidDiagonalTuple()
{ (2.0, 2.0, 2.0), (1.0, 1.0, 1.0) }
};
var FAHP = new KonSchool.Models.FAHP(ComparisonMatrix);

Assert.Null(FAHP.CriteriaWeights);
}
}
Expand Down
46 changes: 46 additions & 0 deletions tests/ModelTests/Query.test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using KonSchool.Models;
using KonSchool.Tests.Mocks;
using Xunit;
using Moq;

namespace KonSchool.Tests.ModelTests
{
Expand Down Expand Up @@ -31,6 +32,18 @@ public void CtorTest()
Assert.Equal("Other", query.Occupation);
}

[Theory]
[InlineData(null, "Other")]
[InlineData("Superman", "Other")]
[InlineData("Worker", "Worker")]
public void Can_SetOccupation(string providedValue, string setValue)
{
Query query = new Query(new MockSchoolService());
query.Occupation = providedValue;
Assert.Equal(setValue, query.Occupation);
}


[Fact]
public void Can_SetLocation()
{
Expand All @@ -42,6 +55,31 @@ public void Can_SetLocation()
Assert.Equal("Ward no. 1", query.Union_Ward);
}

[Theory]
[InlineData("Dhaka", "Dhaka", "Rampura", "Ward no. 1", 1.0)]
[InlineData("Dhaka", "Dhaka", "Rampura", "Ward no. 2", 0.9)]
[InlineData("Dhaka", "Dhaka", "Banani", "Ward no. 1", 0.7)]
[InlineData("Dhaka", "Gazipur", "Rampura", "Ward no. 1", 0.4)]
[InlineData("Comilla", "Dhaka", "Rampura", "Ward no. 1", 0.0)]
public void Can_SetLOC(string div, string dis, string thana, string uw, double loc)
{
var schoolService = new Mock<KonSchool.Services.ISchoolService>();
schoolService.Setup(ss => ss.GetSchools()).Returns(new List<School>
{
new School(1212121)
{
Division = "Dhaka",
District = "Dhaka",
Thana = "Rampura",
Union_Ward = "Ward no. 1"
}
});
Query query = new Query(schoolService.Object);
query.SetLocation(div, dis, thana, uw);
query.SetValues();
Assert.Equal(loc, query.Alternatives[0].LOC);
}

[Fact]
public void Can_CheckEligibility_Female()
{
Expand Down Expand Up @@ -105,5 +143,13 @@ public void Can_CheckEligibility_Male()

Assert.Equal(2, query.Alternatives.Count);
}

[Fact]
public void Can_CheckEligibility_JunSec()
{
School s = new School(1212121) { Level = "Junior Secondary" };
Query query = new Query(new MockSchoolService()) { Class = 9 };
Assert.False(query.IsEligible(s));
}
}
}
8 changes: 8 additions & 0 deletions tests/ModelTests/School.test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public void Can_SetValuesFromArray()
Assert.Equal(0.4, school.SEScore2);
Assert.Equal(0.3, school.SEScore3);
Assert.Equal(0.1, school.SEScore4);
}

[Fact]
public void Can_ThrowException_ForInvalidArrays()
{
School school = new School(121212);
Assert.Throws<System.Exception>(() => school.AverAge = new double[] { 11.0, 12.0, 13.0, 14.0 });
Assert.Throws<System.Exception>(() => school.SEScore = new double[] { 0.2, 0.4, 0.3 });
}
}
}
10 changes: 6 additions & 4 deletions tests/ModelTests/Stat.test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public void Can_Normalize_BySum()
Assert.Equal(0.2, dummy[2]);
}

[Fact]
public void Can_Normalize_ByLimits()
[Theory]
[InlineData(0.5, 1.0, 2.0, 3.0, 4.0, 5.0)] // ascending
[InlineData(0.5, 5.0, 4.0, 3.0, 2.0, 1.0)] // descending
public void Can_Normalize_ByLimits(double value, params double[] numbers)
{
double[] dummy = new double[] { 1, 2, 3, 4, 5 };
double[] dummy = numbers;
NormalizeByLimits(ref dummy);
Assert.Equal(0.5, dummy[2]);
Assert.Equal(value, dummy[2]);
}

[Fact]
Expand Down

0 comments on commit 0846e27

Please sign in to comment.