-
Notifications
You must be signed in to change notification settings - Fork 7
Unit testing improvements #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6b23c40
f87fda6
a1ae7a5
29ce958
e87983d
e6976be
c47b20b
309f852
a63d96a
1657cd2
ecfa0d7
bc66238
5840b23
606024d
9199b85
1c23358
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,5 +27,4 @@ jobs: | |
| - name: Run Tests | ||
| run: | | ||
| uv run --python ${{ matrix.env }} pytest \ | ||
| tests/test_classes \ | ||
| --color=yes | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +0,0 @@ | ||
| from collectoss.tasks.github.contributors import * | ||
| from collectoss.tasks.github.events import * | ||
| from collectoss.tasks.github.issues import * | ||
| from collectoss.tasks.github.messages import * | ||
| from collectoss.tasks.github.pull_requests.tasks import * | ||
| from collectoss.tasks.github.repo_info.tasks import * | ||
| from collectoss.tasks.github.releases.tasks import * | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,75 +14,128 @@ | |
| return Mock() | ||
|
|
||
|
|
||
| def test_jsonconfig_readonly_flags(mock_logger): | ||
| cfg = JsonConfig({"A": {"x": 1}}, mock_logger) | ||
| assert cfg.writable is False | ||
| assert cfg.empty is False | ||
|
|
||
|
|
||
| def test_jsonconfig_empty_true_false(mock_logger): | ||
| assert JsonConfig({}, mock_logger).empty is True | ||
| assert JsonConfig({"A": {}}, mock_logger).empty is False | ||
|
|
||
|
|
||
| def test_jsonconfig_write_protection(mock_logger): | ||
| # JsonConfig should be not writeable by default, so we should be unable to change | ||
| # its values, even by abusing references | ||
|
|
||
| data = {"Alpha": {"a": 1, "b": "str"}, "Beta": {}} | ||
| cfg = JsonConfig(data, mock_logger) | ||
|
|
||
| # mutation via input | ||
| data["Alpha"]["a"] = 2 | ||
|
|
||
| config_test = cfg.retrieve_dict() | ||
| assert config_test != data # the data in the config should not change | ||
|
|
||
| # mutation via output | ||
| config_test["Alpha"]["a"] = 3 | ||
|
|
||
| config_test = cfg.retrieve_dict() | ||
| assert config_test != data # the data in the config should not change | ||
|
|
||
| def test_jsonconfig_retrieve_has_get(mock_logger): | ||
| data = {"Alpha": {"a": 1, "b": "str"}, "Beta": {}} | ||
| cfg = JsonConfig(data, mock_logger) | ||
|
|
||
| # retrieve full dict | ||
| assert cfg.retrieve_dict() == data | ||
|
|
||
| # has/get section | ||
| assert cfg.has_section("Alpha") is True | ||
| assert cfg.has_section("Missing") is False | ||
| assert cfg.get_section("Alpha") == {"a": 1, "b": "str"} | ||
| assert cfg.get_section("Missing") is None | ||
|
|
||
| # has/get value | ||
| assert cfg.has_value("Alpha", "a") is True | ||
| assert cfg.has_value("Alpha", "missing") is False | ||
| assert cfg.has_value("Missing", "a") is False | ||
| assert cfg.get_value("Alpha", "a") == 1 | ||
| assert cfg.get_value("Alpha", "missing") is None | ||
| assert cfg.get_value("Missing", "a") is None | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "callable_name, args, kwargs", | ||
| [ | ||
| ("load_dict", ({"X": {"y": 2}},), {"ignore_existing": False}), | ||
| ("clear", tuple(), {}), | ||
| ("remove_section", ("X",), {}), | ||
| ("create_section", ("X", {"y": 2}), {"ignore_existing": False}), | ||
| ("remove_value", ("X", "y"), {}), | ||
| ("add_value", ("X", "y", 2), {"ignore_existing": False}), | ||
| ], | ||
| ) | ||
| def test_jsonconfig_mutations_raise_not_writable(mock_logger, callable_name, args, kwargs): | ||
| cfg = JsonConfig({"A": {"x": 1}}, mock_logger) | ||
| with pytest.raises(NotWriteableException): | ||
| getattr(cfg, callable_name)(*args, **kwargs) | ||
| class TestJSONConfig: | ||
|
|
||
| def test_jsonconfig_readonly_flags(self, mock_logger): | ||
| cfg = JsonConfig({"A": {"x": 1}}, mock_logger) | ||
| assert cfg.writable is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert cfg.empty is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
|
|
||
| def test_jsonconfig_empty_true_false(self, mock_logger): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pylint] reported by reviewdog 🐶 |
||
| assert JsonConfig({}, mock_logger).empty is True | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert JsonConfig({"A": {}}, mock_logger).empty is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
|
|
||
| def test_jsonconfig_write_protection(self, mock_logger): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pylint] reported by reviewdog 🐶 |
||
| # JsonConfig should be not writeable by default, so we should be unable to change | ||
| # its values, even by abusing references | ||
|
|
||
| data = {"Alpha": {"a": 1, "b": "str"}, "Beta": {}} | ||
| cfg = JsonConfig(data, mock_logger) | ||
|
|
||
| # mutation via input | ||
| data["Alpha"]["a"] = 2 | ||
|
|
||
| config_test = cfg.retrieve_dict() | ||
| assert config_test != data # the data in the config should not change | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| # mutation via output | ||
| config_test["Alpha"]["a"] = 3 | ||
|
|
||
| config_test = cfg.retrieve_dict() | ||
| assert config_test != data # the data in the config should not change | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| def test_jsonconfig_retrieve_has_get(self, mock_logger): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pylint] reported by reviewdog 🐶 |
||
| data = {"Alpha": {"a": 1, "b": "str"}, "Beta": {}} | ||
| cfg = JsonConfig(data, mock_logger) | ||
|
|
||
| # retrieve full dict | ||
| assert cfg.retrieve_dict() == data | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| # has/get section | ||
| assert cfg.has_section("Alpha") is True | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert cfg.has_section("Missing") is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert cfg.get_section("Alpha") == {"a": 1, "b": "str"} | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert cfg.get_section("Missing") is None | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
| # has/get value | ||
| assert cfg.has_value("Alpha", "a") is True | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert cfg.has_value("Alpha", "missing") is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert cfg.has_value("Missing", "a") is False | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert cfg.get_value("Alpha", "a") == 1 | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert cfg.get_value("Alpha", "missing") is None | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
| assert cfg.get_value("Missing", "a") is None | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "callable_name, args, kwargs", | ||
| [ | ||
| ("load_dict", ({"X": {"y": 2}},), {"ignore_existing": False}), | ||
| ("clear", tuple(), {}), | ||
| ("remove_section", ("X",), {}), | ||
| ("create_section", ("X", {"y": 2}), {"ignore_existing": False}), | ||
| ("remove_value", ("X", "y"), {}), | ||
| ("add_value", ("X", "y", 2), {"ignore_existing": False}), | ||
| ], | ||
| ) | ||
| def test_jsonconfig_mutations_raise_not_writable(self, mock_logger, callable_name, args, kwargs): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pylint] reported by reviewdog 🐶 |
||
| cfg = JsonConfig({"A": {"x": 1}}, mock_logger) | ||
| with pytest.raises(NotWriteableException): | ||
| getattr(cfg, callable_name)(*args, **kwargs) | ||
|
|
||
|
|
||
| def test_fetching_real_defaults(self, mock_logger, mock_session): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pylint] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pylint] reported by reviewdog 🐶 |
||
| cfg = SystemConfig(mock_logger, mock_session) | ||
| cfg.config_sources = [JsonConfig(default_config, mock_logger)] | ||
|
|
||
| assert cfg.get_value("Redis", "cache_group") == 0 | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
|
|
||
| def test_load_config_utilizes_hierarchy(self): | ||
|
|
||
| default_dict = { | ||
| "Section1": {"alpha": 1, "beta": "x"}, | ||
| "Section2": {"gamma": False, "delta": 3.14}, | ||
| } | ||
|
|
||
| override_dict = { | ||
| "Section1": {"beta": "y"}, | ||
| "Section2": {"Epsilon": True, "delta": 6.28}, | ||
| "Section3": {"hi": "there"} | ||
| } | ||
|
|
||
| cfg = SystemConfig(None, None, [JsonConfig(default_dict, mock_logger), JsonConfig(override_dict, mock_logger)]) | ||
|
|
||
| expected_dict = { | ||
| "Section1": {"alpha": 1, "beta": "y"}, | ||
| "Section2": {"gamma": False, "Epsilon": True, "delta": 6.28}, | ||
| "Section3": {"hi": "there"} # test that new sections are accounted for too | ||
| } | ||
|
|
||
| assert cfg.load_config() == expected_dict | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
|
|
||
| def test_get_section_incorporates_hierarchy(self): | ||
|
|
||
| default_dict = { | ||
| "Section1": {"alpha": 1, "beta": "x"}, | ||
| "Section2": {"gamma": False, "delta": 3.14}, | ||
| } | ||
|
|
||
| override_dict = { | ||
| "Section1": {"beta": "y"}, | ||
| "Section2": {"gamma": False, "delta": 3.14}, | ||
| } | ||
|
|
||
| cfg = SystemConfig(None, None, [JsonConfig(default_dict, mock_logger), JsonConfig(override_dict, mock_logger)]) | ||
|
|
||
| expected_dict = {"alpha": 1, "beta": "y"} | ||
|
|
||
| assert cfg.get_section("Section1") == expected_dict | ||
Check noticeCode scanning / Bandit Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
|
||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_dict_to_config_table_happy_path(): | ||
| input_dict = { | ||
| "Section1": {"alpha": 1, "beta": "x"}, | ||
|
|
@@ -122,53 +175,3 @@ | |
| assert rows == expected | ||
|
|
||
|
|
||
|
|
||
| def test_fetching_real_defaults(mock_logger, mock_session): | ||
| cfg = SystemConfig(mock_logger, mock_session) | ||
| cfg.config_sources = [JsonConfig(default_config, mock_logger)] | ||
|
|
||
| assert cfg.get_value("Redis", "cache_group") == 0 | ||
|
|
||
|
|
||
| def test_load_config_utilizes_hierarchy(): | ||
|
|
||
| default_dict = { | ||
| "Section1": {"alpha": 1, "beta": "x"}, | ||
| "Section2": {"gamma": False, "delta": 3.14}, | ||
| } | ||
|
|
||
| override_dict = { | ||
| "Section1": {"beta": "y"}, | ||
| "Section2": {"Epsilon": True, "delta": 6.28}, | ||
| "Section3": {"hi": "there"} | ||
| } | ||
|
|
||
| cfg = SystemConfig(None, None, [JsonConfig(default_dict, mock_logger), JsonConfig(override_dict, mock_logger)]) | ||
|
|
||
| expected_dict = { | ||
| "Section1": {"alpha": 1, "beta": "y"}, | ||
| "Section2": {"gamma": False, "Epsilon": True, "delta": 6.28}, | ||
| "Section3": {"hi": "there"} # test that new sections are accounted for too | ||
| } | ||
|
|
||
| assert cfg.load_config() == expected_dict | ||
|
|
||
|
|
||
| def test_get_section_incorporates_hierarchy(): | ||
|
|
||
| default_dict = { | ||
| "Section1": {"alpha": 1, "beta": "x"}, | ||
| "Section2": {"gamma": False, "delta": 3.14}, | ||
| } | ||
|
|
||
| override_dict = { | ||
| "Section1": {"beta": "y"}, | ||
| "Section2": {"gamma": False, "delta": 3.14}, | ||
| } | ||
|
|
||
| cfg = SystemConfig(None, None, [JsonConfig(default_dict, mock_logger), JsonConfig(override_dict, mock_logger)]) | ||
|
|
||
| expected_dict = {"alpha": 1, "beta": "y"} | ||
|
|
||
| assert cfg.get_section("Section1") == expected_dict | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[pylint] reported by reviewdog 🐶
W0621: Redefining name 'mock_logger' from outer scope (line 9) (redefined-outer-name)