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
228 changes: 228 additions & 0 deletions helpers/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,231 @@ 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.
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 dictionary is empty and
no 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 the provided dict_ is
not a dictionary but list.
"""
# 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 raises an exception when the provided dict_ is
not a dictionary but set.
"""
# Prepare inputs.
dict_ = set()
# Run.
with self.assertRaises(AssertionError) as e:
hdict.typed_get(dict_, "key1")
# Check output.
actual = str(e.exception)
expected = r"""
* Failed assertion *
Instance of 'set()' is '<class 'set'>' instead of '<class 'dict'>'
"""
self.assert_equal(actual, expected, fuzzy_match=True)

def test5(self) -> None:
"""
Test that the function raises an exception when the provided dict_ is
not a dictionary but integer.
"""
# Prepare inputs.
mihir1906 marked this conversation as resolved.
Show resolved Hide resolved
dict_ = 42
# Run.
with self.assertRaises(AssertionError) as e:
hdict.typed_get(dict_, "key1")
# Check output.
actual = str(e.exception)
expected = r"""
* Failed assertion *
Instance of '42' is '<class 'int'>' instead of '<class 'dict'>'
"""
self.assert_equal(actual, expected, fuzzy_match=True)

def test6(self) -> None:
"""
Test that the function raises an exception when the provided dict_ is
not a dictionary but string.
"""
# Prepare inputs.
dict_ = "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 'dict' is '<class 'str'>' instead of '<class 'dict'>'
"""
self.assert_equal(actual, expected, fuzzy_match=True)

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

def test8(self) -> None:
"""
Test that the function doesn't raise an exception when dictionary is
empty and default value is provided.
"""
# Prepare inputs.
dict_ = {}
# Run.
actual = hdict.typed_get(dict_, "key1", default_value="default_value")
# Check output.
expected = "default_value"
self.assert_equal(actual, expected)

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

def test10(self) -> None:
mihir1906 marked this conversation as resolved.
Show resolved Hide resolved
"""
Test that the function does not raise an exception when the key is
missing, the default value is used, and the retrieved value's type
matches the 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.
expected = "default_value"
self.assert_equal(actual, expected)


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.
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 different 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