Esempio n. 1
0
 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')
Esempio n. 2
0
 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')
Esempio n. 3
0
  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}]})
    with self.assertRaisesRegex(KeyError, "The key 'b' does not exist"):
      config.override({'key': [{'b': 43}]})
Esempio n. 4
0
    def test_cls(self):
        params = base_config.Config()
        with self.assertRaisesRegex(
                AttributeError,
                '`BUILDER` is a property and `_BUILDER` is the reserved'):
            params.BUILDER = DumpConfig2
        with self.assertRaisesRegex(
                AttributeError,
                '`BUILDER` is a property and `_BUILDER` is the reserved'):
            params._BUILDER = DumpConfig2

        base_config.bind(DumpConfig1)(DumpConfig2)
        params = DumpConfig1()
        self.assertEqual(params.BUILDER, DumpConfig2)
        with self.assertRaisesRegex(ValueError,
                                    'Inside a program, we should not bind'):
            base_config.bind(DumpConfig1)(DumpConfig2)

        def _test():
            return 'test'

        base_config.bind(DumpConfig2)(_test)
        params = DumpConfig2()
        self.assertEqual(params.BUILDER(), 'test')
Esempio n. 5
0
 def test_invalid_keys(self):
   params = base_config.Config()
   with self.assertRaises(AttributeError):
     _ = params.a
Esempio n. 6
0
 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))
Esempio n. 7
0
 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)
Esempio n. 8
0
 def test_import_export_unsupport_sequence(self, v):
   with self.assertRaisesRegex(TypeError,
                               'Invalid sequence: only supports single level'):
     _ = base_config.Config({'key': v})
Esempio n. 9
0
 def test_import_unsupport_types(self, v, msg):
   with self.assertRaisesRegex(TypeError, msg):
     _ = base_config.Config({'key': v})
Esempio n. 10
0
 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))
Esempio n. 11
0
 def test_key_error(self, key, msg):
   params = base_config.Config()
   with self.assertRaisesRegex(KeyError, msg):
     params.override({key: True})