Skip to content

Commit

Permalink
Change test according to new cli interface
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferSkare committed Mar 18, 2024
1 parent 661ff29 commit 94af710
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions tests/cli/test_mlfmu_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from argparse import ArgumentError
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Union
from typing import List, Optional, Union

import pytest
from pytest import MonkeyPatch

from mlfmu.api import MlFmuCommand
from mlfmu.cli import mlfmu
from mlfmu.cli.mlfmu import _argparser, main

Expand All @@ -21,26 +22,24 @@ class CliArgs:
verbose: bool = False
log: Union[str, None] = None
log_level: str = field(default_factory=lambda: "WARNING")
config_file: Union[str, None] = field(default_factory=lambda: "test_config_file") # noqa: N815
option: bool = False
command: str = ""


@pytest.mark.parametrize(
"inputs, expected",
[
([], ArgumentError),
(["test_config_file"], CliArgs()),
(["test_config_file", "-q"], CliArgs(quiet=True)),
(["test_config_file", "--quiet"], CliArgs(quiet=True)),
(["test_config_file", "-v"], CliArgs(verbose=True)),
(["test_config_file", "--verbose"], CliArgs(verbose=True)),
(["test_config_file", "-qv"], ArgumentError),
(["test_config_file", "--log", "logFile"], CliArgs(log="logFile")),
(["test_config_file", "--log"], ArgumentError),
(["test_config_file", "--log-level", "INFO"], CliArgs(log_level="INFO")),
(["test_config_file", "--log-level"], ArgumentError),
(["test_config_file", "--option"], CliArgs(option=True)),
(["test_config_file", "-o"], ArgumentError),
(["asd"], ArgumentError),
(["build", "-q"], CliArgs(quiet=True, command="build")),
(["build", "--quiet"], CliArgs(quiet=True, command="build")),
(["build", "-v"], CliArgs(verbose=True, command="build")),
(["build", "--verbose"], CliArgs(verbose=True, command="build")),
(["build", "-qv"], ArgumentError),
(["build", "--log", "logFile"], CliArgs(log="logFile", command="build")),
(["build", "--log"], ArgumentError),
(["build", "--log-level", "INFO"], CliArgs(log_level="INFO", command="build")),
(["build", "--log-level"], ArgumentError),
(["build", "-o"], ArgumentError),
],
)
def test_cli(
Expand All @@ -58,6 +57,8 @@ def test_cli(
args_expected: CliArgs = expected
args = parser.parse_args()
# Assert args
print(args)
print(args_expected)
for key in args_expected.__dataclass_fields__:
assert args.__getattribute__(key) == args_expected.__getattribute__(key)
elif issubclass(expected, Exception):
Expand All @@ -84,28 +85,28 @@ class ConfigureLoggingArgs:
"inputs, expected",
[
([], ArgumentError),
(["test_config_file"], ConfigureLoggingArgs()),
(["test_config_file", "-q"], ConfigureLoggingArgs(log_level_console="ERROR")),
(["build"], ConfigureLoggingArgs()),
(["build", "-q"], ConfigureLoggingArgs(log_level_console="ERROR")),
(
["test_config_file", "--quiet"],
["build", "--quiet"],
ConfigureLoggingArgs(log_level_console="ERROR"),
),
(["test_config_file", "-v"], ConfigureLoggingArgs(log_level_console="INFO")),
(["build", "-v"], ConfigureLoggingArgs(log_level_console="INFO")),
(
["test_config_file", "--verbose"],
["build", "--verbose"],
ConfigureLoggingArgs(log_level_console="INFO"),
),
(["test_config_file", "-qv"], ArgumentError),
(["build", "-qv"], ArgumentError),
(
["test_config_file", "--log", "logFile"],
["build", "--log", "logFile"],
ConfigureLoggingArgs(log_file=Path("logFile")),
),
(["test_config_file", "--log"], ArgumentError),
(["build", "--log"], ArgumentError),
(
["test_config_file", "--log-level", "INFO"],
["build", "--log-level", "INFO"],
ConfigureLoggingArgs(log_level_file="INFO"),
),
(["test_config_file", "--log-level"], ArgumentError),
(["build", "--log-level"], ArgumentError),
],
)
def test_logging_configuration(
Expand All @@ -129,8 +130,11 @@ def fake_configure_logging(
args.log_level_file = log_level_file

def fake_run(
config_file: Path,
option: bool,
command: str,
interface_file: Optional[str],
model_file: Optional[str],
fmu_path: Optional[str],
source_folder: Optional[str],
):
pass

Expand Down Expand Up @@ -158,17 +162,18 @@ def fake_run(
@dataclass()
class ApiArgs:
# Values that main() is expected to pass to run() by default when invoking the API
config_file: Path = field(default_factory=lambda: Path("test_config_file"))
option: bool = False
command: Optional[MlFmuCommand] = None
interface_file: Optional[str] = None
model_file: Optional[str] = None
fmu_path: Optional[str] = None
source_folder: Optional[str] = None


@pytest.mark.parametrize(
"inputs, expected",
[
([], ArgumentError),
(["test_config_file"], ApiArgs()),
(["test_config_file", "--option"], ApiArgs(option=True)),
(["test_config_file", "-o"], ArgumentError),
(["build"], ApiArgs()),
],
)
def test_api_invokation(
Expand All @@ -183,11 +188,17 @@ def test_api_invokation(
args: ApiArgs = ApiArgs()

def fake_run(
config_file: Path,
option: bool = False,
command: str,
interface_file: Optional[str],
model_file: Optional[str],
fmu_path: Optional[str],
source_folder: Optional[str],
):
args.config_file = config_file
args.option = option
args.command = MlFmuCommand.from_string(command)
args.interface_file = interface_file
args.model_file = model_file
args.fmu_path = fmu_path
args.source_folder = source_folder

monkeypatch.setattr(mlfmu, "run", fake_run)
# Execute
Expand Down

0 comments on commit 94af710

Please sign in to comment.