Source

jp-places / src / test_goitaikei.py

Full commit
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------#
# test_goitaikei.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():
    test_suite = unittest.TestSuite((
            unittest.makeSuite(GoitaikeiTestCase)
        ))
    return test_suite

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

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

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

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

    def test_get_bad_path(self):
        """Tests fetching a bad path."""
        self.assert_raises(KeyError, self.root.get_path, self.bad_path)

    def test_probabilities(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['c_prob'] >= node['c_prob']
        return

    def test_copy(self):
        c = self.root.copy()
        
        # Check they share the same number of nodes.
        self.assert_equal(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 test_prune(self):
        n_nodes = 2705
        self.assert_equal(n_nodes, len(list(self.root.walk())))

        node = self.root.prune(lambda x: len(x.attrib['words']) > 0)
        self.assert_equal(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 test_walk(self):
        for node in self.root.walk_postorder():
            for child in node.children.values():
                assert 'is_marked' in child.attrib
            node.attrib['is_marked'] = 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: