Skip to content

Commit

Permalink
Use new sem dom api (#1735)
Browse files Browse the repository at this point in the history
* Revert "Revert "Semantic domain model/type update (#1718)" (#1732)"
  This reverts commit 65b8c4d.

* Fully implement backend API for retrieving Semantic Domain tree information to support navigation for Data Entry.
  - Regenerate Open API

* Refactor front-end to use the new API instead of asynchronously loading the Semantic Domains from .json files
  - Use a TreeNode version of the domain info during navigation
  - Fetch full semantic domains on completed tree navigation
  - Fix issue #1684

* Modify lift import to support new backend model
  - Import: Add MongoID on each SemanticDomain
  - Import test: Add check for empty MongoId and Clone MongoId when cloning SemanticDomain

Co-authored-by: D. Ror <imnasnainaec@gmail.com>
  • Loading branch information
jasonleenaylor and imnasnainaec authored Oct 17, 2022
1 parent bcd3464 commit fcd5456
Show file tree
Hide file tree
Showing 71 changed files with 1,726 additions and 66,038 deletions.
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ docs/user_guide/site

# Ignore generated JavaScript for Release info
public/scripts/release.js

# Ignore semantic domain import files (valid for mongo but not valid json)
deploy/scripts/semantic_domains/json/*.json
database/semantic_domains/*
2 changes: 1 addition & 1 deletion Backend.Tests/Controllers/AvatarControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Setup()
_permissionService = new PermissionServiceMock(_userRepo);
_avatarController = new AvatarController(_userRepo, _permissionService)
{
// Mock the Http Context because this isn't an actual call avatar controller
// Mock the Http Context because this isn't an actual call controller
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }
};
_userController = new UserController(
Expand Down
6 changes: 6 additions & 0 deletions Backend.Tests/Controllers/LiftControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ public void TestRoundtrip(RoundTripObj roundTripObj)
}
}

// Assert that the first SemanticDomain doesn't have an empty MongoId.
if (allWords[0].Senses.Count > 0 && allWords[0].Senses[0].SemanticDomains.Count > 0)
{
Assert.IsNotEmpty(allWords[0].Senses[0].SemanticDomains[0].MongoId);
}

// Export.
var exportedFilePath = _liftController.CreateLiftExport(proj1.Id).Result;
var exportedDirectory = FileOperations.ExtractZipFile(exportedFilePath, null);
Expand Down
19 changes: 2 additions & 17 deletions Backend.Tests/Controllers/ProjectControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using BackendFramework.Controllers;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using BackendFramework.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NUnit.Framework;
Expand All @@ -17,7 +16,6 @@ public class ProjectControllerTests
private IUserRepository _userRepo = null!;
private UserRoleRepositoryMock _userRoleRepo = null!;
private IPermissionService _permissionService = null!;
private ISemanticDomainService _semDomService = null!;
private ProjectController _projController = null!;

private User _jwtAuthenticatedUser = null!;
Expand All @@ -29,11 +27,9 @@ public void Setup()
_userRepo = new UserRepositoryMock();
_userRoleRepo = new UserRoleRepositoryMock();
_permissionService = new PermissionServiceMock(_userRepo);
_semDomService = new SemanticDomainService();
_projController = new ProjectController(_projRepo, _semDomService, _userRoleRepo,
_userRepo, _permissionService)
_projController = new ProjectController(_projRepo, _userRoleRepo, _userRepo, _permissionService)
{
// Mock the Http Context because this isn't an actual call avatar controller
// Mock the Http Context because this isn't an actual call controller
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }
};

Expand Down Expand Up @@ -115,17 +111,6 @@ public void TestDeleteAllProjects()
Assert.That(_projRepo.GetAllProjects().Result, Has.Count.EqualTo(0));
}

[Test]
public void TestParseSemanticDomains()
{
var project = _projRepo.Create(Util.RandomProject()).Result;
var sdList = (List<SemanticDomainWithSubdomains>)(
(ObjectResult)_projController.GetSemDoms(project!.Id).Result).Value!;
Assert.That(sdList, Has.Count.EqualTo(3));
Assert.That(sdList[0].Subdomains, Has.Count.EqualTo(3));
Assert.That(sdList[0].Subdomains[0].Subdomains, Has.Count.EqualTo(3));
}

[Test]
public void TestProjectDuplicateCheck()
{
Expand Down
90 changes: 90 additions & 0 deletions Backend.Tests/Controllers/SemanticDomainControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Backend.Tests.Mocks;
using BackendFramework.Controllers;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.AspNetCore.Mvc;
using NUnit.Framework;

namespace Backend.Tests.Controllers
{
public class SemanticDomainControllerTests
{
private ISemanticDomainRepository _semDomRepository = null!;
private SemanticDomainController _semDomController = null!;

private const string Id = "1";
private const string Lang = "en";
private const string Name = "Universe";

private readonly SemanticDomainFull _semDom = new() { Id = Id, Lang = Lang, Name = Name };

[SetUp]
public void Setup()
{
_semDomRepository = new SemanticDomainRepositoryMock();
_semDomController = new SemanticDomainController(_semDomRepository);
}

[Test]
public void SemanticDomainController_GetSemanticDomainFull_DomainFound()
{
((SemanticDomainRepositoryMock)_semDomRepository).SetNextResponse(_semDom);
var domain = (SemanticDomainFull?)(
(ObjectResult)_semDomController.GetSemanticDomainFull(Id, Lang).Result).Value;
Assert.That(domain?.Id, Is.EqualTo(Id));
Assert.That(domain?.Lang, Is.EqualTo(Lang));
Assert.That(domain?.Name, Is.EqualTo(Name));
}

[Test]
public void SemanticDomainController_GetSemanticDomainFull_DomainNotFound()
{
((SemanticDomainRepositoryMock)_semDomRepository).SetNextResponse(null);
var domain = (SemanticDomainFull?)(
(ObjectResult)_semDomController.GetSemanticDomainFull(Id, Lang).Result).Value;
Assert.That(domain, Is.Null);
}

[Test]
public void SemanticDomainController_GetSemanticDomainTreeNode_DomainFound()
{
var treeNode = new SemanticDomainTreeNode(_semDom);
((SemanticDomainRepositoryMock)_semDomRepository).SetNextResponse(treeNode);
var domain = (SemanticDomainTreeNode?)(
(ObjectResult)_semDomController.GetSemanticDomainTreeNode(Id, Lang).Result).Value;
Assert.That(domain?.Id, Is.EqualTo(Id));
Assert.That(domain?.Lang, Is.EqualTo(Lang));
Assert.That(domain?.Name, Is.EqualTo(Name));
}

[Test]
public void SemanticDomainController_GetSemanticDomainTreeNode_DomainNotFound()
{
((SemanticDomainRepositoryMock)_semDomRepository).SetNextResponse(null);
var domain = (SemanticDomainTreeNode?)(
(ObjectResult)_semDomController.GetSemanticDomainTreeNode(Id, Lang).Result).Value;
Assert.That(domain, Is.Null);
}

[Test]
public void SemanticDomainController_GetSemanticDomainTreeNodeByName_DomainFound()
{
var treeNode = new SemanticDomainTreeNode(_semDom);
((SemanticDomainRepositoryMock)_semDomRepository).SetNextResponse(treeNode);
var domain = (SemanticDomainTreeNode?)(
(ObjectResult)_semDomController.GetSemanticDomainTreeNodeByName(Name, Lang).Result).Value;
Assert.That(domain?.Id, Is.EqualTo(Id));
Assert.That(domain?.Lang, Is.EqualTo(Lang));
Assert.That(domain?.Name, Is.EqualTo(Name));
}

[Test]
public void SemanticDomainController_GetSemanticDomainTreeNodeByName_DomainNotFound()
{
((SemanticDomainRepositoryMock)_semDomRepository).SetNextResponse(null);
var domain = (SemanticDomainTreeNode?)(
(ObjectResult)_semDomController.GetSemanticDomainTreeNodeByName(Name, Lang).Result).Value;
Assert.That(domain, Is.Null);
}
}
}
31 changes: 31 additions & 0 deletions Backend.Tests/Mocks/SemanticDomainRepositoryMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading.Tasks;
using BackendFramework.Interfaces;
using BackendFramework.Models;

namespace Backend.Tests.Mocks
{
public class SemanticDomainRepositoryMock : ISemanticDomainRepository
{
private object? _responseObj;

public Task<SemanticDomainFull?> GetSemanticDomainFull(string id, string lang)
{
return Task.FromResult((SemanticDomainFull?)_responseObj);
}

public Task<SemanticDomainTreeNode?> GetSemanticDomainTreeNode(string id, string lang)
{
return Task.FromResult((SemanticDomainTreeNode?)_responseObj);
}

public Task<SemanticDomainTreeNode?> GetSemanticDomainTreeNodeByName(string name, string lang)
{
return Task.FromResult((SemanticDomainTreeNode?)_responseObj);
}

internal void SetNextResponse(object? response)
{
_responseObj = response;
}
}
}
2 changes: 1 addition & 1 deletion Backend.Tests/Models/ProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void TestClone()
{
var system = new WritingSystem { Name = "WritingSystemName", Bcp47 = "en", Font = "calibri" };
var project = new Project { Name = "ProjectName", VernacularWritingSystem = system };
var domain = new SemanticDomain { Name = "SemanticDomainName", Id = "1", Description = "text" };
var domain = new SemanticDomain { Name = "SemanticDomainName", Id = "1" };
project.SemanticDomains.Add(domain);

var customField = new CustomField { Name = "CustomFieldName", Type = "type" };
Expand Down
88 changes: 88 additions & 0 deletions Backend.Tests/Models/SemanticDomainTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using BackendFramework.Models;
using NUnit.Framework;

namespace Backend.Tests.Models
{
public class SemanticDomainTests
{
private const string Name = "Home";

[Test]
public void TestEquals()
{
var domain = new SemanticDomain { Name = Name };
Assert.That(domain.Equals(new SemanticDomain { Name = Name }));
}

[Test]
public void TestEqualsNull()
{
var domain = new SemanticDomain { Name = Name };
Assert.IsFalse(domain.Equals(null));
}

[Test]
public void TestHashCode()
{
Assert.AreNotEqual(
new SemanticDomain { Id = "1" }.GetHashCode(),
new SemanticDomain { Id = "2" }.GetHashCode()
);

Assert.AreNotEqual(
new SemanticDomain { Name = "1" }.GetHashCode(),
new SemanticDomain { Name = "2" }.GetHashCode()
);

Assert.AreNotEqual(
new SemanticDomain { Guid = Guid.NewGuid().ToString() }.GetHashCode(),
new SemanticDomain { Name = Guid.NewGuid().ToString() }.GetHashCode()
);
}
}

public class SemanticDomainFullTests
{
private const string Name = "Home";

[Test]
public void TestEquals()
{
var domain = new SemanticDomainFull { Name = Name };
Assert.That(domain.Equals(new SemanticDomainFull { Name = Name }));
}

[Test]
public void TestEqualsNull()
{
var domain = new SemanticDomainFull { Name = Name };
Assert.IsFalse(domain.Equals(null));
}

[Test]
public void TestHashCode()
{
Assert.AreNotEqual(
new SemanticDomainFull { Id = "1" }.GetHashCode(),
new SemanticDomainFull { Id = "2" }.GetHashCode()
);

Assert.AreNotEqual(
new SemanticDomainFull { Name = "1" }.GetHashCode(),
new SemanticDomainFull { Name = "2" }.GetHashCode()
);

Assert.AreNotEqual(
new SemanticDomainFull { Description = "1" }.GetHashCode(),
new SemanticDomainFull { Description = "2" }.GetHashCode()
);

Assert.AreNotEqual(
new SemanticDomainFull { Questions = new List<string> { "1" } }.GetHashCode(),
new SemanticDomainFull { Questions = new List<string> { "2" } }.GetHashCode()
);
}
}
}
19 changes: 0 additions & 19 deletions Backend.Tests/Models/WordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,23 +433,4 @@ public void TestHashCode()
);
}
}

public class SemanticDomainTests
{
private const string Name = "Home";

[Test]
public void TestEquals()
{
var domain = new SemanticDomain { Name = Name };
Assert.That(domain.Equals(new SemanticDomain { Name = Name }));
}

[Test]
public void TestEqualsNull()
{
var domain = new SemanticDomain { Name = Name };
Assert.IsFalse(domain.Equals(null));
}
}
}
1 change: 0 additions & 1 deletion Backend.Tests/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public static SemanticDomain RandomSemanticDomain(string? id = null)
{
Name = RandString(),
Id = id ?? RandString(),
Description = RandString()
};
}

Expand Down
23 changes: 23 additions & 0 deletions Backend/Contexts/SemanticDomainContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class SemanticDomainContext : ISemanticDomainContext
{
private readonly IMongoDatabase _db;

public SemanticDomainContext(IOptions<Startup.Settings> options)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
}

public IMongoCollection<SemanticDomainTreeNode> SemanticDomains => _db.GetCollection<SemanticDomainTreeNode>("SemanticDomainTree");
public IMongoCollection<SemanticDomainFull> FullSemanticDomains => _db.GetCollection<SemanticDomainFull>("SemanticDomains");
}
}
Loading

0 comments on commit fcd5456

Please sign in to comment.