Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
platsajacki committed Apr 8, 2024
1 parent 792627d commit d6bc7aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
16 changes: 6 additions & 10 deletions tests/apps/companies/test_materials_statistic_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]))

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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"]
2 changes: 2 additions & 0 deletions tests/fixtures/apps/companies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -94,4 +95,5 @@
"stock",
"stock_data",
"stock_material",
"quantity_materials_in_stocks",
]
43 changes: 30 additions & 13 deletions tests/fixtures/apps/companies/materials_statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,40 @@ 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
@freeze_time("2000-01-01 10:23:40")
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


Expand All @@ -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


Expand All @@ -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},
}

0 comments on commit d6bc7aa

Please sign in to comment.