1 from mock import patch, Mock |
1 from mock import patch, Mock |
2 from twisted.trial import unittest |
2 from twisted.trial import unittest |
3 import signal |
3 import signal |
4 import io |
4 import io |
5 import ConfigParser |
5 import ConfigParser |
6 from iro import config, error |
6 from collections import OrderedDict |
|
7 from functools import partial |
|
8 |
|
9 from iro import config, error, validate |
7 from iro.offer.provider import Provider, providers |
10 from iro.offer.provider import Provider, providers |
8 |
11 |
9 class TestModuleConfig(unittest.TestCase): |
12 class TestModuleConfig(unittest.TestCase): |
10 '''test config class''' |
13 '''test config class''' |
11 |
14 |
12 def setUp(self): |
15 def setUp(self): |
13 self._reloadList=config.configParser.reloadList |
16 self._reloadList=config.configParser.reloadList |
14 config.configParser.reloadList=[] |
17 config.configParser.reloadList=[] |
15 |
18 |
16 def tearDown(self): |
19 def tearDown(self): |
74 x.assert_called_once_with() |
77 x.assert_called_once_with() |
75 |
78 |
76 |
79 |
77 class TestRead(unittest.TestCase): |
80 class TestRead(unittest.TestCase): |
78 |
81 |
|
82 |
|
83 |
79 def tearDown(self): |
84 def tearDown(self): |
80 for s in config.configParser.sections(): |
85 for s in config.configParser.sections(): |
81 config.configParser.remove_section(s) |
86 config.configParser.remove_section(s) |
82 |
87 |
83 @patch('iro.config.ConfigParser.read') |
88 @patch('iro.config.ConfigParser.read') |
135 @patch('iro.config.ConfigParser.read') |
140 @patch('iro.config.ConfigParser.read') |
136 def testProviders(self, pRead): |
141 def testProviders(self, pRead): |
137 v=Mock() |
142 v=Mock() |
138 class TestProvider(Provider): |
143 class TestProvider(Provider): |
139 def __init__(self,name): |
144 def __init__(self,name): |
140 Provider.__init__(self,name) |
145 o = [("test",config.Option(v))] |
141 self.options.update({"test":config.Option(v)}) |
146 Provider.__init__(self, name, options=o) |
142 providers["test"]=TestProvider |
147 providers["test"]=TestProvider |
143 try: |
148 try: |
144 sample_config = """[p] |
149 sample_config = """[p] |
145 """ |
150 """ |
146 config.configParser.readfp(io.BytesIO(sample_config)) |
151 config.configParser.readfp(io.BytesIO(sample_config)) |
156 finally: |
161 finally: |
157 del(providers["test"]) |
162 del(providers["test"]) |
158 |
163 |
159 |
164 |
160 class TestConfig(unittest.TestCase): |
165 class TestConfig(unittest.TestCase): |
|
166 |
|
167 def config(self,name): |
|
168 return config.Config(name, OrderedDict([ |
|
169 ("dburl",config.Option(lambda x,y:x,long="Connection URL to database",must=True)), |
|
170 ("port",config.Option(partial(validate.vInteger,minv=0),long="Port under that twisted is running",must=True)), |
|
171 ]) |
|
172 ) |
|
173 |
161 def testSame(self): |
174 def testSame(self): |
162 c1 = config.Config("1") |
175 c1 = self.config("1") |
163 c1.port = 1 |
176 c1.port = 1 |
164 c1.dburl = "dburl1" |
177 c1.dburl = "dburl1" |
165 c2 = config.Config("2") |
178 c2 = self.config("2") |
166 c2.port = 1 |
179 c2.port = 1 |
167 c2.dburl = "dburl1" |
180 c2.dburl = "dburl1" |
168 self.assertTrue(c1.same(c2)) |
181 self.assertTrue(c1.same(c2)) |
169 self.assertTrue(c1.same(c1)) |
182 self.assertTrue(c1.same(c1)) |
170 self.assertTrue(c2.same(c1)) |
183 self.assertTrue(c2.same(c1)) |
175 c2.dburl = "dburl2" |
188 c2.dburl = "dburl2" |
176 self.assertFalse(c2.same(c1)) |
189 self.assertFalse(c2.same(c1)) |
177 self.assertFalse(c1.same(c2)) |
190 self.assertFalse(c1.same(c2)) |
178 |
191 |
179 def testSampleConf(self): |
192 def testSampleConf(self): |
180 c1 = config.Config("1") |
193 c1 = self.config("1") |
181 self.assertEqual(c1.sampleConf(),["[1]", |
194 self.assertEqual(c1.sampleConf(),["[1]", |
182 "# Connection URL to database", |
195 "# Connection URL to database", |
183 "dburl = ","", |
196 "dburl = ","", |
184 "# Port under that twisted is running", |
197 "# Port under that twisted is running", |
185 "port = ",""]) |
198 "port = ",""]) |
186 |
199 |
187 def testsampleConfDefault(self): |
200 def testSampleConfDefault(self): |
188 c1 = config.Config("1") |
201 c1 = self.config("1") |
189 c1.options["port"].default = 12345 |
202 c1.options["port"].default = 12345 |
190 c1.options["port"].must = False |
203 c1.options["port"].must = False |
191 c1.options["dburl"].default = True |
204 c1.options["dburl"].default = True |
192 self.assertEqual(c1.sampleConf(),["[1]", |
205 self.assertEqual(c1.sampleConf(),["[1]", |
193 "# Connection URL to database", |
206 "# Connection URL to database", |