-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TST: Refactor struct module to use pytest
- Loading branch information
1 parent
ac0d247
commit 82ac172
Showing
3 changed files
with
156 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,25 @@ | ||
import unittest | ||
from structs.email.email_params import EmailParams | ||
from structs.email.email_template_details import EmailTemplateDetails | ||
|
||
|
||
class TestEmailParams(unittest.TestCase): | ||
def test_from_dict(): | ||
""" | ||
Tests that methods in EmailParams dataclass work expectedly | ||
Tests that from_dict() static method works properly | ||
this method should build a EmailParams dataclass from a valid dictionary | ||
""" | ||
mock_valid_kwargs = { | ||
"subject": "subject1", | ||
"email_from": "[email protected]", | ||
"email_to": "[email protected]", | ||
"email_cc": ["[email protected]", "[email protected]"], | ||
"attachment_filepaths": ["path/to/file1", "path/to/file2"], | ||
"email_templates": EmailTemplateDetails( | ||
template_name="template1", | ||
template_params={"arg1": "val1", "arg2": "val2"}, | ||
), | ||
} | ||
mock_invalid_kwargs = {"to_ignore1": "val1", "to_ignore2": "val2"} | ||
|
||
def test_from_dict(self): | ||
""" | ||
Tests that from_dict() static method works properly | ||
this method should build a EmailParams dataclass from a valid dictionary | ||
""" | ||
mock_valid_kwargs = { | ||
"subject": "subject1", | ||
"email_from": "[email protected]", | ||
"email_to": "[email protected]", | ||
"email_cc": ["[email protected]", "[email protected]"], | ||
"attachment_filepaths": ["path/to/file1", "path/to/file2"], | ||
"email_templates": EmailTemplateDetails( | ||
template_name="template1", | ||
template_params={"arg1": "val1", "arg2": "val2"}, | ||
), | ||
} | ||
mock_invalid_kwargs = {"to_ignore1": "val1", "to_ignore2": "val2"} | ||
|
||
res = EmailParams.from_dict({**mock_valid_kwargs, **mock_invalid_kwargs}) | ||
for key, val in mock_valid_kwargs.items(): | ||
self.assertEqual(val, getattr(res, key)) | ||
res = EmailParams.from_dict({**mock_valid_kwargs, **mock_invalid_kwargs}) | ||
for key, val in mock_valid_kwargs.items(): | ||
assert val == getattr(res, key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,83 @@ | ||
import unittest | ||
import pytest | ||
from structs.query.query_output_details import QueryOutputDetails | ||
from enums.query.props.server_properties import ServerProperties | ||
from enums.query.query_output_types import QueryOutputTypes | ||
|
||
|
||
class QueryOutputDetailsTests(unittest.TestCase): | ||
@pytest.fixture(name="instance") | ||
def instance_fixture(): | ||
""" | ||
This class tests that helper functions in the QueryOutputDetails | ||
Returns an instance to run tests with | ||
""" | ||
return QueryOutputDetails | ||
|
||
def setUp(self) -> None: | ||
self.instance = QueryOutputDetails | ||
|
||
def _run_from_kwargs_case(self, kwargs): | ||
@pytest.fixture(name="run_kwargs_test_case") | ||
def run_kwargs_test_case_fixture(instance): | ||
""" | ||
Fixture runs a test case with different kwargs | ||
""" | ||
|
||
def _run_from_kwargs_case(kwargs): | ||
""" | ||
Helper function for running from_kwargs test cases | ||
""" | ||
return self.instance.from_kwargs(prop_cls=ServerProperties, **kwargs) | ||
return instance.from_kwargs(prop_cls=ServerProperties, **kwargs) | ||
|
||
def test_from_kwargs_none_given(self): | ||
""" | ||
tests that from_kwargs static method works expectedly - no kwargs given | ||
method should create a QueryOutputDetails with default params | ||
""" | ||
res = self._run_from_kwargs_case({}) | ||
self.assertEqual(res.properties_to_select, list(ServerProperties)) | ||
self.assertEqual(res.output_type, QueryOutputTypes.TO_STR) | ||
self.assertEqual(res.group_by, None) | ||
self.assertEqual(res.sort_by, None) | ||
self.assertEqual(res.group_ranges, None) | ||
self.assertEqual(res.include_ungrouped_results, False) | ||
|
||
def test_from_kwargs_props_given(self): | ||
""" | ||
tests that from_kwargs static method works expectedly - props given | ||
method should create a QueryOutputDetails with props set | ||
""" | ||
res = self._run_from_kwargs_case( | ||
{"properties_to_select": ["server_id", "server_name"]} | ||
) | ||
self.assertEqual( | ||
res.properties_to_select, | ||
[ServerProperties.SERVER_ID, ServerProperties.SERVER_NAME], | ||
) | ||
|
||
def test_from_kwargs_output_type_given(self): | ||
""" | ||
tests that from_kwargs static method works expectedly - output type given | ||
method should create a QueryOutputDetails with output type set | ||
""" | ||
res = self._run_from_kwargs_case({"output_type": "TO_LIST"}) | ||
self.assertEqual(res.output_type, QueryOutputTypes.TO_LIST) | ||
return _run_from_kwargs_case | ||
|
||
def test_from_kwargs_group_by_given(self): | ||
""" | ||
tests that from_kwargs static method works expectedly - group by given | ||
method should create a QueryOutputDetails with group by set | ||
""" | ||
res = self._run_from_kwargs_case({"group_by": "server_name"}) | ||
self.assertEqual(res.group_by, ServerProperties.SERVER_NAME) | ||
|
||
def test_from_kwargs_sort_by_given(self): | ||
""" | ||
tests that from_kwargs static method works expectedly - sort by given | ||
method should create a QueryOutputDetails with sort by set | ||
""" | ||
res = self._run_from_kwargs_case({"sort_by": ["server_name", "server_id"]}) | ||
self.assertEqual( | ||
res.sort_by, | ||
[ | ||
(ServerProperties.SERVER_NAME, False), | ||
(ServerProperties.SERVER_ID, False), | ||
], | ||
) | ||
def test_from_kwargs_none_given(run_kwargs_test_case): | ||
""" | ||
tests that from_kwargs static method works expectedly - no kwargs given | ||
method should create a QueryOutputDetails with default params | ||
""" | ||
res = run_kwargs_test_case({}) | ||
assert res.properties_to_select == list(ServerProperties) | ||
assert res.output_type == QueryOutputTypes.TO_STR | ||
assert not res.group_by | ||
assert not res.sort_by | ||
assert not res.group_ranges | ||
assert not res.include_ungrouped_results | ||
|
||
|
||
def test_from_kwargs_props_given(run_kwargs_test_case): | ||
""" | ||
tests that from_kwargs static method works expectedly - props given | ||
method should create a QueryOutputDetails with props set | ||
""" | ||
res = run_kwargs_test_case({"properties_to_select": ["server_id", "server_name"]}) | ||
assert res.properties_to_select == [ | ||
ServerProperties.SERVER_ID, | ||
ServerProperties.SERVER_NAME, | ||
] | ||
|
||
|
||
def test_from_kwargs_output_type_given(run_kwargs_test_case): | ||
""" | ||
tests that from_kwargs static method works expectedly - output type given | ||
method should create a QueryOutputDetails with output type set | ||
""" | ||
res = run_kwargs_test_case({"output_type": "TO_LIST"}) | ||
assert res.output_type == QueryOutputTypes.TO_LIST | ||
|
||
|
||
def test_from_kwargs_group_by_given(run_kwargs_test_case): | ||
""" | ||
tests that from_kwargs static method works expectedly - group by given | ||
method should create a QueryOutputDetails with group by set | ||
""" | ||
res = run_kwargs_test_case({"group_by": "server_name"}) | ||
assert res.group_by == ServerProperties.SERVER_NAME | ||
|
||
|
||
def test_from_kwargs_sort_by_given(run_kwargs_test_case): | ||
""" | ||
tests that from_kwargs static method works expectedly - sort by given | ||
method should create a QueryOutputDetails with sort by set | ||
""" | ||
res = run_kwargs_test_case({"sort_by": ["server_name", "server_id"]}) | ||
assert res.sort_by == [ | ||
(ServerProperties.SERVER_NAME, False), | ||
(ServerProperties.SERVER_ID, False), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,86 @@ | ||
import unittest | ||
|
||
import pytest | ||
|
||
from structs.email.smtp_account import SMTPAccount | ||
|
||
|
||
class TestEmailParams(unittest.TestCase): | ||
""" | ||
Tests that methods in EmailParams dataclass work expectedly | ||
""" | ||
|
||
def setUp(self) -> None: | ||
"""setup for tests""" | ||
self.mock_smtp_accounts_config = [ | ||
{ | ||
"name": "config1", | ||
"username": "user1", | ||
"password": "some-pass", | ||
"server": "sever", | ||
"port": "port", | ||
"secure": True, | ||
"smtp_auth": True, | ||
}, | ||
{ | ||
"name": "config2", | ||
"username": "user2", | ||
"password": "some-pass2", | ||
"server": "sever", | ||
"port": "port", | ||
"secure": False, | ||
"smtp_auth": False, | ||
}, | ||
] | ||
self.mock_pack_config = {"smtp_accounts": self.mock_smtp_accounts_config} | ||
|
||
def test_from_dict(self): | ||
""" | ||
Tests that from_dict() static method works properly | ||
this method should build a SMTPAccount dataclass from a valid dictionary | ||
""" | ||
|
||
mock_valid_kwargs = { | ||
@pytest.fixture(name="mock_smtp_accounts") | ||
def mock_smtp_accounts_fixture(): | ||
return [ | ||
{ | ||
"name": "config1", | ||
"username": "user1", | ||
"password": "some-pass", | ||
"server": "sever", | ||
"port": "port", | ||
"secure": True, | ||
"smtp_auth": True, | ||
} | ||
mock_invalid_kwargs = {"to_ignore1": "val1", "to_ignore2": "val2"} | ||
}, | ||
{ | ||
"name": "config2", | ||
"username": "user2", | ||
"password": "some-pass2", | ||
"server": "sever", | ||
"port": "port", | ||
"secure": False, | ||
"smtp_auth": False, | ||
}, | ||
] | ||
|
||
|
||
res = SMTPAccount.from_dict({**mock_valid_kwargs, **mock_invalid_kwargs}) | ||
for key, val in mock_valid_kwargs.items(): | ||
self.assertEqual(val, getattr(res, key)) | ||
@pytest.fixture(name="mock_pack_config") | ||
def mock_pack_config_fixture(mock_smtp_accounts): | ||
"""Fixture sets up a mock pack config to test with""" | ||
return {"smtp_accounts": mock_smtp_accounts} | ||
|
||
def test_from_pack_config_valid(self): | ||
""" | ||
Tests that from_pack_config() static method works properly | ||
this method should build a SMTPAccount dataclass from a valid | ||
stackstorm pack_config and an smtp_account_name | ||
""" | ||
expected_attrs = {**self.mock_smtp_accounts_config[0]} | ||
expected_attrs.pop("name") | ||
|
||
res = SMTPAccount.from_pack_config(self.mock_pack_config, "config1") | ||
for key, val in expected_attrs.items(): | ||
self.assertEqual(val, getattr(res, key)) | ||
def test_from_dict(): | ||
""" | ||
Tests that from_dict() static method works properly | ||
this method should build a SMTPAccount dataclass from a valid dictionary | ||
""" | ||
|
||
mock_valid_kwargs = { | ||
"username": "user1", | ||
"password": "some-pass", | ||
"server": "sever", | ||
"port": "port", | ||
"secure": True, | ||
"smtp_auth": True, | ||
} | ||
mock_invalid_kwargs = {"to_ignore1": "val1", "to_ignore2": "val2"} | ||
|
||
def test_from_pack_config_invalid_name(self): | ||
""" | ||
Tests that from_pack_config() method works properly - when given an invalid smtp_account_name | ||
should raise an error if pack config does not contain entry matching smtp_account_name | ||
""" | ||
with pytest.raises(KeyError): | ||
SMTPAccount.from_pack_config(self.mock_pack_config, "invalid-config") | ||
res = SMTPAccount.from_dict({**mock_valid_kwargs, **mock_invalid_kwargs}) | ||
for key, val in mock_valid_kwargs.items(): | ||
assert val == getattr(res, key) | ||
|
||
def test_from_pack_config_invalid_pack(self): | ||
""" | ||
Tests that from_pack_config() method works properly - when given an invalid pack_config | ||
should raise an error if pack config could not be found | ||
""" | ||
with pytest.raises(ValueError): | ||
SMTPAccount.from_pack_config({}, "config1") | ||
|
||
def test_from_pack_config_valid(mock_smtp_accounts, mock_pack_config): | ||
""" | ||
Tests that from_pack_config() static method works properly | ||
this method should build a SMTPAccount dataclass from a valid | ||
stackstorm pack_config and an smtp_account_name | ||
""" | ||
expected_attrs = dict(mock_smtp_accounts[0]) | ||
expected_attrs.pop("name") | ||
|
||
res = SMTPAccount.from_pack_config(mock_pack_config, "config1") | ||
for key, val in expected_attrs.items(): | ||
assert val == getattr(res, key) | ||
|
||
|
||
def test_from_pack_config_invalid_name(mock_pack_config): | ||
""" | ||
Tests that from_pack_config() method works properly - when given an invalid smtp_account_name | ||
should raise an error if pack config does not contain entry matching smtp_account_name | ||
""" | ||
with pytest.raises(KeyError): | ||
SMTPAccount.from_pack_config(mock_pack_config, "invalid-config") | ||
|
||
|
||
def test_from_pack_config_invalid_pack(): | ||
""" | ||
Tests that from_pack_config() method works properly - when given an invalid pack_config | ||
should raise an error if pack config could not be found | ||
""" | ||
with pytest.raises(ValueError): | ||
SMTPAccount.from_pack_config({}, "config1") |