-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconftest.py
More file actions
65 lines (47 loc) · 1.52 KB
/
conftest.py
File metadata and controls
65 lines (47 loc) · 1.52 KB
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
from uuid import uuid4
import pytest
from cart.models import Cart
from django.contrib.auth import get_user_model
from model_bakery import baker
from products.models import Category, Product
User = get_user_model()
@pytest.fixture
def user(db):
return baker.make(
User,
email="u@example.com",
phone="+15550000001",
first_name="Test",
last_name="User",
)
@pytest.fixture
def category(db) -> Category:
"""A basic category."""
return baker.make(Category)
@pytest.fixture
def product(db, category: Category) -> Product:
"""An available in-stock product."""
return baker.make(Product, category=category, stock=10, is_available=True)
@pytest.fixture
def cart(db) -> Cart:
"""A session-bound cart."""
return baker.make(Cart, session_key=f"sk-{uuid4().hex[:8]}")
@pytest.fixture
def product_factory(db, category: Category):
"""Factory function to create products with sensible defaults."""
def _make(**overrides) -> Product:
defaults = dict(category=category, stock=10, is_available=True)
defaults.update(overrides)
return baker.make(Product, **defaults)
return _make
@pytest.fixture
def ajax(client):
"""Helper to POST as AJAX with JSON-acceptable headers."""
def _post(url, data=None, **extra):
headers = {
"HTTP_X_REQUESTED_WITH": "XMLHttpRequest",
"HTTP_ACCEPT": "application/json",
}
headers.update(extra)
return client.post(url, data or {}, **headers)
return _post