Exemple #1
0
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
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
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
Exemple #5
0
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
Exemple #6
0
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
Exemple #7
0
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
Exemple #8
0
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
Exemple #9
0
def test_string_enum_valid():
  req = {
    "username": "******",
    "password": "******",
    "gender": "female",
  }
  val = rigour.from_json(Request, req)
  assert val.gender == "female"
Exemple #10
0
def test_array():
  req = {
    "username": "******",
    "password": "******",
    "titles": ["Programmer"],
  }
  val = rigour.from_json(Request, req)
  assert val.titles == ["Programmer"]
Exemple #11
0
def test_extra_number():
  req = {
    "username": "******",
    "password": "******",
    "extra": 4,
  }
  val = rigour.from_json(Request, req)
  assert val.extra == 4
Exemple #12
0
def test_basic_valid():
  req = {
    "username": "******",
    "password": "******",
  }
  val = rigour.from_json(Request, req)
  assert val.username == "svk"
  assert val.password == "hunter2"
Exemple #13
0
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
Exemple #14
0
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
Exemple #15
0
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
Exemple #16
0
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"}
Exemple #17
0
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"
Exemple #18
0
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