Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend tests for Schema with empty list or dict #438

Merged
merged 1 commit into from
Dec 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 47 additions & 16 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,56 @@ def test_maybe_returns_subvalidator_error():
assert False, "Did not raise correct Invalid"


def test_empty_list_as_exact():
def test_schema_empty_list():
s = Schema([])
assert_raises(Invalid, s, [1])
s([])

try:
s([123])
except MultipleInvalid as e:
assert_equal(str(e), "not a valid value @ data[123]")
else:
assert False, "Did not raise correct Invalid"

try:
s({'var': 123})
except MultipleInvalid as e:
assert_equal(str(e), "expected a list")
else:
assert False, "Did not raise correct Invalid"


def test_schema_empty_dict():
s = Schema({})
s({})

try:
s({'var': 123})
except MultipleInvalid as e:
assert_equal(str(e), "extra keys not allowed @ data['var']")
else:
assert False, "Did not raise correct Invalid"

try:
s([123])
except MultipleInvalid as e:
assert_equal(str(e), "expected a dictionary")
else:
assert False, "Did not raise correct Invalid"


def test_schema_empty_dict_key():
""" https://github.com/alecthomas/voluptuous/pull/434 """
s = Schema({'var': []})
s({'var': []})

try:
s({'var': [123]})
except MultipleInvalid as e:
assert_equal(str(e), "not a valid value for dictionary value @ data['var']")
else:
assert False, "Did not raise correct Invalid"


def test_schema_decorator_match_with_args():
@validate(int)
Expand Down Expand Up @@ -1554,17 +1599,3 @@ def test_any_with_discriminant():
assert_equal(str(e), 'expected bool for dictionary value @ data[\'implementation\'][\'c-value\']')
else:
assert False, "Did not raise correct Invalid"


def test_empty_list_raises_error_of_key_not_values():
""" https://github.com/alecthomas/voluptuous/issues/397 """
schema = Schema({
Required('variables', default=[]): []
})

try:
schema({'variables': ['x']})
except MultipleInvalid as e:
assert_equal(str(e), "not a valid value for dictionary value @ data['variables']")
else:
assert False, "Did not raise correct Invalid"