-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
416 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from app import create_app | ||
from settings import Settings | ||
|
||
app = create_app() | ||
app = create_app(Settings()) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# pytest.ini | ||
[pytest] | ||
asyncio_default_fixture_loop_scope = session |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import pytest | ||
import pytest_asyncio | ||
from sqlalchemy import URL, create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from app import create_app | ||
from const import LikeStatus | ||
from orm.base import Base | ||
from orm.cart import Cart | ||
from orm.like import Like | ||
from orm.product import Product | ||
from orm.user import User | ||
from settings import TestSettings | ||
from tests import helper | ||
|
||
|
||
@pytest_asyncio.fixture(scope="session") | ||
async def settings(): | ||
return TestSettings() | ||
|
||
|
||
@pytest_asyncio.fixture(scope="session") | ||
async def test_fastapi_app(settings): | ||
return create_app(settings) | ||
|
||
|
||
@pytest_asyncio.fixture(scope="function") | ||
async def engine(settings): | ||
url = URL.create( | ||
drivername="mysql+pymysql", | ||
username=settings.db_user, | ||
password=settings.db_password, | ||
host=settings.db_host, | ||
port=settings.db_port, | ||
database=settings.db_name | ||
) | ||
eng = create_engine( | ||
url=url | ||
) | ||
Base.metadata.create_all(eng) | ||
yield eng | ||
Base.metadata.drop_all(eng) | ||
|
||
|
||
@pytest_asyncio.fixture(scope="function") | ||
async def session(engine): | ||
sessionLocal = sessionmaker(engine) | ||
return sessionLocal() | ||
|
||
|
||
@pytest_asyncio.fixture(scope="function") | ||
async def user(): | ||
return [ | ||
User( | ||
id=1, | ||
email=helper.TEST_USER_EMAIL_1, | ||
password=helper.TEST_USER_PASSWORD_1 | ||
), | ||
User( | ||
id=2, | ||
email=helper.TEST_USER_EMAIL_2, | ||
password=helper.TEST_USER_PASSWORD_2 | ||
) | ||
] | ||
|
||
|
||
@pytest_asyncio.fixture(scope="function") | ||
async def product(): | ||
return [ | ||
Product( | ||
id=1, | ||
name=helper.TEST_PRODUCT_NAME_1, | ||
image_path=helper.TEST_PRODUCT_IMAGE_PATH_1, | ||
price=helper.TEST_PRODUCT_PRICE_1, | ||
summary=helper.TEST_PRODUCT_SUMMARY_1 | ||
), | ||
Product( | ||
id=2, | ||
name=helper.TEST_PRODUCT_NAME_2, | ||
image_path=helper.TEST_PRODUCT_IMAGE_PATH_2, | ||
price=helper.TEST_PRODUCT_PRICE_2, | ||
summary=helper.TEST_PRODUCT_SUMMARY_2 | ||
) | ||
] | ||
|
||
|
||
@pytest_asyncio.fixture(scope="function") | ||
async def shopping_cart(): | ||
return [ | ||
Cart( | ||
id=1, | ||
user_id=1, | ||
product_id=1, | ||
count=5 | ||
) | ||
] | ||
|
||
|
||
@pytest_asyncio.fixture(scope="function") | ||
async def like(): | ||
return [ | ||
Like( | ||
id=1, | ||
is_like=LikeStatus.DISLIKE, | ||
user_id=1, | ||
product_id=1 | ||
) | ||
] | ||
|
||
|
||
@pytest_asyncio.fixture(scope="function") | ||
async def bootstrap( | ||
session, | ||
user, | ||
product, | ||
shopping_cart, | ||
like, | ||
): | ||
session.bulk_save_objects(user) | ||
session.bulk_save_objects(product) | ||
session.bulk_save_objects(shopping_cart) | ||
session.bulk_save_objects(like) | ||
|
||
session.commit() | ||
|
||
yield "bootstrap" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import pytest | ||
from fastapi import status | ||
from httpx import ASGITransport, AsyncClient | ||
|
||
from tests import helper | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_list_shoppingcart_returns_200( | ||
test_fastapi_app, bootstrap, shopping_cart | ||
): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=test_fastapi_app), base_url="http://test" | ||
) as client: | ||
response = await client.get("/cart/1") | ||
|
||
assert response.status_code == status.HTTP_200_OK | ||
|
||
result = response.json() | ||
assert len(result["products"]) == len( | ||
[i for i in shopping_cart if i.user_id == 1] | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_add_shoppingcart_returns_201( | ||
test_fastapi_app, bootstrap | ||
): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=test_fastapi_app), base_url="http://test" | ||
) as client: | ||
response = await client.post( | ||
"/cart", | ||
headers={ | ||
"Content-Type": "application/json" | ||
}, | ||
json={ | ||
"user_id": 1, | ||
"product_id": 2 | ||
} | ||
) | ||
|
||
assert response.status_code == status.HTTP_201_CREATED | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_update_shoppingcart_returns_202( | ||
test_fastapi_app, bootstrap | ||
): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=test_fastapi_app), base_url="http://test" | ||
) as client: | ||
response = await client.put( | ||
"/cart/1", | ||
headers={ | ||
"Content-Type": "application/json" | ||
}, | ||
json={ | ||
"count": 123 | ||
} | ||
) | ||
|
||
assert response.status_code == status.HTTP_202_ACCEPTED | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_update_shoppingcart_returns_400_when_invalid_count( | ||
test_fastapi_app, bootstrap | ||
): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=test_fastapi_app), base_url="http://test" | ||
) as client: | ||
response = await client.put( | ||
"/cart/1", | ||
headers={ | ||
"Content-Type": "application/json" | ||
}, | ||
json={ | ||
"count": 0 | ||
} | ||
) | ||
|
||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_update_shoppingcart_returns_404( | ||
test_fastapi_app, bootstrap | ||
): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=test_fastapi_app), base_url="http://test" | ||
) as client: | ||
response = await client.put( | ||
"/cart/-1", | ||
headers={ | ||
"Content-Type": "application/json" | ||
}, | ||
json={ | ||
"count": 123 | ||
} | ||
) | ||
|
||
assert response.status_code == status.HTTP_404_NOT_FOUND | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_delete_shoppingcart_returns_204( | ||
test_fastapi_app, bootstrap | ||
): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=test_fastapi_app), base_url="http://test" | ||
) as client: | ||
response = await client.delete( | ||
"/cart/1", | ||
) | ||
|
||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_delete_shoppingcart_returns_404( | ||
test_fastapi_app, bootstrap | ||
): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=test_fastapi_app), base_url="http://test" | ||
) as client: | ||
response = await client.delete( | ||
"/cart/-1", | ||
) | ||
|
||
assert response.status_code == status.HTTP_404_NOT_FOUND |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pytest | ||
from fastapi import status | ||
from httpx import ASGITransport, AsyncClient | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_like_returns_201( | ||
test_fastapi_app, bootstrap | ||
): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=test_fastapi_app), base_url="http://test" | ||
) as client: | ||
response = await client.post( | ||
"/like", | ||
headers={ | ||
"Content-Type": "application/json" | ||
}, | ||
json={ | ||
"user_id": 2, | ||
"product_id": 1 | ||
} | ||
) | ||
assert response.status_code == status.HTTP_201_CREATED | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_dislike_returns_201( | ||
test_fastapi_app, bootstrap | ||
): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=test_fastapi_app), base_url="http://test" | ||
) as client: | ||
response = await client.post( | ||
"/dislike", | ||
headers={ | ||
"Content-Type": "application/json" | ||
}, | ||
json={ | ||
"user_id": 2, | ||
"product_id": 1 | ||
} | ||
) | ||
assert response.status_code == status.HTTP_201_CREATED |
Oops, something went wrong.