Skip to content

Commit

Permalink
Merge pull request #18 from timothycrosley/feature/one-of-validation
Browse files Browse the repository at this point in the history
Feature/one of validation
  • Loading branch information
timothycrosley committed Aug 8, 2015
2 parents fd1d9d9 + 592d1fa commit 7d22623
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
15 changes: 13 additions & 2 deletions hug/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def number(value):
return int(value)


def list(value):
def multiple(value):
"""Multiple Values"""
return type(value) == list and value or [value]
return value if isinstance(value, list) else [value]


def comma_separated_list(value):
Expand All @@ -30,3 +30,14 @@ def text(value):
def inline_dictionary(value):
"""A single line dictionary, where items are separted by commas and key:value are separated by a pipe"""
return {key.strip(): value.strip() for key, value in (item.split(":") for item in value.split("|"))}


def one_of(values):
"""Ensures the value is within a set of acceptable values"""
def matches(value):
if not value in values:
raise KeyError('Value one of acceptable values: ({0})'.format("|".join(values)))
return value

matches.__doc__ = 'One of the following values: ({0})'.format("|".join(values))
return matches
39 changes: 24 additions & 15 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,47 @@


def test_number():
hug.types.number('1') == 1
hug.types.number(1) == 1
assert hug.types.number('1') == 1
assert hug.types.number(1) == 1
with pytest.raises(ValueError):
hug.types.number('bacon')


def test_list():
hug.types.list('value') == ['value']
hug.types.list(['value1', 'value2']) == ['value1', 'value2']
def test_multiple():
assert hug.types.multiple('value') == ['value']
assert hug.types.multiple(['value1', 'value2']) == ['value1', 'value2']


def test_comma_separated_list():
hug.types.comma_separated_list('value') == ['value']
hug.types.comma_separated_list('value1,value2') == ['value1', 'value2']
assert hug.types.comma_separated_list('value') == ['value']
assert hug.types.comma_separated_list('value1,value2') == ['value1', 'value2']


def test_decimal():
hug.types.decimal('1.1') == 1.1
hug.types.decimal('1') == float(1)
hug.types.decimal(1.1) == 1.1
assert hug.types.decimal('1.1') == 1.1
assert hug.types.decimal('1') == float(1)
assert hug.types.decimal(1.1) == 1.1
with pytest.raises(ValueError):
hug.types.decimal('bacon')


def test_text():
hug.types.text('1') == '1'
hug.types.text(1) == '1'
hug.types.text('text') == 'text'
assert hug.types.text('1') == '1'
assert hug.types.text(1) == '1'
assert hug.types.text('text') == 'text'


def test_inline_dictionary():
hug.types.inline_dictionary('1:2') == {'1': '2'}
hug.types.inline_dictionary('1:2|3:4') == {'1': '2', '3': '4'}
assert hug.types.inline_dictionary('1:2') == {'1': '2'}
assert hug.types.inline_dictionary('1:2|3:4') == {'1': '2', '3': '4'}
with pytest.raises(ValueError):
hug.types.inline_dictionary('1')


def test_one_of():
assert hug.types.one_of(('bacon', 'sausage', 'pancakes'))('bacon') == 'bacon'
assert hug.types.one_of(['bacon', 'sausage', 'pancakes'])('sausage') == 'sausage'
assert hug.types.one_of({'bacon', 'sausage', 'pancakes'})('pancakes') == 'pancakes'
with pytest.raises(KeyError):
hug.types.one_of({'bacon', 'sausage', 'pancakes'})('syrup')

0 comments on commit 7d22623

Please sign in to comment.