def testDelWord(self): """ Test deleting a word """ # This parent I've chosen has only one child db = self.getDB() num_trees, matched_words = cf.searchDB(db, "varch") test_parent = matched_words[0][1] num_trees, matched_words = cf.searchDB(db, "ferkel") test_word = matched_words[0][1] # # Test deleting a word that's not in the tree? # word_dets = {'lang': 'English', 'text': ['banana', 'pineapple'], # 'morpheme': 'bannana', 'def': 'A fruity thing'} # orphan_word = cf.createWord(word_dets) # self.assertRaises(cf.EtymExceptWord, cf.deleteWord, orphan_word) # Test deleting a word cf.deleteWord(test_word) self.assertEqual(cf.loadWordChildren(test_parent), []) num_trees, matched_words = cf.searchDB(db, "ferkel") self.assertEqual(num_trees, 0) # Reload the db, delete the parent and ensure the child moves up db = self.getDB() num_trees, matched_words = cf.searchDB(db, "varch") test_parent = matched_words[0][1] num_trees, matched_words = cf.searchDB(db, "ferkel") test_word = matched_words[0][1] test_grandparent = cf.loadWordParents(test_parent) cf.deleteWord(test_parent) self.assertEqual(cf.loadWordParents(test_word), test_grandparent)
def testChangeChildren(self): """ Tests modifying the children of a word """ chosen_word = self.getWord("far") real_children = cf.loadWordChildren(chosen_word) word_dets = {"lang": "English", "text": ["banana", "pineapple"], "morpheme": "bannana", "def": "A fruity thing"} test_child = cf.createWord(word_dets) cf.validateWord(test_child) # Test output with good input # (chosen_word only has one child) new_children = list(real_children) new_children.append(test_child) cf.editWordChildren(chosen_word, new_children) self.assertEqual(cf.countWordChildren(chosen_word), 2) # This removes children from chosen_word cf.editWordChildren(chosen_word, None) self.assertEqual(cf.countWordChildren(chosen_word), 0) self.assertEqual(cf.loadWordParents(real_children[0]), None) self.assertEqual(cf.loadWordParents(test_child), None) # Let's add a child back on cf.editWordChildren(chosen_word, test_child) self.assertEqual(cf.countWordChildren(chosen_word), 1) self.assertEqual(cf.loadWordParents(test_child), chosen_word)
def testCreateWord(self): """ Test creating a new word """ # Try some bad inputs word_dets = None self.assertRaises(cf.EtymExceptWord, cf.createWord, word_dets) word_dets = {} self.assertRaises(cf.EtymExceptWord, cf.createWord, word_dets) word_dets = {"lang": "English"} self.assertRaises(cf.EtymExceptWord, cf.createWord, word_dets) word_dets = {"lang": "English", "text": "banana", "morpheme": "bannana", "def": "A fruity thing"} self.assertRaises(cf.EtymExceptWord, cf.createWord, word_dets) # Now a good input, test the output word_dets = { "lang": "English", "text": ["banana", "pineapple"], "morpheme": "bannana", "def": "A fruity thing", "tag": "word", } new_word = cf.createWord(word_dets) new_word_details = cf.loadWordDetails(new_word) self.assertEqual(word_dets, new_word_details) # Try creating a word with a specified parent/children child_dets = {"lang": "Spanglish", "text": ["strawberry"], "morpheme": "strawberry", "def": "A fruity thing"} parent_dets = {"lang": "Fromesian", "text": ["raspberry"], "morpheme": "raspberry", "def": "A fruity thing"} word_dets = {"lang": "English", "text": ["banana"], "morpheme": "banana", "def": "A fruity thing"} new_child = cf.createWord(child_dets) new_parent = cf.createWord(parent_dets) new_word = cf.createWord(word_dets, word_parent=new_parent, word_children=new_child) self.assertEqual(cf.loadWordChildren(new_word)[0], new_child) self.assertEqual(cf.loadWordParents(new_word), new_parent)
def testMoveWord(self): """ Test moving a word to a different location in the tree """ db = self.getDB() # Here we move ross to be a child of hross # First get references to the words num_trees, matched_words = cf.searchDB(db, "ross") test_source = matched_words[0][1] test_source_parent = cf.loadWordParents(test_source) num_trees, matched_words = cf.searchDB(db, "hross") test_dest_parent = matched_words[0][1] self.assertEqual(cf.countWordChildren(test_dest_parent), 0) # Perform the move cf.moveWord(test_source, test_dest_parent) # Make sure it really happened self.assertEqual(cf.countWordChildren(test_dest_parent), 1) self.assertEqual(cf.countWordChildren(test_source_parent), 0) self.assertEqual(cf.loadWordParents(test_source), test_dest_parent) # Let's test for some bad inputs self.assertRaises(cf.EtymExceptWord, cf.moveWord, None, None) self.assertRaises(cf.EtymExceptWord, cf.moveWord, test_source, None) self.assertRaises(cf.EtymExceptWord, cf.moveWord, None, test_source) # Another test, this time make sure the children are maintained # We move fearh (which has a child and a grandchild) to be a child # of porcus (which has two children and a few further descendants) db = self.getDB() # First get references to the words num_trees, matched_words = cf.searchDB(db, "fearh") test_source = matched_words[0][1] test_source_parent = cf.loadWordParents(test_source) num_trees, matched_words = cf.searchDB(db, "porcus") test_dest_parent = matched_words[0][1] self.assertEqual(cf.countWordChildren(test_dest_parent), 2) self.assertEqual(cf.countWordChildren(test_source), 1) # Perform the move cf.moveWord(test_source, test_dest_parent) # Make sure it really happened self.assertEqual(cf.countWordChildren(test_dest_parent), 3) self.assertEqual(cf.countWordChildren(test_source_parent), 2) self.assertEqual(cf.loadWordParents(test_source), test_dest_parent) self.assertEqual(cf.countWordChildren(test_source), 1) num_trees, matched_words = cf.searchDB(db, "far") test_dest_child = matched_words[0][1] self.assertEqual(cf.loadWordParents(test_dest_child), test_source)
def TreeItemAddSib(self, event): """ Add a sibling to the selected tree item """ # Create a word with some default values #wordDets = cf.loadWordDetails(self.current_node) word_dets = {'text': ['NEW WORD'], 'morpheme': 'NEW WORD', 'lang': 'UNKNOWN', 'def': 'Change me!'} new_word = cf.createWord(word_dets, word_parent=cf.loadWordParents(self.current_node)) # Refresh the tree, select new word self.DisplayTree(self.search_root, self.search_words, select=new_word)
def testChangeParent(self): """ Tests modifying the parent of a word """ # This word has a parent, 'equinus' chosen_word = self.getWord("equine") # Make sure it can read the parent test_parent = cf.loadWordParents(chosen_word) parent_details = cf.loadWordDetails(test_parent) self.assertEqual(parent_details["text"], ["equinus"]) self.assertEqual(parent_details["lang"], "Latin") # Add a new word and set its parent word_dets = {"lang": "English", "text": ["banana", "pineapple"], "morpheme": "bannana", "def": "A fruity thing"} new_word = cf.createWord(word_dets) cf.editWordParent(new_word, test_parent) self.assertEqual(cf.countWordChildren(test_parent), 2) self.assertEqual(cf.countWordChildren(chosen_word), 0) cf.editWordParent(new_word, chosen_word) self.assertEqual(cf.countWordChildren(chosen_word), 1) # Add a new word and it to be parent to a word new_word = cf.createWord(word_dets) cf.editWordParent(chosen_word, new_word) self.assertEqual(new_word, cf.loadWordParents(chosen_word))
def testReadWordParents(self): """ Test reading the parents of a word """ # The root shouldn't have any parents # TODO: Re-enable this once I can search for roots # chosen_word = self.getWord('khursa') # word_parents = cf.loadWordParents(chosen_word) # self.assertEqual(word_parents, None) # self.assertRaises(cf.EtymExceptWord, cf.loadWordDetails, word_parents[0]) # This word should have one parent chosen_word = self.getWord("horse") word_parents = cf.loadWordParents(chosen_word) wordDets = cf.loadWordDetails(word_parents) self.assertEqual(wordDets["text"][0], "hors") self.assertEqual(wordDets["lang"], "Old English") self.assertEqual(wordDets["def"], "A man-eating beast")