Skip to content

Commit

Permalink
Merge dab8860 into e8ef3bf
Browse files Browse the repository at this point in the history
  • Loading branch information
akaIDIOT committed Apr 1, 2019
2 parents e8ef3bf + dab8860 commit fa486b5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
9 changes: 9 additions & 0 deletions confidence/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def _split_keys(mapping, separator='.', colliding=None):
Recursively walks *mapping* to split keys that contain the separator into
nested mappings.
.. note::
All keys are coerced to `str` for use within a `.Configuration` object.
:param mapping: the mapping to process
:param separator: the character (sequence) to use as the separator between
keys
Expand All @@ -64,6 +68,11 @@ def _split_keys(mapping, separator='.', colliding=None):
# recursively split key(s) in value
value = _split_keys(value, separator)

# reject non-str keys, avoid complicating access patterns
if not isinstance(key, str):
raise ValueError('non-str type keys ({0}, {0.__class__.__module__}.{0.__class__.__name__}) '
'not supported'.format(key))

if separator in key:
# update key to be the first part before the separator
key, rest = key.split(separator, 1)
Expand Down
11 changes: 11 additions & 0 deletions tests/files/complicated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
a.complicated:
example: example
'2019':
a: 'a'
2019.b: 'b'

a:
simple:
reference: ${a.complicated}
complicated.reference: value with ${a.complicated.2019.a} reference in it
'2019-03-28': 2019-03-28
14 changes: 13 additions & 1 deletion tests/test_dict.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from collections.abc import Mapping, Sequence
from os import path

import pytest

from confidence import Configuration, ConfigurationError
from confidence import Configuration, ConfigurationError, loadf
from confidence.models import _NoDefault


test_files = path.join(path.dirname(__file__), 'files')


def test_empty():
def run_test(subject):
assert subject.get('path.without.value', default=None) is None
Expand Down Expand Up @@ -50,3 +54,11 @@ def test_as_type():

def test_no_default_doc_friendly():
assert 'raise' in repr(_NoDefault)


def test_key_types_from_file():
config = loadf(path.join(test_files, 'complicated.yaml'))

assert isinstance(config.get('a.complicated.2019'), Configuration)
assert config.get('a.complicated.2019.a') == 'a'
assert config.get('a.complicated.2019.b') == 'b'
16 changes: 15 additions & 1 deletion tests/test_references.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from os import path

import pytest

from confidence import Configuration, ConfigurationError, ConfiguredReferenceError, NotConfigured
from confidence import Configuration, ConfigurationError, ConfiguredReferenceError, loadf, NotConfigured


test_files = path.join(path.dirname(__file__), 'files')


def test_reference_syntax():
Expand Down Expand Up @@ -145,3 +150,12 @@ def test_loop_recursion():
assert not config.ns.key

assert 'ns.key' in str(e.value) and 'recursive' in str(e.value)


def test_references_from_file():
config = loadf(path.join(test_files, 'complicated.yaml'))

assert isinstance(config.a.simple.reference, Configuration)
assert config.a.simple.reference.example == 'example'

assert config.a.complicated.reference == 'value with a reference in it'
32 changes: 32 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from datetime import date

import pytest

from confidence.utils import _Conflict, _merge, _split_keys


Expand Down Expand Up @@ -132,3 +136,31 @@ def test_split_overlap_complex():
}
}
}


def test_split_key_types():
subject = {
'ns.1234.key': 42,
'ns': {
1234: {'key2': 43}
}
}

with pytest.raises(ValueError) as e:
assert not _split_keys(subject)

assert '1234' in str(e.value)
assert 'int' in str(e.value)

subject = {
'ns.2019-04-01.key': 42,
'ns': {
date(2019, 4, 1): {'key2': 43}
}
}

with pytest.raises(ValueError) as e:
assert not _split_keys(subject)

assert '2019-04-01' in str(e.value)
assert 'datetime.date' in str(e.value)

0 comments on commit fa486b5

Please sign in to comment.