-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Horlawhumy-dev/tests
Tests Models and Serializers
- Loading branch information
Showing
4 changed files
with
179 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
_#!/bin/bash | ||
export PYTHONPATH=./ | ||
rm -rf db.sqlite | ||
rm -rf db.sqlite3 | ||
pytest |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import pytest | ||
from django.contrib.auth import get_user_model | ||
from inventory.models import Product, Order, OrderItem | ||
|
||
User = get_user_model() | ||
|
||
@pytest.fixture | ||
def user(): | ||
return User.objects.create_user( | ||
username='testuser', | ||
password='password123', | ||
email='[email protected]' | ||
) | ||
|
||
@pytest.fixture | ||
def admin_user(): | ||
return User.objects.create_superuser( | ||
username='adminuser', | ||
password='adminpassword', | ||
email='[email protected]' | ||
) | ||
|
||
@pytest.mark.django_db | ||
def test_product_creation(user): | ||
product = Product.objects.create( | ||
name='Test Product', | ||
description='Test Description', | ||
quantity=100, | ||
price=10, | ||
owner=user | ||
) | ||
|
||
assert product.name == 'Test Product' | ||
assert product.description == 'Test Description' | ||
assert product.quantity == 100 | ||
assert product.price == 10 | ||
assert product.owner == user | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_order_creation(user): | ||
order = Order.objects.create(owner=user, status='pending') | ||
|
||
assert order.owner == user | ||
assert order.status == 'pending' | ||
assert order.items.count() == 0 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_order_item_creation(user): | ||
product = Product.objects.create( | ||
name='Test Product', | ||
description='Test Description', | ||
quantity=100, | ||
price=10, | ||
owner=user | ||
) | ||
order = Order.objects.create(owner=user, status='pending') | ||
order_item = OrderItem.objects.create(order=order, product=product, quantity=10) | ||
|
||
assert order_item.order == order | ||
assert order_item.product == product | ||
assert order_item.quantity == 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import pytest | ||
from rest_framework.exceptions import ValidationError | ||
from inventory.models import Product, Order, OrderItem | ||
from inventory.serializers import OrderSerializer, OrderItemSerializer | ||
from django.contrib.auth import get_user_model | ||
|
||
User = get_user_model() | ||
|
||
@pytest.mark.django_db | ||
def test_order_item_serializer(): | ||
user = User.objects.create_user(username='testuser', email='[email protected]', password='password') | ||
product = Product.objects.create(name='Test Product', description='Test Description', quantity=100, price=10, owner=user) | ||
order = Order.objects.create(owner=user, status='pending') | ||
|
||
data = { | ||
'product': product.id, | ||
'quantity': 10 | ||
} | ||
|
||
serializer = OrderItemSerializer(data=data) | ||
assert serializer.is_valid() | ||
item = serializer.save(order=order) | ||
|
||
assert item.order == order | ||
assert item.product == product | ||
assert item.quantity == 10 | ||
|
||
@pytest.mark.django_db | ||
def test_order_serializer(): | ||
user = User.objects.create_user(username='testuser2', email='[email protected]', password='password') | ||
product = Product.objects.create(name='Test Product2', description='Test Description', quantity=100, price=10, owner=user) | ||
|
||
data = { | ||
'status': 'pending', | ||
'items': [ | ||
{ | ||
'product': product.id, | ||
'quantity': 10 | ||
} | ||
] | ||
} | ||
|
||
serializer = OrderSerializer(data=data) | ||
assert serializer.is_valid() | ||
|
||
order = serializer.save(owner=user) | ||
|
||
assert order.owner == user | ||
assert order.status == 'pending' | ||
assert order.items.count() == 1 | ||
|
||
item = order.items.first() | ||
assert item.product == product | ||
assert item.quantity == 10 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_order_serializer_invalid_product(): | ||
user = User.objects.create_user(username='testuser3', email='[email protected]', password='password') | ||
|
||
data = { | ||
'status': 'pending', | ||
'items': [ | ||
{ | ||
'product': 999, # Invalid product ID | ||
'quantity': 10 | ||
} | ||
] | ||
} | ||
|
||
serializer = OrderSerializer(data=data) | ||
assert not serializer.is_valid() | ||
assert 'items' in serializer.errors | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_order_serializer_insufficient_stock(): | ||
user = User.objects.create_user(username='testuser4', email='[email protected]', password='password') | ||
product = Product.objects.create(name='Test Product3', description='Test Description', quantity=5, price=10, owner=user) | ||
|
||
data = { | ||
'status': 'pending', | ||
'items': [ | ||
{ | ||
'product': product.id, | ||
'quantity': 10 | ||
} | ||
] | ||
} | ||
|
||
serializer = OrderSerializer(data=data) | ||
assert serializer.is_valid() | ||
|
||
with pytest.raises(ValidationError) as excinfo: | ||
serializer.save(owner=user) | ||
|
||
assert 'Insufficient stock' in str(excinfo.value) | ||
|
||
@pytest.mark.django_db | ||
def test_order_serializer_partial_update(): | ||
user = User.objects.create_user(username='testuser5', email='[email protected]', password='password') | ||
product = Product.objects.create(name='Test Product4', description='Test Description', quantity=100, price=10, owner=user) | ||
order = Order.objects.create(owner=user, status='pending') | ||
OrderItem.objects.create(order=order, product=product, quantity=10) | ||
|
||
data = { | ||
'status': 'completed' | ||
} | ||
|
||
serializer = OrderSerializer(order, data=data, partial=True) | ||
assert serializer.is_valid() | ||
|
||
updated_order = serializer.save() | ||
assert updated_order.status == 'completed' | ||
assert updated_order.items.count() == 1 |