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 testCountWordChildren(self): """ Tests counting word children """ # This word shouldn't have any children chosen_word = self.getWord("hross") word_children = cf.countWordChildren(chosen_word) self.assertEqual(word_children, 0) # This word should have one child chosen_word = self.getWord("far") word_children = cf.countWordChildren(chosen_word) self.assertEqual(word_children, 1) # This word should have two children chosen_word = self.getWord("porcus") word_children = cf.countWordChildren(chosen_word) self.assertEqual(word_children, 2)
def _populate_tree(self, node, node_elem, emph_nodes, select): """ Recursive private function to fill in the rest of the tree control node: the ElementTree element we're working on. node_elem: the corresponding object in the TreeCtrl class. emph_nodes: a list of ElementTree elements, these will be emphasized. select: an element that will be selected (focused). If not None then it overrides focusing on emphasized nodes. """ # len() of a node returns how many children it has if cf.countWordChildren(node) > 0: for child in cf.loadWordChildren(node): child_details = cf.loadWordDetails(child) # Just display the first alternate child_label = child_details['text'][0] child_elem = self.treebox.AppendItem(node_elem, child_label, data = wx.TreeItemData(child)) if child in emph_nodes and select is None: # Emphasize the node self.treebox.SetItemBold(child_elem) # Select the node self.treebox.SelectItem(child_elem) if select is not None and child == select: self.treebox.SelectItem(child_elem) # Recurse! self._populate_tree(child, child_elem, emph_nodes, select)
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 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 display_children(node, depth, word, search_word): """ Recursive function to display children of a node depth is what level we're on. word is the word element we're looking for (it will be emphasized in the tree). search_word is the word text we're looking for. """ # The len() of a node returns how many children it has if cf.countWordChildren(node) > 0: for child in cf.loadWordChildren(node): depth_marker = ' '*depth child_markup = '' child_details = cf.loadWordDetails(child) if child in word: # First we have the emphasized word # Then the other matches, if there child_markup = ', '.join(['*{0}*'.format(text_var) for text_var in child_details['text'] if text_var == search_word]) child_markup_rest = ', '.join(['{0}'.format(text_var) for text_var in child_details['text'] if text_var != search_word]) if child_markup_rest is not '': child_markup = ', '.join([child_markup, child_markup_rest]) else: child_markup = ', '.join(['{0}'.format(text_var) for text_var in child_details['text']]) print(u'{0}Child: {1} ({2}, "{3}")'.format( depth_marker, child_markup, child_details['lang'], child_details['def'])) display_children(child, depth+1, word, search_word) else: # The base of the recursion simply does nothing pass