Skip to content
Open
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
3 changes: 2 additions & 1 deletion VueApp/src/CTS/pages/ManageMilestones.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ async function load() {
])

//filter competencies to just those without a milestone already defined
const milestoneCompetencyIds = new Set(milestones.value.map((m) => m.competencyId))
competencies.value = competencies.value.filter(
(c) => !milestones.value.some((m) => m.competencyId === c.competencyId),
(c) => c.competencyId === null || !milestoneCompetencyIds.has(c.competencyId),
)

loaded.value = true
Expand Down
3 changes: 2 additions & 1 deletion VueApp/src/Effort/pages/ScheduledCliWeeks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ const tableRows = computed<CliWeeksRow[]>(() => {
if (!report.value) return []
return report.value.instructors.map((inst) => {
const row: CliWeeksRow = { mothraId: inst.mothraId, instructor: inst.instructor }
const termsByName = new Map(inst.terms.map((t) => [t.termName, t]))
for (const termName of report.value!.termNames) {
const term = inst.terms.find((t) => t.termName === termName)
const term = termsByName.get(termName)
if (term) {
row[termName] = Object.entries(term.weeksByService)
.filter(([, weeks]) => weeks > 0)
Expand Down
3 changes: 2 additions & 1 deletion VueApp/src/Students/pages/StudentClassYear.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ async function getStudents() {
//get problems
u = apiUrl + "students/dvm/classYearReport?classYear=" + classYear.value.value
const problems = await get(u)
const studentsByPersonId = new Map(allStudents.value.map((std) => [std.personId, std]))
problems.result.forEach((p: any) => {
var s = allStudents.value.find((std) => std.personId === p.personId)
const s = studentsByPersonId.get(p.personId)
if (s !== undefined) {
s.problems = p.problems
}
Expand Down
134 changes: 134 additions & 0 deletions test/CMS/CMSLinkCollectionLinksTest.cs
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);
}
}
}
79 changes: 79 additions & 0 deletions test/CTS/CompetencyControllerTest.cs
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);
}
}
}
39 changes: 27 additions & 12 deletions web/Areas/CMS/Controllers/CMSLinkCollectionLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ public CMSLinkCollectionLinks(VIPERContext context)
[HttpGet("{collectionId}/links")]
public async Task<ActionResult<IEnumerable<LinkDto>>> GetLinks(int collectionId, string? groupByTagCategory = null)
{
if (await _context.LinkCollections.FirstOrDefaultAsync(lc => lc.LinkCollectionId == collectionId) == null)
if (await _context.LinkCollections.AsNoTracking().FirstOrDefaultAsync(lc => lc.LinkCollectionId == collectionId) == null)
{
return NotFound();
}

var links = await _context.Links
.AsNoTracking()
Comment thread
rlorenzo marked this conversation as resolved.
.Include(l => l.LinkTags.OrderBy(lt => lt.LinkCollectionTagCategory.SortOrder).ThenBy(lt => lt.SortOrder).ThenBy(lt => lt.Value))
.ThenInclude(lt => lt.LinkCollectionTagCategory)
.Where(l => l.LinkCollectionId == collectionId)
Expand All @@ -50,11 +51,12 @@ public async Task<ActionResult<IEnumerable<LinkDto>>> GetLinks(int collectionId,
Value = lt.Value,
CategoryName = lt.LinkCollectionTagCategory.LinkCollectionTagCategoryName
}).OrderBy(lt => lt.Value).ToList()
});
}).ToList();

