Skip to content

Commit c79c311

Browse files
tarekbadrshafrittoli
authored andcommitted
add first cli tests
1 parent 293ea28 commit c79c311

File tree

7 files changed

+191
-3
lines changed

7 files changed

+191
-3
lines changed

cli/cdevents/cli/configuration_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
from typing import Union
99

1010
from cdevents.cli.configuration_reader import ConfigurationReader
11-
from cdevents.cli.constants import DEAFULT_CONFIGURATION_FILE
11+
from cdevents.cli.constants import DEFAULT_CONFIGURATION_FILE
1212
from cdevents.cli.utils import DictUtils
1313

1414

1515
def get_default_configuration_file() -> str:
1616
"""Returns the default configuration file path."""
17-
return DEAFULT_CONFIGURATION_FILE
17+
return DEFAULT_CONFIGURATION_FILE
1818

1919
def new_default_configuration_handler() -> ConfigurationHandler:
2020
"""Returnes a configuration handler with the default configuration file"""

cli/cdevents/cli/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# configuration/configuration.yaml
55
_CLI_CDEVENTS_DIR = Path(__file__).parent.parent
6-
DEAFULT_CONFIGURATION_FILE = str(Path(_CLI_CDEVENTS_DIR, "configuration", "configuration.yaml"))
6+
DEFAULT_CONFIGURATION_FILE = str(Path(_CLI_CDEVENTS_DIR, "configuration", "configuration.yaml"))
77
LOGGING_CONFIGURATION_FILE = str(
88
Path(_CLI_CDEVENTS_DIR, "configuration", "logging-configuration.yaml")
99
)

