def test_import_config(self):
     params = base_config.Config({'a': [{'b': 2}, {'c': {'d': 3}}]})
     self.assertLen(params.a, 2)
     self.assertEqual(params.a[0].b, 2)
     self.assertEqual(type(params.a[0]), base_config.Config)
     self.assertEqual(pprint.pformat(params.a[0].b), '2')
     self.assertEqual(type(params.a[1]), base_config.Config)
     self.assertEqual(type(params.a[1].c), base_config.Config)
     self.assertEqual(pprint.pformat(params.a[1].c.d), '3')
 def test_override(self):
     params = base_config.Config({'a': [{'b': 2}, {'c': {'d': 3}}]})
     params.override({'a': [{'b': 4}, {'c': {'d': 5}}]}, is_strict=False)
     self.assertEqual(type(params.a), list)
     self.assertEqual(type(params.a[0]), base_config.Config)
     self.assertEqual(pprint.pformat(params.a[0].b), '4')
     self.assertEqual(type(params.a[1]), base_config.Config)
     self.assertEqual(type(params.a[1].c), base_config.Config)
     self.assertEqual(pprint.pformat(params.a[1].c.d), '5')
    def test_override_is_strict_true(self):
        params = base_config.Config({
            'a': 'aa',
            'b': 2,
            'c': {
                'c1': 'cc',
                'c2': 20
            }
        })
        params.override({'a': 2, 'c': {'c1': 'ccc'}}, is_strict=True)
        self.assertEqual(params.a, 2)
        self.assertEqual(params.c.c1, 'ccc')
        with self.assertRaises(KeyError):
            params.override({'d': 'ddd'}, is_strict=True)
        with self.assertRaises(KeyError):
            params.override({'c': {'c3': 30}}, is_strict=True)

        config = base_config.Config({'key': [{'a': 42}]})
        config.override({'key': [{'b': 43}]})
        self.assertEqual(config.key[0].b, 43)
        with self.assertRaisesRegex(AttributeError,
                                    'The key `a` does not exist'):
            _ = config.key[0].a
 def test_invalid_keys(self):
     params = base_config.Config()
     with self.assertRaises(AttributeError):
         _ = params.a
 def assertImportExport(self, v):
     config = base_config.Config({'key': v})
     back = config.as_dict()['key']
     self.assertEqual(pprint.pformat(back), pprint.pformat(v))
     self.assertHasSameTypes(config.key, v, msg='=%s v' % pprint.pformat(v))
 def test_config_vs_params_dict(self, v):
     d = {'key': v}
     self.assertEqual(type(base_config.Config(d).key[0]),
                      base_config.Config)
     self.assertEqual(type(base_config.params_dict.ParamsDict(d).key[0]),
                      dict)
 def test_import_export_unsupport_sequence(self, v):
     with self.assertRaisesRegex(
             TypeError, 'Invalid sequence: only supports single level'):
         _ = base_config.Config({'key': v})
 def test_import_unsupport_types(self, v, msg):
     with self.assertRaisesRegex(TypeError, msg):
         _ = base_config.Config({'key': v})
 def test_import_export_immutable_types(self, v):
     self.assertImportExport(v)
     out = base_config.Config({'key': v})
     self.assertEqual(pprint.pformat(v), pprint.pformat(out.key))
 def test_key_error(self, key, msg):
     params = base_config.Config()
     with self.assertRaisesRegex(KeyError, msg):
         params.override({key: True})