This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
def test_given_dicts_with_the_same_keys_values_then_they_are_equal(): | |
user = {"name": "John", "lastname": "Doe", "age": 42} | |
employee = {"name": "John", "lastname": "Doe", "age": 42} | |
assert user == employee, "Ideally, these would be different" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Dict | |
ids_to_names: Dict[str, str] = { | |
"1234": "John", | |
"2345": "Doe", | |
"3456": "Alice", | |
"4567": "Bob", | |
} # 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def crawl_page(url, crawl_request): | |
output_file = crawl_request["output_file"] # 1 | |
num_retries = crawl_request["num_retries"] # 1 | |
is_polite = crawl_request["is_polite"] # 1 | |
follow_redirects = crawl_request["follow_redirects"] # 1 | |
# rest of the implementation excluded for the sake of brevity | |
crawl_request = { | |
"output_file": "page_content.csv", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def crawl_page(url, output_file, num_retries): # 1 | |
""" | |
Implementation omitted for the sake of brevity | |
""" | |
crawl_page( | |
url="http://hopefully.doesnt.exi.st", output_file="page_content.csv", num_retries=4 | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from typing import List | |
from pydantic import BaseModel | |
class Names(BaseModel): | |
values: List[str] | |
def test_values_is_not_part_of_the_input_data(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from pydantic import BaseModel, ValidationError, StrictInt, StrictBool | |
class UserResponse(BaseModel): | |
user_input: StrictBool # 1 | |
def test_only_bool_responses_are_accepted(): | |
assert UserResponse(user_input=True).user_input is True # 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel, PositiveInt | |
class UserResponse(BaseModel): | |
user_input: bool # 1 | |
def test_what_did_the_user_mean(): | |
assert UserResponse(user_input='yes').user_input is True # 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from pydantic import BaseModel, conlist, constr, ValidationError | |
class User(BaseModel): | |
name: constr(min_length=1) # 1 | |
scores: conlist(int, min_items=1) # 2 | |
def report(user: User): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from pydantic import BaseModel, ValidationError | |
class Pizza(BaseModel): | |
toppings_count: int | |
size: str | |
class Config: # 1 | |
validate_assignment = True # 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel | |
class Pizza(BaseModel): | |
toppings_count: int | |
size: str | |
def test_set_field_doesnt_perform_coercion(): | |
pizza = Pizza(toppings_count='4', size='XL') # 1 |