1# -*- coding: utf-8 -*- 2import webapp2 3 4from webapp2_extras import config as app_config 5 6import test_base 7 8 9class TestConfig(test_base.BaseTestCase): 10 def tearDown(self): 11 pass 12 13 def test_get(self): 14 config = app_config.Config({'foo': { 15 'bar': 'baz', 16 'doo': 'ding', 17 }}) 18 19 self.assertEqual(config.get('foo'), { 20 'bar': 'baz', 21 'doo': 'ding', 22 }) 23 24 self.assertEqual(config.get('bar'), {}) 25 26 def test_get_existing_keys(self): 27 config = app_config.Config({'foo': { 28 'bar': 'baz', 29 'doo': 'ding', 30 }}) 31 32 self.assertEqual(config.get_config('foo', 'bar'), 'baz') 33 self.assertEqual(config.get_config('foo', 'doo'), 'ding') 34 35 def test_get_existing_keys_from_default(self): 36 config = app_config.Config({}, {'foo': { 37 'bar': 'baz', 38 'doo': 'ding', 39 }}) 40 41 self.assertEqual(config.get_config('foo', 'bar'), 'baz') 42 self.assertEqual(config.get_config('foo', 'doo'), 'ding') 43 44 def test_get_non_existing_keys(self): 45 config = app_config.Config() 46 47 self.assertRaises(KeyError, config.get_config, 'foo', 'bar') 48 49 def test_get_dict_existing_keys(self): 50 config = app_config.Config({'foo': { 51 'bar': 'baz', 52 'doo': 'ding', 53 }}) 54 55 self.assertEqual(config.get_config('foo'), { 56 'bar': 'baz', 57 'doo': 'ding', 58 }) 59 60 def test_get_dict_non_existing_keys(self): 61 config = app_config.Config() 62 63 self.assertRaises(KeyError, config.get_config, 'bar') 64 65 def test_get_with_default(self): 66 config = app_config.Config() 67 68 self.assertRaises(KeyError, config.get_config, 'foo', 'bar', 'ooops') 69 self.assertRaises(KeyError, config.get_config, 'foo', 'doo', 'wooo') 70 71 def test_get_with_default_and_none(self): 72 config = app_config.Config({'foo': { 73 'bar': None, 74 }}) 75 76 self.assertEqual(config.get_config('foo', 'bar', 'ooops'), None) 77 78 def test_update(self): 79 config = app_config.Config({'foo': { 80 'bar': 'baz', 81 'doo': 'ding', 82 }}) 83 84 self.assertEqual(config.get_config('foo', 'bar'), 'baz') 85 self.assertEqual(config.get_config('foo', 'doo'), 'ding') 86 87 config.update('foo', {'bar': 'other'}) 88 89 self.assertEqual(config.get_config('foo', 'bar'), 'other') 90 self.assertEqual(config.get_config('foo', 'doo'), 'ding') 91 92 def test_setdefault(self): 93 config = app_config.Config() 94 95 self.assertRaises(KeyError, config.get_config, 'foo') 96 97 config.setdefault('foo', { 98 'bar': 'baz', 99 'doo': 'ding', 100 }) 101 102 self.assertEqual(config.get_config('foo', 'bar'), 'baz') 103 self.assertEqual(config.get_config('foo', 'doo'), 'ding') 104 105 def test_setdefault2(self): 106 config = app_config.Config({'foo': { 107 'bar': 'baz', 108 }}) 109 110 self.assertEqual(config.get_config('foo'), { 111 'bar': 'baz', 112 }) 113 114 config.setdefault('foo', { 115 'bar': 'wooo', 116 'doo': 'ding', 117 }) 118 119 self.assertEqual(config.get_config('foo', 'bar'), 'baz') 120 self.assertEqual(config.get_config('foo', 'doo'), 'ding') 121 122 def test_setitem(self): 123 config = app_config.Config() 124 config['foo'] = {'bar': 'baz'} 125 126 self.assertEqual(config, {'foo': {'bar': 'baz'}}) 127 self.assertEqual(config['foo'], {'bar': 'baz'}) 128 129 def test_init_no_dict_values(self): 130 self.assertRaises(AssertionError, app_config.Config, {'foo': 'bar'}) 131 self.assertRaises(AssertionError, app_config.Config, {'foo': None}) 132 self.assertRaises(AssertionError, app_config.Config, 'foo') 133 134 def test_init_no_dict_default(self): 135 self.assertRaises(AssertionError, app_config.Config, {}, {'foo': 'bar'}) 136 self.assertRaises(AssertionError, app_config.Config, {}, {'foo': None}) 137 self.assertRaises(AssertionError, app_config.Config, {}, 'foo') 138 139 def test_update_no_dict_values(self): 140 config = app_config.Config() 141 142 self.assertRaises(AssertionError, config.update, {'foo': 'bar'}, 'baz') 143 self.assertRaises(AssertionError, config.update, {'foo': None}, 'baz') 144 self.assertRaises(AssertionError, config.update, 'foo', 'bar') 145 146 def test_setdefault_no_dict_values(self): 147 config = app_config.Config() 148 149 self.assertRaises(AssertionError, config.setdefault, 'foo', 'bar') 150 self.assertRaises(AssertionError, config.setdefault, 'foo', None) 151 152 def test_setitem_no_dict_values(self): 153 config = app_config.Config() 154 155 def setitem(key, value): 156 config[key] = value 157 return config 158 159 self.assertRaises(AssertionError, setitem, 'foo', 'bar') 160 self.assertRaises(AssertionError, setitem, 'foo', None) 161 162 163class TestLoadConfig(test_base.BaseTestCase): 164 def tearDown(self): 165 pass 166 167 def test_default_config(self): 168 config = app_config.Config() 169 170 from resources.template import default_config as template_config 171 from resources.i18n import default_config as i18n_config 172 173 self.assertEqual(config.get_config('resources.template', 'templates_dir'), template_config['templates_dir']) 174 self.assertEqual(config.get_config('resources.i18n', 'locale'), i18n_config['locale']) 175 self.assertEqual(config.get_config('resources.i18n', 'timezone'), i18n_config['timezone']) 176 177 def test_default_config_with_non_existing_key(self): 178 config = app_config.Config() 179 180 from resources.i18n import default_config as i18n_config 181 182 # In the first time the module config will be loaded normally. 183 self.assertEqual(config.get_config('resources.i18n', 'locale'), i18n_config['locale']) 184 185 # In the second time it won't be loaded, but won't find the value and then use the default. 186 self.assertEqual(config.get_config('resources.i18n', 'i_dont_exist', 'foo'), 'foo') 187 188 def test_override_config(self): 189 config = app_config.Config({ 190 'resources.template': { 191 'templates_dir': 'apps/templates' 192 }, 193 'resources.i18n': { 194 'locale': 'pt_BR', 195 'timezone': 'America/Sao_Paulo', 196 }, 197 }) 198 199 self.assertEqual(config.get_config('resources.template', 'templates_dir'), 'apps/templates') 200 self.assertEqual(config.get_config('resources.i18n', 'locale'), 'pt_BR') 201 self.assertEqual(config.get_config('resources.i18n', 'timezone'), 'America/Sao_Paulo') 202 203 def test_override_config2(self): 204 config = app_config.Config({ 205 'resources.i18n': { 206 'timezone': 'America/Sao_Paulo', 207 }, 208 }) 209 210 self.assertEqual(config.get_config('resources.i18n', 'locale'), 'en_US') 211 self.assertEqual(config.get_config('resources.i18n', 'timezone'), 'America/Sao_Paulo') 212 213 def test_get(self): 214 config = app_config.Config({'foo': { 215 'bar': 'baz', 216 }}) 217 218 self.assertEqual(config.get_config('foo', 'bar'), 'baz') 219 220 def test_get_with_default(self): 221 config = app_config.Config() 222 223 self.assertEqual(config.get_config('resources.i18n', 'bar', 'baz'), 'baz') 224 225 def test_get_with_default_and_none(self): 226 config = app_config.Config({'foo': { 227 'bar': None, 228 }}) 229 230 self.assertEqual(config.get_config('foo', 'bar', 'ooops'), None) 231 232 def test_get_with_default_and_module_load(self): 233 config = app_config.Config() 234 self.assertEqual(config.get_config('resources.i18n', 'locale'), 'en_US') 235 self.assertEqual(config.get_config('resources.i18n', 'locale', 'foo'), 'en_US') 236 237 def test_required_config(self): 238 config = app_config.Config() 239 self.assertRaises(KeyError, config.get_config, 'resources.i18n', 'foo') 240 241 def test_missing_module(self): 242 config = app_config.Config() 243 self.assertRaises(KeyError, config.get_config, 'i_dont_exist', 'i_dont_exist') 244 245 def test_missing_module2(self): 246 config = app_config.Config() 247 self.assertRaises(KeyError, config.get_config, 'i_dont_exist') 248 249 def test_missing_key(self): 250 config = app_config.Config() 251 self.assertRaises(KeyError, config.get_config, 'resources.i18n', 'i_dont_exist') 252 253 def test_missing_default_config(self): 254 config = app_config.Config() 255 self.assertRaises(KeyError, config.get_config, 'tipfy', 'foo') 256 257 258class TestLoadConfigGetItem(test_base.BaseTestCase): 259 def tearDown(self): 260 pass 261 262 def test_default_config(self): 263 config = app_config.Config() 264 265 from resources.template import default_config as template_config 266 from resources.i18n import default_config as i18n_config 267 268 self.assertEqual(config['resources.template']['templates_dir'], template_config['templates_dir']) 269 self.assertEqual(config['resources.i18n']['locale'], i18n_config['locale']) 270 self.assertEqual(config['resources.i18n']['timezone'], i18n_config['timezone']) 271 272 def test_default_config_with_non_existing_key(self): 273 config = app_config.Config() 274 275 from resources.i18n import default_config as i18n_config 276 277 # In the first time the module config will be loaded normally. 278 self.assertEqual(config['resources.i18n']['locale'], i18n_config['locale']) 279 280 # In the second time it won't be loaded, but won't find the value and then use the default. 281 self.assertEqual(config['resources.i18n'].get('i_dont_exist', 'foo'), 'foo') 282 283 def test_override_config(self): 284 config = app_config.Config({ 285 'resources.template': { 286 'templates_dir': 'apps/templates' 287 }, 288 'resources.i18n': { 289 'locale': 'pt_BR', 290 'timezone': 'America/Sao_Paulo', 291 }, 292 }) 293 294 self.assertEqual(config['resources.template']['templates_dir'], 'apps/templates') 295 self.assertEqual(config['resources.i18n']['locale'], 'pt_BR') 296 self.assertEqual(config['resources.i18n']['timezone'], 'America/Sao_Paulo') 297 298 def test_override_config2(self): 299 config = app_config.Config({ 300 'resources.i18n': { 301 'timezone': 'America/Sao_Paulo', 302 }, 303 }) 304 305 self.assertEqual(config['resources.i18n']['locale'], 'en_US') 306 self.assertEqual(config['resources.i18n']['timezone'], 'America/Sao_Paulo') 307 308 def test_get(self): 309 config = app_config.Config({'foo': { 310 'bar': 'baz', 311 }}) 312 313 self.assertEqual(config['foo']['bar'], 'baz') 314 315 def test_get_with_default(self): 316 config = app_config.Config() 317 318 self.assertEqual(config['resources.i18n'].get('bar', 'baz'), 'baz') 319 320 def test_get_with_default_and_none(self): 321 config = app_config.Config({'foo': { 322 'bar': None, 323 }}) 324 325 self.assertEqual(config['foo'].get('bar', 'ooops'), None) 326 327 def test_get_with_default_and_module_load(self): 328 config = app_config.Config() 329 self.assertEqual(config['resources.i18n']['locale'], 'en_US') 330 self.assertEqual(config['resources.i18n'].get('locale', 'foo'), 'en_US') 331 332 def test_required_config(self): 333 config = app_config.Config() 334 self.assertRaises(KeyError, config['resources.i18n'].__getitem__, 'foo') 335 self.assertRaises(KeyError, config['resources.i18n'].__getitem__, 'required') 336 337 def test_missing_module(self): 338 config = app_config.Config() 339 self.assertRaises(KeyError, config.__getitem__, 'i_dont_exist') 340 341 def test_missing_key(self): 342 config = app_config.Config() 343 self.assertRaises(KeyError, config['resources.i18n'].__getitem__, 'i_dont_exist') 344 345 346if __name__ == '__main__': 347 test_base.main() 348