Skip to content

Commit

Permalink
Refactor using mark.parametrize
Browse files Browse the repository at this point in the history
  • Loading branch information
dkellner committed Aug 15, 2018
1 parent 076d843 commit d71dd94
Showing 1 changed file with 26 additions and 51 deletions.
77 changes: 26 additions & 51 deletions cerberus/tests/test_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from copy import deepcopy
from tempfile import NamedTemporaryFile

from pytest import mark

from cerberus import Validator, errors
from cerberus.tests import (
assert_fail,
Expand All @@ -12,6 +14,10 @@
)


def must_not_be_called(*args, **kwargs):
raise RuntimeError('This shall not be called.')


def test_coerce():
schema = {'amount': {'coerce': int}}
document = {'amount': '1'}
Expand Down Expand Up @@ -264,15 +270,10 @@ def test_coercion_of_sequence_items(validator):
assert isinstance(x, float)


def test_default_missing():
_test_default_missing({'default': 'bar_value'})


def test_default_setter_missing():
_test_default_missing({'default_setter': lambda doc: 'bar_value'})


def _test_default_missing(default):
@mark.parametrize(
'default', ({'default': 'bar_value'}, {'default_setter': lambda doc: 'bar_value'})
)
def test_default_missing(default):
bar_schema = {'type': 'string'}
bar_schema.update(default)
schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
Expand All @@ -281,53 +282,32 @@ def _test_default_missing(default):
assert_normalized(document, expected, schema)


def test_default_existent():
_test_default_existent({'default': 'bar_value'})


def test_default_setter_existent():
def raise_error(doc):
raise RuntimeError('should not be called')

_test_default_existent({'default_setter': raise_error})


def _test_default_existent(default):
@mark.parametrize(
'default', ({'default': 'bar_value'}, {'default_setter': must_not_be_called})
)
def test_default_existent(default):
bar_schema = {'type': 'string'}
bar_schema.update(default)
schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
document = {'foo': 'foo_value', 'bar': 'non_default'}
assert_normalized(document, document.copy(), schema)


def test_default_none_nullable():
_test_default_none_nullable({'default': 'bar_value'})


def test_default_setter_none_nullable():
def raise_error(doc):
raise RuntimeError('should not be called')

_test_default_none_nullable({'default_setter': raise_error})


def _test_default_none_nullable(default):
@mark.parametrize(
'default', ({'default': 'bar_value'}, {'default_setter': must_not_be_called})
)
def test_default_none_nullable(default):
bar_schema = {'type': 'string', 'nullable': True}
bar_schema.update(default)
schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
document = {'foo': 'foo_value', 'bar': None}
assert_normalized(document, document.copy(), schema)


def test_default_none_nonnullable():
_test_default_none_nonnullable({'default': 'bar_value'})


def test_default_setter_none_nonnullable():
_test_default_none_nonnullable({'default_setter': lambda doc: 'bar_value'})


def _test_default_none_nonnullable(default):
@mark.parametrize(
'default', ({'default': 'bar_value'}, {'default_setter': lambda doc: 'bar_value'})
)
def test_default_none_nonnullable(default):
bar_schema = {'type': 'string', 'nullable': False}
bar_schema.update(default)
schema = {'foo': {'type': 'string'}, 'bar': bar_schema}
Expand All @@ -346,15 +326,10 @@ def test_default_none_default_value():
assert_normalized(document, expected, schema)


def test_default_missing_in_subschema():
_test_default_missing_in_subschema({'default': 'bar_value'})


def test_default_setter_missing_in_subschema():
_test_default_missing_in_subschema({'default_setter': lambda doc: 'bar_value'})


def _test_default_missing_in_subschema(default):
@mark.parametrize(
'default', ({'default': 'bar_value'}, {'default_setter': lambda doc: 'bar_value'})
)
def test_default_missing_in_subschema(default):
bar_schema = {'type': 'string'}
bar_schema.update(default)
schema = {
Expand Down

0 comments on commit d71dd94

Please sign in to comment.