Skip to content
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

KaizenTask1119 Write unit tests for the functions typed_get() and checked_get() #1120

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
214 changes: 214 additions & 0 deletions helpers/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,217 @@ def test9(self) -> None:
actual_result = list(hdict.get_nested_dict_iterator(dict_))
expected_result = [(("key0", "key00"), config), (("key1",), "value1")]
self.assertListEqual(actual_result, expected_result)


class Test_typed_get(hunitest.TestCase):
mihir1906 marked this conversation as resolved.
Show resolved Hide resolved
def test1(self) -> None:
"""
Test that the function doesn't raise an exception when the key is
present in the dictionary.
"""
# Prepare inputs.
dict_ = {"key0": "value0", "key1": "value1"}
# Run.
actual = hdict.typed_get(dict_, "key1")
# Check output.
self.assertIsInstance(actual, str)
expected = "value1"
self.assert_equal(actual, expected)
mihir1906 marked this conversation as resolved.
Show resolved Hide resolved

def test2(self) -> None:
"""
Test that the function raises an exception when key is not present in
dictionary without default value.
"""
# Prepare inputs.
dict_ = {}
# Run.
with self.assertRaises(AssertionError) as e:
hdict.typed_get(dict_, "key1")
# Check output.
actual_exp = str(e.exception)
expected_exp = """
* Failed assertion *
'key1' in '{}'
"""
self.assert_equal(actual_exp, expected_exp, fuzzy_match=True)

def test3(self) -> None:
"""
Test that the function raises an exception when a non-dict type is
provided instead of dictionary.
"""
# Prepare inputs.
dict_ = []
# Run.
with self.assertRaises(AssertionError) as e:
hdict.typed_get(dict_, "key1")
# Check output.
actual = str(e.exception)
expected = r"""
* Failed assertion *
Instance of '[]' is '<class 'list'>' instead of '<class 'dict'>'
"""
self.assert_equal(actual, expected, fuzzy_match=True)

def test4(self) -> None:
mihir1906 marked this conversation as resolved.
Show resolved Hide resolved
"""
Test that the function doesn't raise an exception when key is not
present in the dictionary with default value provided.
"""
# Prepare inputs.
dict_ = {"key0": "value0", "key1": "value1"}
# Run.
actual = hdict.typed_get(dict_, "key2", default_value="default_value")
# Check output.
self.assertIsInstance(actual, str)
expected = "default_value"
self.assert_equal(actual, expected)

def test5(self) -> None:
"""
Test that the function doesn't raise an exception when key is not
present in the dictionary with default value as None.
"""
# Prepare inputs.
mihir1906 marked this conversation as resolved.
Show resolved Hide resolved
dict_ = {"key0": "value0", "key1": "value1"}
# Run.
actual = hdict.typed_get(dict_, "key2", default_value=None)
# Check output.
expected = None
self.assertIsNone(actual, expected)

def test6(self) -> None:
"""
Test that the function doesn't raise an exception when the key is
present in the dictionary and value is of the expected type.
"""
# Prepare inputs.
dict_ = {"key0": "value0", "key1": "value1"}
# Run.
actual = hdict.typed_get(dict_, "key0", expected_type=str)
# Check output.
self.assertIsInstance(actual, str)
expected = "value0"
self.assert_equal(actual, expected)

def test7(self) -> None:
"""
Test that the function raises an exception when the key is
present in the dictionary and the value is not of the expected type.
"""
# Prepare inputs.
dict_ = {"key0": "value0", "key1": "value1"}
# Run.
with self.assertRaises(AssertionError) as e:
hdict.typed_get(dict_, "key0", expected_type=int)
# Check output.
actual_exp = str(e.exception)
expected_exp = r"""
* Failed assertion *
Instance of 'value0' is '<class 'str'>' instead of '<class 'int'>'
"""
self.assert_equal(actual_exp, expected_exp, fuzzy_match=True)

def test8(self) -> None:
"""
Test that the function doesn't raise an exception when the key is not
present in dictionary and default value matches expected type.
"""
# Prepare inputs.
dict_ = {"key0": "value0", "key1": "value1"}
# Run.
actual = hdict.typed_get(
dict_, "key2", default_value="default_value", expected_type=str
)
# Check output.
self.assertIsInstance(actual, str)
expected = "default_value"
self.assert_equal(actual, expected)

def test9(self) -> None:
"""
Test that the function raises an exception when the key is not present
in dictionary and default value doesn't match correct expected type.
"""
# Prepare inputs.
dict_ = {"key0": "value0", "key1": "value1"}
# Run.
with self.assertRaises(AssertionError) as e:
hdict.typed_get(
dict_, "key2", default_value="dft_value", expected_type=int)
# Check output.
actual_exp = str(e.exception)
expected_exp = r"""
* Failed assertion *
Instance of 'dft_value' is '<class 'str'>' instead of '<class 'int'>'
"""
self.assert_equal(actual_exp, expected_exp, fuzzy_match=True)


class Test_checked_get(hunitest.TestCase):
def test1(self) -> None:
"""
Test that the function doesn't raise an exception when key is present
in the dictionary.
"""
# Prepare inputs.
dict_ = {"key0": "value0", "key1": "value1"}
# Run.
actual = hdict.checked_get(dict_, "key0")
# Check output.
self.assertIsInstance(actual, str)
expected = "value0"
self.assert_equal(actual, expected)

def test2(self) -> None:
"""
Test that the function raises an exception when key is not present in
the dictionary.
"""
# Prepare inputs.
dict_ = {"key0": "value0", "key1": "value1"}
# Run.
with self.assertRaises(AssertionError) as e:
hdict.checked_get(dict_, "key2")
# Check output.
actual = str(e.exception)
expected = r"""
* Failed assertion *
'key2' in '{'key0': 'value0', 'key1': 'value1'}'
"""
self.assert_equal(actual, expected, fuzzy_match=True)

def test3(self) -> None:
"""
Test that the function raises an exception with key of non-string type.
"""
# Prepare inputs.
dict_ = {"0": "value0", "1": "value1"}
# Run.
with self.assertRaises(AssertionError) as e:
hdict.checked_get(dict_, 1)
# Check output.
actual = str(e.exception)
expected = r"""
* Failed assertion *
'1' in '{'0': 'value0', '1': 'value1'}'
"""
self.assert_equal(actual, expected, fuzzy_match=True)

def test4(self) -> None:
"""
Test that the function raises an exception with key of None type.
"""
# Prepare inputs.
dict_ = {"0": "value0", "1": "value1"}
# Run.
with self.assertRaises(AssertionError) as e:
hdict.checked_get(dict_, None)
# Check output.
actual = str(e.exception)
expected = r"""
* Failed assertion *
'None' in '{'0': 'value0', '1': 'value1'}'
"""
self.assert_equal(actual, expected, fuzzy_match=True)
Loading