if (!string.IsNullOrEmpty(groupByTagCategory))
{
var tagCategories = await _context.LinkCollectionTagCategories
.AsNoTracking()
.Where(tc => tc.LinkCollectionId == collectionId)
.Where(tc => tc.LinkCollectionTagCategoryName.ToLower() == groupByTagCategory.ToLower())
.FirstOrDefaultAsync();
Expand All @@ -64,20 +66,33 @@ public async Task<ActionResult<IEnumerable<LinkDto>>> GetLinks(int collectionId,
}

var allValues = await _context.LinkTags
.AsNoTracking()
.Where(lt => lt.LinkCollectionTagCategoryId == tagCategories.LinkCollectionTagCategoryId)
Comment thread
rlorenzo marked this conversation as resolved.
.Select(lt => lt.Value)
.Where(lt => lt.Value != null)
.Select(lt => lt.Value!)
Comment thread
rlorenzo marked this conversation as resolved.
.Distinct()
.ToListAsync();

var orderedGroupedResult = new List<LinkDto>();
var added = new HashSet<int>();
foreach (var r in allValues
.SelectMany(v => result
.Where(r => r.LinkTags.Any(lt => lt.Value == v && lt.LinkCollectionTagCategoryId == tagCategories.LinkCollectionTagCategoryId) && !added.Contains(r.LinkId))))
{
added.Add(r.LinkId);
orderedGroupedResult.Add(r);
}
var linksByTagValue = result
.SelectMany(r => r.LinkTags
.Where(lt => lt.LinkCollectionTagCategoryId == tagCategories.LinkCollectionTagCategoryId
&& lt.Value != null)
.Select(lt => new { Value = lt.Value!, Link = r }))
.GroupBy(x => x.Value)
.ToDictionary(g => g.Key, g => g.Select(x => x.Link).ToList());

// Group links by tag value in the order the values appear in allValues, then
// append links whose only value in this category is null (uncategorized) so the
// dictionary path, which cannot key on null, does not drop them. DistinctBy keeps
// each link at its first (grouped) position.
var uncategorizedLinks = result.Where(r => r.LinkTags
.Any(lt => lt.LinkCollectionTagCategoryId == tagCategories.LinkCollectionTagCategoryId && lt.Value == null));

var orderedGroupedResult = allValues
.SelectMany(v => linksByTagValue.GetValueOrDefault(v) ?? Enumerable.Empty<LinkDto>())
.Concat(uncategorizedLinks)
.DistinctBy(r => r.LinkId)
.ToList();

return Ok(orderedGroupedResult);
}
Expand Down
10 changes: 4 additions & 6 deletions web/Areas/CTS/Controllers/CompetencyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,23 @@ public async Task<ActionResult<List<CompetencyDto>>> GetChildren(int competencyI
public async Task<ActionResult<List<CompetencyHierarchyDto>>> GetCompetencyHierarchy()
{
var comps = await context.Competencies
.AsNoTracking()
.Include(c => c.Domain)
.OrderBy(c => c.Domain.Order)
.ThenBy(c => c.Order)
.ToListAsync();
var compHierarchy = new List<CompetencyHierarchyDto>();
var allCompDtos = CtsMapper.ToCompetencyHierarchyDtos(comps);
var compsById = allCompDtos.ToDictionary(c => c.CompetencyId);
foreach (var comp in allCompDtos)
{
if (comp.ParentId == null)
{
compHierarchy.Add(comp);
}
else
else if (compsById.TryGetValue(comp.ParentId.Value, out var parent))
{
var parent = allCompDtos.FirstOrDefault(c => c.CompetencyId == comp.ParentId);
if (parent != null)
{
parent.Children.Add(comp);
}
parent.Children.Add(comp);
}
}
return compHierarchy;
Expand Down
8 changes: 4 additions & 4 deletions web/Classes/SQLContext/VIPERContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public VIPERContext(DbContextOptions<VIPERContext> options)

public virtual DbSet<WorkflowStageVersion> WorkflowStageVersions { get; set; }

public DbSet<LinkCollection> LinkCollections { get; set; }
public DbSet<LinkCollectionTagCategory> LinkCollectionTagCategories { get; set; }
public DbSet<Link> Links { get; set; }
public DbSet<LinkTag> LinkTags { get; set; }
public virtual DbSet<LinkCollection> LinkCollections { get; set; }
public virtual DbSet<LinkCollectionTagCategory> LinkCollectionTagCategories { get; set; }
public virtual DbSet<Link> Links { get; set; }
public virtual DbSet<LinkTag> LinkTags { get; set; }

/* users */
public virtual DbSet<Person> People { get; set; }
Expand Down
Loading