initial commit

This commit is contained in:
2024-11-10 07:27:36 -08:00
commit 5efd61d236
43 changed files with 1503 additions and 0 deletions

0
tests/__init__.py Normal file
View File

33
tests/conftest.py Normal file
View File

@@ -0,0 +1,33 @@
import sys
import pytest
from project_name import create_app
from project_name.ext.commands import populate_db
from project_name.ext.database import db
@pytest.fixture(scope="session")
def app():
app = create_app(FORCE_ENV_FOR_DYNACONF="testing")
with app.app_context():
db.create_all(app=app)
yield app
db.drop_all(app=app)
@pytest.fixture(scope="session")
def products(app):
with app.app_context():
return populate_db()
# each test runs on cwd to its temp dir
@pytest.fixture(autouse=True)
def go_to_tmpdir(request):
# Get the fixture dynamically by its name.
tmpdir = request.getfixturevalue("tmpdir")
# ensure local test created packages can be imported
sys.path.insert(0, str(tmpdir))
# Chdir only for the duration of the test.
with tmpdir.as_cwd():
yield

28
tests/test_api.py Normal file
View File

@@ -0,0 +1,28 @@
from decimal import Decimal
def test_products_get_all(client, products): # Arrange
"""Test get all products"""
# Act
response = client.get("/api/v1/product/")
# Assert
assert response.status_code == 200
data = response.json["products"]
assert len(data) == 3
for product in products:
assert product.id in [item["id"] for item in data]
assert product.name in [item["name"] for item in data]
assert product.price in [Decimal(item["price"]) for item in data]
def test_products_get_one(client, products): # Arrange
"""Test get one product"""
for product in products:
# Act
response = client.get(f"/api/v1/product/{product.id}")
data = response.json
# Assert
assert response.status_code == 200
assert data["name"] == product.name
assert Decimal(data["price"]) == product.price
assert data["description"] == product.description