Esempio n. 1
0
def test_set_not_string():

    dictionary = {'key1': 'value1'}

    patcher = Patcher(dictionary)
    with pytest.raises(exceptions.InvalidValueTypeException):
        patcher.set('key1', 5).finish()
Esempio n. 2
0
def configure(ctx, alias):

    repo = ctx.parent.repo

    file_path = repo.path(alias)

    try:
        parsed = parser.load(file_path=file_path, fmt=repo.fmt(alias))
    except exceptions.CorruptFileException as e:
        e.cause = causes.EDITED_MANUALLY
        e.possible_solutions = [
            solutions.edit_manually(),
            solutions.reset_to_latest(alias)
        ]
        raise

    # detect if the file was manually edited since the last command.
    # if so, warn because it means the current version of the file is not
    # under version control and will be lost after the change
    latest = parser.loads(repo.contents(alias=alias, version='latest'),
                          fmt=repo.fmt(alias))
    if latest != parsed:

        exception = click.ClickException(message='Cannot perform operation')
        exception.cause = causes.DIFFER_FROM_LATEST
        exception.possible_solutions = [
            solutions.reset_to_latest(alias),
            solutions.commit(alias)
        ]
        raise exception

    patcher = Patcher(parsed)
    ctx.patcher = patcher
Esempio n. 3
0
def test_delete_non_existing_key():

    dictionary = {'key': 'value'}

    patcher = Patcher(dictionary)

    with pytest.raises(exceptions.KeyNotFoundException):
        patcher.delete('non-existing')
Esempio n. 4
0
def test_add_to_non_list():

    dictionary = {'key': 'value1'}

    patcher = Patcher(dictionary)

    with pytest.raises(exceptions.InvalidKeyTypeException):
        patcher.add(key='key', value='value2')
Esempio n. 5
0
def test_get_compund_object_unsupported_format():

    dictionary = {'key1': ['value1', 'value2']}

    patcher = Patcher(dictionary)

    with pytest.raises(exceptions.UnsupportedFormatException):
        patcher.get('key1', fmt='unsupported')
Esempio n. 6
0
def test_get_non_existing_two_level_complex_key():

    dictionary = {'key1': {'key2': {'key3': 'value1'}}}

    patcher = Patcher(dictionary)

    with pytest.raises(exceptions.KeyNotFoundException):
        patcher.get('key1:key2:key4')
Esempio n. 7
0
def test_get_dict_as_yaml():

    dictionary = {'key1': {'key2': 'value1'}}

    expected_value = 'key2: value1\n'

    patcher = Patcher(dictionary)
    value = patcher.get('key1', fmt=constants.YAML)

    assert expected_value == value
Esempio n. 8
0
def test_set_float():

    dictionary = {'key1': 'value1'}

    expected_dictionary = {'key1': 5.5}

    patcher = Patcher(dictionary)
    dictionary = patcher.set('key1', '5.5').finish()

    assert expected_dictionary == dictionary
Esempio n. 9
0
def test_get_float():

    dictionary = {'key1': 5.5}

    expected_value = '5.5'

    patcher = Patcher(dictionary)
    value = patcher.get('key1')

    assert expected_value == value
Esempio n. 10
0
def test_get_integer():

    dictionary = {'key1': 5}

    expected_value = '5'

    patcher = Patcher(dictionary)
    value = patcher.get('key1')

    assert expected_value == value
Esempio n. 11
0
def test_get_string():

    dictionary = {'key1': 'value1'}

    expected_value = 'value1'

    patcher = Patcher(dictionary)
    value = patcher.get('key1')

    assert expected_value == value
Esempio n. 12
0
def test_delete():

    dictionary = {'key1': 'value1', 'key2': 'value2'}

    expected_dictionary = {'key2': 'value2'}

    patcher = Patcher(dictionary)
    dictionary = patcher.delete('key1').finish()

    assert expected_dictionary == dictionary
Esempio n. 13
0
def test_get_existing_one_level_complex_key():

    dictionary = {'key1': {'key2': 'value1'}}

    expected_value = 'value1'

    patcher = Patcher(dictionary)
    value = patcher.get('key1:key2')

    assert expected_value == value
