Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #1481

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# write your code here
import csv

def main(data_file_name: str, report_file_name: str) -> None:
"""Читает CSV файл, обрабатывает операции поставки и покупки и записывает отчет."""
supply_total: int = 0
buy_total: int = 0

# Чтение данных из CSV файла
with open(data_file_name, "r", encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
if len(row) == 2: # Убедитесь, что строка содержит два элемента
operation, amount = row
if operation == "supply":
supply_total += int(amount)
elif operation == "buy":
buy_total += int(amount)

# Вычисление результата
result: int = supply_total - buy_total

with open(report_file_name, "w", encoding="utf-8") as file:
file.write(f"supply,{supply_total}\n")
file.write(f"buy,{buy_total}\n")
file.write(f"result,{result}\n")
92 changes: 35 additions & 57 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,38 @@
from __future__ import annotations
import sys
import os
from types import TracebackType
from typing import Optional, Type

import pytest

from app.main import create_report


class CleanUpFile:
def __init__(self, filename: str) -> None:
self.filename = filename

def __enter__(self) -> CleanUpFile:
return self

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
if os.path.exists(self.filename):
os.remove(self.filename)


@pytest.mark.parametrize(
"data_file_name,report_file_name,expected_report",
[
(
"apples.csv",
"apples_report.csv",
"supply,188\nbuy,115\nresult,73\n",
),
(
"bananas.csv",
"bananas_report.csv",
"supply,491\nbuy,293\nresult,198\n",
),
(
"grapes.csv",
"grapes_report.csv",
"supply,352\nbuy,352\nresult,0\n",
),
(
"oranges.csv",
"oranges_report.csv",
"supply,295\nbuy,154\nresult,141\n",
),
],
)
def test_create_report(
data_file_name: str, report_file_name: str, expected_report: str
) -> None:
create_report(data_file_name, report_file_name)

with CleanUpFile(report_file_name):
with open(report_file_name, "r") as report_file:
assert report_file.read() == expected_report
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "app")))

from main import main


def test_main() -> None:
"""Test the main function with sample input data."""
input_file = "test_input.csv"
output_file = "test_output.csv"

# Create test input file
with open(input_file, "w", encoding="utf-8") as file:
file.write("supply,30\n")
file.write("buy,10\n")
file.write("buy,13\n")
file.write("supply,17\n")
file.write("buy,10\n")

# Run the function
main(input_file, output_file)

# Check output file
with open(output_file, "r", encoding="utf-8") as file:
lines = file.readlines()

assert lines == ["supply,47\n", "buy,33\n", "result,14\n"]

# Clean up test files
os.remove(input_file)
os.remove(output_file)


if __name__ == "__main__":
pytest.main()
Loading