Skip to content

Commit

Permalink
Fix merge ignore processing, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Barabanov committed Aug 25, 2023
1 parent 601ed4c commit bc8f4a1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
3 changes: 1 addition & 2 deletions hworker/check/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def validate_wo_store(validator: Check, solution: Solution, check_num: int = 0)
if validator_type == get_validator_name():
result = v(solution, *validator_args)
else:
result = v(search(Solution,
Criteria("ID", "==", solution.ID)), *validator_args)
result = v(search(Solution, Criteria("ID", "==", solution.ID)), *validator_args)
except Exception as error:
stderr = str(error).encode()
finally:
Expand Down
19 changes: 14 additions & 5 deletions hworker/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def process_configs(user_config: str, *extras: str) -> Path:
merge(content, read_from_path(Path(folder) / f"{name}{extension}"))
fill_final_config(content)
clear_underscores(content)
no_merge_processing(content, user_config, [])
no_merge_processing(content, user_config, content.get("formalization", {}).get("no_merge", []), [])
finalpath = userdir / f"{username}{final_name_suffix}{extension}"
create_config(finalpath, content)
profile[:] = [content, *extras, username]
Expand Down Expand Up @@ -102,22 +102,31 @@ def clear_underscores(final_content: dict) -> None:
clear_underscores(v)


def no_merge_processing(final_content: dict, user_config: str, keys: list) -> None:
def no_merge_processing(final_content: dict, user_config: str, no_merge: list, keys: list) -> None:
"""Change merge rules for fields in no_merge list
:param final_content: final content dict
:param user_config: user config name
:param no_merge: list of merge ignore fields
:param keys: keys to current subdict
:return: -
"""
for k, v in copy(final_content).items():
user_content = read_from_path(Path(user_config))
if k in user_content.get("formalization", {}).get("no_merge", {}):
if k in no_merge:
for key in keys:
user_content = user_content.get(key, {})
final_content[k] = user_content[k]
final_content[k] = user_content.get(k, final_content[k])
elif isinstance(v, dict):
no_merge_processing(v, user_config, keys + [k, ])
no_merge_processing(
v,
user_config,
no_merge,
keys
+ [
k,
],
)


def get_git_directory() -> str:
Expand Down
2 changes: 1 addition & 1 deletion hworker/control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def generate_homeworks_with_versions():


def do_score():
""" Perform qualifiers and get score results
"""Perform qualifiers and get score results
:return: -
"""
Expand Down
44 changes: 42 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
""""""

import pytest
from mergedeep import merge

from datetime import datetime
from .user_config import user_config

from .user_config import user_config
from hworker.config import (
repo_to_uid,
uid_to_repo,
Expand All @@ -15,8 +17,16 @@
get_prog_name,
get_remote_name,
get_task_info,
create_config,
no_merge_processing,
)
import hworker.config


@pytest.fixture(scope="function")
def tmp_config(request, tmp_path):
config = tmp_path / "testconfig.toml"
create_config(config, request.param)
return config.as_posix()


class TestConfig:
Expand Down Expand Up @@ -61,3 +71,33 @@ def test_task_info(self, user_config):
"soft_deadline": datetime(2024, 1, 7, 0, 0),
"soft_deadline_delta": "open_date+6d",
}

@pytest.mark.parametrize(
"tmp_config",
[
{
"tasks": {
"task_ID": {
"checks": {"The teacher:first/123": [], "The teacher:first/456": []},
}
}
}
],
indirect=True,
)
def test_no_merge(self, tmp_config):
final_content = {
"formalization": {
"no_merge": [
"checks",
]
},
"tasks": {"task_ID": {"checks": {"The teacher:first/deadline": [], "The teacher:first/attendance": []}}},
}
merge(
final_content,
{"tasks": {"task_ID": {"checks": {"The teacher:first/123": [], "The teacher:first/456": []}}}},
)
no_merge_processing(final_content, tmp_config, final_content["formalization"]["no_merge"], [])

assert final_content["tasks"]["task_ID"]["checks"] == {"The teacher:first/123": [], "The teacher:first/456": []}

0 comments on commit bc8f4a1

Please sign in to comment.