-
Notifications
You must be signed in to change notification settings - Fork 0
perf: replace O(n²) lookups with Map/Dictionary indexes #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rlorenzo
wants to merge
1
commit into
main
Choose a base branch
from
fix/n2-queries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,134 @@ | ||
| using Areas.CMS.Models; | ||
| using Areas.CMS.Models.DTOs; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using MockQueryable.NSubstitute; | ||
| using NSubstitute; | ||
| using Viper.Areas.CMS.Controllers; | ||
| using Viper.Classes.SQLContext; | ||
|
|
||
| namespace Viper.test.CMS | ||
| { | ||
| /// <summary> | ||
| /// Covers the grouped-links ordering in <c>GetLinks</c>: links are grouped by | ||
| /// tag value, de-duplicated, and links whose only tag value in the category is | ||
| /// null are appended (not dropped) after the grouped links. | ||
| /// </summary> | ||
| public class CMSLinkCollectionLinksTest | ||
| { | ||
| private const int CollectionId = 1; | ||
| private const int AudienceCategoryId = 10; | ||
| private const int OtherCategoryId = 99; | ||
|
|
||
| private readonly VIPERContext _mockContext; | ||
| private readonly CMSLinkCollectionLinks _controller; | ||
|
|
||
| private static readonly LinkCollectionTagCategory _audience = new() | ||
| { | ||
| LinkCollectionTagCategoryId = AudienceCategoryId, | ||
| LinkCollectionId = CollectionId, | ||
| LinkCollectionTagCategoryName = "Audience", | ||
| SortOrder = 1, | ||
| }; | ||
| private static readonly LinkCollectionTagCategory _other = new() | ||
| { | ||
| LinkCollectionTagCategoryId = OtherCategoryId, | ||
| LinkCollectionId = CollectionId, | ||
| LinkCollectionTagCategoryName = "Other", | ||
| SortOrder = 2, | ||
| }; | ||
|
|
||
| public CMSLinkCollectionLinksTest() | ||
| { | ||
| _mockContext = Substitute.For<VIPERContext>(); | ||
| _controller = new CMSLinkCollectionLinks(_mockContext); | ||
| } | ||
|
|
||
| private static Link MakeLink(int id, int sortOrder, LinkCollectionTagCategory category, string? tagValue) | ||
| { | ||
| var link = new Link | ||
| { | ||
| LinkId = id, | ||
| LinkCollectionId = CollectionId, | ||
| Url = $"/link/{id}", | ||
| Title = $"Link {id}", | ||
| SortOrder = sortOrder, | ||
| }; | ||
| link.LinkTags.Add(new LinkTag | ||
| { | ||
| LinkTagId = id * 10, | ||
| LinkId = id, | ||
| LinkCollectionTagCategoryId = category.LinkCollectionTagCategoryId, | ||
| LinkCollectionTagCategory = category, | ||
| SortOrder = 1, | ||
| Value = tagValue, | ||
| Link = link, | ||
| }); | ||
| return link; | ||
| } | ||
|
|
||
| private void Setup(List<Link> links) | ||
| { | ||
| // BuildMockDbSet() makes its own NSubstitute calls, so materialize every | ||
| // mock set before any .Returns() or NSubstitute loses track of the last call. | ||
| var collections = new List<LinkCollection> { new() { LinkCollectionId = CollectionId } }.BuildMockDbSet(); | ||
| var categories = new List<LinkCollectionTagCategory> { _audience, _other }.BuildMockDbSet(); | ||
| var linkSet = links.BuildMockDbSet(); | ||
| var tagSet = links.SelectMany(l => l.LinkTags).ToList().BuildMockDbSet(); | ||
|
|
||
| _mockContext.LinkCollections.Returns(collections); | ||
| _mockContext.LinkCollectionTagCategories.Returns(categories); | ||
| _mockContext.Links.Returns(linkSet); | ||
| _mockContext.LinkTags.Returns(tagSet); | ||
| } | ||
|
|
||
| private static List<LinkDto> OkLinks(ActionResult<IEnumerable<LinkDto>> result) | ||
| { | ||
| var ok = Assert.IsType<OkObjectResult>(result.Result); | ||
| return Assert.IsAssignableFrom<IEnumerable<LinkDto>>(ok.Value).ToList(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetLinks_GroupedByCategory_OrdersByTagValueThenAppendsNullValuedLinks() | ||
| { | ||
| Setup(new List<Link> | ||
| { | ||
| MakeLink(1, 1, _audience, "Students"), | ||
| MakeLink(2, 2, _audience, "Faculty"), | ||
| MakeLink(3, 3, _audience, "Students"), // shares a value with link 1 | ||
| MakeLink(4, 4, _audience, null), // uncategorized -> appended last | ||
| MakeLink(5, 5, _other, "Other"), // different category -> excluded | ||
| }); | ||
|
|
||
| var dtos = OkLinks(await _controller.GetLinks(CollectionId, "Audience")); | ||
|
|
||
| // Students group (1, 3 in sort order), then Faculty (2), then null-valued (4). | ||
| // Link 5 (other category) is not part of this grouping. | ||
| Assert.Equal(new[] { 1, 3, 2, 4 }, dtos.Select(d => d.LinkId).ToArray()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetLinks_NoGrouping_ReturnsAllLinksInSortOrder() | ||
| { | ||
| Setup(new List<Link> | ||
| { | ||
| MakeLink(1, 1, _audience, "Students"), | ||
| MakeLink(2, 2, _audience, "Faculty"), | ||
| MakeLink(5, 5, _other, "Other"), | ||
| }); | ||
|
|
||
| var dtos = OkLinks(await _controller.GetLinks(CollectionId)); | ||
|
|
||
| Assert.Equal(new[] { 1, 2, 5 }, dtos.Select(d => d.LinkId).ToArray()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetLinks_UnknownCollection_ReturnsNotFound() | ||
| { | ||
| Setup(new List<Link> { MakeLink(1, 1, _audience, "Students") }); | ||
|
|
||
| var result = await _controller.GetLinks(999); | ||
|
|
||
| Assert.IsType<NotFoundResult>(result.Result); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,79 @@ | ||
| using MockQueryable.NSubstitute; | ||
| using NSubstitute; | ||
| using Viper.Areas.CTS.Controllers; | ||
| using Viper.Classes.SQLContext; | ||
| using Viper.Models.CTS; | ||
|
|
||
| namespace Viper.test.CTS | ||
| { | ||
| public class CompetencyControllerTest | ||
| { | ||
| private readonly VIPERContext _mockContext; | ||
| private readonly CompetencyController _controller; | ||
| private static readonly Domain _domain = new() { DomainId = 1, Name = "Domain", Order = 1 }; | ||
|
|
||
| public CompetencyControllerTest() | ||
| { | ||
| _mockContext = Substitute.For<VIPERContext>(); | ||
| _controller = new CompetencyController(_mockContext); | ||
| } | ||
|
|
||
| private void SetupCompetencies(params Competency[] comps) | ||
| { | ||
| // BuildMockDbSet() makes its own NSubstitute calls, so materialize it | ||
| // before .Returns() or NSubstitute loses track of the last call. | ||
| var mockDbSet = comps.ToList().BuildMockDbSet(); | ||
| _mockContext.Competencies.Returns(mockDbSet); | ||
| } | ||
|
|
||
| private static Competency Comp(int id, string number, int? parentId, int order) | ||
| => new() | ||
| { | ||
| CompetencyId = id, | ||
| Number = number, | ||
| Name = $"Competency {number}", | ||
| DomainId = _domain.DomainId, | ||
| Domain = _domain, | ||
| ParentId = parentId, | ||
| Order = order, | ||
| }; | ||
|
|
||
| [Fact] | ||
| public async Task GetCompetencyHierarchy_NestsChildrenUnderParents() | ||
| { | ||
| SetupCompetencies( | ||
| Comp(1, "1", null, 1), // root | ||
| Comp(2, "1.1", 1, 2), // child of 1 | ||
| Comp(3, "1.1.1", 2, 3), // grandchild (child of 2) | ||
| Comp(4, "2", null, 4)); // second root | ||
|
|
||
| var result = await _controller.GetCompetencyHierarchy(); | ||
|
|
||
| var roots = result.Value!; | ||
| Assert.Equal(new[] { 1, 4 }, roots.Select(r => r.CompetencyId).ToArray()); | ||
|
|
||
| var root1 = roots.Single(c => c.CompetencyId == 1); | ||
| var child = Assert.Single(root1.Children); | ||
| Assert.Equal(2, child.CompetencyId); | ||
| var grandchild = Assert.Single(child.Children); | ||
| Assert.Equal(3, grandchild.CompetencyId); | ||
|
|
||
| Assert.Empty(roots.Single(c => c.CompetencyId == 4).Children); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetCompetencyHierarchy_OrphanWithMissingParent_IsDroppedNotDuplicated() | ||
| { | ||
| SetupCompetencies( | ||
| Comp(1, "1", null, 1), // root | ||
| Comp(2, "9.9", 999, 2)); // parent 999 is not in the set | ||
|
|
||
| var result = await _controller.GetCompetencyHierarchy(); | ||
|
|
||
| var roots = result.Value!; | ||
| var root = Assert.Single(roots); | ||
| Assert.Equal(1, root.CompetencyId); | ||
| Assert.Empty(root.Children); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.