public void MergeWordsMultiChildTest() { // Build a mergeWords with a parent with 3 children. var mergeWords = new MergeWords { Parent = Util.RandomWord(ProjId) }; const int numberOfChildren = 3; foreach (var _ in Enumerable.Range(0, numberOfChildren)) { var child = Util.RandomWord(ProjId); var id = _wordRepo.Create(child).Result.Id; Assert.IsNotNull(_wordRepo.GetWord(ProjId, id).Result); mergeWords.Children.Add(new MergeSourceWord { SrcWordId = id }); } Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(numberOfChildren)); var mergeWordsList = new List <MergeWords> { mergeWords }; var newWords = _mergeService.Merge(ProjId, mergeWordsList).Result; // Check for correct history length; var dbParent = newWords.First(); Assert.That(dbParent.History, Has.Count.EqualTo(numberOfChildren)); // Confirm that parent added to repo and children not in frontier. Assert.IsNotNull(_wordRepo.GetWord(ProjId, dbParent.Id).Result); Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(1)); }
/// <summary> Prepares a merge parent to be added to the database. </summary> /// <returns> Word to add. </returns> private async Task <Word> MergePrepParent(string projectId, MergeWords mergeWords) { var parent = mergeWords.Parent.Clone(); parent.ProjectId = projectId; parent.History = new List <string>(); // Add child to history. foreach (var childSource in mergeWords.Children) { parent.History.Add(childSource.SrcWordId); if (childSource.GetAudio) { var child = await _wordRepo.GetWord(projectId, childSource.SrcWordId); if (child is null) { throw new KeyNotFoundException($"Unable to locate word: ${childSource.SrcWordId}"); } parent.Audio.AddRange(child.Audio); } } // Remove duplicates. parent.Audio = parent.Audio.Distinct().ToList(); parent.History = parent.History.Distinct().ToList(); // Clear fields to be automatically regenerated. parent.Id = ""; parent.Modified = ""; return(parent); }
public void MergeWordsOneChildTest() { var thisWord = Util.RandomWord(ProjId); thisWord = _wordRepo.Create(thisWord).Result; var mergeObject = new MergeWords { Parent = thisWord, Children = new List <MergeSourceWord> { new MergeSourceWord { SrcWordId = thisWord.Id } } }; var newWords = _mergeService.Merge(ProjId, new List <MergeWords> { mergeObject }).Result; // There should only be 1 word added and it should be identical to what we passed in Assert.That(newWords, Has.Count.EqualTo(1)); Assert.That(newWords.First().ContentEquals(thisWord)); // Check that the only word in the frontier is the new word var frontier = _wordRepo.GetFrontier(ProjId).Result; Assert.That(frontier, Has.Count.EqualTo(1)); Assert.That(frontier.First(), Is.EqualTo(newWords.First())); // Check that new word has the right history Assert.That(newWords.First().History, Has.Count.EqualTo(1)); Assert.That(newWords.First().History.First(), Is.EqualTo(thisWord.Id)); }
public async Task <IActionResult> Put(string projectId, [FromBody] MergeWords mergeWords) { if (!_permissionService.HasProjectPermission(Permission.MergeAndCharSet, HttpContext)) { return(new ForbidResult()); } // Ensure project exists var proj = _projectService.GetProject(projectId); if (proj == null) { return(new NotFoundObjectResult(projectId)); } // Ensure MergeWords is alright if (mergeWords == null || mergeWords.Parent == null) { return(new BadRequestResult()); } try { var newWordList = await _wordService.Merge(projectId, mergeWords); return(new ObjectResult(newWordList.Select(i => i.Id).ToList())); } catch (Exception) { return(new BadRequestResult()); } }
public async Task <IActionResult> Put([FromBody] MergeWords mergeVals) { try { var parent = mergeVals.parent; List <string> children = mergeVals.children; state changes = mergeVals.mergeType; foreach (string child in children) { var childWord = await _wordService.GetWord(child); var parentWord = await _wordService.GetWord(parent); //create duplicate nodes Word newChild = childWord; Word newParent = parentWord; newChild.Id = null; newParent.Id = null; //set as deleted newChild.Accessability = state.deleted; //add to database to set ID await _wordService.Create(newChild); //add child to history of new child newChild.History.Add(childWord.Id); //connect parent to child newParent.History.Add(newChild.Id); //add newparent to collection await _wordService.Create(newParent); //upadate fronteir await _wordService.DeleteFrontier(childWord.Id); await _wordService.DeleteFrontier(parentWord.Id); await _wordService.AddFrontier(newParent); } } catch (Exception) { return(new NotFoundResult()); } return(new OkResult()); }
public void MergeWords() { // The parent word is inherently correct as it is calculated by the frontend as the desired result of the // merge var parentChildMergeObject = new MergeWords { Parent = RandomWord(), Time = Util.RandString(), ChildrenWords = new List <MergeSourceWord>() }; // Set the child info var childWords = new List <Word> { RandomWord(), RandomWord(), RandomWord() }; foreach (var child in childWords) { // Generate mergeSourceWord with new child Id and desired child state list var newGenChild = new MergeSourceWord { SrcWordId = _repo.Add(child).Result.Id, SenseStates = new List <State> { State.Duplicate, State.Sense, State.Separate } }; parentChildMergeObject.ChildrenWords.Add(newGenChild); } var newWordList = _wordService.Merge(_projId, parentChildMergeObject).Result; // Check for parent is in the db var dbParent = newWordList.FirstOrDefault(); Assert.IsNotNull(dbParent); Assert.AreEqual(dbParent.Senses.Count, 3); Assert.AreEqual(dbParent.History.Count, 3); // Check the separarte words were made Assert.AreEqual(newWordList.Count, 4); foreach (var word in newWordList) { Assert.Contains(_repo.GetWord(_projId, word.Id).Result, _repo.GetAllWords(_projId).Result); } }
public void MergeWordsIdentity() { var thisWord = RandomWord(); thisWord = _repo.Create(thisWord).Result; var mergeObject = new MergeWords { Parent = thisWord, ChildrenWords = new List <MergeSourceWord> { new MergeSourceWord { SrcWordId = thisWord.Id, SenseStates = new List <State> { State.Sense, State.Sense, State.Sense } } } }; var newWords = _wordService.Merge(_projId, mergeObject).Result; // There should only be 1 word added and it should be identical to what we passed in Assert.That(newWords, Has.Count.EqualTo(1)); Assert.IsTrue(newWords.First().ContentEquals(thisWord)); // Check that the only word in the frontier is the new word var frontier = _repo.GetFrontier(_projId).Result; Assert.That(frontier, Has.Count.EqualTo(1)); Assert.AreEqual(frontier.First(), newWords.First()); // Check that new word has the right history Assert.That(newWords.First().History, Has.Count.EqualTo(1)); var intermediateWord = _repo.GetWord(_projId, newWords.First().History.First()).Result; Assert.That(intermediateWord.History, Has.Count.EqualTo(1)); Assert.AreEqual(intermediateWord.History.First(), thisWord.Id); }
public void MergeWordsMultipleTest() { var mergeWordsA = new MergeWords { Parent = Util.RandomWord(ProjId) }; var mergeWordsB = new MergeWords { Parent = Util.RandomWord(ProjId) }; var mergeWordsList = new List <MergeWords> { mergeWordsA, mergeWordsB }; var newWords = _mergeService.Merge(ProjId, mergeWordsList).Result; Assert.That(newWords, Has.Count.EqualTo(2)); Assert.AreNotEqual(newWords.First().Id, newWords.Last().Id); var frontier = _wordRepo.GetFrontier(ProjId).Result; Assert.That(frontier, Has.Count.EqualTo(2)); Assert.AreNotEqual(frontier.First().Id, frontier.Last().Id); Assert.Contains(frontier.First(), newWords); Assert.Contains(frontier.Last(), newWords); }
/// <summary> Makes a parent from merging other words and some number of separate words </summary> /// <returns> List of words added: Parent first, followed by separate words in order added </returns> public async Task <List <Word> > Merge(string projectId, MergeWords mergeWords) { var newWordsList = new List <Word>(); var addParent = mergeWords.Parent.Clone(); addParent.History = new List <string>(); // Generate new child words form child word field foreach (var newChildWordState in mergeWords.ChildrenWords) { // Get child word var currentChildWord = await _repo.GetWord(projectId, newChildWordState.SrcWordId); // Remove child from frontier await _repo.DeleteFrontier(projectId, currentChildWord.Id); // Iterate through senses of that word and change to corresponding state in mergewords if (currentChildWord.Senses.Count != newChildWordState.SenseStates.Count) { throw new FormatException("Sense counts don't match"); } for (var i = 0; i < currentChildWord.Senses.Count; i++) { currentChildWord.Senses[i].Accessibility = (int)newChildWordState.SenseStates[i]; } // Change the child word's history to its previous self currentChildWord.History = new List <string>() { newChildWordState.SrcWordId }; // Add child word to the database currentChildWord.Id = ""; var newChildWord = await _repo.Add(currentChildWord); // Handle different states var separateWord = currentChildWord.Clone(); separateWord.Senses = new List <Sense>(); separateWord.Id = ""; for (var i = 0; i < currentChildWord.Senses.Count; i++) { switch (newChildWordState.SenseStates[i]) { // Add the word to the parent's history case State.Sense: case State.Duplicate: if (!addParent.History.Contains(currentChildWord.Id)) { addParent.History.Add(currentChildWord.Id); } break; // Add the sense to a separate word and the word to its history case State.Separate: currentChildWord.Senses[i].Accessibility = (int)State.Active; separateWord.Senses.Add(currentChildWord.Senses[i]); if (!separateWord.History.Contains(currentChildWord.Id)) { separateWord.History.Add(currentChildWord.Id); } break; default: throw new NotSupportedException(); } } // Add a new word to the database with all of the senses with separate tags from this word if (separateWord.Senses.Count != 0) { separateWord.ProjectId = projectId; var newSeparate = await _repo.Create(separateWord); newWordsList.Add(newSeparate); } } // Add parent with child history to the database addParent.ProjectId = projectId; var newParent = await _repo.Create(addParent); newWordsList.Insert(0, newParent); return(newWordsList); }
/// <summary> Deletes all the merge children from the frontier. </summary> /// <returns> Number of words deleted. </returns> private async Task <long> MergeDeleteChildren(string projectId, MergeWords mergeWords) { var childIds = mergeWords.Children.Select(c => c.SrcWordId).ToList(); return(await _wordRepo.DeleteFrontier(projectId, childIds)); }