Skip to content

Commit

Permalink
Replace GroupUid with Uid outside academies db project
Browse files Browse the repository at this point in the history
We want to keep our language in the web project consistent with that of our users in order to make the system easier to maintain - we'd started to drift already with `GroupUid`, `Uid` and `TrustId` all meaning the same thing. `GroupUid` is language specific to the academies database so we will keep it there and use `Uid` in all other scenarios.

Also refactored `SearchModelTests`
  • Loading branch information
dynamictulip committed Oct 16, 2023
1 parent 7b162b9 commit 3c803f7
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public TrustProvider(IAcademiesDbContext academiesDbContext)
_academiesDbContext = academiesDbContext;
}

public async Task<Trust?> GetTrustByGroupUidAsync(string groupUid)
public async Task<Trust?> GetTrustByUidAsync(string uid)
{
Trust? trust = null;

var group = await _academiesDbContext.Groups.SingleOrDefaultAsync(g => g.GroupUid == groupUid);
var group = await _academiesDbContext.Groups.SingleOrDefaultAsync(g => g.GroupUid == uid);
if (group is not null)
{
trust = new Trust(
Expand Down
2 changes: 1 addition & 1 deletion DfE.FindInformationAcademiesTrusts.Data/ITrustProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace DfE.FindInformationAcademiesTrusts.Data;

public interface ITrustProvider
{
public Task<Trust?> GetTrustByGroupUidAsync(string groupUid);
public Task<Trust?> GetTrustByUidAsync(string uid);
}
2 changes: 1 addition & 1 deletion DfE.FindInformationAcademiesTrusts.Data/Trust.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace DfE.FindInformationAcademiesTrusts.Data;

public record Trust(string GroupUid, string Name, string? Ukprn, string Type);
public record Trust(string Uid, string Name, string? Ukprn, string Type);
8 changes: 4 additions & 4 deletions DfE.FindInformationAcademiesTrusts/Pages/Search.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public SearchModel(ITrustProvider trustProvider, ITrustSearch trustSearch)

public string InputId => "search";
[BindProperty(SupportsGet = true)] public string KeyWords { get; set; } = string.Empty;
[BindProperty(SupportsGet = true)] public string TrustId { get; set; } = string.Empty;
[BindProperty(SupportsGet = true)] public string Uid { get; set; } = string.Empty;
public IEnumerable<TrustSearchEntry> Trusts { get; set; } = Array.Empty<TrustSearchEntry>();

public async Task<IActionResult> OnGetAsync()
{
if (!string.IsNullOrWhiteSpace(TrustId))
if (!string.IsNullOrWhiteSpace(Uid))
{
var trust = await _trustProvider.GetTrustByGroupUidAsync(TrustId);
var trust = await _trustProvider.GetTrustByUidAsync(Uid);
if (trust != null && string.Equals(trust.Name, KeyWords, StringComparison.CurrentCultureIgnoreCase))
{
return RedirectToPage("/Trusts/Details", new { Uid = TrustId });
return RedirectToPage("/Trusts/Details", new { Uid });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ContactsModel(ITrustProvider trustProvider)

public async Task<IActionResult> OnGetAsync()
{
var trust = await _trustProvider.GetTrustByGroupUidAsync(Uid);
var trust = await _trustProvider.GetTrustByUidAsync(Uid);

if (trust == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public DetailsModel(ITrustProvider trustProvider)

public async Task<IActionResult> OnGetAsync()
{
var trust = await _trustProvider.GetTrustByGroupUidAsync(Uid);
var trust = await _trustProvider.GetTrustByUidAsync(Uid);

if (trust == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
<div class="govuk-caption-m app-side-navigation__title">@ViewConstants.AboutTheTrustSectionName</div>
<ul class="ds_side-navigation__list app-side-navigation__list">
<li class="ds_side-navigation__item app-side-navigation-highlight-item">
<a asp-page="./Details" asp-route-uid="@Model.Trust.GroupUid" class="ds_side-navigation__link govuk-body govuk-link govuk-link--no-visited-state @GetCurrentLinkClassIf("Details")">
<a asp-page="./Details" asp-route-uid="@Model.Trust.Uid" class="ds_side-navigation__link govuk-body govuk-link govuk-link--no-visited-state @GetCurrentLinkClassIf("Details")">
<span class="visually-hidden">Trust</span>Details
</a>
</li>
<li class="ds_side-navigation__item app-side-navigation-highlight-item">
<a asp-page="./Contacts" asp-route-uid="@Model.Trust.GroupUid" class="ds_side-navigation__link govuk-body govuk-link govuk-link--no-visited-state @GetCurrentLinkClassIf("Contacts")">
<a asp-page="./Contacts" asp-route-uid="@Model.Trust.Uid" class="ds_side-navigation__link govuk-body govuk-link govuk-link--no-visited-state @GetCurrentLinkClassIf("Contacts")">
<span class="visually-hidden">Trust</span>Contacts
</a>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ public TrustProviderTests()
}

[Fact]
public async Task GetTrustsByGroupUidAsync_should_return_a_trust_if_group_found()
public async Task GetTrustsByUidAsync_should_return_a_trust_if_group_found()
{
_groups.Add(new Group
{ GroupName = "trust 1", GroupUid = "1234", GroupType = "Multi-academy trust", Ukprn = "my ukprn" });

var result = await _sut.GetTrustByGroupUidAsync("1234");
var result = await _sut.GetTrustByUidAsync("1234");

result.Should().BeEquivalentTo(new Trust("1234", "trust 1",
"my ukprn",
"Multi-academy trust"));
}

[Fact]
public async Task GetTrustsByGroupUidAsync_should_return_null_when_group_not_found()
public async Task GetTrustsByUidAsync_should_return_null_when_group_not_found()
{
var result = await _sut.GetTrustByGroupUidAsync("987654321");
var result = await _sut.GetTrustByUidAsync("987654321");
result.Should().BeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,10 @@ namespace DfE.FindInformationAcademiesTrusts.UnitTests.Pages;

public class SearchModelTests
{
private readonly Mock<ITrustProvider> _mockTrustProvider;
private const string SearchTermThatMatchesAllFakeTrusts = "trust";
private readonly SearchModel _sut;
private readonly Mock<ITrustSearch> _mockTrustSearch;

public SearchModelTests()
{
_mockTrustProvider = new Mock<ITrustProvider>();
_mockTrustSearch = new Mock<ITrustSearch>();

_sut = new SearchModel(_mockTrustProvider.Object, _mockTrustSearch.Object);
}

private readonly TrustSearchEntry[] _fakeTrusts =
{
new("trust 1", "Dorthy Inlet, Kingston upon Hull, City of, JY36 9VC", "2044", ""),
Expand All @@ -27,17 +19,24 @@ public SearchModelTests()
};

private readonly Trust _fakeTrust =
new("123", "trust 1", "2044", "Multi-academy trust");
new("2044", "trust 1", "100123456", "Multi-academy trust");

private const string TrustId = "1234";
public SearchModelTests()
{
Mock<ITrustProvider> mockTrustProvider = new();
_mockTrustSearch = new Mock<ITrustSearch>();

mockTrustProvider.Setup(s => s.GetTrustByUidAsync(_fakeTrust.Uid).Result)
.Returns(_fakeTrust);
_mockTrustSearch.Setup(s => s.SearchAsync(SearchTermThatMatchesAllFakeTrusts).Result).Returns(_fakeTrusts);

_sut = new SearchModel(mockTrustProvider.Object, _mockTrustSearch.Object);
}

[Fact]
public async Task OnGetAsync_should_search_if_query_parameter()
public async Task OnGetAsync_should_search_if_query_parameter_provided()
{
const string query = "trust";

_mockTrustSearch.Setup(s => s.SearchAsync(query).Result).Returns(_fakeTrusts);
_sut.KeyWords = query;
_sut.KeyWords = SearchTermThatMatchesAllFakeTrusts;

await _sut.OnGetAsync();

Expand All @@ -53,12 +52,10 @@ public async Task OnGetAsync_should_default_to_empty_trusts_if_no_query()
}

[Fact]
public async Task OnGetAsync_should_redirect_to_trust_details_if_given_trustId()
public async Task OnGetAsync_should_redirect_to_trust_details_if_given_uid_and_query_is_trust_name()
{
_sut.TrustId = TrustId;
_sut.KeyWords = "trust 1";
_mockTrustProvider.Setup(s => s.GetTrustByGroupUidAsync(TrustId).Result)
.Returns(_fakeTrust);
_sut.Uid = _fakeTrust.Uid;
_sut.KeyWords = _fakeTrust.Name;

var result = await _sut.OnGetAsync();

Expand All @@ -70,43 +67,36 @@ public async Task OnGetAsync_should_redirect_to_trust_details_if_given_trustId()
[Fact]
public async Task OnGetAsync_should_pass_trustId_to_trust_details_if_given_trustId()
{
_sut.TrustId = TrustId;
_sut.KeyWords = "trust 1";
_mockTrustProvider.Setup(s => s.GetTrustByGroupUidAsync(TrustId).Result)
.Returns(_fakeTrust);
_sut.Uid = _fakeTrust.Uid;
_sut.KeyWords = _fakeTrust.Name;

var result = await _sut.OnGetAsync();

result.Should().BeOfType<RedirectToPageResult>();
var redirectResult = (RedirectToPageResult)result;
redirectResult.RouteValues.Should().ContainKey("Uid").WhoseValue.Should().Be(TrustId);
redirectResult.RouteValues.Should().ContainKey("Uid").WhoseValue.Should().Be(_fakeTrust.Uid);
}

[Fact]
public async Task OnGetAsync_should_not_redirect_to_trust_details_if_trustId_does_not_match_query()
{
const string query = "trust 3";

_mockTrustSearch.Setup(s => s.SearchAsync(query).Result).Returns(_fakeTrusts);
_mockTrustProvider.Setup(s => s.GetTrustByGroupUidAsync(TrustId).Result)
.Returns(_fakeTrust);
var differentFakeTrust = new TrustSearchEntry("other trust", "Some address", "987", "TR0987");
_mockTrustSearch.Setup(s => s.SearchAsync(differentFakeTrust.Name).Result)
.Returns(new[] { differentFakeTrust });

_mockTrustSearch.Setup(s => s.SearchAsync(query).Result).Returns(_fakeTrusts);
_sut.KeyWords = query;
_sut.TrustId = TrustId;
_sut.KeyWords = differentFakeTrust.Name;
_sut.Uid = _fakeTrust.Uid;

var result = await _sut.OnGetAsync();

result.Should().BeOfType<PageResult>();
_sut.Trusts.Should().BeEquivalentTo(_fakeTrusts);
_sut.Trusts.Should().ContainSingle(t => t == differentFakeTrust);
}

[Fact]
public async Task OnGetPopulateAutocompleteAsync_should_return_trusts_matching_keyword()
{
const string query = "trust";
_mockTrustSearch.Setup(s => s.SearchAsync(query).Result).Returns(_fakeTrusts);
_sut.KeyWords = query;
_sut.KeyWords = SearchTermThatMatchesAllFakeTrusts;

var result = await _sut.OnGetPopulateAutocompleteAsync();

Expand All @@ -129,7 +119,7 @@ public void KeyWords_property_is_empty_by_default()
[Fact]
public void TrustId_property_is_empty_by_default()
{
_sut.TrustId.Should().Be("");
_sut.Uid.Should().Be("");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ public ContactsModelTests()
}

[Fact]
public async void OnGetAsync_should_fetch_a_trust_by_GroupUid()
public async void OnGetAsync_should_fetch_a_trust_by_Uid()
{
_mockTrustProvider.Setup(s => s.GetTrustByGroupUidAsync("1234").Result)
_mockTrustProvider.Setup(s => s.GetTrustByUidAsync("1234").Result)
.Returns(new Trust("test", "test", "test", "Multi-academy trust"));
_sut.Uid = "1234";
await _sut.OnGetAsync();
_sut.Trust.Should().BeEquivalentTo(new Trust("test", "test", "test", "Multi-academy trust"));
}

[Fact]
public async void GroupUid_should_be_empty_string_by_default()
public async void Uid_should_be_empty_string_by_default()
{
await _sut.OnGetAsync();
_sut.Uid.Should().BeEquivalentTo(string.Empty);
Expand All @@ -47,7 +47,7 @@ public void PageSection_should_be_AboutTheTrust()
[Fact]
public async void OnGetAsync_should_return_not_found_result_if_trust_is_not_found()
{
_mockTrustProvider.Setup(s => s.GetTrustByGroupUidAsync("1111").Result)
_mockTrustProvider.Setup(s => s.GetTrustByUidAsync("1111").Result)
.Returns((Trust?)null);

_sut.Uid = "1111";
Expand All @@ -57,7 +57,7 @@ public async void OnGetAsync_should_return_not_found_result_if_trust_is_not_foun
}

[Fact]
public async void OnGetAsync_should_return_not_found_result_if_GroupUid_is_not_provided()
public async void OnGetAsync_should_return_not_found_result_if_Uid_is_not_provided()
{
var result = await _sut.OnGetAsync();
result.Should().BeOfType<NotFoundResult>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public DetailsModelTests()
}

[Fact]
public async void OnGetAsync_should_fetch_a_trust_by_groupUid()
public async void OnGetAsync_should_fetch_a_trust_by_uid()
{
_mockTrustProvider.Setup(s => s.GetTrustByGroupUidAsync("1234").Result)
_mockTrustProvider.Setup(s => s.GetTrustByUidAsync("1234").Result)
.Returns(new Trust("test", "test", "test", "Multi-academy trust"));
_sut.Uid = "1234";

Expand Down Expand Up @@ -48,7 +48,7 @@ public void PageSection_should_be_AboutTheTrust()
[Fact]
public async void OnGetAsync_should_return_not_found_result_if_trust_is_not_found()
{
_mockTrustProvider.Setup(s => s.GetTrustByGroupUidAsync("1111").Result)
_mockTrustProvider.Setup(s => s.GetTrustByUidAsync("1111").Result)
.Returns((Trust?)null);

_sut.Uid = "1111";
Expand All @@ -57,7 +57,7 @@ public async void OnGetAsync_should_return_not_found_result_if_trust_is_not_foun
}

[Fact]
public async void OnGetAsync_should_return_not_found_result_if_Ukprn_is_not_provided()
public async void OnGetAsync_should_return_not_found_result_if_Uid_is_not_provided()
{
var result = await _sut.OnGetAsync();
result.Should().BeOfType<NotFoundResult>();
Expand Down

0 comments on commit 3c803f7

Please sign in to comment.