class MyContract(Contract): property1 = fields.Boolean() # property2 = fields.Date() property3 = fields.Integer() property4 = fields.String() property5 = fields.List(fields.Integer()) # property6 = fields.UUID() # property7 = fields.DateTime() property8 = fields.Float() property9 = fields.Nested(NestedContract)
def test_valid_outputs(self): field = fields.List(fields.Integer()) self._dump_equal(field, [], []) self._dump_equal(field, [1, 2, 3], [1, 2, 3]) self._dump_equal(field, ['1', '2', '3'], [1, 2, 3]) self._dump_equal(field, {1, 2, 3}, [1, 2, 3]) self._dump_equal(field, ('1', '2', '3'), [1, 2, 3])
def test_valid_inputs(self): field = fields.List(fields.Integer()) self._load_equal(field, [], []) self._load_equal(field, [1, 2, 3], [1, 2, 3]) self._load_equal(field, ['1', '2', '3'], [1, 2, 3]) self._load_equal(field, {1, 2}, [1, 2]) self._load_equal(field, (1, 2), [1, 2])
def test_invalid_outputs(self): field = fields.Integer() self._dump_raises(field, 'abc', "invalid literal for int() with base 10: 'abc'") self._dump_raises( field, [], "int() argument must be a string or a number, not 'list'")
def test_valid_inputs(self): field = fields.Integer() self._load_equal(field, '1', 1) self._load_equal(field, '0', 0) self._load_equal(field, 1, 1) self._load_equal(field, 0, 0) self._load_equal(field, 1.0, 1) self._load_equal(field, 0.0, 0)
def test_disallow_empty(self): field = fields.List(fields.Integer(), allow_empty=False) self._load_raises(field, [], ['This list may not be empty.'])
def test_invalid_inputs(self): field = fields.List(fields.Integer()) self._load_raises(field, 'not a list', ['Not a valid list.']) self._load_raises(field, [1, 2, 'error'], [{ 2: ['A valid integer is required.'] }])
def test_valid_outputs(self): field = fields.Integer(min_value=1, max_value=3) self._dump_equal(field, 0, 0) self._dump_equal(field, 2, 2) self._dump_equal(field, 4, 4)
def test_invalid_inputs(self): field = fields.Integer(min_value=1, max_value=3) self._load_raises(field, 0, ['Must be at least 1.']) self._load_raises(field, 4, ['Must be at most 3.'])
def test_valid_inputs(self): field = fields.Integer(min_value=1, max_value=3) self._load_equal(field, 1, 1) self._load_equal(field, 3, 3)
def test_valid_outputs(self): field = fields.Integer() self._dump_equal(field, 1, 1) self._dump_equal(field, 1.0, 1) self._dump_equal(field, '1', 1)
def test_invalid_inputs(self): field = fields.Integer() self._load_raises(field, 'abc', ['A valid integer is required.']) self._load_raises(field, '1.0', ['A valid integer is required.'])