Skip to content

Commit

Permalink
fix unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
WiredNerd committed Nov 20, 2023
1 parent b74872c commit 78bf0e4
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 43 deletions.
40 changes: 25 additions & 15 deletions src/poodle/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,15 @@ def build_config(command_line_sources: tuple[Path], config_file: Path | None, ve
config_file_path = get_config_file_path(config_file)
config_file_data = get_config_file_data(config_file_path)

cmd_log_level = {
"q": logging.ERROR,
"v": logging.INFO,
"vv": logging.DEBUG,
}.get(verbosity, None)

log_format = get_str_from_config("log_format", config_file_data, default=default_log_format)
log_level: int | str = get_any_from_config(
"log_level", config_file_data, default=default_log_level, command_line=cmd_log_level
"log_level",
config_file_data,
default=default_log_level,
command_line=get_cmd_line_log_level(verbosity),
)
logging.basicConfig(format=log_format, level=log_level)

cmd_echo_enabled = {
"q": False,
"v": True,
"vv": True,
}.get(verbosity, None)

return PoodleConfig(
config_file=config_file_path,
source_folders=get_source_folders(command_line_sources, config_file_data),
Expand All @@ -60,18 +51,37 @@ def build_config(command_line_sources: tuple[Path], config_file: Path | None, ve
log_format=log_format,
log_level=log_level,
echo_enabled=get_bool_from_config(
"echo_enabled", config_file_data, default=True, command_line=cmd_echo_enabled
"echo_enabled",
config_file_data,
default=True,
command_line=get_cmd_line_echo_enabled(verbosity),
),
mutator_opts=get_dict_from_config("mutator_opts", config_file_data, default=default_mutator_opts),
skip_mutators=get_str_list_from_config("skip_mutators", config_file_data, default=[]),
add_mutators=get_any_list_from_config("add_mutators", config_file_data),
runner=get_str_from_config("runner", config_file_data, default=default_runner),
runner_opts=get_dict_from_config("runner_opts", config_file_data, default=default_runner_opts),
reporters=get_str_list_from_config("reporters", config_file_data, default_reporters),
reporters=get_str_list_from_config("reporters", config_file_data, default=default_reporters),
reporter_opts=get_dict_from_config("reporter_opts", config_file_data, default=default_reporter_opts),
)


def get_cmd_line_log_level(verbosity: str | None) -> int | None:
return {
"q": logging.ERROR,
"v": logging.INFO,
"vv": logging.DEBUG,
}.get(verbosity, None)


def get_cmd_line_echo_enabled(verbosity: str | None) -> bool | None:
return {
"q": False,
"v": True,
"vv": True,
}.get(verbosity, None)


def get_config_file_path(config_file: Path | None) -> Path | None:
"""Identify which configuration file to use.
Expand Down
10 changes: 10 additions & 0 deletions tests/data_types/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ def create_poodle_config(cls):
file_filters=["test_"],
file_copy_filters=["skip"],
work_folder=Path(".poodle"),
log_format="$(message)s",
log_level=0,
echo_enabled=True,
mutator_opts={"bin_op_level": 2},
skip_mutators=["null"],
add_mutators=["custom"],
runner="command_line",
runner_opts={"command_line": "pytest tests"},
reporters=["summary"],
reporter_opts={"summary": "value"}
)

def test_poodle_config(self):
Expand All @@ -27,11 +32,16 @@ def test_poodle_config(self):
assert config.file_filters == ["test_"]
assert config.file_copy_filters == ["skip"]
assert config.work_folder == Path(".poodle")
assert config.log_format=="$(message)s"
assert config.log_level==0
assert config.echo_enabled==True
assert config.mutator_opts == {"bin_op_level": 2}
assert config.skip_mutators == ["null"]
assert config.add_mutators == ["custom"]
assert config.runner == "command_line"
assert config.runner_opts == {"command_line": "pytest tests"}
assert config.reporters == ["summary"]
assert config.reporter_opts == {"summary": "value"}


class TestFileMutation:
Expand Down
5 changes: 3 additions & 2 deletions tests/data_types/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def test_create_mutations():
assert create_mutations(config=None, parsed_ast=None, other=None) is None
assert create_mutations(config=None, echo=None, parsed_ast=None, other=None) is None


class TestMutator:
Expand All @@ -21,7 +21,7 @@ def test_abstract(self):

def test_init(self):
config = mock.MagicMock(spec=PoodleConfig)
mutator = self.DummyMutator(config=config, other="value")
mutator = self.DummyMutator(config=config, echo=None, other="value")

assert mutator.config == config

Expand All @@ -30,6 +30,7 @@ def test_runner():
assert (
runner(
config=None,
echo=None,
run_folder=None,
mutant=None,
other="value",
Expand Down
29 changes: 14 additions & 15 deletions tests/mutators/test_bin_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

@pytest.fixture()
def mock_echo():
with mock.patch("poodle.mutators.bin_op.echo") as mock_echo:
yield mock_echo
return mock.MagicMock()


example_file = """
Expand All @@ -24,30 +23,30 @@ def subtraction(x, z):
class TestBinaryOperationMutator:
def test_init_default(self, mock_echo):
config = mock.MagicMock(mutator_opts={})
mutator = BinaryOperationMutator(config=config, other="value")
mutator = BinaryOperationMutator(config=config, echo=mock_echo, other="value")
assert mutator.config == config
assert mutator.mutants == []
assert mutator.type_map == mutator.type_map_levels["std"]
mock_echo.assert_not_called()

def test_init_valid_level(self, mock_echo):
config = mock.MagicMock(mutator_opts={"bin_op_level": "min"})
mutator = BinaryOperationMutator(config)
mutator = BinaryOperationMutator(config, mock_echo)
assert mutator.config == config
assert mutator.mutants == []
assert mutator.type_map == mutator.type_map_levels["min"]
mock_echo.assert_not_called()

def test_init_invalid_level(self, mock_echo):
config = mock.MagicMock(mutator_opts={"bin_op_level": "super"})
mutator = BinaryOperationMutator(config)
mutator = BinaryOperationMutator(config, mock_echo)
assert mutator.config == config
assert mutator.mutants == []
assert mutator.type_map == mutator.type_map_levels["std"]
mock_echo.assert_called_with("WARN: Invalid value operator_opts.bin_op_level=super. Using Default value 'std'")

def test_create_mutants(self):
mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={}))
def test_create_mutants(self, mock_echo):
mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={}), mock_echo)

