Buckets:

download
raw
4 kB
from typing import Any
import pytest
from pydantic import BaseModel, ValidationError
from fief.schemas.generics import Address, CountryCode, PhoneNumber, Timezone
class TestPhoneNumber:
class Model(BaseModel):
phone_number: PhoneNumber
@pytest.mark.parametrize("phone_number", ["123", "0102030405", "+33123"])
def test_invalid(self, phone_number: str):
with pytest.raises(ValidationError) as e:
TestPhoneNumber.Model(phone_number=phone_number)
errors = e.value.errors()
assert len(errors) == 1
assert errors[0]["loc"] == ("phone_number",)
@pytest.mark.parametrize(
"phone_number,formatted",
[
("+33102030405", "+33102030405"),
("+33 1 02 03 04 05", "+33102030405"),
],
)
def test_valid(self, phone_number: str, formatted: str):
o = TestPhoneNumber.Model(phone_number=phone_number)
assert o.phone_number == formatted
class TestCountryCode:
class Model(BaseModel):
country: CountryCode
@pytest.mark.parametrize("country", ["FRANCE", "ZZ"])
def test_invalid(self, country: str):
with pytest.raises(ValidationError) as e:
TestCountryCode.Model(country=country)
errors = e.value.errors()
assert len(errors) == 1
assert errors[0]["loc"] == ("country",)
@pytest.mark.parametrize(
"country,formatted",
[
("fr", "FR"),
("US", "US"),
],
)
def test_valid(self, country: str, formatted: str):
o = TestCountryCode.Model(country=country)
assert o.country == formatted
class TestAddress:
class Model(BaseModel):
address: Address
@pytest.mark.parametrize(
"address,nb_errors",
[
({}, 4),
({"line1": "4 place Marc Elder"}, 3),
(
{
"line1": "4 place Marc Elder",
"postal_code": "44000",
"city": "Nantes",
"country": "FRANCE",
},
1,
),
(
{
"line1": "",
"postal_code": "",
"city": "",
"country": "",
},
4,
),
],
)
def test_invalid(self, address: dict[str, Any], nb_errors: int):
with pytest.raises(ValidationError) as e:
TestAddress.Model(address=address)
errors = e.value.errors()
assert len(errors) == nb_errors
assert errors[0]["loc"][0] == "address"
def test_valid(self):
o = TestAddress.Model(
address={
"line1": "4 place Marc Elder",
"postal_code": "44000",
"city": "Nantes",
"country": "FR",
},
)
assert o.address.line1 == "4 place Marc Elder"
assert o.address.line2 is None
assert o.address.postal_code == "44000"
assert o.address.city == "Nantes"
assert o.address.state is None
assert o.address.country == "FR"
class TestTimezone:
class Model(BaseModel):
timezone: Timezone
@pytest.mark.parametrize("timezone", ["Europe/Nantes", "US/Paris"])
def test_invalid(self, timezone: str):
with pytest.raises(ValidationError) as e:
TestTimezone.Model(timezone=timezone)
errors = e.value.errors()
assert len(errors) == 1
assert errors[0]["loc"] == ("timezone",)
@pytest.mark.parametrize(
"timezone,formatted",
[
("Europe/Paris", "Europe/Paris"),
("US/Eastern", "US/Eastern"),
("EUROPE/PARIS", "Europe/Paris"),
("us/eastern", "US/Eastern"),
],
)
def test_valid(self, timezone: str, formatted: str):
o = TestTimezone.Model(timezone=timezone)
assert o.timezone == formatted

Xet Storage Details

Size:
4 kB
·
Xet hash:
171f1015c659d2a880b27599247720224c277cff2c6fcb313d3228071aabd053

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.