public void RemovingLink_BetweenTwoWords_WillCreateNewMasterWordForIt(bool cleanCache) { // Arrange var masterA = new MasterWord(); var wordA = new Word(masterA, "Hi", new Language("English")); var wordB = new Word(masterA, "Hola", new Language("Spanish")); MasterWordsService.Add(masterA); if (cleanCache) { RefreshServicesAndClearCache(); wordA = WordsService.GetAll().First(w => w.ID == wordA.ID); wordB = WordsService.GetAll().First(w => w.ID == wordB.ID); } // Act WordsService.RemoveTranslation(wordA, wordB); // Assert var newA = WordsService.Get(wordA.ID); var newB = WordsService.Get(wordB.ID); masterA = MasterWordsService.Get(masterA.ID); Assert.AreNotEqual(newA.MasterWord, newB.MasterWord, "Master words should differ. New Ref"); Assert.AreNotEqual(wordA.MasterWord, wordB.MasterWord, "Master words should differ. Old Ref"); Assert.AreEqual(1, masterA.Words.Count(), "MasterWord word count should be 1"); }
public void RemovingLink_BetweenTwoWords_FirstArgWillKeepTheMasterWithAllTranslations() { // Arrange var masterA = new MasterWord(); var wordInFirstArg = new Word(masterA, "Hi", new Language("English")); var translation = new Word(masterA, "Hola", new Language("Spanish")); new Word(masterA, "some word", new Language("some lang")); MasterWordsService.Add(masterA); // Act WordsService.RemoveTranslation(wordInFirstArg, translation); // Assert var newMaster = MasterWordsService.Get(masterA.ID); var newWord = WordsService.Get(wordInFirstArg.ID); var newTranslation = WordsService.Get(translation.ID); Assert.AreEqual(2, newMaster.Words.Count, "Master should have two words left"); Assert.AreEqual(newMaster, newWord.MasterWord, "Word in first argument should keep the master word"); Assert.AreEqual(1, newTranslation.MasterWord.Words.Count, "Translation master should only have 1 translation"); Assert.AreNotEqual(newTranslation.MasterWord, newWord.MasterWord, "Word in second argument should get new master created"); }
public void RemoveTranslation(Word word, Word translation) { // Words might be loaded without master word word = Get(word.ID); translation = Get(translation.ID); var oldMaster = word.MasterWord; var newMaster = new MasterWord(); // Move translation to new master word Transfer(oldMaster, newMaster, translation); // If books was specifically for language 'a' and translation is in language 'a', prefer to link newly created master word // which has translation in question, and not default one. foreach (var id in oldMaster.BookIDs.ToArray()) { var book = BooksService.Get(id); if (book.Language.ID == translation.Language.ID) { book.ReplaceWord(oldMaster, newMaster); } } using (MasterWordsService.BatchRequests()) { MasterWordsService.Update(oldMaster); MasterWordsService.Add(newMaster); } }
private static bool IsMergePossibleInternal(MasterWord masterA, MasterWord masterB) { // Check if merge is possible. If merging MasterWords will procude multiple words of same language under same master, it's invalid var set = masterA.Words.ToHashSet(new LanguageEqualityComparer()); set.AddRange(masterB.Words); return(!(set.Count() < masterA.Words.Count() + masterB.Words.Count())); }
private void PrepareTwoWords(out MasterWord masterA, out Word wordA, out MasterWord masterB, out Word wordB, Action <MasterWord, MasterWord> ac = null) { masterA = new MasterWord(); wordA = new Word(masterA, "Hi", new Language("English")); masterB = new MasterWord(); wordB = new Word(masterB, "Hola", new Language("Spanish")); ac?.Invoke(masterA, masterB); MasterWordsService.Add(masterA); MasterWordsService.Add(masterB); }
public void RemovingLink_BetweenTwoWords_WillRelinkWordsInBooks( [Values(true, false)] bool enBookLang, [Values(true, false)] bool invertRemoval) { // Arrange var langEn = new Language("English"); var langEsp = new Language("Spanish"); var masterA = new MasterWord(); var wordA = new Word(masterA, "Hi", langEn); var wordB = new Word(masterA, "Hola", langEsp); new Word(masterA, "some word", new Language("some lang")); var book = new Book("name", enBookLang ? langEn : langEsp); book.AddWord(masterA); MasterWordsService.Add(masterA); BooksService.Add(book); // Act RefreshServicesAndClearCache(); wordA = WordsService.GetAll().First(w => w.ID == wordA.ID); wordB = WordsService.GetAll().First(w => w.ID == wordB.ID); if (invertRemoval) { WordsService.RemoveTranslation(wordA, wordB); } else { WordsService.RemoveTranslation(wordB, wordA); } // Assert book = BooksService.Get(book.ID); Assert.AreEqual(1, book.WordCount, "Book word count should be 1. Old Ref"); Assert.AreEqual(1, book.WordCount, "Book word count should be 1. New Ref"); Assert.IsNotNull(book.Words.First()[book.Language], "After removing translation between two words, book should keep the master word which aligns with book language. New Ref"); var wordInBook = enBookLang ? wordA : wordB; var wordWithoutBook = enBookLang ? wordB : wordA; Assert.AreEqual(book, wordInBook.MasterWord.Books.First(), "word should be in a book"); Assert.AreEqual(book.Language, wordInBook.Language, "book and word languages should be the same"); Assert.AreEqual(book.Words.First(), wordInBook.MasterWord, "master word should be the same of word left of correct language"); Assert.AreEqual(0, wordWithoutBook.MasterWord.Books.Count(), "other word should have no books"); }
public void RemovingWord_WithNotLoadedMaster_StillWorksFine() { // Arrange var masterA = new MasterWord(); var wordA = new Word(masterA, "Hi", new Language("English")); MasterWordsService.Add(masterA); RefreshServicesAndClearCache(); // Act WordsService.Remove(WordsService.GetAll().WithID(wordA)); // Assert Assert.IsNull(MasterWordsService.Get(masterA.ID), "Master should be destroyed"); Assert.IsNull(WordsService.Get(wordA.ID), "Word should be destroyed"); }
public void AddingLink_BetweenTwoWords_WillRefuseToMergeIfItsAlreadyTranslatedToThatLanguage( [Values(true, false)] bool inverseAddition, [Values(true, false)] bool cleanCache) { // Arrange var langEn = new Language("English"); var langEsp = new Language("Spanish"); var langJp = new Language("Japanese"); var masterA = new MasterWord(); var masterB = new MasterWord(); var wordA1 = new Word(masterA, "Hi orig", langEn); var wordA2 = new Word(masterA, "Hola", langEsp); var wordB1 = new Word(masterA, "Hi duplicate", langEn); var wordB2 = new Word(masterA, "おはよう", langJp); MasterWordsService.Add(masterA); MasterWordsService.Add(masterB); if (cleanCache) { RefreshServicesAndClearCache(); wordA2 = WordsService.GetAll().First(w => w.ID == wordA2.ID); wordB2 = WordsService.GetAll().First(w => w.ID == wordB2.ID); } // Act Assert.Throws <InvalidOperationException>(() => { if (inverseAddition) { WordsService.AddTranslation(wordA2, wordB2); } else { WordsService.AddTranslation(wordB2, wordA2); } }); }
public void RemovingAllWords_DestroysMasterWordAsWell_UsingRemoveRange([Values(true, false)] bool cleanCache) { // Arrange var masterA = new MasterWord(); new Word(masterA, "Hi", new Language("English")); new Word(masterA, "Hola", new Language("Spanish")); MasterWordsService.Add(masterA); if (cleanCache) { RefreshServicesAndClearCache(); } // Act WordsService.Remove(WordsService.GetAll().ToArray()); // Assert masterA = MasterWordsService.Get(masterA.ID); Assert.IsNull(masterA, "Master should be destroyed"); }
public void WordsService_IsMergePossible_WorksCorrectly( [Values(true, false)] bool cleanCache, [Values(true, false)] bool wordAlreadyTranslated) { // Arrange var langEn = new Language("English"); var langEsp = new Language("Spanish"); var langJp = new Language("Japanese"); var masterA = new MasterWord(); var masterB = new MasterWord(); new Word(masterA, "Hi orig", langEn); if (wordAlreadyTranslated) { new Word(masterB, "Hi duplicate", langEn); } var wordA2 = new Word(masterA, "Hola", langEsp); var wordB2 = new Word(masterB, "おはよう", langJp); MasterWordsService.Add(masterA); MasterWordsService.Add(masterB); if (cleanCache) { RefreshServicesAndClearCache(); wordA2 = WordsService.Get(wordA2.ID); wordB2 = WordsService.Get(wordB2.ID); } // Act var res = WordsService.IsMergePossible(wordA2.MasterWord, wordB2.MasterWord); // Assert Assert.AreEqual(wordAlreadyTranslated, !res, "If word is already translated via another language, merge should not be possible"); }
public char[] ToCharArray() { return(MasterWord.ToCharArray()); }
public void ToLowerCase() { MasterWord = MasterWord.ToLower(); MasterSentence = MasterSentence.ToLower(); }
private void Transfer(MasterWord From, MasterWord to, Word word) { From.Words.Remove(word); to.Words.Add(word); }
public bool IsMergePossible(MasterWord masterA, MasterWord masterB) { masterA = MasterWordsService.GetAllWithWords().WithID(masterA.ID); masterB = MasterWordsService.GetAllWithWords().WithID(masterB.ID); return(IsMergePossibleInternal(masterA, masterB)); }