-
Notifications
You must be signed in to change notification settings - Fork 4
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 #97 from AnnaDodson/develop
Admin page for steps totals
- Loading branch information
Showing
17 changed files
with
696 additions
and
21 deletions.
There are no files selected for viewing
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,19 @@ | ||
using System.Threading.Tasks; | ||
using StepChallenge.Services; | ||
|
||
namespace StepChallenge.Tests | ||
{ | ||
public class StepsServiceStub : StepsService | ||
{ | ||
private readonly int _averageTeamSize; | ||
public StepsServiceStub(StepContext stepContext, int averageTeamSize = 3) : base(stepContext) | ||
{ | ||
_averageTeamSize = averageTeamSize; | ||
} | ||
|
||
public override async Task<int> GetAverageTeamSize() | ||
{ | ||
return _averageTeamSize; | ||
} | ||
} | ||
} |
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,116 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Model; | ||
using NUnit.Framework; | ||
|
||
namespace StepChallenge.Tests | ||
{ | ||
public class TestOverviewData | ||
{ | ||
private static DateTime StartDate = new DateTime(2019,09,16, 0,0,0); | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
} | ||
|
||
public static IQueryable<Team> GetTeams(){ | ||
var teams = CreateThreeTeams(); | ||
return teams; | ||
} | ||
|
||
public static IQueryable<Team> GetTeamWithThreePeople() | ||
{ | ||
var teams = CreateTeamWithOneLessPerson(); | ||
return teams; | ||
} | ||
|
||
private static IQueryable<Team> CreateTeamWithOneLessPerson() | ||
{ | ||
return (new List<Team> | ||
{ | ||
new Team | ||
{ | ||
TeamId = 1, | ||
TeamName = "Team_1", | ||
NumberOfParticipants = 3, | ||
Participants = CreateParticipants(10) | ||
} | ||
}).AsQueryable(); | ||
} | ||
|
||
private static IQueryable<Team> CreateThreeTeams() | ||
{ | ||
return (new List<Team> | ||
{ | ||
new Team | ||
{ | ||
TeamId = 1, | ||
TeamName = "Team_1", | ||
NumberOfParticipants = 3, | ||
Participants = CreateParticipants(10) | ||
}, | ||
new Team | ||
{ | ||
TeamId = 2, | ||
TeamName = "Team_2", | ||
NumberOfParticipants = 3, | ||
Participants = CreateParticipants(20) | ||
}, | ||
new Team | ||
{ | ||
TeamId = 3, | ||
TeamName = "Team_3", | ||
NumberOfParticipants = 3, | ||
Participants = CreateParticipants(30) | ||
} | ||
}).AsQueryable(); | ||
} | ||
|
||
private static ICollection<Participant> CreateParticipants(int id) | ||
{ | ||
var participants = new List<Participant> | ||
{ | ||
new Participant | ||
{ | ||
ParticipantName = "ParticipantNameOne", | ||
ParticipantId = id + 1, | ||
Steps = CreateSteps(20) | ||
}, | ||
new Participant | ||
{ | ||
ParticipantName = "ParticipantNameTwo", | ||
ParticipantId = id + 2, | ||
Steps = CreateSteps(20) | ||
}, | ||
new Participant | ||
{ | ||
ParticipantName = "ParticipantNameThree", | ||
ParticipantId = id + 3, | ||
Steps = CreateSteps(20) | ||
}, | ||
}; | ||
return participants; | ||
} | ||
|
||
private static ICollection<Steps> CreateSteps(int stepCount) | ||
{ | ||
var steps = new List<Steps>(); | ||
|
||
for (int i = 0; i < 3; i++) | ||
{ | ||
steps.Add( | ||
new Steps | ||
{ | ||
StepCount = stepCount, | ||
DateOfSteps = StartDate.AddDays(i), | ||
} | ||
); | ||
} | ||
|
||
return steps; | ||
} | ||
} | ||
} |
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,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Model; | ||
using Moq; | ||
using NUnit.Framework; | ||
using StepChallenge.Services; | ||
|
||
namespace StepChallenge.Tests | ||
{ | ||
public class TeamOverviewTests : BaseTests | ||
{ | ||
/// <summary> | ||
/// Test a teams step total is counted correctly | ||
/// </summary> | ||
[Test] | ||
public async Task Test_TeamOverview_TotalSteps_AreCountedCorrectly() | ||
{ | ||
var team = TestOverviewData.GetTeams().ToList(); | ||
|
||
var stepsService = new StepsServiceStub(GetMockStepContext(team)); | ||
var result = await stepsService.GetTeamsOverview(); | ||
|
||
var resultTotal = result.Teams.ToList().First(r => r.TeamId == 1).TeamTotalSteps; | ||
var expectedTotal = 180; | ||
|
||
Assert.IsTrue(resultTotal == expectedTotal, $"Expected total steps to be: {expectedTotal} but got {resultTotal}"); | ||
} | ||
|
||
/// <summary> | ||
/// Test a teams step total is counted correctly and ignore steps counted outside of the challenge dates | ||
/// </summary> | ||
[Test] | ||
[TestCase("01/01/2012")] | ||
[TestCase("01/01/2020")] | ||
public async Task Test_TeamOverview_TotalSteps_DoNotCountStepsOutsideOfDates(DateTime date) | ||
{ | ||
var team = TestOverviewData.GetTeams().ToList(); | ||
team.First(r => r.TeamId == 1).Participants.First(p => p.ParticipantId == 11).Steps.Add(new Steps | ||
{ | ||
StepCount = 30, | ||
DateOfSteps = date, | ||
}); | ||
|
||
var stepsService = new StepsServiceStub(GetMockStepContext(team)); | ||
var result = await stepsService.GetTeamsOverview(); | ||
|
||
var resultTotal = result.Teams.ToList().First(r => r.TeamId == 1).TeamTotalSteps; | ||
var expectedTotal = 180; | ||
|
||
Assert.IsTrue(resultTotal == expectedTotal, $"Expected total steps to be: {expectedTotal} but got {resultTotal}"); | ||
} | ||
|
||
/// <summary> | ||
/// Test a teams step total adds an average person correctly for teams with less people | ||
/// </summary> | ||
[Test] | ||
public async Task Test_TeamOverview_TotalSteps_TeamsWithLessPeopleGetAverageSteps() | ||
{ | ||
var team = TestOverviewData.GetTeamWithThreePeople().ToList(); | ||
|
||
var averageTeamSize = 4; | ||
var stepsService = new StepsServiceStub(GetMockStepContext(team), averageTeamSize); | ||
var result = await stepsService.GetTeamsOverview(); | ||
|
||
var resultTotal = result.Teams.ToList().First(r => r.TeamId == 1).TeamTotalStepsWithAverage; | ||
var expectedTotal = 240; | ||
|
||
Assert.IsTrue(resultTotal == expectedTotal, $"Expected total steps to be: {expectedTotal} but got {resultTotal}"); | ||
} | ||
|
||
private StepContext GetMockStepContext(List<Team> teams) | ||
{ | ||
var mockContext = new Mock<StepContext>(); | ||
|
||
mockContext.Setup(x => x.Team).Returns(GetDbSetTeams(teams).Object); | ||
|
||
return mockContext.Object; | ||
} | ||
|
||
|
||
private Mock<DbSet<Team>> GetDbSetTeams(List<Team> teamList) | ||
{ | ||
var teams = teamList.AsQueryable(); | ||
|
||
var mockParticipantSet = new Mock<DbSet<Team>>(); | ||
mockParticipantSet.As<IQueryable<Team>>().Setup(m => m.Expression).Returns(teams.Expression); | ||
mockParticipantSet.As<IQueryable<Team>>().Setup(m => m.Provider).Returns(teams.Provider); | ||
mockParticipantSet.As<IQueryable<Team>>().Setup(m => m.Expression).Returns(teams.Expression); | ||
mockParticipantSet.As<IQueryable<Team>>().Setup(m => m.ElementType).Returns(teams.ElementType); | ||
mockParticipantSet.As<IQueryable<Team>>().Setup(m => m.GetEnumerator()).Returns(teams.GetEnumerator()); | ||
|
||
return mockParticipantSet; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.