Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using Dapper;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.Models;
using DigitalLearningSolutions.Data.Models.Progress;
using DigitalLearningSolutions.Data.Tests.TestHelpers;
using FakeItEasy;
using FizzWare.NBuilder;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.Data.SqlClient;
Expand Down Expand Up @@ -359,6 +361,24 @@ public void UnlockCourseProgress_updates_progress_record()
}
}

[Test]
public void LockProgress_updates_progress_record()
{
using var transaction = new TransactionScope();

// Given
const int progressId = 1;
var statusBeforeLock = progressTestHelper.GetCourseProgressLockedStatusByProgressId(progressId);

// When
progressDataService.LockProgress(progressId);
var statusAfterLocked = progressTestHelper.GetCourseProgressLockedStatusByProgressId(progressId);

// Then
statusBeforeLock.Should().BeFalse();
statusAfterLocked.Should().BeTrue();
}

[Test]
public void GetLearningLogEntries_gets_records_correctly()
{
Expand Down Expand Up @@ -683,5 +703,124 @@ public void UpdateProgressCompletedDate_updates_progress_record_correctly()
var progressCompletedDate = progressTestHelper.GetProgressCompletedDateById(progressId);
progressCompletedDate.Should().Be(expectedCompletedDate);
}

[Test]
public void GetAssessAttemptsForProgressSection_gets_all_appropriate_records()
{
// Given
const int progressId = 1;
const int sectionNumber = 2;

// When
var results = progressDataService.GetAssessAttemptsForProgressSection(progressId, sectionNumber);

// Then
var expectedAssessAttemptResults = Builder<AssessAttempt>.CreateListOfSize(2).All()
.With(a => a.CandidateId = 1)
.With(a => a.CustomisationId = 100)
.With(a => a.CustomisationVersion = 1)
.With(a => a.AssessInstance = 3)
.With(a => a.SectionNumber = 2)
.With(a => a.Score = 100)
.With(a => a.Status = true)
.With(a => a.ProgressId = 1)
.TheFirst(1).With(a => a.AssessAttemptId = 3)
.And(a => a.Date = new DateTime(2010, 09, 22, 7, 54, 40, 307))
.TheLast(1).With(a => a.AssessAttemptId = 4)
.And(a => a.Date = new DateTime(2010, 09, 22, 7, 58, 04, 937))
.Build();
results.Should().BeEquivalentTo(expectedAssessAttemptResults);
}

[Test]
public void InsertAssessAttempt_inserts_details_correctly()
{
using var transaction = new TransactionScope();

// Given
const int candidateId = 987;
const int customisationId = 123;
const int customisationVersion = 2;
const int sectionNumber = 4;
const int score = 42;
const bool status = false;
const int progressId = 1;
var insertionDate = new DateTime(2022, 06, 14, 12, 23, 54, 937);

// When
var recordsPriorToInsertion = progressDataService.GetAssessAttemptsForProgressSection(
progressId,
sectionNumber
);

progressDataService.InsertAssessAttempt(
candidateId,
customisationId,
customisationVersion,
insertionDate,
sectionNumber,
score,
status,
progressId
);
var result = progressDataService.GetAssessAttemptsForProgressSection(
progressId,
sectionNumber
).ToList();

// Then
using (new AssertionScope())
{
recordsPriorToInsertion.Count().Should().Be(1);
result.Count.Should().Be(2);
var insertedRecord = result.OrderByDescending(aa => aa.AssessAttemptId).First();
insertedRecord.CandidateId.Should().Be(candidateId);
insertedRecord.CustomisationId.Should().Be(customisationId);
insertedRecord.CustomisationVersion.Should().Be(customisationVersion);
insertedRecord.Date.Should().Be(insertionDate);
insertedRecord.AssessInstance.Should().Be(1);
insertedRecord.SectionNumber.Should().Be(sectionNumber);
insertedRecord.Score.Should().Be(score);
insertedRecord.Status.Should().Be(status);
insertedRecord.ProgressId.Should().Be(progressId);
}
}

[Test]
public void GetSectionAndApplicationDetailsForAssessAttempts_gets_all_appropriate_details()
{
// Given
const int sectionId = 1609;
const int customisationId = 21072;

// When
var result =
progressDataService.GetSectionAndApplicationDetailsForAssessAttempts(sectionId, customisationId);

// Then
using (new AssertionScope())
{
result.Should().NotBeNull();
result!.AssessAttempts.Should().Be(2);
result.PlaPassThreshold.Should().Be(50);
result.SectionNumber.Should().Be(1);
}
}

