def testReadWordChildren(self): """ Test reading the children of a word """ # This word shouldn't have any children chosen_word = self.getWord("hross") word_children = cf.loadWordChildren(chosen_word) self.assertEqual(word_children, []) self.assertRaises(cf.EtymExceptWord, cf.loadWordDetails, word_children) # This word should have one child chosen_word = self.getWord("far") word_children = cf.loadWordChildren(chosen_word) wordDets = cf.loadWordDetails(word_children[0]) self.assertEqual(wordDets["text"][0], "farrow") self.assertEqual(wordDets["lang"], "Modern English") self.assertEqual(wordDets["def"], "An obsolete word for a pig")
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 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 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