15 changed files with 223 additions and 0 deletions
@ -0,0 +1,18 @@ |
|||
import runpy |
|||
from unittest.mock import patch |
|||
|
|||
import pytest |
|||
|
|||
|
|||
@pytest.mark.parametrize( |
|||
"module_name", |
|||
[ |
|||
"tutorial001_py39", |
|||
"tutorial002_py39", |
|||
], |
|||
) |
|||
def test_run_module(module_name: str): |
|||
with patch("builtins.print") as mock_print: |
|||
runpy.run_module(f"docs_src.python_types.{module_name}", run_name="__main__") |
|||
|
|||
mock_print.assert_called_with("John Doe") |
|||
@ -0,0 +1,12 @@ |
|||
import pytest |
|||
|
|||
from docs_src.python_types.tutorial003_py39 import get_name_with_age |
|||
|
|||
|
|||
def test_get_name_with_age_pass_int(): |
|||
with pytest.raises(TypeError): |
|||
get_name_with_age("John", 30) |
|||
|
|||
|
|||
def test_get_name_with_age_pass_str(): |
|||
assert get_name_with_age("John", "30") == "John is this old: 30" |
|||
@ -0,0 +1,5 @@ |
|||
from docs_src.python_types.tutorial004_py39 import get_name_with_age |
|||
|
|||
|
|||
def test_get_name_with_age_pass_int(): |
|||
assert get_name_with_age("John", 30) == "John is this old: 30" |
|||
@ -0,0 +1,12 @@ |
|||
from docs_src.python_types.tutorial005_py39 import get_items |
|||
|
|||
|
|||
def test_get_items(): |
|||
res = get_items( |
|||
"item_a", |
|||
"item_b", |
|||
"item_c", |
|||
"item_d", |
|||
"item_e", |
|||
) |
|||
assert res == ("item_a", "item_b", "item_c", "item_d", "item_d", "item_e") |
|||
@ -0,0 +1,16 @@ |
|||
from unittest.mock import patch |
|||
|
|||
from docs_src.python_types.tutorial006_py39 import process_items |
|||
|
|||
|
|||
def test_process_items(): |
|||
with patch("builtins.print") as mock_print: |
|||
process_items(["item_a", "item_b", "item_c"]) |
|||
|
|||
assert mock_print.call_count == 3 |
|||
call_args = [arg.args for arg in mock_print.call_args_list] |
|||
assert call_args == [ |
|||
("item_a",), |
|||
("item_b",), |
|||
("item_c",), |
|||
] |
|||
@ -0,0 +1,8 @@ |
|||
from docs_src.python_types.tutorial007_py39 import process_items |
|||
|
|||
|
|||
def test_process_items(): |
|||
items_t = (1, 2, "foo") |
|||
items_s = {b"a", b"b", b"c"} |
|||
|
|||
assert process_items(items_t, items_s) == (items_t, items_s) |
|||
@ -0,0 +1,17 @@ |
|||
from unittest.mock import patch |
|||
|
|||
from docs_src.python_types.tutorial008_py39 import process_items |
|||
|
|||
|
|||
def test_process_items(): |
|||
with patch("builtins.print") as mock_print: |
|||
process_items({"a": 1.0, "b": 2.5}) |
|||
|
|||
assert mock_print.call_count == 4 |
|||
call_args = [arg.args for arg in mock_print.call_args_list] |
|||
assert call_args == [ |
|||
("a",), |
|||
(1.0,), |
|||
("b",), |
|||
(2.5,), |
|||
] |
|||
@ -0,0 +1,27 @@ |
|||
import importlib |
|||
from types import ModuleType |
|||
from unittest.mock import patch |
|||
|
|||
import pytest |
|||
|
|||
from ...utils import needs_py310 |
|||
|
|||
|
|||
@pytest.fixture( |
|||
name="module", |
|||
params=[ |
|||
pytest.param("tutorial008b_py39"), |
|||
pytest.param("tutorial008b_py310", marks=needs_py310), |
|||
], |
|||
) |
|||
def get_module(request: pytest.FixtureRequest): |
|||
mod = importlib.import_module(f"docs_src.python_types.{request.param}") |
|||
return mod |
|||
|
|||
|
|||
def test_process_items(module: ModuleType): |
|||
with patch("builtins.print") as mock_print: |
|||
module.process_item("a") |
|||
|
|||
assert mock_print.call_count == 1 |
|||
mock_print.assert_called_with("a") |
|||
@ -0,0 +1,33 @@ |
|||
import importlib |
|||
from types import ModuleType |
|||
from unittest.mock import patch |
|||
|
|||
import pytest |
|||
|
|||
from ...utils import needs_py310 |
|||
|
|||
|
|||
@pytest.fixture( |
|||
name="module", |
|||
params=[ |
|||
pytest.param("tutorial009_py39"), |
|||
pytest.param("tutorial009_py310", marks=needs_py310), |
|||
pytest.param("tutorial009b_py39"), |
|||
], |
|||
) |
|||
def get_module(request: pytest.FixtureRequest): |
|||
mod = importlib.import_module(f"docs_src.python_types.{request.param}") |
|||
return mod |
|||
|
|||
|
|||
def test_say_hi(module: ModuleType): |
|||
with patch("builtins.print") as mock_print: |
|||
module.say_hi("FastAPI") |
|||
module.say_hi() |
|||
|
|||
assert mock_print.call_count == 2 |
|||
call_args = [arg.args for arg in mock_print.call_args_list] |
|||
assert call_args == [ |
|||
("Hey FastAPI!",), |
|||
("Hello World",), |
|||
] |
|||
@ -0,0 +1,33 @@ |
|||
import importlib |
|||
import re |
|||
from types import ModuleType |
|||
from unittest.mock import patch |
|||
|
|||
import pytest |
|||
|
|||
from ...utils import needs_py310 |
|||
|
|||
|
|||
@pytest.fixture( |
|||
name="module", |
|||
params=[ |
|||
pytest.param("tutorial009c_py39"), |
|||
pytest.param("tutorial009c_py310", marks=needs_py310), |
|||
], |
|||
) |
|||
def get_module(request: pytest.FixtureRequest): |
|||
mod = importlib.import_module(f"docs_src.python_types.{request.param}") |
|||
return mod |
|||
|
|||
|
|||
def test_say_hi(module: ModuleType): |
|||
with patch("builtins.print") as mock_print: |
|||
module.say_hi("FastAPI") |
|||
|
|||
mock_print.assert_called_once_with("Hey FastAPI!") |
|||
|
|||
with pytest.raises( |
|||
TypeError, |
|||
match=re.escape("say_hi() missing 1 required positional argument: 'name'"), |
|||
): |
|||
module.say_hi() |
|||
@ -0,0 +1,5 @@ |
|||
from docs_src.python_types.tutorial010_py39 import Person, get_person_name |
|||
|
|||
|
|||
def test_get_person_name(): |
|||
assert get_person_name(Person("John Doe")) == "John Doe" |
|||
@ -0,0 +1,25 @@ |
|||
import runpy |
|||
from unittest.mock import patch |
|||
|
|||
import pytest |
|||
|
|||
from ...utils import needs_py310 |
|||
|
|||
|
|||
@pytest.mark.parametrize( |
|||
"module_name", |
|||
[ |
|||
pytest.param("tutorial011_py39"), |
|||
pytest.param("tutorial011_py310", marks=needs_py310), |
|||
], |
|||
) |
|||
def test_run_module(module_name: str): |
|||
with patch("builtins.print") as mock_print: |
|||
runpy.run_module(f"docs_src.python_types.{module_name}", run_name="__main__") |
|||
|
|||
assert mock_print.call_count == 2 |
|||
call_args = [str(arg.args[0]) for arg in mock_print.call_args_list] |
|||
assert call_args == [ |
|||
"id=123 name='John Doe' signup_ts=datetime.datetime(2017, 6, 1, 12, 22) friends=[1, 2, 3]", |
|||
"123", |
|||
] |
|||
@ -0,0 +1,7 @@ |
|||
from docs_src.python_types.tutorial012_py39 import User |
|||
|
|||
|
|||
def test_user(): |
|||
user = User(name="John Doe", age=30) |
|||
assert user.name == "John Doe" |
|||
assert user.age == 30 |
|||
@ -0,0 +1,5 @@ |
|||
from docs_src.python_types.tutorial013_py39 import say_hello |
|||
|
|||
|
|||
def test_say_hello(): |
|||
assert say_hello("FastAPI") == "Hello FastAPI" |
|||
Loading…
Reference in new issue