Esempio n. 14
0
def test_set_existing_two_level_complex_key():

    dictionary = {'key1': {'key2': {'key3': 'value1'}}}

    expected_dictionary = {'key1': {'key2': {'key3': 'value2'}}}

    patcher = Patcher(dictionary)
    dictionary = patcher.set('key1:key2:key3', 'value2').finish()

    assert expected_dictionary == dictionary
Esempio n. 15
0
def test_set_complex_key_compound_value():

    dictionary = {'key1': {'key2': 'value1'}}

    expected_dictionary = {'key1': {'key2': {'key3': 'value2'}}}

    patcher = Patcher(dictionary)
    dictionary = patcher.set('key1:key2', '{"key3":"value2"}').finish()

    assert expected_dictionary == dictionary
Esempio n. 16
0
def test_set_list():

    dictionary = {'key1': 'value1'}

    expected_dictionary = {'key1': ['value1', 'value2']}

    patcher = Patcher(dictionary)
    dictionary = patcher.set('key1', '["value1", "value2"]').finish()

    assert expected_dictionary == dictionary
Esempio n. 17
0
def test_set_dict():

    dictionary = {'key1': 'value1'}

    expected_dictionary = {'key1': {'key2': 'value2'}}

    patcher = Patcher(dictionary)
    dictionary = patcher.set('key1', '{"key2":"value2"}').finish()

    assert expected_dictionary == dictionary
Esempio n. 18
0
def test_set_unicode():

    dictionary = {'key1': 'value1'}

    expected_dictionary = {'key1': 'value2'}

    patcher = Patcher(dictionary)
    dictionary = patcher.set('key1', u'value2').finish()

    assert expected_dictionary == dictionary
Esempio n. 19
0
def test_remove_from_complex_key():

    dictionary = {'key1': {'key2': ['value1', 'value2']}}

    expected_dictionary = {'key1': {'key2': ['value1']}}

    patcher = Patcher(dictionary)
    patcher.remove(key='key1:key2', value='value2')

    assert expected_dictionary == dictionary
Esempio n. 20
0
def test_remove():

    dictionary = {'key1': ['value1', 'value2']}

    expected_dictionary = {'key1': ['value1']}

    patcher = Patcher(dictionary)
    patcher.remove(key='key1', value='value2')

    assert expected_dictionary == dictionary
Esempio n. 21
0
def test_add_to_complex_key():

    dictionary = {'key1': {'key2': ['value1']}}

    expected_dictionary = {'key1': {'key2': ['value1', 'value2']}}

    patcher = Patcher(dictionary)
    patcher.add(key='key1:key2', value='value2')

    assert expected_dictionary == dictionary
Esempio n. 22
0
def test_add():

    dictionary = {'key': ['value1']}

    expected_dictionary = {'key': ['value1', 'value2']}

    patcher = Patcher(dictionary)
    patcher.add(key='key', value='value2')

    assert expected_dictionary == dictionary
Esempio n. 23
0
def test_delete_complex_key():

    dictionary = {'key1': {'key2': 'value1', 'key3': 'value2'}}

    expected_dictionary = {'key1': {'key2': 'value1'}}

    patcher = Patcher(dictionary)
    dictionary = patcher.delete('key1:key3').finish()

    assert expected_dictionary == dictionary
Esempio n. 24
0
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
Esempio n. 25
0
def test_get_list_as_yaml():

    dictionary = {'key1': ['value1', 'value2']}

    expected_value = '''- value1
- value2\n'''

    patcher = Patcher(dictionary)
    value = patcher.get('key1', fmt=constants.YAML)

    assert expected_value == value
Esempio n. 26
0
def test_get_dict_as_json():

    dictionary = {'key1': {'key2': 'value1'}}

    expected_value = '''{
  "key2": "value1"
}'''

    patcher = Patcher(dictionary)
    value = patcher.get('key1')

    assert expected_value == value
Esempio n. 27
0
def test_get_list_as_json():

    dictionary = {'key1': ['value1', 'value2']}

    expected_value = '''[
"value1",
"value2"
]'''

    patcher = Patcher(dictionary)
    value = patcher.get('key1')

    actual = value.replace(' ', '')

    assert expected_value == actual