Browse Source

Add tests for `docs_src.python_types.**`

pull/14569/head
Yurii Motov 7 months ago
parent
commit
1022dded86
  1. 0
      tests/test_tutorial/test_python_types/__init__.py
  2. 18
      tests/test_tutorial/test_python_types/test_tutorial001_tutorial002.py
  3. 12
      tests/test_tutorial/test_python_types/test_tutorial003.py
  4. 5
      tests/test_tutorial/test_python_types/test_tutorial004.py
  5. 12
      tests/test_tutorial/test_python_types/test_tutorial005.py
  6. 16
      tests/test_tutorial/test_python_types/test_tutorial006.py
  7. 8
      tests/test_tutorial/test_python_types/test_tutorial007.py
  8. 17
      tests/test_tutorial/test_python_types/test_tutorial008.py
  9. 27
      tests/test_tutorial/test_python_types/test_tutorial008b.py
  10. 33
      tests/test_tutorial/test_python_types/test_tutorial009_tutorial009b.py
  11. 33
      tests/test_tutorial/test_python_types/test_tutorial009c.py
  12. 5
      tests/test_tutorial/test_python_types/test_tutorial010.py
  13. 25
      tests/test_tutorial/test_python_types/test_tutorial011.py
  14. 7
      tests/test_tutorial/test_python_types/test_tutorial012.py
  15. 5
      tests/test_tutorial/test_python_types/test_tutorial013.py

0
tests/test_tutorial/test_python_types/__init__.py

18
tests/test_tutorial/test_python_types/test_tutorial001_tutorial002.py

@ -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")

12
tests/test_tutorial/test_python_types/test_tutorial003.py

@ -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"

5
tests/test_tutorial/test_python_types/test_tutorial004.py

@ -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"

12
tests/test_tutorial/test_python_types/test_tutorial005.py

@ -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")

16
tests/test_tutorial/test_python_types/test_tutorial006.py

@ -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",),
]

8
tests/test_tutorial/test_python_types/test_tutorial007.py

@ -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)

17
tests/test_tutorial/test_python_types/test_tutorial008.py

@ -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,),
]

27
tests/test_tutorial/test_python_types/test_tutorial008b.py

@ -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")

33
tests/test_tutorial/test_python_types/test_tutorial009_tutorial009b.py

@ -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",),
]

33
tests/test_tutorial/test_python_types/test_tutorial009c.py

@ -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()

5
tests/test_tutorial/test_python_types/test_tutorial010.py

@ -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"

25
tests/test_tutorial/test_python_types/test_tutorial011.py

@ -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",
]

7
tests/test_tutorial/test_python_types/test_tutorial012.py

@ -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

5
tests/test_tutorial/test_python_types/test_tutorial013.py

@ -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…
Cancel
Save