cli/tests/configuration_test.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# N.B. The file is used for testing the configuration handler.
2+
# The content should reflect the content of the default configuration file cli/configuration/configuration.yaml
3+
---
4+
configuration:
5+
source:
6+
name: cde-cli
7+
client:
8+
host: http://localhost:8080
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Module for tests related to configuration_handler."""
2+
import os
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
from cdevents.cli.configuration_handler import ConfigurationHandler
8+
9+
# pylint: disable=missing-function-docstring, protected-access, missing-class-docstring
10+
THIS_DIR: Path = Path(__file__).parent
11+
TEST_CONFIG_FILE: str = Path(THIS_DIR, "configuration_test.yaml").as_posix()
12+
13+
14+
@pytest.mark.unit
15+
def test_create_config_handler():
16+
17+
handler = ConfigurationHandler.create_new(TEST_CONFIG_FILE)
18+
assert isinstance(handler, ConfigurationHandler)
19+
20+
assert handler.source.name == "cde-cli"
21+
assert handler.client.host == "http://localhost:8080"
22+
23+
24+
@pytest.mark.unit
25+
def test_create_config_handler_with_override():
26+
27+
expected_source_name = "MySourceName"
28+
expected_client_host = "http://myhost:5000"
29+
override_config = {
30+
"source": {
31+
"name": expected_source_name,
32+
},
33+
"client": {
34+
"host": expected_client_host,
35+
},
36+
}
37+
handler = ConfigurationHandler.create_new(TEST_CONFIG_FILE, override_config=override_config)
38+
assert isinstance(handler, ConfigurationHandler)
39+
40+
assert handler.source.name == expected_source_name
41+
assert handler.client.host == expected_client_host
42+
43+
44+
@pytest.mark.unit
45+
def test_create_empty_override_dict():
46+
47+
expected = {}
48+
actual = ConfigurationHandler.create_override_config()
49+
assert expected == actual
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""Module testing ConfigurationReader"""
2+
import json
3+
import unittest
4+
from pathlib import Path
5+
from unittest.mock import patch
6+
7+
import pytest
8+
import yaml
9+
10+
from cdevents.cli.configuration_reader import ConfigurationReader
11+
12+
# pylint: disable=missing-function-docstring, protected-access, missing-class-docstring
13+
TEST_DIR = Path(__file__).parent
14+
CONFIG_DIR: Path = Path(TEST_DIR, "configurations")
15+
16+
YAML_CONFIG_1 = """---
17+
configuration:
18+
source:
19+
name: cde-cli
20+
client:
21+
host: http://localhost:8080
22+
"""
23+
DICT_CONFIG_1 = yaml.safe_load(YAML_CONFIG_1)
24+
25+
JSON_CONFIG_1 = json.dumps(DICT_CONFIG_1)
26+
YAML_CONFIG_2 = """---
27+
configuration:
28+
source:
29+
name: mysource
30+
client:
31+
host: http://myhost:5000
32+
"""
33+
DICT_CONFIG_2 = {
34+
"configuration": {
35+
"source": {"name": "mysource"},
36+
"client": {"host": "http://myhost:5000"},
37+
},
38+
}
39+
40+
DUMMY_PATH = "my/dummy/path/dummy.yaml"
41+
42+
43+
class TestConfigurationReader(unittest.TestCase):
44+
# pylint: disable=unused-argument
45+
def setUp(self) -> None:
46+
self.reader = ConfigurationReader()
47+
48+
@pytest.mark.unit
49+
@patch.object(Path, "exists", return_value=False)
50+
def test_read_configuration_with_invalid_file_path_then_exception(self, mocked_exists):
51+
with self.assertRaises(FileNotFoundError):
52+
self.reader.read_configuration(Path(DUMMY_PATH))
53+
54+
@pytest.mark.unit
55+
@patch.object(Path, "read_text", return_value=YAML_CONFIG_1)
56+
@patch.object(Path, "exists", return_value=True)
57+
def test_read_configuration_with_valid_yaml_file(self, mocked_exists, mocked_read_text):
58+
self.reader.read_configuration(Path(DUMMY_PATH))
59+
actual = self.reader.configuration
60+
self.assertDictEqual(actual, DICT_CONFIG_1)
61+
62+
@pytest.mark.unit
63+
@patch.object(Path, "read_text", return_value=JSON_CONFIG_1)
64+
@patch.object(Path, "exists", return_value=True)
65+
def test_read_configuration_with_valid_json_file(self, mocked_exists, mocked_read_text):
66+
self.reader.read_configuration(Path("path/to/my/config.json"))
67+
actual = self.reader.configuration
68+
self.assertDictEqual(actual, DICT_CONFIG_1)
69+
70+
@pytest.mark.unit
71+
@patch.object(Path, "exists", return_value=True)
72+
def test_read_configuration_with_invalid_filetype_then_value_error(
73+
self,
74+
mocked_exists,
75+
):
76+
with self.assertRaises(ValueError):
77+
self.reader.read_configuration(Path("path/to/my/invalid_config.jsonX"))
78+
79+
@pytest.mark.unit
80+
@patch.object(Path, "read_text", side_effect=[YAML_CONFIG_1, YAML_CONFIG_2])
81+
@patch.object(Path, "exists", return_value=True)
82+
def test_read_configuration_with_2_configs(self, mocked_exists, mocked_read_text):
83+
"""Second configuration overwrites previous."""
84+
self.reader.read_configuration(Path(DUMMY_PATH))
85+
actual = self.reader.configuration
86+
self.assertDictEqual(actual, DICT_CONFIG_1)
87+
self.reader.read_configuration(Path("dummy_config2.yml"))
88+
actual_2 = self.reader.configuration
89+
self.assertDictEqual(actual_2, DICT_CONFIG_2)
90+
91+
@pytest.mark.unit
92+
def test_is_supported_suffix_with_valid_suffix(self):
93+
self.assertTrue(ConfigurationReader._is_supported_json_suffix(Path("valid_suffix.json")))
94+
self.assertTrue(ConfigurationReader._is_supported_yaml_suffix(Path("valid_suffix.yml")))
95+
self.assertTrue(ConfigurationReader._is_supported_yaml_suffix(Path("valid_suffix.yAMl")))
96+
97+
@pytest.mark.unit
98+
def test_is_supported_suffix_with_invalid_suffix(self):
99+
self.assertFalse(ConfigurationReader._is_supported_yaml_suffix(Path("valid_suffix.berra")))

cli/tests/test_constants.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Tests for constants."""
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
from cdevents.cli.constants import DEFAULT_CONFIGURATION_FILE, LOGGING_CONFIGURATION_FILE
7+
8+
# pylint: disable=missing-function-docstring, missing-class-docstring
9+
10+
@pytest.mark.unit
11+
def test_default_config_exist():
12+
assert Path(DEFAULT_CONFIGURATION_FILE).exists()
13+
14+
15+
@pytest.mark.unit
16+
def test_logging_config_file_exist():
17+
assert Path(LOGGING_CONFIGURATION_FILE).exists()

cli/tests/test_main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
from click.testing import CliRunner
3+
4+
from cdevents.cli.__main__ import cli
5+
6+
@pytest.fixture
7+
def runner() -> CliRunner:
8+
return CliRunner()
9+
10+
11+
@pytest.mark.unit
12+
def test_cli_main(runner: CliRunner):
13+
14+
result = runner.invoke(cli, ["--help"])
15+
assert result.exit_code == 0

0 commit comments

Comments
 (0)