[Test]
public void
GetSectionAndApplicationDetailsForAssessAttempts_returns_null_if_section_and_customisation_do_not_match()
{
// Given
const int sectionId = 11;
const int customisationId = 12;

// When
var result =
progressDataService.GetSectionAndApplicationDetailsForAssessAttempts(sectionId, customisationId);

// Then
result.Should().BeNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
internal class SessionDataServiceTests
{
private const int TwoMinutesInMilliseconds = 120 * 1000;
private readonly DateTime currentTime = new DateTime(2022, 06, 14, 11, 12, 13, 14);
private SessionDataService sessionDataService = null!;
private SessionTestHelper sessionTestHelper = null!;

Expand Down Expand Up @@ -112,7 +113,7 @@ public void UpdateDelegateSessionDuration_Should_Only_Update_Given_Active_Sessio

// When
const int sessionId = 473;
sessionDataService.UpdateDelegateSessionDuration(sessionId);
sessionDataService.UpdateDelegateSessionDuration(sessionId, currentTime);

// Then
var updatedSessions = sessionTestHelper.GetCandidateSessions(candidateId).ToList();
Expand All @@ -124,7 +125,7 @@ public void UpdateDelegateSessionDuration_Should_Only_Update_Given_Active_Sessio

var activeSession = updatedSessions.First(session => session.SessionId == sessionId);
activeSession.LoginTime.AddMinutes(activeSession.Duration)
.Should().BeCloseTo(DateTime.UtcNow, TwoMinutesInMilliseconds);
.Should().BeCloseTo(currentTime, TwoMinutesInMilliseconds);
}
}

Expand All @@ -139,7 +140,7 @@ public void UpdateDelegateSessionDuration_Should_Not_Update_Inactive_Session()

// When
const int sessionId = 468;
sessionDataService.UpdateDelegateSessionDuration(sessionId);
sessionDataService.UpdateDelegateSessionDuration(sessionId, currentTime);

// Then
var updatedSessions = sessionTestHelper.GetCandidateSessions(candidateId);
Expand Down
29 changes: 18 additions & 11 deletions DigitalLearningSolutions.Data.Tests/Services/SessionServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace DigitalLearningSolutions.Data.Tests.Services
{
using System;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.Services;
using DigitalLearningSolutions.Data.Tests.Helpers;
using DigitalLearningSolutions.Data.Tests.TestHelpers;
using FakeItEasy;
using FluentAssertions;
Expand All @@ -14,20 +14,22 @@ public class SessionServiceTests
private const int CandidateId = 11;
private const int CustomisationId = 12;
private const int DefaultSessionId = 13;
private ISession httpContextSession;
private ISessionDataService sessionDataService;
private ISessionService sessionService;
private IClockService clockService = null!;
private ISession httpContextSession = null!;
private ISessionDataService sessionDataService = null!;
private ISessionService sessionService = null!;

[SetUp]
public void SetUp()
{
clockService = A.Fake<IClockService>();
sessionDataService = A.Fake<ISessionDataService>();
A.CallTo(() => sessionDataService.StartOrRestartDelegateSession(A<int>._, A<int>._))
.Returns(DefaultSessionId);

httpContextSession = new MockHttpContextSession();

sessionService = new SessionService(sessionDataService);
sessionService = new SessionService(clockService, sessionDataService);
}

[Test]
Expand All @@ -46,11 +48,14 @@ public void StartOrUpdateDelegateSession_should_StartOrRestartDelegateSession_fo
A.CallTo(() => sessionDataService.StartOrRestartDelegateSession(CandidateId, newCourseId))
.MustHaveHappenedOnceExactly();
A.CallTo(() => sessionDataService.StartOrRestartDelegateSession(A<int>._, A<int>._))
.WhenArgumentsMatch((int candidateId, int customisationId) =>
candidateId != CandidateId || customisationId != newCourseId)
.WhenArgumentsMatch(
(int candidateId, int customisationId) =>
candidateId != CandidateId || customisationId != newCourseId
)
.MustNotHaveHappened();

A.CallTo(() => sessionDataService.UpdateDelegateSessionDuration(A<int>._)).MustNotHaveHappened();
A.CallTo(() => sessionDataService.UpdateDelegateSessionDuration(A<int>._, A<DateTime>._))
.MustNotHaveHappened();
}

[Test]
Expand All @@ -77,15 +82,17 @@ public void StartOrUpdateDelegateSession_should_UpdateDelegateSession_for_course
httpContextSession.Clear();
const int courseInSession = CustomisationId;
httpContextSession.SetInt32($"SessionID-{courseInSession}", DefaultSessionId);
var currentUtcTime = new DateTime(2022, 06, 14, 12, 01, 01);
A.CallTo(() => clockService.UtcNow).Returns(currentUtcTime);

// When
sessionService.StartOrUpdateDelegateSession(CandidateId, courseInSession, httpContextSession);

// Then
A.CallTo(() => sessionDataService.UpdateDelegateSessionDuration(DefaultSessionId))
A.CallTo(() => sessionDataService.UpdateDelegateSessionDuration(DefaultSessionId, currentUtcTime))
.MustHaveHappenedOnceExactly();
A.CallTo(() => sessionDataService.UpdateDelegateSessionDuration(A<int>._))
.WhenArgumentsMatch((int sessionId) => sessionId != DefaultSessionId)
A.CallTo(() => sessionDataService.UpdateDelegateSessionDuration(A<int>._, A<DateTime>._))
.WhenArgumentsMatch((int sessionId, DateTime currentTime) => sessionId != DefaultSessionId)
.MustNotHaveHappened();

A.CallTo(() => sessionDataService.StartOrRestartDelegateSession(A<int>._, A<int>._)).MustNotHaveHappened();
Expand Down
Loading