jp-places / src / test_goitaikei.py

# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------#
# testGoitaikei.py
# Lars Yencken <lars.yencken@gmail.com>
# vim: ts=4 sw=4 sts=4 et tw=78:
# Fri Jun  8 09:57:21 2007
#
#----------------------------------------------------------------------------# 

import sys
import codecs
import unittest
from goitaikei import GoitaikeiHierarchy

if type(sys.stdout) == file:
    # Wrap the output with a utf8 decoder for OS X.
    sys.stdout = codecs.getwriter('utf8')(sys.stdout)

#----------------------------------------------------------------------------#

def suite():
    testSuite = unittest.TestSuite((
            unittest.makeSuite(GoitaikeiTestCase)
        ))
    return testSuite

#----------------------------------------------------------------------------#

class GoitaikeiTestCase(unittest.TestCase):
    """
    This class tests the Goitaikei class. 
    """
    def setUp(self):
        self.obj = GoitaikeiHierarchy.get_cached()
        self.root = self.obj.root
        self.ekiPath = [u'具体', u'場', u'施設', u'公共施設', u'駅・港', u'駅']
        self.badPath = [u'具体', u'場', u'sour eggs']
        pass

    def testFindNode(self):
        """Tests finding a node by label."""
        eki_node = self.root.find_node(u'駅')
        self.assertEqual(eki_node.label, u'駅')
        assert u'秋葉原' in eki_node.attrib['words']

    def testGetPath(self):
        """Test fetching a node at a known path."""
        eki_node = self.root.get_path(self.ekiPath)
        self.assertEqual(eki_node.label, u'駅')
        assert u'秋葉原' in eki_node.attrib['words']
        self.assertEqual(eki_node, self.root.find_node(u'駅'))
        return

    def testGetBadPath(self):
        """Tests fetching a bad path."""
        self.assertRaises(KeyError, self.root.get_path, self.badPath)

    def testProbabilities(self):
        """Tests that node probabilities steadily decrease down the tree."""
        for node in self.root.walk():
            if node.parent is not None:
                assert node.parent['cProb'] >= node['cProb']
        return

    def testCopy(self):
        c = self.root.copy()
        
        # Check they share the same number of nodes.
        self.assertEqual(len(list(c.walk())), len(list(self.root.walk())))

        # Mark every node in the copy, and make sure that none of the original
        # nodes were marked (and thus shared with the copy).
        for node in c.walk():
            node.attrib['odd'] = 'purple monkey dishwasher'

        for node in self.root.walk():
            assert 'odd' not in node.attrib

        return

    def testPrune(self):
        n_nodes = 2705
        self.assertEqual(n_nodes, len(list(self.root.walk())))

        node = self.root.prune(lambda x: len(x.attrib['words']) > 0)
        self.assertEqual(n_nodes, len(list(node.walk())))

        node = self.root.prune(lambda x: len(x.attrib['words']) > 10)
        assert 0 < len(list(node.walk())) < n_nodes

        node = self.root.prune(lambda x: len(x.attrib['words']) > 100)
        assert 0 < len(list(node.walk())) < n_nodes

    def testWalk(self):
        for node in self.root.walk_postorder():
            for child in node.children.values():
                assert 'isMarked' in child.attrib
            node.attrib['isMarked'] = True
        return

    def tearDown(self):
        pass

#----------------------------------------------------------------------------#

if __name__ == "__main__":
    unittest.TextTestRunner(verbosity=1).run(suite())

#----------------------------------------------------------------------------#

# vim: ts=4 sw=4 sts=4 et tw=78:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.