Skip to content

Commit

Permalink
Merge pull request #27 from AndrewSergienko/update_statistic
Browse files Browse the repository at this point in the history
added costs_num_by_days field to statistic
  • Loading branch information
andiserg authored Apr 29, 2023
2 parents 3d622a3 + eb5d3b8 commit 767ada5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/app/domain/statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
class Statistic:
costs_sum: int
categories_costs: dict[int, int]
costs_num_by_days: dict[str, int]
13 changes: 13 additions & 0 deletions src/app/services/statistic.py
Original file line number Diff line number Diff line change
@@ -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

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

Expand All @@ -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)
1 change: 1 addition & 0 deletions src/schemas/statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ class StatisticSchema(BaseModel):

costs_sum: int
categories_costs: dict[int, int]
costs_num_by_days: dict[str, int]
43 changes: 39 additions & 4 deletions tests/unit/statistic.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand All @@ -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[<date>: str, <num>: 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

0 comments on commit 767ada5

Please sign in to comment.