def test_array_with_wrong_type(): req = { "username": "******", "password": "******", "titles": ["Programmer", 42], } with pytest.raises(ValidationFailed) as excinfo: rigour.from_json(Request, req) message = str(excinfo) print(message) assert "expected string" in message
def test_nonexistent(): req = { "username": "******", "password": "******", "this_field_does_not_exist": 42, } with pytest.raises(ValidationFailed) as excinfo: rigour.from_json(Request, req) message = str(excinfo) print(message) assert "'this_field_does_not_exist'" in message
def test_fixed_array_wrong_length(): req = { "username": "******", "password": "******", "position": [123, 456, 789], } with pytest.raises(ValidationFailed) as excinfo: rigour.from_json(Request, req) message = str(excinfo) print(message) assert "expected 2 elements, got 3" in message
def test_basic_invalid(): req = { "username": "******", "password": "******", } with pytest.raises(ValidationFailed) as excinfo: rigour.from_json(Request, req) message = str(excinfo) print(message) assert "username:"******"too short" in message assert "value was: 'x'" in message
def test_string_enum_invalid(): req = { "username": "******", "password": "******", "gender": "femal3", } with pytest.raises(ValidationFailed) as excinfo: rigour.from_json(Request, req) message = str(excinfo) print(message) assert "gender:" in message assert "femal3" in message assert "female" in message
def test_luhn_validation(): req = { "username": "******", "password": "******", "payment_info": { "card_number": "1234-5678-9012-3456", } } with pytest.raises(ValidationFailed) as excinfo: rigour.from_json(Request, req) message = str(excinfo) print(message) assert "invalid check digit" in message
def test_required_nested_field(): req = { "username": "******", "password": "******", "name": { "given_name": "Harald", }, } with pytest.raises(ValidationFailed) as excinfo: rigour.from_json(Request, req) message = str(excinfo) print(message) assert "name: missing field 'family_name'" in message
def test_secret_invalid(): req = { "username": "******", "password": "******", } with pytest.raises(ValidationFailed) as excinfo: rigour.from_json(Request, req) message = str(excinfo) print(message) assert "password:"******"too short" in message assert "hunt" not in message assert "value was: <elided>" in message
def test_string_enum_valid(): req = { "username": "******", "password": "******", "gender": "female", } val = rigour.from_json(Request, req) assert val.gender == "female"
def test_array(): req = { "username": "******", "password": "******", "titles": ["Programmer"], } val = rigour.from_json(Request, req) assert val.titles == ["Programmer"]
def test_extra_number(): req = { "username": "******", "password": "******", "extra": 4, } val = rigour.from_json(Request, req) assert val.extra == 4
def test_basic_valid(): req = { "username": "******", "password": "******", } val = rigour.from_json(Request, req) assert val.username == "svk" assert val.password == "hunter2"
def test_fixed_array_valid(): req = { "username": "******", "password": "******", "position": [123, 456], } val = rigour.from_json(Request, req) assert val.position[0] == 123 assert val.position[1] == 456
def test_timestamp(): req = { "username": "******", "password": "******", "timestamp": "2015-08-20T01:58:42.205677 UTC" } val = rigour.from_json(Request, req) assert val.timestamp.year == 2015 assert val.timestamp.second == 42 assert val.timestamp.tzinfo
def test_birthdate(): req = { "username": "******", "password": "******", "birthdate": "1937-02-21", } val = rigour.from_json(Request, req) assert val.birthdate.year == 1937 assert val.birthdate.month == 2 assert val.birthdate.day == 21
def test_extra_dict(): req = { "username": "******", "password": "******", "extra": { "hello": "world", "hi": "there", }, } val = rigour.from_json(Request, req) assert val.extra == {"hello": "world", "hi": "there"}
def test_nested_fields(): req = { "username": "******", "password": "******", "name": { "given_name": "Buffy", "middle_name": "Anne", "family_name": "Summers", }, } val = rigour.from_json(Request, req) assert val.name.given_name == "Buffy" assert val.name.middle_name == "Anne" assert val.name.family_name == "Summers"
def test_basic_roundtrip(): req = { "username": "******", "password": "******", "birthdate": "1937-02-21", "timestamp": "2015-08-20T01:58:42.205677+00:00", "titles": ["Programmer"], "payment_info": { "card_number": "4111-1111-1111-1111", }, "name": { "given_name": "Harald", "family_name": "Rex", }, "position": [123, 456], "gender": "male", } val = rigour.from_json(Request, req) js = rigour.to_json(Request, val) assert req == js