def test_structure__invite_with_a_guest(): expected = Invite(email="*****@*****.**", guests=[Guest(first_name="Bobby Jim")]) value_to_structure = { "email": "*****@*****.**", "guests": [{ "first_name": "Bobby Jim" }], } assert structure(value_to_structure, Invite) == expected
def test_structure__datetime(): input_datetime = "2018-02-01T02:02:02+00:00" expected = datetime.datetime(2018, 2, 1, 2, 2, 2, tzinfo=datetime.timezone.utc) assert structure(input_datetime, datetime.datetime) == expected
def test_structure__bad_str__uuid(): with pytest.raises(ValueError) as exinfo: structure("Tomato", uuid.UUID) assert ( "Could not convert Tomato of type <class 'str'> into a <class 'uuid.UUID'>" in str(exinfo.value))
def test_structure__guest_without_first_name(): expected = SlottedGuest(first_name=None) assert structure({"first_name": None}, SlottedGuest) == expected
def test_structure__union__more_specific_types__datetime(): assert structure("Potato", Union[str, datetime]) == "Potato" datetime_obj = datetime(2018, 9, 1, 0, 57, 11, tzinfo=timezone.utc) datetime_str = "2018-09-01T00:57:11+00:00" assert structure(datetime_str, Union[str, datetime]) == datetime_obj
def test_structure__union__more_specific_types__none(): assert structure("Potato", Union[str, None]) == "Potato" assert structure(None, Union[str, None]) is None assert structure("Potato", Optional[str]) == "Potato" assert structure(None, Optional[str]) is None
def test_structure__homogeneous_list(): assert structure([], List[int]) == [] assert structure(["1"], List[int]) == [1] assert structure(["1"], List[str]) == ["1"]
def test_structure__bool(): assert structure(True, bool) is True assert structure(False, bool) is False
def test_structure__date(): assert structure("2018-08-28", datetime.date) == datetime.date(2018, 8, 28)
def test_structure__animal(): assert structure("BEE", Animal) == Animal.BEE
def test_structure__sounds(): assert structure("DOG", Sounds) == Sounds.DOG
def test_structure__hetrogeneous_set(): assert structure(["1", 2], Set[int]) == set([1, 2]) assert structure(["1", 2], Set[str]) == set(["1", "2"]) assert structure(["1", 2], Set[str]) == set(["1", "2"])
def test_structure__homogeneous_set(): assert structure([], Set[int]) == set([]) assert structure(["1"], Set[int]) == set([1]) assert structure(["1"], Set[str]) == set(["1"])
def test_structure__invite_with_no_guests(): expected = Invite(email="*****@*****.**", guests=[]) value_to_structure = {"email": "*****@*****.**", "guests": []} assert structure(value_to_structure, Invite) == expected
def test_structure__bad_str__str_enum(): with pytest.raises(ValueError) as exinfo: structure("Tomato", SoundsEnum) assert ( "Could not convert Tomato of type <class 'str'> into a <enum 'SoundsEnum'> enum." in str(exinfo.value))
def test_structure__float(): assert structure(1.0, float) == 1.0 assert structure(1, float) == 1.0 assert structure("1.0", float) == 1.0
def test_structure__decimal(): assert structure(1.0, decimal.Decimal) == decimal.Decimal(1.0) assert structure(1, decimal.Decimal) == decimal.Decimal(1.0) assert structure("1.0", decimal.Decimal) == decimal.Decimal(1.0)
def structure_assorted_primatives(): structure("Tomato", str) structure(1, int) structure(1.0, float)
def structure_assorted_simple_types(): structure("Tomato", str) structure(1, int) structure(1.0, float)
def structure_enums(): structure("BEE", AnimalEnum) structure("DOG", SoundsEnum)
def test_structure__uuid(): uuid_str = "d1337d56-4e6a-4cf7-8f7a-730bf4ea3fac" expected = uuid.UUID(uuid_str) assert structure(uuid_str, uuid.UUID) == expected
def structure_unions(): structure("Potato", typing.Union[int, str]) structure(1, typing.Union[int, str]) structure(None, typing.Optional[str]) structure("Potato", typing.Optional[str])
def test_structure__hetrogeneous_list(): assert structure(["1", 2], List[int]) == [1, 2] assert structure(["1", 2], List[str]) == ["1", "2"] assert structure(["1", 2], List[str]) == ["1", "2"]
def structure_dataclass(): structure({"first_name": "Bobby Jim"}, DataClassGuest)
def test_structure__union__more_specific_types__uuid(): assert structure("Potato", Union[str, UUID]) == "Potato" uuid_str = "a0b8bcd4-9c83-46bd-bc0b-d8d62d8c22d6" assert structure(uuid_str, Union[str, UUID]) == UUID(uuid_str)
def test_structure__str(): assert structure("Tomato", str) == "Tomato"
def test_structure__union__primatives(): assert structure("Potato", Union[int, str]) == "Potato" assert structure(1, Union[int, str]) == 1
def test_structure__int(): assert structure(1, int) == 1
def test_structure__guest_with_first_name(): expected = SlottedGuest(first_name="Bobby Jim") assert structure({"first_name": "Bobby Jim"}, SlottedGuest) == expected
def test_structure__bad_str__datetime(): with pytest.raises(ValueError) as exinfo: structure("Tomato", datetime.datetime) assert ( "Could not convert Tomato of type <class 'str'> into a <class 'datetime.datetime'>" in str(exinfo.value))