Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 9, 2021 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velotiotech/3464eab9ebb0da6e2cf5b23a9724a301 to your computer and use it in GitHub Desktop.
Save velotiotech/3464eab9ebb0da6e2cf5b23a9724a301 to your computer and use it in GitHub Desktop.
import random
import string
import pytest
from product.models import Category, Product, Retail
@pytest.fixture(scope="module")
def category(django_db_blocker):
with django_db_blocker.unblock():
return Category.objects.create(name="Books")
@pytest.fixture(scope="module")
def retailer_abc(django_db_blocker):
with django_db_blocker.unblock():
return Retail.objects.create(name="ABC")
@pytest.fixture(scope="module")
def product_factory(django_db_blocker, category, retailer_abc):
def create_product(
name, description="A Book", mrp=None, is_available=True, retailers=None
):
if retailers is None:
retailers = []
sku = "".join(random.choices(
string.ascii_uppercase + string.digits, k=6)
)
with django_db_blocker.unblock():
product = Product.objects.create(
sku=sku,
name=name,
description=description,
mrp=mrp,
is_available=is_available,
category=category,
)
product.retails.add(retailer_abc)
if retailers:
product.retails.set(retailers)
return product
return create_product
@pytest.fixture(scope="module")
def product_one(product_factory):
return product_factory(name="Book 1", mrp="100.2")
@pytest.fixture(scope="module")
def product_two(product_factory):
return product_factory(name="Novel Book", mrp="51")
def test_product_retailer(db, retailer_abc, product_one):
assert product_one.retails.filter(name=retailer_abc.name).exists()
def test_product_one(product_one):
assert product_one.name == "Book 1"
assert product_one.is_available
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment