diff --git a/src/app/domain/statistic.py b/src/app/domain/statistic.py index 6274dc3..7123bcb 100644 --- a/src/app/domain/statistic.py +++ b/src/app/domain/statistic.py @@ -5,3 +5,4 @@ class Statistic: costs_sum: int categories_costs: dict[int, int] + costs_num_by_days: dict[str, int] diff --git a/src/app/services/statistic.py b/src/app/services/statistic.py index 2debe21..8ee5bb5 100644 --- a/src/app/services/statistic.py +++ b/src/app/services/statistic.py @@ -1,3 +1,6 @@ +from collections import Counter +from datetime import datetime + from src.app.domain.operations import Operation from src.app.domain.statistic import Statistic @@ -11,6 +14,7 @@ def get_statistic(operations: list[Operation]) -> Statistic: statistic = Statistic( costs_sum=get_costs_sum(operations), categories_costs=get_categories_costs(operations), + costs_num_by_days=get_costs_num_by_days(operations), ) return statistic @@ -29,3 +33,12 @@ def get_categories_costs(operations: list[Operation]) -> dict[int, int]: ) for mcc in operations_mcc } + + +def get_costs_num_by_days(operations: list[Operation]) -> dict[str, int]: + costs_num_by_days = Counter() + for operation in operations: + costs_num_by_days[ + datetime.fromtimestamp(operation.time).strftime("%Y-%m-%d") + ] += 1 + return dict(costs_num_by_days) diff --git a/src/schemas/statistic.py b/src/schemas/statistic.py index 66434cc..ef52349 100644 --- a/src/schemas/statistic.py +++ b/src/schemas/statistic.py @@ -6,3 +6,4 @@ class StatisticSchema(BaseModel): costs_sum: int categories_costs: dict[int, int] + costs_num_by_days: dict[str, int] diff --git a/tests/unit/statistic.py b/tests/unit/statistic.py index 318bef6..5977be1 100644 --- a/tests/unit/statistic.py +++ b/tests/unit/statistic.py @@ -1,5 +1,6 @@ -import random -from datetime import datetime +from collections import Counter +from datetime import datetime, timedelta +from random import randint import pytest @@ -12,9 +13,9 @@ async def test_get_statistic(): # TODO: Переписати без рандомних операцій і перевірити результат operations = [ Operation( - amount=random.randint(-10000, -10), + amount=randint(-10000, -10), description="description", - mcc=random.randint(9996, 9999), + mcc=randint(9996, 9999), source_type="manual", time=int(datetime.now().timestamp()), user_id=1, @@ -32,3 +33,37 @@ async def test_get_statistic_with_empty_operations(): assert statictic.costs_sum == 0 assert statictic.categories_costs == {} + + +@pytest.mark.asyncio +async def test_statistic_by_days(): + """ + Статистика повинна містити в собі поле кількості операцій + на кожен день у наступному вигляді: + + costs_num_by_days: dict[: str, : int] + + В тесті створюються операції, записується їх кількість по дням + а потім йде перевірка на відповідність зі отриманою статистикою + """ + operations_num_by_days = {} + operations = [] + now = datetime.now() + date = datetime(now.year, now.month, now.day) - timedelta(days=6) + for _ in range(5): + date = date + timedelta(days=1) + opertations_num = randint(1, 10) + operations_num_by_days[date.strftime("%Y-%m-%d")] = opertations_num + operations += [ + Operation( + amount=randint(-10000, -10), + description="description", + mcc=randint(9990, 9999), + source_type="manual", + time=int(date.timestamp() + randint(1, 8399)), # random time of day + user_id=1, + ) + for _ in range(opertations_num) + ] + statistic = get_statistic(operations) + assert statistic.costs_num_by_days == operations_num_by_days