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
@@ -0,0 +1,26 @@
namespace DigitalLearningSolutions.Data.Migrations
{
using FluentMigrator;

[Migration(202206131339)]
public class AddMultiPageFormDataTable : Migration
{
public override void Up()
{
Create.Table("MultiPageFormData")
.WithColumn("ID").AsInt32().NotNullable().PrimaryKey().Identity()
.WithColumn("TempDataGuid").AsGuid().NotNullable()
.WithColumn("Json").AsCustom("NVARCHAR(MAX)").NotNullable()
.WithColumn("Feature").AsString(100).NotNullable()
.WithColumn("CreatedDate").AsDateTime().NotNullable();

Create.Index("IX_MultiPageFormData_TempDataGuid_Feature").OnTable("MultiPageFormData")
.OnColumn("TempDataGuid").Unique().OnColumn("Feature").Unique().WithOptions().Unique();
}

public override void Down()
{
Delete.Table("MultiPageFormData");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
namespace DigitalLearningSolutions.Data.Tests.DataServices
{
using System;
using System.Transactions;
using Dapper;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.Enums;
using DigitalLearningSolutions.Data.Models.MultiPageFormData;
using DigitalLearningSolutions.Data.Tests.TestHelpers;
using FizzWare.NBuilder;
using FluentAssertions;
using Microsoft.Data.SqlClient;
using NUnit.Framework;

public class MultiPageFormDataServiceTests
{
private readonly MultiPageFormData dataInDb = new MultiPageFormData
{
Id = 2,
TempDataGuid = Guid.NewGuid(),
Json = "test json",
Feature = MultiPageFormDataFeature.AddNewCourse.Name,
CreatedDate = new DateTime(2022, 06, 14, 14, 20, 12),
};

private SqlConnection connection = null!;

private IMultiPageFormDataService multiPageFormDataService = null!;

[SetUp]
public void SetUp()
{
connection = ServiceTestHelper.GetDatabaseConnection();
multiPageFormDataService = new MultiPageFormDataService(connection);
}

[Test]
public void GetMultiPageFormDataByGuidAndFeature_returns_expected_MultiPageFormData()
{
// Given
using var transaction = new TransactionScope();
InsertMultiPageFormData(dataInDb);

// When
var result = multiPageFormDataService.GetMultiPageFormDataByGuidAndFeature(
dataInDb.TempDataGuid,
MultiPageFormDataFeature.AddNewCourse.Name
);

// Then
result.Should().BeEquivalentTo(dataInDb);
}

[Test]
public void GetMultiPageFormDataByGuidAndFeature_returns_null_with_incorrect_guid()
{
// Given
using var transaction = new TransactionScope();
InsertMultiPageFormData(dataInDb);

// When
var result = multiPageFormDataService.GetMultiPageFormDataByGuidAndFeature(
Guid.NewGuid(),
MultiPageFormDataFeature.AddNewCourse.Name
);

// Then
result.Should().BeNull();
}

[Test]
public void GetMultiPageFormDataByGuidAndFeature_returns_null_with_incorrect_feature()
{
// Given
using var transaction = new TransactionScope();
InsertMultiPageFormData(dataInDb);

// When
var result = multiPageFormDataService.GetMultiPageFormDataByGuidAndFeature(
dataInDb.TempDataGuid,
"incorrect feature"
);

// Then
result.Should().BeNull();
}

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

// When
multiPageFormDataService.InsertMultiPageFormData(dataInDb);
var result = multiPageFormDataService.GetMultiPageFormDataByGuidAndFeature(
dataInDb.TempDataGuid,
MultiPageFormDataFeature.AddNewCourse.Name
);

// Then
result.Should().BeEquivalentTo(dataInDb, options => options.Excluding(d => d.Id));
}

[Test]
public void UpdateJsonByGuid_updates_expected_value()
{
// Given
using var transaction = new TransactionScope();
const string newJsonString = "new json";
InsertMultiPageFormData(dataInDb);

// When
multiPageFormDataService.UpdateJsonByGuid(dataInDb.TempDataGuid, newJsonString);
var result = multiPageFormDataService.GetMultiPageFormDataByGuidAndFeature(
dataInDb.TempDataGuid,
MultiPageFormDataFeature.AddNewCourse.Name
);

// Then
result.Should().NotBeNull();
result!.Json.Should().BeEquivalentTo(newJsonString);
}

[Test]
public void UpdateJsonByGuid_does_not_update_other_records()
{
// Given
using var transaction = new TransactionScope();
const string newJsonString = "new json";
var secondDataInDb = Builder<MultiPageFormData>
.CreateNew()
.With(d => d.TempDataGuid = Guid.NewGuid())
.Build();
InsertMultiPageFormData(dataInDb);
InsertMultiPageFormData(secondDataInDb);

// When
multiPageFormDataService.UpdateJsonByGuid(secondDataInDb.TempDataGuid, newJsonString);
var result = multiPageFormDataService.GetMultiPageFormDataByGuidAndFeature(
dataInDb.TempDataGuid,
MultiPageFormDataFeature.AddNewCourse.Name
);

// Then
result.Should().BeEquivalentTo(dataInDb);
}

[Test]
public void DeleteByGuid_deletes_expected_record()
{
// Given
using var transaction = new TransactionScope();
InsertMultiPageFormData(dataInDb);

// When
multiPageFormDataService.DeleteByGuid(dataInDb.TempDataGuid);
var result = multiPageFormDataService.GetMultiPageFormDataByGuidAndFeature(
dataInDb.TempDataGuid,
MultiPageFormDataFeature.AddNewCourse.Name
);

// Then
result.Should().BeNull();
}

[Test]
public void DeleteByGuid_does_not_delete_other_records()
{
// Given
using var transaction = new TransactionScope();
var secondDataInDb = Builder<MultiPageFormData>
.CreateNew()
.With(d => d.TempDataGuid = Guid.NewGuid())
.Build();
InsertMultiPageFormData(dataInDb);
InsertMultiPageFormData(secondDataInDb);

// When
multiPageFormDataService.DeleteByGuid(secondDataInDb.TempDataGuid);
var result = multiPageFormDataService.GetMultiPageFormDataByGuidAndFeature(
dataInDb.TempDataGuid,
MultiPageFormDataFeature.AddNewCourse.Name
);

// Then
result.Should().BeEquivalentTo(dataInDb);
}

private void InsertMultiPageFormData(MultiPageFormData data)
{
connection.Execute(
@"SET IDENTITY_INSERT dbo.MultiPageFormData ON
INSERT MultiPageFormData (ID, TempDataGuid, Json, Feature, CreatedDate)
VALUES (@Id, @TempDataGuid, @Json, @Feature, @CreatedDate)
SET IDENTITY_INSERT dbo.MultiPageFormData OFF",
data
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace DigitalLearningSolutions.Web.Tests.Helpers
namespace DigitalLearningSolutions.Data.Tests.Helpers
{
using System.Collections.Generic;
using DigitalLearningSolutions.Web.Helpers;
using DigitalLearningSolutions.Data.Helpers;
using FluentAssertions;
using FluentAssertions.Execution;
using NUnit.Framework;
Expand Down
Loading