-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
_tests.py
71 lines (55 loc) · 2.15 KB
/
_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# mypy: no-disallow-untyped-decorators
# pylint: disable=E0611,E0401
import datetime
from typing import AsyncGenerator
import pytest
import pytz
from asgi_lifespan import LifespanManager
from httpx import ASGITransport, AsyncClient
from main import app, app_east
from models import Users
@pytest.fixture(scope="module")
def anyio_backend() -> str:
return "asyncio"
@pytest.fixture(scope="module")
async def client() -> AsyncGenerator[AsyncClient, None]:
async with LifespanManager(app):
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as c:
yield c
@pytest.mark.anyio
async def test_create_user(client: AsyncClient) -> None: # nosec
response = await client.post("/users", json={"username": "admin"})
assert response.status_code == 200, response.text
data = response.json()
assert data["username"] == "admin"
assert "id" in data
user_id = data["id"]
user_obj = await Users.get(id=user_id)
assert user_obj.id == user_id
@pytest.fixture(scope="module")
async def client_east() -> AsyncGenerator[AsyncClient, None]:
async with LifespanManager(app_east):
transport = ASGITransport(app=app_east)
async with AsyncClient(transport=transport, base_url="http://test") as c:
yield c
@pytest.mark.anyio
async def test_create_user_east(client_east: AsyncClient) -> None: # nosec
response = await client_east.post("/users_east", json={"username": "admin"})
assert response.status_code == 200, response.text
data = response.json()
assert data["username"] == "admin"
assert "id" in data
user_id = data["id"]
user_obj = await Users.get(id=user_id)
assert user_obj.id == user_id
# Verify that the time zone is East 8.
created_at = user_obj.created_at
# Asia/Shanghai timezone
asia_tz = pytz.timezone("Asia/Shanghai")
asia_now = datetime.datetime.now(pytz.utc).astimezone(asia_tz)
assert created_at.hour - asia_now.hour == 0
# UTC timezone
utc_tz = pytz.timezone("UTC")
utc_now = datetime.datetime.now(pytz.utc).astimezone(utc_tz)
assert created_at.hour - utc_now.hour == 8