Skip to content

Commit 846d280

Browse files
🧪 test: add tests for ProductLine model's clean method
1 parent 587f809 commit 846d280

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

drfecommerce/test/conftest.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from pytest_factoryboy import register
44
from rest_framework.test import APIClient
5-
from .factories import CategoryFactory, BrandFactory, ProductFactory
5+
from .factories import CategoryFactory, BrandFactory, ProductFactory, ProductLineFactory
66

77
register(CategoryFactory)
88
register(BrandFactory)
99
register(ProductFactory)
10+
register(ProductLineFactory)
1011

1112

1213
@pytest.fixture

drfecommerce/test/factories.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import factory
2-
from drfecommerce.product.models import Category, Brand, Product
2+
from drfecommerce.product.models import Category, Brand, Product, ProductLine
33

44

55
class CategoryFactory(factory.django.DjangoModelFactory):
@@ -26,3 +26,13 @@ class Meta:
2626
description = "test_description"
2727
is_digital = True
2828
is_active = True
29+
30+
31+
class ProductLineFactory(factory.django.DjangoModelFactory):
32+
class Meta:
33+
model = ProductLine
34+
price = 10.00
35+
sku = '12345'
36+
stock_qty = 1
37+
product = factory.SubFactory(ProductFactory)
38+
is_active = True

drfecommerce/test/product/test_models.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from django.core.exceptions import ValidationError
23
pytestmark = pytest.mark.django_db
34

45

@@ -27,3 +28,18 @@ def test_str_method(self, product_factory):
2728
obj = product_factory(name="test_product")
2829
# Assert
2930
assert obj.__str__() == "test_product"
31+
32+
33+
class TestProductLineModel:
34+
def test_str_method(self, product_line_factory):
35+
# Arrange
36+
# Act
37+
obj = product_line_factory(sku="12345")
38+
# Assert
39+
assert obj.__str__() == "12345"
40+
41+
def test_duplicate_order_values(self, product_line_factory, product_factory):
42+
obj = product_factory()
43+
product_line_factory(order=1, product=obj)
44+
with pytest.raises(ValidationError):
45+
product_line_factory(order=1, product=obj).clean()

pytest.ini

-4
This file was deleted.

setup.cfg

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[tool:pytest]
2+
DJANGO_SETTINGS_MODULE = drfecommerce.settings.local
3+
4+
python_files = test_*.py
5+
6+
; addopts = --cov

0 commit comments

Comments
 (0)