def test_delete_complex_key(configure): fmt = configure.fmt if fmt in constants.COMPOUND_FORMATS: write_file( dictionary={ 'key1': { 'key2': 'value1', 'key3': 'value2' } }, configure=configure) result = configure.run('delete --key key1:key3', catch_exceptions=True) if fmt not in constants.COMPOUND_FORMATS: expected = 'Error: Unsupported operation: delete with complex keys (format={0})'\ .format(fmt) if fmt == constants.INI: expected = "Error: Key 'key1:key3' does not exist" actual = result.std_out else: expected = writer.dumps( obj={ 'key1': { 'key2': 'value1' } }, fmt=configure.fmt) actual = read_file(configure=configure) assert expected in actual
def test_contents(repo, request): alias = request.node.name contents = repo.contents(alias=alias, version=0) assert writer.dumps(get_test_dict(repo.test_fmt), fmt=repo.test_fmt) == contents
def test_remove(configure): fmt = configure.fmt if fmt in constants.COMPOUND_FORMATS: write_file( dictionary={ 'key1': ['value1', 'value2'] }, configure=configure) expected_message = configure.alias result = configure.run('remove --key {0} --value value2 --message {1}' .format(get_key('key1', configure), expected_message), catch_exceptions=True) if fmt not in constants.COMPOUND_FORMATS: expected = 'Error: Unsupported operation: remove (format={0})'.format(fmt) actual = result.std_out assert expected in actual else: expected = write_string( dictionary={ 'key1': ['value1'] }, configure=configure) actual = read_file(configure=configure) assert expected == actual expected = writer.dumps(obj=['value1'], fmt=fmt) assert expected.strip() in result.std_out assert expected_message == configure.repo.message(alias=configure.alias, version=2)
def test_put_complex_key_compound_value(configure): fmt = configure.fmt if fmt in constants.COMPOUND_FORMATS: write_file( dictionary={ 'key1': { 'key2': 'value1' } }, configure=configure) result = configure.run('put --key key1:key2 --value {"key3":"value2"}', catch_exceptions=True, escape=True) if fmt not in constants.COMPOUND_FORMATS: expected = 'Error: Unsupported operation: put with complex {0} (format={1})'.format( 'keys' if fmt == constants.PROPERTIES else 'values', fmt) actual = result.std_out else: expected = writer.dumps( obj={ 'key1': { 'key2': { 'key3': 'value2' } } }, fmt=configure.fmt) actual = read_file(configure=configure) assert expected in actual
def write_string(dictionary, configure): if configure.fmt == constants.INI: # ini format must have its sections as the # first level keys of the dictionary dictionary = {'section1': copy.deepcopy(dictionary)} return writer.dumps(obj=dictionary, fmt=configure.fmt)
def test_show_latest(repository): alias = repository.alias result = repository.run('show --alias {0} --version latest'.format(alias)) expected = writer.dumps(obj=get_test_dict(repository), fmt=repository.fmt) assert expected.strip() in result.std_out
def test_get_ini_section(): dictionary = {'section': {'key1': 'key2'}} expected_value = writer.dumps(obj={'key1': 'key2'}, fmt=constants.PROPERTIES) patcher = Patcher(dictionary) value = patcher.get('section', fmt=constants.INI) assert expected_value == value
def test_get_ini_section(): dictionary = { 'section': { 'key1': 'key2' } } expected_value = writer.dumps(obj={'key1': 'key2'}, fmt=constants.PROPERTIES) patcher = Patcher(dictionary) value = patcher.get('section', fmt=constants.INI) assert expected_value == value
def _serialize(self, value, fmt): self._logger.debug('Serializing value ({0}): {1}'.format(type(value), value)) if isinstance(value, flatdict.FlatDict): value = value.as_dict() if isinstance(value, (dict, list, set)): if fmt == constants.INI: # an ini dictionary is actually # a properties file, not an ini fmt = constants.PROPERTIES value = writer.dumps(value, fmt=fmt) return str(value)
def _serialize(self, value, fmt): self._logger.debug('Serializing value ({0}): {1}'.format( type(value), value)) if isinstance(value, flatdict.FlatDict): value = value.as_dict() if isinstance(value, (dict, list, set)): if fmt == constants.INI: # an ini dictionary is actually # a properties file, not an ini fmt = constants.PROPERTIES value = writer.dumps(value, fmt=fmt) return str(value)
def test_reset_latest(repository): alias = repository.alias file_path = repository.repo.path(alias) with open(file_path, 'w') as stream: stream.write('corrupted') repository.run('reset --alias {0} --version latest'.format(alias)) with open(file_path) as stream: expected = writer.dumps(obj=get_test_dict(repository), fmt=repository.fmt) actual = stream.read() revisions = repository.repo.revisions(alias) expected_number_of_revisions = 2 assert expected == actual assert expected_number_of_revisions == len(revisions)
def test_commit(repository): alias = repository.alias file_path = repository.repo.path(alias) with open(file_path, 'w') as stream: changed = get_dict(base_dict={'key5': 'value5'}, repository=repository) contents = writer.dumps(obj=changed, fmt=repository.fmt) stream.write(contents) expected_message = "my message" repository.run('commit --alias {0} --message "{1}"'.format(alias, expected_message)) revisions = repository.repo.revisions(alias) expected_number_of_revisions = 2 assert expected_number_of_revisions == len(revisions) assert contents == repository.repo.contents(alias=alias, version=1) assert expected_message in repository.repo.message(alias=alias, version=1)
def test_dumps_properties_key_not_primitive(): with pytest.raises(exceptions.InvalidValueTypeException): writer.dumps({'key1': ['value1', 'value2']}, fmt=constants.PROPERTIES)
def test_dumps_properties_not_dict(): with pytest.raises(exceptions.InvalidValueTypeException): writer.dumps([], fmt=constants.PROPERTIES)
def test_dumps_unsupported_format(): with pytest.raises(exceptions.UnsupportedFormatException): writer.dumps({}, fmt='unsupported')