Skip to content

Commit

Permalink
fix: update unit tests
Browse files Browse the repository at this point in the history
chore: Linted code for plan-technology-for-your-school.sln solution

Revert "chore: Linted code for plan-technology-for-your-school.sln solution"

This reverts commit b35cdc9.

revert last changes

fix: merge issue

chore: remove removed var

chore: Linted code for plan-technology-for-your-school.sln solution

chore: revert tests

chore: remove unused using
  • Loading branch information
jimwashbrook committed Oct 9, 2024
1 parent ca470ca commit 6cb77d9
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ private IQueryable<ButtonWithEntryReferenceDbEntity> ButtonWithEntryReferencesQu
{
var buttonWithEntryReferences = db.ButtonWithEntryReferences.Where(button => button.ContentPages.Any(contentPage => contentPage.Id == page.Id));

var pageButtons = db.Pages.Where(p => buttonWithEntryReferences.Any(button => button.LinkToEntryId == p.Id))
var linkedPages = db.Pages.Where(p => buttonWithEntryReferences.Any(button => button.LinkToEntryId == p.Id))
.Select(p => new
{
p.Id,
p.Slug
});

var questionButtons = db.Questions.Where(question => buttonWithEntryReferences.Any(button => button.LinkToEntryId == page.Id))
var linkedQuestions = db.Questions.Where(question => buttonWithEntryReferences.Any(button => button.LinkToEntryId == page.Id))
.Select(q => new
{
q.Id,
Expand All @@ -75,14 +75,12 @@ private IQueryable<ButtonWithEntryReferenceDbEntity> ButtonWithEntryReferencesQu
return buttonWithEntryReferences.Select(button => new
{
button.Id,
page = pageButtons.FirstOrDefault(pageButton => pageButton.Id == button.Id),
question = questionButtons.FirstOrDefault(quesionButton => quesionButton.Id == button.Id),
page = linkedPages.FirstOrDefault(pageForButton => pageForButton.Id == button.LinkToEntryId),
question = linkedQuestions.FirstOrDefault(questionForButton => questionForButton.Id == button.LinkToEntryId),
}).Select(button => new ButtonWithEntryReferenceDbEntity
{
Id = button.Id,
LinkToEntry = button.page != null ?
new PageDbEntity() { Slug = button.page.Slug } :
new QuestionDbEntity() { Slug = button.question!.Slug }
LinkToEntry = button.page != null ? new PageDbEntity { Slug = button.page.Slug } : new QuestionDbEntity { Slug = button.question!.Slug }
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class GetPageFromDbQuery(
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching {page} from database", slug);
logger.LogError(ex, "Error fetching {page} from database", slug);
return null;
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/Dfe.PlanTech.Application/Content/Queries/GetPageQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ namespace Dfe.PlanTech.Application.Content.Queries;

public class GetPageQuery(GetPageFromContentfulQuery getPageFromContentfulQuery, GetPageFromDbQuery getPageFromDbQuery) : IGetPageQuery
{
private readonly GetPageFromDbQuery _getPageFromDbQuery = getPageFromDbQuery;
private readonly GetPageFromContentfulQuery _getPageFromContentfulQuery = getPageFromContentfulQuery;

/// <summary>
/// Fetches page from <see chref="IContentRepository"/> by slug
/// </summary>
/// <param name="slug">Slug for the Page</param>
/// <param name="cancellationToken"></param>
/// <returns>Page matching slug</returns>
public async Task<Page?> GetPageBySlug(string slug, CancellationToken cancellationToken = default)
{
var page = await _getPageFromDbQuery.GetPageBySlug(slug, cancellationToken) ??
await _getPageFromContentfulQuery.GetPageBySlug(slug, cancellationToken);
var page = await getPageFromDbQuery.GetPageBySlug(slug, cancellationToken) ?? await getPageFromContentfulQuery.GetPageBySlug(slug, cancellationToken);

return page;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
Href = pageContent.Slug
};

<govuk-button-link href="@pageContent.Slug">
class="govuk-link">
@Model.Button.Value
</govuk-button-link>
<partial name="components/ButtonWithLink" model=@buttonWithLink/>
break;
}
case Question _:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dfe.PlanTech.Domain.Content.Interfaces;
using Dfe.PlanTech.Domain.Content.Models;
using Dfe.PlanTech.Domain.Content.Models.Buttons;
using Dfe.PlanTech.Domain.Questionnaire.Models;
using Microsoft.Extensions.Logging;
using NSubstitute;

Expand All @@ -15,56 +16,60 @@ public class GetButtonWithEntryReferencesQueryTests

private readonly GetButtonWithEntryReferencesQuery _getButtonWithEntryReferencesQuery;

private readonly static PageDbEntity _pageWithButton = new()
private static readonly PageDbEntity _pageWithButton = new()
{
Id = "Page-id",
Content = new()
{

}
Content = []
};


private readonly static PageDbEntity _pageWithoutButton = new()
private static readonly PageDbEntity _pageWithoutButton = new()
{
Id = "Page-id",
Content = new()
{

}
Content = []
};

private readonly static PageDbEntity _linkedPage = new()
private static readonly PageDbEntity _linkedPage = new()
{
Id = "linked-page",
Slug = "/linked-page-slug"
};

private readonly static ButtonWithEntryReferenceDbEntity _button = new()
private readonly ButtonWithEntryReferenceDbEntity _button = new()
{
Id = "ABCD",
LinkToEntryId = _linkedPage.Id,
LinkToEntry = _linkedPage,
ContentPages = new(){
_pageWithButton
}
ContentPages = [_pageWithButton]
};

private readonly List<ButtonWithEntryReferenceDbEntity> _buttons;

private readonly List<ButtonWithEntryReferenceDbEntity> _returnedButtons = new();
private readonly List<ButtonWithEntryReferenceDbEntity> _returnedButtons = [];
private readonly List<PageDbEntity> _pages = [_pageWithButton, _linkedPage, _pageWithoutButton];
private readonly List<QuestionDbEntity> _questions = [];

public GetButtonWithEntryReferencesQueryTests()
{
_getButtonWithEntryReferencesQuery = new GetButtonWithEntryReferencesQuery(_db, _logger);

_buttons = new()
{
_button
};
List<ButtonWithEntryReferenceDbEntity> buttons = [_button];

_pageWithButton.Content.Add(_button);
_db.ButtonWithEntryReferences.Returns(_buttons.AsQueryable());
_db.ButtonWithEntryReferences.Returns(buttons.AsQueryable());

_db.Pages.Returns(_pages.AsQueryable());
_db.Questions.Returns(_questions.AsQueryable());
_db.ToListAsync(Arg.Any<IQueryable<PageDbEntity>>(), Arg.Any<CancellationToken>()).Returns(callinfo =>
{
var queryable = callinfo.ArgAt<IQueryable<PageDbEntity>>(0);

return queryable.ToList();
});

_db.ToListAsync(Arg.Any<IQueryable<QuestionDbEntity>>(), Arg.Any<CancellationToken>()).Returns(callinfo =>
{
var queryable = callinfo.ArgAt<IQueryable<QuestionDbEntity>>(0);

return queryable.ToList();
});

_db.ToListAsync(Arg.Any<IQueryable<ButtonWithEntryReferenceDbEntity>>(), Arg.Any<CancellationToken>())
.Returns(callinfo =>
Expand All @@ -84,8 +89,7 @@ public async Task Should_Retrieve_ButtonWithEntryReferences_For_Page_When_Existi
{
await _getButtonWithEntryReferencesQuery.TryLoadChildren(_pageWithButton, CancellationToken.None);

await _db.ReceivedWithAnyArgs(1)
.ToListAsync(Arg.Any<IQueryable<ButtonWithEntryReferenceDbEntity>>(), Arg.Any<CancellationToken>());
await _db.ReceivedWithAnyArgs(1).ToListAsync(Arg.Any<IQueryable<ButtonWithEntryReferenceDbEntity>>(), Arg.Any<CancellationToken>());

Assert.Single(_returnedButtons);

Expand All @@ -110,7 +114,6 @@ public async Task Should_Not_Retrieve_ButtonWithEntryReferences_For_Page_When_No
{
await _getButtonWithEntryReferencesQuery.TryLoadChildren(_pageWithoutButton, CancellationToken.None);

await _db.ReceivedWithAnyArgs(0)
.ToListAsync(Arg.Any<IQueryable<ButtonWithEntryReferenceDbEntity>>(), Arg.Any<CancellationToken>());
await _db.ReceivedWithAnyArgs(0).ToListAsync(Arg.Any<IQueryable<ButtonWithEntryReferenceDbEntity>>(), Arg.Any<CancellationToken>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public GetPageQueryTests()

var queries = options.Queries;

var slugQuery = queries?.OfType<ContentQueryEquals>().Where(query => query.Field == "fields.slug").FirstOrDefault();
var slugQuery = (queries?.OfType<ContentQueryEquals>()).FirstOrDefault(query => query.Field == "fields.slug");
if (slugQuery != null && slugQuery.Value.Equals(_page.Slug))
{
pages.Add(_page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public class GetRichTextsQueryTests

private readonly GetRichTextsForPageQuery _getRichTextsQuery;

private readonly static PageDbEntity _loadedPage = new()
private static readonly PageDbEntity _loadedPage = new()
{
Id = "Page-id",
Content = new()
Content = []
};

private readonly static List<RichTextContentWithSlugDbEntity> _richTextContentsWithSlug = new()
{
private static readonly List<RichTextContentWithSlugDbEntity> _richTextContentsWithSlug =
[
new()
{
Data = new()
Expand All @@ -39,6 +39,7 @@ public class GetRichTextsQueryTests
Value = "rich-text",
Id = 1,
},

new()
{
Data = new()
Expand All @@ -56,9 +57,9 @@ public class GetRichTextsQueryTests
Value = "rich-text",
Id = 2
}
};
];

private readonly List<RichTextContentDbEntity> _returnedRichTextContents = new();
private readonly List<RichTextContentDbEntity> _returnedRichTextContents = [];

private readonly ContentfulOptions _contentfulOptions = new(false);

Expand All @@ -75,22 +76,32 @@ public GetRichTextsQueryTests()
{
var queryable = callinfo.ArgAt<IQueryable<RichTextContentDbEntity>>(0);

return queryable.ToList();
});

_db.ToListAsync(Arg.Any<IQueryable<RichTextContentWithSlugDbEntity>>(), Arg.Any<CancellationToken>())
.Returns(callinfo =>
{
var queryable = callinfo.ArgAt<IQueryable<RichTextContentWithSlugDbEntity>>(0);

return queryable.ToList();
});
}

[Fact]
public async Task Should_Retrieve_RichTextContents_When_Matching()
{
var content = _richTextContentsWithSlug.First();

_loadedPage.Content.Add(new TextBodyDbEntity()
{
RichText = _richTextContentsWithSlug.First()
RichText = _richTextContentsWithSlug.First(),
RichTextId = content.Id
});

await _getRichTextsQuery.TryLoadChildren(_loadedPage, CancellationToken.None);

await _db.ReceivedWithAnyArgs(1)
.ToListAsync(Arg.Any<IQueryable<RichTextContentWithSlugDbEntity>>(), Arg.Any<CancellationToken>());
await _db.ReceivedWithAnyArgs(1).ToListAsync(Arg.Any<IQueryable<RichTextContentWithSlugDbEntity>>(), Arg.Any<CancellationToken>());
}

[Fact]
Expand All @@ -109,9 +120,11 @@ public async Task Should_Only_Retrieve_Draft_RichTextContents_When_UsePreviewEna
{
var richTextQuery = new GetRichTextsForPageQuery(_db, _logger, new ContentfulOptions(usePreview));

var content = _richTextContentsWithSlug.First();
_loadedPage.Content.Add(new TextBodyDbEntity()
{
RichText = _richTextContentsWithSlug.First(),
RichText = content,
RichTextId = content.Id,
Published = false
});

Expand Down

0 comments on commit 6cb77d9

Please sign in to comment.