Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 9, 2021 12:35
Show Gist options
  • Save velotiotech/c0ca86a23d3f1724234ba172962d891c to your computer and use it in GitHub Desktop.
Save velotiotech/c0ca86a23d3f1724234ba172962d891c to your computer and use it in GitHub Desktop.
import random
import string
import pytest
from product.models import Category, Product, Retail
@pytest.fixture
def category(db) -> Category:
return Category.objects.create(name="Books")
@pytest.fixture
def retailer_abc(db):
return Retail.objects.create(name="ABC")
@pytest.fixture
def product_factory(db, 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))
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
def product_one(product_factory):
return product_factory(name="Book 1", mrp="100.2")
@pytest.fixture
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