aboutsummaryrefslogtreecommitdiff
path: root/test/test_config/test_sections.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_config/test_sections.py')
-rw-r--r--test/test_config/test_sections.py156
1 files changed, 73 insertions, 83 deletions
diff --git a/test/test_config/test_sections.py b/test/test_config/test_sections.py
index 39b41a7..2ca7e4f 100644
--- a/test/test_config/test_sections.py
+++ b/test/test_config/test_sections.py
@@ -17,101 +17,91 @@
17 17
18import sys 18import sys
19import os 19import os
20import ConfigParser
21import unittest 20import unittest
22path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) 21path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
23sys.path.append(path) 22sys.path.append(path)
23from dodai.config.sections import ConfigSections
24from dodai.tools.himo import Himo 24from dodai.tools.himo import Himo
25from dodai.tools.himo import String2Himo 25from dodai.tools.himo import String2Himo
26from dodai.config.sections import ConfigSections 26from dodai.tools.odict import OrderedDict
27from dodai.tools import Section
28
29class Parser(object):
30
31 DATA = OrderedDict()
32 DATA['sec_a'] = OrderedDict()
33 DATA['sec_a']['opt_a'] = 'foo'
34 DATA['sec_a']['opt_b'] = 'bar'
35 DATA['sec_a']['opt_c'] = '1'
36 DATA['sec_a']['opt_d'] = '22'
37
38 DATA['sec_b'] = OrderedDict()
39 DATA['sec_b']['opt_a'] = 'lt'
40 DATA['sec_b']['opt_b'] = 'el'
41 DATA['sec_b']['opt_c'] = 'mg'
42 DATA['sec_b']['opt_d'] = 'ds'
43
44 def sections(self):
45 return self.DATA.keys()
46
47 def options(self, section):
48 return self.DATA[section].keys()
49
50 def get(self, section, option):
51 return self.DATA[section][option]
27 52
28 53
29class TestSections(unittest.TestCase): 54class TestSections(unittest.TestCase):
30 55
31 def setUp(self): 56 def setUp(self):
32 path = os.path.dirname(__file__) 57 s2h = String2Himo()
33 filepath = os.path.join(path, 'config.cfg') 58 self.obj = ConfigSections(OrderedDict, Section, s2h)
34 self.parser = ConfigParser.ConfigParser() 59 self.parser = Parser()
35 self.parser.readfp(open(filepath))
36 self.sections = ConfigSections(String2Himo())
37 60
38 def test_call(self): 61 def test_call(self):
39 self.sections(self.parser) 62 self.obj(self.parser)
40 count = len(self.parser.sections()) 63
41 self.assertEqual(count, len(self.sections)) 64 def test_two(self):
42 65 self.obj(self.parser)
43 def test_iterator(self): 66 key = String2Himo('sec_a')
44 result = True 67 self.obj.__getitem__('sec_a')
45 for section in self.sections: 68 self.obj.sec_a
46 if not section.get_title() in self.parser.sections(): 69
47 result = False 70 def test_results_one(self):
48 self.assertTrue(result==True) 71 self.obj(self.parser)
49 72 for section in self.obj.keys():
50 def test_get_section(self): 73 self.assertTrue(section in self.parser.sections())
51 self.sections(self.parser) 74
52 self.sections(self.parser) 75 def test_results_two(self):
53 check = self.sections.get_section('test_db') 76 self.obj(self.parser)
54 self.assertEqual(check.get_title(), 'test_db') 77 for section, data in self.obj.items():
55 78 for key in data.keys():
56 def test_build_string_object(self): 79 self.assertTrue(key in self.parser.options(section))
57 sections = ConfigSections() 80
58 sections(self.parser) 81 def test_results_three(self):
59 count = len(self.parser.sections()) 82 self.obj(self.parser)
60 self.assertEqual(count, len(sections)) 83 for section, data in self.obj.items():
61 84 for option, val in data.items():
62 def test_getitem(self): 85 self.assertTrue(val, self.parser.get(section, option))
63 self.sections(self.parser) 86
64 title = self.sections['test_db'].get_title() 87 def test_results_four(self):
65 self.assertEqual(title, 'test_db') 88 self.obj(self.parser)
66 89 for section in self.parser.sections():
67 def test_getattr(self): 90 self.assertTrue(section in self.obj)
68 self.sections(self.parser) 91
69 title = self.sections.test_db.get_title() 92 def test_results_five(self):
70 self.assertEqual(title, 'test_db') 93 self.obj(self.parser)
71 94 for section in self.parser.sections():
72 def test_get_keys(self): 95 for option in self.parser.options(section):
73 self.sections(self.parser) 96 self.assertTrue(option in self.obj[section])
74 keys = self.sections.keys() 97
75 self.assertTrue(len(keys)) 98 def test_results_six(self):
76 99 self.obj(self.parser)
77 def test_section_object_one(self): 100 for section in self.parser.sections():
78 self.sections(self.parser) 101 for option in self.parser.options(section):
79 keys = self.sections.test_db.keys() 102 val = self.parser.get(section, option)
80 self.assertTrue(len(keys)) 103 self.assertEquals(val, self.obj[section][option])
81 104
82 def test_section_object_two(self):
83 self.sections(self.parser)
84 keys = self.sections.test_db.___options___.keys()
85 self.assertTrue(len(keys))
86
87 def test_section_object_three(self):
88 self.sections(self.parser)
89 self.sections.test_db.___blah___ = 'test'
90 val = self.sections.test_db.__getattr__('___blah___')
91 self.assertTrue(val == 'test')
92
93 def test_section_object_four(self):
94 self.sections(self.parser)
95 self.sections.test_db.foo = 'bar'
96 val = self.sections.test_db.__getattr__('foo')
97 self.assertTrue(val == 'bar')
98
99 def test_section_object_five(self):
100 self.sections(self.parser)
101 keys = []
102 for key in self.sections.test_db:
103 keys.append(key)
104 self.assertTrue(keys)
105
106 def test_section_object_six(self):
107 self.sections(self.parser)
108 self.sections.test_db.foo = 'bar'
109 val = self.sections.test_db['foo']
110 self.assertTrue(val == 'bar')
111
112 def test_section_object_seven(self):
113 self.sections(self.parser, 'unicode_escape')
114 self.assertEqual(self.sections.extra.name, u'\u8c61')
115 105
116 106
117if __name__ == '__main__': 107if __name__ == '__main__':