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

Added support for default values in Inclusive schemas #382

Merged
merged 3 commits into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,10 @@ class Inclusive(Optional):
True
"""

def __init__(self, schema, group_of_inclusion, msg=None, description=None):
def __init__(self, schema, group_of_inclusion,
msg=None, description=None, default=UNDEFINED):
super(Inclusive, self).__init__(schema, msg=msg,
default=default,
description=description)
self.group_of_inclusion = group_of_inclusion

Expand Down
57 changes: 57 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,3 +1349,60 @@ def test_any_required_with_subschema():
"required key not provided @ data['a']")
else:
assert False, "Did not raise Invalid for MultipleInvalid"

def test_inclusive():
schema = Schema({
Inclusive('x', 'stuff'): int,
Inclusive('y', 'stuff'): int,
})

r = schema({})
assert_equal(r, {})

r = schema({'x':1, 'y':2})
assert_equal(r, {'x':1, 'y':2})

try:
r = schema({'x':1})
except MultipleInvalid as e:
assert_equal(str(e),
"some but not all values in the same group of inclusion 'stuff' @ data[<stuff>]")
else:
assert False, "Did not raise Invalid for incomplete Inclusive group"

def test_inclusive_defaults():
schema = Schema({
Inclusive('x', 'stuff', default=3): int,
Inclusive('y', 'stuff', default=4): int,
})

r = schema({})
assert_equal(r, {'x':3, 'y':4})

try:
r = schema({'x':1})
except MultipleInvalid as e:
assert_equal(str(e),
"some but not all values in the same group of inclusion 'stuff' @ data[<stuff>]")
else:
assert False, "Did not raise Invalid for incomplete Inclusive group with defaults"

def test_exclusive():
schema = Schema({
Exclusive('x', 'stuff'): int,
Exclusive('y', 'stuff'): int,
})

r = schema({})
assert_equal(r, {})

r = schema({'x':1})
assert_equal(r, {'x':1})

try:
r = schema({'x':1, 'y': 2})
except MultipleInvalid as e:
assert_equal(str(e),
"two or more values in the same group of exclusion 'stuff' @ data[<stuff>]")
else:
assert False, "Did not raise Invalid for multiple values in Exclusive group"