def test_save_config():
"""Test Config object can save to update a file."""
mock_config = mock.mock_open(read_data=CONFIG_DATA)
with mock.patch('two1.commands.util.config.open', mock_config, create=True):
c = config.Config('config_file')
num_config_keys = len(c.state.keys())
# Update an existing key and add a new one
with mock.patch('two1.commands.util.config.open', mock_config, create=True):
c.set('username', 'TEST_USERNAME', should_save=True)
c.set('some_list_key', [123, 456, 789], should_save=True)
# Import the newly saved configuration file
new_config = json.loads(mock_config.return_value.write.call_args[0][0])
mock_config.assert_called_with('config_file', mode='w')
assert c.username == 'TEST_USERNAME'
assert c.some_list_key == [123, 456, 789]
assert new_config['username'] == 'TEST_USERNAME'
assert new_config['some_list_key'] == [123, 456, 789]
assert len(new_config.keys()) == num_config_keys + 1
评论列表
文章目录