def test_config_with_circular_import(self): with tempfile.TemporaryDirectory() as temp: with open(os.path.join(temp, 'foo.json'), mode='w') as f: json.dump({'import': 'bar.json'}, f) with open(os.path.join(temp, 'bar.json'), mode='w') as f: json.dump([{'import': './foo.json'}], f) with self.assertRaises(RuntimeError): Config.load_path(os.path.join(temp, 'foo.json'))
def test_config_with_circular_import(self): with tempfile.TemporaryDirectory() as temp: with open(os.path.join(temp, 'foo.json'), mode='w') as f: json.dump({'a': {'import': 'bar.json'}}, f) with open(os.path.join(temp, 'bar.json'), mode='w') as f: json.dump([{'import': './foo.json'}], f) with self.assertRaises(RuntimeError) as cm: Config.load_path(os.path.join(temp, 'foo.json')) self.assertEqual( cm.exception.args, ('Circular import', '!/ of {foo} -> !/a of {foo} -> !/ of {bar}' ' -> !/0 of {bar} -> !/ of {foo}'.format( foo=os.path.join(temp, 'foo.json'), bar=os.path.join(temp, 'bar.json'))))
def test_config_load_path(self): with tempfile.TemporaryDirectory() as temp0, \ tempfile.TemporaryDirectory() as temp1: with open(os.path.join(temp0, 'foo.json'), mode='w') as f: json.dump( { 'foo': { 'v0': { 'type': 'func_0', 'a': 1, 'b': 2 } }, 'bar': { 'import': os.path.join(temp1, 'bar.json') }, 'baz': { 'import': 'baz.json', '0/b': 3, '1/d': [1, 2], }, }, f) with open(os.path.join(temp1, 'bar.json'), mode='w') as f: json.dump({'type': 'func_0', 'a': 3, 'b': 4}, f) with open(os.path.join(temp0, 'baz.json'), mode='w') as f: json.dump([ { 'type': 'func_1', 'a': 1, 'b': 2 }, { 'd': 3, 'e': 4 }, ], f) config = Config.load_path(os.path.join(temp0, 'foo.json'), types=self.types) self.assertEqual(config['!/foo'], {'v0': { 'type': 'func_0', 'a': 1, 'b': 2 }}) self.assertEqual(config['/foo'], {'v0': 13}) self.assertEqual(config['!/bar'], {'type': 'func_0', 'a': 3, 'b': 4}) self.assertEqual(config['/bar'], 17) self.assertEqual(config['!/baz'], [ { 'type': 'func_1', 'a': 1, 'b': 3 }, { 'd': [1, 2], 'e': 4 }, ]) self.assertEqual(config['/baz'], [ { 'd': 3, 'e': 13 }, { 'd': [1, 2], 'e': 4 }, ])