file_mutants = mutator.create_mutations(ast.parse(example_file))

Expand Down Expand Up @@ -87,8 +86,8 @@ def test_create_mutants(self):
(ast.MatMult, []),
],
)
def test_visit_BinOp_level_min(self, op_type, text_out):
mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={"bin_op_level": "min"}))
def test_visit_BinOp_level_min(self, op_type, text_out, mock_echo):
mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={"bin_op_level": "min"}), mock_echo)

node = ast.BinOp()
node.left = ast.Constant()
Expand Down Expand Up @@ -124,8 +123,8 @@ def test_visit_BinOp_level_min(self, op_type, text_out):
(ast.MatMult, []),
],
)
def test_visit_BinOp_level_std(self, op_type, text_out):
mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={"bin_op_level": "std"}))
def test_visit_BinOp_level_std(self, op_type, text_out, mock_echo):
mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={"bin_op_level": "std"}), mock_echo)

node = ast.BinOp()
node.left = ast.Constant()
Expand Down Expand Up @@ -161,8 +160,8 @@ def test_visit_BinOp_level_std(self, op_type, text_out):
(ast.MatMult, []),
],
)
def test_visit_BinOp_level_max(self, op_type, text_out):
mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={"bin_op_level": "max"}))
def test_visit_BinOp_level_max(self, op_type, text_out, mock_echo):
mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={"bin_op_level": "max"}), mock_echo)

node = ast.BinOp()
node.left = ast.Constant()
Expand All @@ -180,7 +179,7 @@ def test_visit_BinOp_level_max(self, op_type, text_out):

assert [file_mutant.text for file_mutant in mutator.mutants] == text_out

def test_create_file_mutant(self):
def test_create_file_mutant(self, mock_echo):
node = ast.BinOp()
node.left = ast.Constant()
node.left.value = 1
Expand All @@ -195,7 +194,7 @@ def test_create_file_mutant(self):

new_type = ast.Sub

mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={}))
mutator = BinaryOperationMutator(mock.MagicMock(mutator_opts={}), mock_echo)
fm = mutator.create_mutant(node, new_type)

assert fm.lineno == 20
Expand Down
Loading

0 comments on commit 78bf0e4

Please sign in to comment.