Skip to content

Commit

Permalink
refactor: removes deprecated report extension 'json'.
Browse files Browse the repository at this point in the history
Refs: #205.
  • Loading branch information
Artanias authored Oct 2, 2024
1 parent 01996c9 commit 4d44f1c
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 126 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UTIL_VERSION := 0.5.6
UTIL_VERSION := 0.5.7
UTIL_NAME := codeplag
PWD := $(shell pwd)

Expand Down
7 changes: 5 additions & 2 deletions src/codeplag/handlers/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from codeplag.getfeatures import AbstractGetter
from codeplag.logger import codeplag_logger as logger
from codeplag.pyplag.utils import PyFeaturesGetter
from codeplag.reporters import CSVReporter, JSONReporter
from codeplag.reporters import CSVReporter
from codeplag.types import (
ASTFeatures,
CompareInfo,
Expand Down Expand Up @@ -92,7 +92,10 @@ def __init__(
reports = settings_conf.get("reports")
if reports is not None:
reports_extension = settings_conf["reports_extension"]
Reporter = CSVReporter if reports_extension == "csv" else JSONReporter
if reports_extension == "csv":
Reporter = CSVReporter
else:
raise ValueError(f"Unsupported reports extension '{reports_extension}'.")
self.reporter = Reporter(reports)
else:
self.reporter = None
Expand Down
37 changes: 0 additions & 37 deletions src/codeplag/reporters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""This module contains logic for saving a comparison result into JSON or CSV."""

import json
import uuid
from abc import ABC, abstractmethod
from datetime import datetime
from pathlib import Path
Expand All @@ -11,15 +10,13 @@
import pandas as pd
from typing_extensions import Self

from codeplag.config import write_config
from codeplag.consts import CSV_REPORT_COLUMNS, CSV_REPORT_FILENAME, CSV_SAVE_TICK_SEC
from codeplag.logger import codeplag_logger as logger
from codeplag.types import (
ASTFeatures,
CompareInfo,
FastMetrics,
StructuresInfo,
WorksReport,
)


Expand Down Expand Up @@ -107,40 +104,6 @@ def get_compare_result_from_cache(
return deserialize_compare_result(cache_val.iloc[0])


# DEPRECATED
class JSONReporter(AbstractReporter):
def save_result(
self: Self,
first_work: ASTFeatures,
second_work: ASTFeatures,
compare_info: CompareInfo,
) -> None:
if not self.reports.is_dir():
logger.error("The folder for reports isn't exists.")
return
assert compare_info.structure is not None

struct_info_dict = compare_info.structure._asdict()
struct_info_dict["compliance_matrix"] = struct_info_dict["compliance_matrix"].tolist()
report = WorksReport(
date=_get_current_date(),
first_path=first_work.filepath.__str__(),
first_modify_date=first_work.modify_date,
second_path=second_work.filepath.__str__(),
second_modify_date=second_work.modify_date,
first_heads=first_work.head_nodes,
second_heads=second_work.head_nodes,
fast=compare_info.fast._asdict(),
structure=struct_info_dict,
)

try:
report_file = self.reports / f"{uuid.uuid4().hex}.json"
write_config(report_file, report)
except PermissionError:
logger.error("Not enough rights to write reports to the folder.")


def read_df(path: Path) -> pd.DataFrame:
return pd.read_csv(path, sep=";", index_col=0, dtype=object) # type: ignore

Expand Down
15 changes: 1 addition & 14 deletions src/codeplag/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Flag = Literal[0, 1]
Mode = Literal["many_to_many", "one_to_one"]
NgramsLength = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ReportsExtension = Literal["json", "csv"]
ReportsExtension = Literal["csv"]
ReportType = Literal["general", "sources"]
Language = Literal["en", "ru"]
LogLevel = Literal["debug", "info", "warning", "error"]
Expand Down Expand Up @@ -130,19 +130,6 @@ class CompareInfo(NamedTuple):
structure: StructuresInfo | None = None


# TODO: Rework it structure
class WorksReport(TypedDict):
date: str
first_path: str
second_path: str
first_modify_date: str
second_modify_date: str
first_heads: list[str]
second_heads: list[str]
fast: dict[str, int] # dict from FastMetrics
structure: dict # dict from StructuresInfo


# Exceptions
# ----------------------------------------------------------------------------

Expand Down
24 changes: 0 additions & 24 deletions test/auto/functional/test_check.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from __future__ import annotations

import json
import os
import re

import pytest
from const import REPORTS_FOLDER
from utils import modify_settings, run_check, run_util

from codeplag.consts import CONFIG_PATH, UTIL_NAME, UTIL_VERSION
from codeplag.types import WorksReport

CWD = os.getcwd()
CPP_FILES = [
Expand Down Expand Up @@ -145,23 +141,3 @@ def test_check_failed_when_path_regexp_provided_without_required_args(

result.assert_failed()
assert result.cmd_res.returncode == 2


def test_save_reports(create_reports_folder: None):
modify_settings(reports=REPORTS_FOLDER, reports_extension="json").assert_success()
run_check(
[
"--directories",
"./src",
]
).assert_success()
reports_files = os.listdir(REPORTS_FOLDER)

assert len(reports_files) > 0
for file in reports_files:
assert re.search(".*[.]json$", file)
filepath = f"{REPORTS_FOLDER}/{file}"
with open(filepath, "r") as f:
report = json.loads(f.read())
for key in set(WorksReport.__annotations__.keys()):
assert key in report
4 changes: 2 additions & 2 deletions test/auto/functional/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class TestSettingsModify:
@pytest.mark.parametrize(
"env,reports,threshold,ngrams_length,show_progress,reports_extension,language,log_level,workers",
[
(f"src/{UTIL_NAME}/types.py", "src", 83, 2, 0, "json", "en", "debug", 1),
(f"src/{UTIL_NAME}/types.py", "src", 83, 2, 0, "csv", "en", "debug", 1),
("setup.py", "test", 67, 3, 1, "csv", "ru", "info", os.cpu_count() or 1),
(f"src/{UTIL_NAME}/utils.py", "debian", 93, 4, 0, "json", "en", "warning", 1),
(f"src/{UTIL_NAME}/utils.py", "debian", 93, 4, 0, "csv", "en", "warning", 1),
],
)
def test_modify_settings(
Expand Down
48 changes: 2 additions & 46 deletions test/unit/codeplag/test_reporters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from unittest.mock import MagicMock, call
from unittest.mock import MagicMock

import pandas as pd
import pytest
Expand All @@ -8,11 +8,10 @@

from codeplag.consts import CSV_REPORT_COLUMNS, CSV_REPORT_FILENAME
from codeplag.handlers.report import deserialize_compare_result
from codeplag.reporters import CSVReporter, JSONReporter
from codeplag.reporters import CSVReporter
from codeplag.types import (
ASTFeatures,
CompareInfo,
WorksReport,
)


Expand All @@ -26,49 +25,6 @@ def mock_write_config(mocker: MockerFixture) -> MagicMock:
return mocker.patch("codeplag.reporters.write_config")


class TestJSONReporter:
REPORTER = JSONReporter(Path("."))

def test_save_result_not_occurred_due_absent_dir(
self: Self,
mock_default_logger: MagicMock,
first_features: ASTFeatures,
second_features: ASTFeatures,
first_compare_result: CompareInfo,
) -> None:
self.REPORTER.reports = Path("/bad_directory")
self.REPORTER.save_result(first_features, second_features, first_compare_result)
assert mock_default_logger.error.call_args == call("The folder for reports isn't exists.")

def test_save_result_not_occurred_due_permission_error(
self: Self,
mocker: MockerFixture,
mock_default_logger: MagicMock,
first_features: ASTFeatures,
second_features: ASTFeatures,
first_compare_result: CompareInfo,
) -> None:
mocker.patch.object(Path, "open", side_effect=PermissionError)
self.REPORTER.reports = Path("/etc")
self.REPORTER.save_result(first_features, second_features, first_compare_result)
Path.open.assert_called_once()
assert mock_default_logger.error.call_args == call(
"Not enough rights to write reports to the folder."
)

def test_save_result_with_modify_date(
self: Self,
mock_write_config: MagicMock,
first_features: ASTFeatures,
second_features: ASTFeatures,
first_compare_result: CompareInfo,
) -> None:
mock_write_config.reset_mock()
self.REPORTER.save_result(first_features, second_features, first_compare_result)
mock_write_config.assert_called_once()
assert mock_write_config.call_args[0][1].keys() == WorksReport.__annotations__.keys()


class TestCSVReporter:
REPORTER = CSVReporter(Path("./src"))

Expand Down

0 comments on commit 4d44f1c

Please sign in to comment.