Skip to content

Commit

Permalink
Merge branch 'configure-strict-pytest' of github.com:graingert/fastap…
Browse files Browse the repository at this point in the history
…i into anyio
  • Loading branch information
graingert committed Jun 23, 2021
2 parents 15b9f76 + 9f50ab2 commit 0349712
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 70 deletions.
7 changes: 5 additions & 2 deletions docs_src/custom_response/tutorial008.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@

@app.get("/")
def main():
file_like = open(some_file_path, mode="rb")
return StreamingResponse(file_like, media_type="video/mp4")
def iterfile():
with open(some_file_path, mode="rb") as file_like:
yield from file_like

return StreamingResponse(iterfile(), media_type="video/mp4")
19 changes: 17 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Documentation = "https://fastapi.tiangolo.com/"

[tool.flit.metadata.requires-extra]
test = [
"pytest ==5.4.3",
"pytest-cov ==2.10.0",
"pytest ==6.2.4",
"pytest-cov ==2.12.1",
"mypy ==0.812",
"flake8 >=3.8.3,<4.0.0",
"black ==20.8b1",
Expand Down Expand Up @@ -99,3 +99,18 @@ all = [
[tool.isort]
profile = "black"
known_third_party = ["fastapi", "pydantic", "starlette"]

[tool.pytest.ini_options]
addopts = [
"--strict-config",
"--strict-markers",
]
xfail_strict = true
junit_family = "xunit2"
filterwarnings = [
"error",
'ignore:"@coroutine" decorator is deprecated since Python 3\.8, use "async def" instead:DeprecationWarning',
'ignore:The explicit passing of coroutine objects to asyncio\.wait\(\) is deprecated since Python 3\.8:DeprecationWarning',
'ignore:int_from_bytes is deprecated, use int\.from_bytes instead:cryptography.utils.CryptographyDeprecationWarning',
'ignore:Exception ignored in. <socket\.socket fd=-1:pytest.PytestUnraisableExceptionWarning',
]
32 changes: 15 additions & 17 deletions tests/test_tutorial/test_request_files/test_tutorial001.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from fastapi.testclient import TestClient

from docs_src.request_files.tutorial001 import app
Expand Down Expand Up @@ -152,35 +150,35 @@ def test_post_body_json():
assert response.json() == file_required


def test_post_file(tmpdir):
path = os.path.join(tmpdir, "test.txt")
with open(path, "wb") as file:
file.write(b"<file content>")
def test_post_file(tmp_path):
path = tmp_path / "test.txt"
path.write_bytes(b"<file content>")

client = TestClient(app)
response = client.post("/files/", files={"file": open(path, "rb")})
with path.open("rb") as file:
response = client.post("/files/", files={"file": file})
assert response.status_code == 200, response.text
assert response.json() == {"file_size": 14}


def test_post_large_file(tmpdir):
def test_post_large_file(tmp_path):
default_pydantic_max_size = 2 ** 16
path = os.path.join(tmpdir, "test.txt")
with open(path, "wb") as file:
file.write(b"x" * (default_pydantic_max_size + 1))
path = tmp_path / "test.txt"
path.write_bytes(b"x" * (default_pydantic_max_size + 1))

client = TestClient(app)
response = client.post("/files/", files={"file": open(path, "rb")})
with path.open("rb") as file:
response = client.post("/files/", files={"file": file})
assert response.status_code == 200, response.text
assert response.json() == {"file_size": default_pydantic_max_size + 1}


def test_post_upload_file(tmpdir):
path = os.path.join(tmpdir, "test.txt")
with open(path, "wb") as file:
file.write(b"<file content>")
def test_post_upload_file(tmp_path):
path = tmp_path / "test.txt"
path.write_bytes(b"<file content>")

client = TestClient(app)
response = client.post("/uploadfile/", files={"file": open(path, "rb")})
with path.open("rb") as file:
response = client.post("/uploadfile/", files={"file": file})
assert response.status_code == 200, response.text
assert response.json() == {"filename": "test.txt"}
56 changes: 26 additions & 30 deletions tests/test_tutorial/test_request_files/test_tutorial002.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from fastapi.testclient import TestClient

from docs_src.request_files.tutorial002 import app
Expand Down Expand Up @@ -172,42 +170,40 @@ def test_post_body_json():
assert response.json() == file_required


def test_post_files(tmpdir):
path = os.path.join(tmpdir, "test.txt")
with open(path, "wb") as file:
file.write(b"<file content>")
path2 = os.path.join(tmpdir, "test2.txt")
with open(path2, "wb") as file:
file.write(b"<file content2>")
def test_post_files(tmp_path):
path = tmp_path / "test.txt"
path.write_bytes(b"<file content>")
path2 = tmp_path / "test2.txt"
path2.write_bytes(b"<file content2>")

client = TestClient(app)
response = client.post(
"/files/",
files=(
("files", ("test.txt", open(path, "rb"))),
("files", ("test2.txt", open(path2, "rb"))),
),
)
with path.open("rb") as file, path2.open("rb") as file2:
response = client.post(
"/files/",
files=(
("files", ("test.txt", file)),
("files", ("test2.txt", file2)),
),
)
assert response.status_code == 200, response.text
assert response.json() == {"file_sizes": [14, 15]}


def test_post_upload_file(tmpdir):
path = os.path.join(tmpdir, "test.txt")
with open(path, "wb") as file:
file.write(b"<file content>")
path2 = os.path.join(tmpdir, "test2.txt")
with open(path2, "wb") as file:
file.write(b"<file content2>")
def test_post_upload_file(tmp_path):
path = tmp_path / "test.txt"
path.write_bytes(b"<file content>")
path2 = tmp_path / "test2.txt"
path2.write_bytes(b"<file content2>")

client = TestClient(app)
response = client.post(
"/uploadfiles/",
files=(
("files", ("test.txt", open(path, "rb"))),
("files", ("test2.txt", open(path2, "rb"))),
),
)
with path.open("rb") as file, path2.open("rb") as file2:
response = client.post(
"/uploadfiles/",
files=(
("files", ("test.txt", file)),
("files", ("test2.txt", file2)),
),
)
assert response.status_code == 200, response.text
assert response.json() == {"filenames": ["test.txt", "test2.txt"]}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import os
from pathlib import Path

from fastapi.testclient import TestClient

from docs_src.request_forms_and_files.tutorial001 import app
Expand Down Expand Up @@ -163,32 +160,30 @@ def test_post_body_json():
assert response.json() == file_and_token_required


def test_post_file_no_token(tmpdir):
path = os.path.join(tmpdir, "test.txt")
with open(path, "wb") as file:
file.write(b"<file content>")
def test_post_file_no_token(tmp_path):
path = tmp_path / "test.txt"
path.write_bytes(b"<file content>")

client = TestClient(app)
response = client.post("/files/", files={"file": open(path, "rb")})
with path.open("rb") as file:
response = client.post("/files/", files={"file": file})
assert response.status_code == 422, response.text
assert response.json() == token_required


def test_post_files_and_token(tmpdir):
patha = Path(tmpdir) / "test.txt"
pathb = Path(tmpdir) / "testb.txt"
def test_post_files_and_token(tmp_path):
patha = tmp_path / "test.txt"
pathb = tmp_path / "testb.txt"
patha.write_text("<file content>")
pathb.write_text("<file b content>")

client = TestClient(app)
response = client.post(
"/files/",
data={"token": "foo"},
files={
"file": patha.open("rb"),
"fileb": ("testb.txt", pathb.open("rb"), "text/plain"),
},
)
with patha.open("rb") as filea, pathb.open("rb") as fileb:
response = client.post(
"/files/",
data={"token": "foo"},
files={"file": filea, "fileb": ("testb.txt", fileb, "text/plain")},
)
assert response.status_code == 200, response.text
assert response.json() == {
"file_size": 14,
Expand Down

0 comments on commit 0349712

Please sign in to comment.