fooder-api/fooder/test/fixtures/product.py

64 lines
1.6 KiB
Python

import pytest
import pytest_asyncio
from fooder.controller.product import ProductController
from fooder.model.product import ProductCreateModel
from fooder.utils import product_finder
from fooder.utils.product_finder import ExternalProduct
@pytest.fixture
def product_payload():
return {
"name": "Chicken Breast",
"protein": 31.0,
"carb": 0.0,
"fat": 3.6,
"fiber": 0.0,
}
@pytest_asyncio.fixture
async def product(ctx):
data = ProductCreateModel(name="Chicken Breast", protein=31.0, carb=0.0, fat=3.6, fiber=0.0)
async with ctx.repo.transaction():
ctrl = await ProductController.create(ctx, data)
return ctrl.obj
@pytest_asyncio.fixture
async def product_with_barcode(ctx):
data = ProductCreateModel(name="Barcoded Product", protein=10.0, carb=5.0, fat=2.0, fiber=1.0, barcode="1234567890")
async with ctx.repo.transaction():
ctrl = await ProductController.create(ctx, data)
return ctrl.obj
@pytest.fixture
def external_product():
return ExternalProduct(
name="External Brand External Product",
kcal=250.0,
fat=8.0,
protein=20.0,
carb=30.0,
fiber=3.0,
)
@pytest.fixture
def mock_product_finder(monkeypatch, external_product):
async def fake_find(barcode: str) -> ExternalProduct:
return external_product
monkeypatch.setattr(product_finder, "find", fake_find)
@pytest.fixture
def mock_product_finder_not_found(monkeypatch):
async def fake_find(barcode: str) -> ExternalProduct:
raise product_finder.NotFound()
monkeypatch.setattr(product_finder, "find", fake_find)