Added basic setup.py files.
[jben2_gui.git] / jbparse / tests / kanjidic2.py
blobdc1af17ecd672789efe88a0ceda7db1d3ec08bd3
1 # -*- coding: utf-8 -*-
3 from __future__ import absolute_import
5 import unittest, time
6 from jbparse import kanjidic2
8 SRC_NAME = "kanjidic2.xml"
9 SRC_DIR = "../../dicts"
11 SRC_NAME = "/".join((SRC_DIR, SRC_NAME))
13 class Kanjidic2Test(unittest.TestCase):
15 def setUp(self):
16 self.parser = kanjidic2.Kanjidic2Parser(SRC_NAME)
18 def test_single_kanji_search(self):
19 """KANJIDIC2: Search for single kanji"""
20 query = u"食"
21 l = [entry for entry in self.parser.search(query)]
22 self.assertEqual(len(l), 1)
23 self.assertEqual(query, l[0].literal)
25 def test_multi_kanji_search(self):
26 """KANJIDIC2: Search for multiple kanji at once"""
27 query = u"上下"
28 l = [entry for entry in self.parser.search(query)]
29 self.assertEqual(len(l), 2)
30 for char in query:
31 self.assertTrue(char in [entry.literal for entry in l])
33 def test_mixed_search(self):
34 """KANJIDIC2: Search query with kanji and non-kanji characters"""
35 query = u"天気はいいから、散歩しましょう。 Right?"
36 l = [entry for entry in self.parser.search(query)]
37 self.assertEqual(len(l), 4)
38 for char in u"天気散歩":
39 self.assertTrue(char in [entry.literal for entry in l])
41 def test_caching(self):
42 """KANJIDIC2: Check that caching is working"""
43 self.assertFalse(self.parser.cache)
44 t = time.time()
45 self.test_single_kanji_search()
46 first_t = time.time() - t
48 self.assertTrue(self.parser.cache)
49 t = time.time()
50 self.test_single_kanji_search()
51 second_t = time.time() - t
53 print "\n\tFirst query time: %f" % first_t
54 print "\tSecond query time: %f" % second_t
55 self.assertTrue(second_t <= first_t)
57 def test_no_cache(self):
58 """KANJIDIC2: Check that parser works without caching."""
59 self.parser = kanjidic2.Kanjidic2Parser(SRC_NAME, use_cache=False)
61 self.assertFalse(self.parser.cache)
62 t = time.time()
63 self.test_single_kanji_search()
64 first_t = time.time() - t
66 self.assertFalse(self.parser.cache)
67 t = time.time()
68 self.test_single_kanji_search()
69 second_t = time.time() - t
71 print "\n\tFirst query time: %f" % first_t
72 print "\tSecond query time: %f" % second_t
74 def tearDown(self):
75 self.parser = None
77 if __name__ == "__main__":
78 unittest.main()