def test_loadcfg_override(sample_spec): """Test that loading a second config file overrides common settings but leaves in settings only present in the first""" with tempfile.NamedTemporaryFile() as output_file_name: parsec_config = config.ParsecConfig(spec=sample_spec, upgrader=None, output_fname=output_file_name.name, tvars=None, validator=None) with tempfile.NamedTemporaryFile() as conf_file1: conf_file1.write(""" [section1] value1 = 'frodo' value2 = 'sam' """.encode()) conf_file1.seek(0) parsec_config.loadcfg(conf_file1.name, "File test_loadcfg") sparse = parsec_config.sparse assert sparse['section1']['value1'] == 'frodo' assert sparse['section1']['value2'] == 'sam' with tempfile.NamedTemporaryFile() as conf_file2: conf_file2.write(""" [section1] value2 = 'pippin' """.encode()) conf_file2.seek(0) parsec_config.loadcfg(conf_file2.name, "File test_loadcfg") sparse = parsec_config.sparse assert sparse['section1']['value1'] == 'frodo' assert sparse['section1']['value2'] == 'pippin'
def test_get_item(sample_spec_2): with tempfile.NamedTemporaryFile() as output_file_name: with tempfile.NamedTemporaryFile() as rcfile: parsec_config = config.ParsecConfig( spec=sample_spec_2, upgrader=None, output_fname=output_file_name.name, tvars=None, validator=cylc_config_validate) rcfile.write(""" [section] name = test [allow_many] anything = yup """.encode()) rcfile.seek(0) parsec_config.loadcfg(rcfile.name, "1.0") cfg = parsec_config.get(keys=None, sparse=None) assert parsec_config.dense == cfg cfg = parsec_config.get(keys=None, sparse=True) assert parsec_config.sparse == cfg cfg = parsec_config.get(keys=['section'], sparse=True) assert parsec_config.sparse['section'] == cfg cfg = parsec_config.get(keys=['section', 'name'], sparse=True) assert 'test' == cfg with pytest.raises(config.ItemNotFoundError): parsec_config.get(keys=['section', 'a'], sparse=True)
def test_loadcfg_with_upgrade(sample_spec): def upg(cfg, description): u = upgrader(cfg, description) u.obsolete('1.0', ['section3', 'entries']) u.upgrade() with tempfile.NamedTemporaryFile() as output_file_name: with tempfile.NamedTemporaryFile() as rcfile: parsec_config = config.ParsecConfig( spec=sample_spec, upgrader=upg, output_fname=output_file_name.name, tvars=None, validator=None # use default ) rcfile.write(""" [section1] value1 = 'test' value2 = 'test' [section2] enabled = True [section3] title = 'Ohm' [[entries]] key = 'product' value = 1, 2, 3, 4 """.encode()) rcfile.seek(0) parsec_config.loadcfg(rcfile.name, "1.1") sparse = parsec_config.sparse # removed by the obsolete upgrade assert 'entries' not in sparse['section3']
def test_expand(self): spec = { 'section': { 'name': [VDR.V_STRING], 'address': [VDR.V_INTEGER_LIST] }, 'allow_many': { '__MANY__': [VDR.V_STRING, ''] } } with tempfile.NamedTemporaryFile() as output_file_name: with tempfile.NamedTemporaryFile() as rcfile: parsec_config = config.ParsecConfig( spec=spec, upgrader=None, output_fname=output_file_name.name, tvars=None, validator=cylc_config_validate) rcfile.write(""" [section] name = test [allow_many] anything = yup """.encode()) rcfile.seek(0) parsec_config.loadcfg(rcfile.name, "1.0") parsec_config.expand() sparse = parsec_config.sparse self.assertEqual('yup', sparse['allow_many']['anything']) self.assertTrue('__MANY__' not in sparse['allow_many'])
def test_get_item(self): spec = { 'section': { 'name': [VDR.V_STRING], 'address': [VDR.V_INTEGER_LIST] }, 'allow_many': { '__MANY__': [VDR.V_STRING, ''] } } with tempfile.NamedTemporaryFile() as output_file_name: with tempfile.NamedTemporaryFile() as rcfile: parsec_config = config.ParsecConfig( spec=spec, upgrader=None, output_fname=output_file_name.name, tvars=None, validator=cylc_config_validate) rcfile.write(""" [section] name = test [allow_many] anything = yup """.encode()) rcfile.seek(0) parsec_config.loadcfg(rcfile.name, "1.0") cfg = parsec_config.get(keys=None, sparse=None) self.assertEqual(parsec_config.dense, cfg) cfg = parsec_config.get(keys=None, sparse=True) self.assertEqual(parsec_config.sparse, cfg) cfg = parsec_config.get(keys=['section'], sparse=True) self.assertEqual(parsec_config.sparse['section'], cfg) cfg = parsec_config.get(keys=['section', 'name'], sparse=True) self.assertEqual('test', cfg) with self.assertRaises(config.ItemNotFoundError): parsec_config.get(keys=['section', 'a'], sparse=True)
def test_validate(self): """ An interesting aspect of the ParsecConfig.validate, is that if you have a sparse dict produced by this class, and you call the validate on that dict again, you may have TypeErrors. That occurs because the values like 'True' are validated against the spec and converted from Strings with quotes, to bool types. So the next type you run the validation if expects Strings... :return: """ spec = { 'section': { 'name': [VDR.V_STRING], 'address': [VDR.V_STRING], } } parsec_config = config.ParsecConfig( spec=spec, upgrader=None, # new spec output_fname=None, # not going to call the loadcfg tvars=None, validator=None # use default ) sparse = OrderedDictWithDefaults() parsec_config.validate(sparse) # empty dict is OK with self.assertRaises(IllegalItemError): sparse = OrderedDictWithDefaults() sparse['name'] = 'True' parsec_config.validate(sparse) # name is not valid sparse = OrderedDictWithDefaults() sparse['section'] = OrderedDictWithDefaults() sparse['section']['name'] = 'Wind' sparse['section']['address'] = 'Corner' parsec_config.validate(sparse)
def test_expand(sample_spec_2): with tempfile.NamedTemporaryFile() as output_file_name: with tempfile.NamedTemporaryFile() as rcfile: parsec_config = config.ParsecConfig( spec=sample_spec_2, upgrader=None, output_fname=output_file_name.name, tvars=None, validator=cylc_config_validate) rcfile.write(""" [section] name = test [allow_many] anything = yup """.encode()) rcfile.seek(0) parsec_config.loadcfg(rcfile.name, "1.0") parsec_config.expand() sparse = parsec_config.sparse assert 'yup' == sparse['allow_many']['anything'] assert '__MANY__' not in sparse['allow_many']
def test_loadcfg(sample_spec): with tempfile.NamedTemporaryFile() as output_file_name: with tempfile.NamedTemporaryFile() as rcfile: parsec_config = config.ParsecConfig( spec=sample_spec, upgrader=None, # new spec output_fname=output_file_name.name, tvars=None, validator=None # use default ) rcfile.write(""" [section1] value1 = 'test' value2 = 'test' [section2] enabled = True [section3] title = 'Ohm' [[entries]] key = 'product' value = 1, 2, 3, 4 """.encode()) rcfile.seek(0) parsec_config.loadcfg(rcfile.name, "File test_loadcfg") sparse = parsec_config.sparse value = sparse['section3']['entries']['value'] assert [1, 2, 3, 4] == value # calling it multiple times should still work parsec_config.loadcfg(rcfile.name, "File test_loadcfg") parsec_config.loadcfg(rcfile.name, "File test_loadcfg") sparse = parsec_config.sparse value = sparse['section3']['entries']['value'] assert [1, 2, 3, 4] == value