Skip to content

Commit

Permalink
Merge pull request #95 from mikeiken:test-6
Browse files Browse the repository at this point in the history
update tests
  • Loading branch information
Kseen715 authored Nov 18, 2024
2 parents 59424a3 + e625aa8 commit 5bffded
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions django/tamprog/garden/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_rent_bed_already_rented(beds, person):
bed.is_rented = True
bed.save()
result = BedService.rent_bed(bed_id=bed.id, person=person)
assert result is False
assert result.status_code == 400


@pytest.mark.django_db
Expand All @@ -47,7 +47,7 @@ def test_rent_bed_success(beds, person):
initial_count = bed.field.count_beds
result = BedService.rent_bed(bed_id=bed.id, person=person)
bed.refresh_from_db()
assert result is True
assert result.status_code == 200
assert bed.is_rented is True
assert bed.rented_by == person
assert bed.field.count_beds == initial_count - 1
Expand All @@ -62,7 +62,7 @@ def test_release_bed_success(beds, person):
initial_count = bed.field.count_beds
result = BedService.release_bed(bed_id=bed.id)
bed.refresh_from_db()
assert result is True
assert result.status_code == 200
assert bed.is_rented is False
assert bed.rented_by is None
assert bed.field.count_beds == initial_count + 1
Expand All @@ -74,7 +74,7 @@ def test_release_bed_not_rented(beds):
bed.is_rented = False
bed.save()
result = BedService.release_bed(bed_id=bed.id)
assert result is False
assert result.status_code == 400

@pytest.mark.django_db
def test_get_user_beds(beds, person):
Expand Down
38 changes: 19 additions & 19 deletions django/tamprog/orders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.utils import timezone
from .services import OrderService
from orders.models import Order
from rest_framework import status
from rest_framework.response import Response

@pytest.mark.django_db
def test_filter_orders(api_client, user, orders):
Expand All @@ -28,29 +30,34 @@ def test_calculate_total_cost(beds, plants, workers):

@pytest.mark.django_db
def test_create_order_success(user, workers, beds, plants, mocker):
mocker.patch("user.services.PersonService.update_wallet_balance", return_value=True)
mocker.patch(
"user.services.PersonService.update_wallet_balance",
return_value=Response(status=status.HTTP_200_OK)
)
action = "planting"

# Проверяем создание заказа для каждой комбинации worker, bed, plant
for worker, bed, plant in zip(workers, beds, plants):
order = OrderService.create_order(user, worker, bed, plant, action)
assert order is not None
response = OrderService.create_order(user, bed, plant, action)
assert response.status_code == status.HTTP_201_CREATED
order_id = response.data['order_id']
order = Order.objects.get(id=order_id)
assert order.user == user
assert order.worker == worker
assert order.worker is not None
assert order.bed == bed
assert order.plant == plant
assert order.total_cost == bed.field.price + plant.price + worker.price
assert order.total_cost == bed.field.price + plant.price + order.worker.price


@pytest.mark.django_db
def test_create_order_insufficient_funds(user, workers, beds, plants, mocker):
mocker.patch("user.services.PersonService.update_wallet_balance", return_value=False)
mocker.patch(
"user.services.PersonService.update_wallet_balance",
return_value=Response(status=status.HTTP_400_BAD_REQUEST, data={"error": "Insufficient funds"})
)
action = "planting"

# Проверяем создание заказа для каждой комбинации worker, bed, plant
for worker, bed, plant in zip(workers, beds, plants):
order = OrderService.create_order(user, worker, bed, plant, action)
assert order is None
response = OrderService.create_order(user, bed, plant, action)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.data['error'] == "Insufficient funds"


@pytest.mark.django_db
Expand All @@ -66,23 +73,16 @@ def test_complete_order(orders):
def test_filter_orders_completed(orders, mocker):
mock_time = timezone.now()
mocker.patch("django.utils.timezone.now", return_value=mock_time)

# Завершаем все заказы
for order in orders:
OrderService.complete_order(order)

completed_orders = OrderService.filter_orders(is_completed=True)

# Проверяем, что все заказы в completed_orders имеют заполненное поле completed_at
assert all(order.completed_at is not None for order in completed_orders)
assert len(completed_orders) == sum(1 for order in orders if order.completed_at is not None)


@pytest.mark.django_db
def test_filter_orders_not_completed(orders):
not_completed_orders = OrderService.filter_orders(is_completed=False)

# Проверяем, что все заказы в not_completed_orders не имеют значения в completed_at
assert all(order.completed_at is None for order in not_completed_orders)
assert len(not_completed_orders) == sum(1 for order in orders if order.completed_at is None)

Expand Down

0 comments on commit 5bffded

Please sign in to comment.