From d6bc7aad9d1916bd4c074f9e5d5b408b72a5b205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=9C?= =?UTF-8?q?=D0=B5=D0=BD=D1=8E=D1=85=D0=BE=D0=B2?= Date: Mon, 8 Apr 2024 17:17:12 +0300 Subject: [PATCH] Update tests. --- .../test_materials_statistic_viewset.py | 16 +++---- tests/fixtures/apps/companies/__init__.py | 2 + .../apps/companies/materials_statistic.py | 43 +++++++++++++------ 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/tests/apps/companies/test_materials_statistic_viewset.py b/tests/apps/companies/test_materials_statistic_viewset.py index 800b59a..982246a 100644 --- a/tests/apps/companies/test_materials_statistic_viewset.py +++ b/tests/apps/companies/test_materials_statistic_viewset.py @@ -136,6 +136,7 @@ def test_materials_statistic_usage_and_stocks_sort_by_date( ), data=material_date_query_params, ) + for movement in [response["stocks"], response["usage"]]: assert all(elem1["date"] < elem2["date"] for elem1, elem2 in zip(movement, movement[1:])) @@ -258,6 +259,7 @@ def test_materials_statistic_different_results_for_each_point( point_with_materials_statistic: Point, another_point: Point, material_date_query_params: dict[str, str], + quantity_materials_in_stocks: dict[Point, dict[str, int]], ): point_response = as_point_managing_staff.get( # type: ignore[no-untyped-call] reverse( @@ -269,9 +271,6 @@ def test_materials_statistic_different_results_for_each_point( ), data=material_date_query_params, ) - point_materials_statistic: QuerySet[Material] = Material.objects.statistic( - point_with_materials_statistic.id, **material_date_query_params - ) another_point_response = ApiClient(another_point.company.owner).get( # type: ignore[no-untyped-call] reverse( @@ -283,12 +282,9 @@ def test_materials_statistic_different_results_for_each_point( ), data=material_date_query_params, ) - another_point_materials_statistic: QuerySet[Material] = Material.objects.statistic( - another_point.id, **material_date_query_params - ) - - assert list(point_materials_statistic) != list(another_point_materials_statistic) for point_materials, another_point_materials in zip(point_response["results"], another_point_response["results"]): - assert point_materials["stocks"] != another_point_materials["stocks"] - assert point_materials["usage"] != another_point_materials["usage"] + assert len(point_materials["stocks"]) == quantity_materials_in_stocks[point_with_materials_statistic]["stocks"] + assert len(point_materials["usage"]) == quantity_materials_in_stocks[point_with_materials_statistic]["usage"] + assert len(another_point_materials["stocks"]) == quantity_materials_in_stocks[another_point]["stocks"] + assert len(another_point_materials["usage"]) == quantity_materials_in_stocks[another_point]["usage"] diff --git a/tests/fixtures/apps/companies/__init__.py b/tests/fixtures/apps/companies/__init__.py index afe9ef4..5d02584 100644 --- a/tests/fixtures/apps/companies/__init__.py +++ b/tests/fixtures/apps/companies/__init__.py @@ -39,6 +39,7 @@ point_with_materials_statistic_30_days, point_with_materials_statistic_the_same_period, point_without_materials_statistic, + quantity_materials_in_stocks, ) from tests.fixtures.apps.companies.point import company_point, company_point_data, company_point_pk from tests.fixtures.apps.companies.stock import ( @@ -94,4 +95,5 @@ "stock", "stock_data", "stock_material", + "quantity_materials_in_stocks", ] diff --git a/tests/fixtures/apps/companies/materials_statistic.py b/tests/fixtures/apps/companies/materials_statistic.py index 7565c47..3d0e737 100644 --- a/tests/fixtures/apps/companies/materials_statistic.py +++ b/tests/fixtures/apps/companies/materials_statistic.py @@ -30,30 +30,32 @@ def create_point_with_materials_statistic( procedure: Procedure, now: datetime, timedelta_stock_material: int, + quantity_stock_material: int, + quantity_used_material: int, ) -> None: stock_materials = [] point = procedure.department.point - for date in [now + timedelta(days=timedelta_stock_material * i) for i in range(1, 3)]: + for date in [now + timedelta(days=timedelta_stock_material * i) for i in range(4)]: stock_material = factory.stock_material( stock=factory.stock(point=point, date=date.date()), ) stock_materials.append(stock_material) - for created_date in [date.date(), date.date() + timedelta(days=50)]: + iter_date = [date.date()] + [date.date() + timedelta(days=i) for i in range(1, quantity_stock_material)] + for created_date in iter_date: stock_materials.append( factory.stock_material( stock=factory.stock(point=point, date=created_date), material=stock_material.material, ) ) - iter_date = [now] + [now + timedelta(days=i) for i in range(5)] - for created_date in iter_date: + iter_datetime = [now] + [now + timedelta(days=i) for i in range(quantity_used_material)] + for created_date in iter_datetime: for stock_material in stock_materials: - used_material = factory.used_material( - procedure=factory.purchase_procedure(procedure=procedure), - material=stock_material, - ) - used_material.created = created_date - used_material.save() + with freeze_time(created_date): + factory.used_material( + procedure=factory.purchase_procedure(procedure=procedure), + material=stock_material, + ) @pytest.fixture @@ -61,7 +63,7 @@ def create_point_with_materials_statistic( def point_with_materials_statistic(factory: FixtureFactory, procedure: Procedure) -> Point: now = timezone.now() point = procedure.department.point - create_point_with_materials_statistic(factory, procedure, now, 200) + create_point_with_materials_statistic(factory, procedure, now, 100, 6, 10) return point @@ -71,7 +73,7 @@ def point_with_materials_statistic_the_same_period(factory: FixtureFactory) -> P now = timezone.now() procedure = factory.procedure(department=factory.department()) point = procedure.department.point - create_point_with_materials_statistic(factory, procedure, now, 200) + create_point_with_materials_statistic(factory, procedure, now, 100, 5, 15) return point @@ -81,10 +83,25 @@ def point_with_materials_statistic_30_days(factory: FixtureFactory) -> Point: now = timezone.now() procedure = factory.procedure(department=factory.department()) point = procedure.department.point - create_point_with_materials_statistic(factory, procedure, now, 15) + create_point_with_materials_statistic(factory, procedure, now, 15, 4, 20) return point @pytest.fixture def point_without_materials_statistic(factory: FixtureFactory) -> Point: return factory.company_point() + + +@pytest.fixture +def quantity_materials_in_stocks( + point_with_materials_statistic: Point, + point_with_materials_statistic_the_same_period: Point, + point_with_materials_statistic_30_days: Point, + point_without_materials_statistic: Point, +) -> dict[Point, dict[str, int]]: + return { + point_with_materials_statistic: {"stocks": 6, "usage": 10}, + point_with_materials_statistic_the_same_period: {"stocks": 5, "usage": 15}, + point_with_materials_statistic_30_days: {"stocks": 4, "usage": 20}, + point_without_materials_statistic: {"stocks": 0, "usage": 0}, + }