teddecor.UnitTest.Asserts.Asserts
Assert that something is true or false
This module provides more descriptive and usefull asserts for things like equal, not null, contains, etc.
1"""Assert that something is true or false 2 3This module provides more descriptive and usefull asserts for things like equal, not null, contains, etc. 4""" 5 6from __future__ import annotations 7from shutil import ExecError 8from typing import Any, Callable, Union 9 10__all__ = [ 11 "assertEqual", 12 "assertNotEqual", 13 "assertRaises", 14 "assertWithin", 15 "assertNone", 16 "assertNotNone", 17] 18 19# PERF: Replace errors with lested/grouped errors if python 3.11 20def assertEqual(left: Any, right: Any, message: str = "") -> Union[bool, None]: 21 """Assert that the left operand (First parameter) is equal to the right operand (Second Parameter). 22 23 Args: 24 left (Any): Left Operand 25 right (Any): Right Operand 26 message (str, optional): User defind error messaage when left != right. 27 Defaults to "Left operand not equal to right operand". 28 29 Raises: 30 AssertionError: When left operand type doesn't equal right operands type 31 AssertionError: When left operand doesn't equal right operand 32 """ 33 34 if left != right: 35 if message == "": 36 message = "Values are not equal" 37 raise AssertionError(message) 38 39 return True 40 41 42def assertGreaterThan(left: Any, right: Any, message: str = "") -> Union[bool, None]: 43 """Assert that the left operand (First parameter) is greater than the right operand (Second Parameter). 44 45 Args: 46 left (Any): Left Operand 47 right (Any): Right Operand 48 message (str, optional): User defind error messaage when left != right. 49 Defaults to "Left operand not equal to right operand". 50 51 Raises: 52 AssertionError: When left operand is less than the right operand 53 """ 54 55 if left < right: 56 if message == "": 57 message = "Value is less than not greater than" 58 raise AssertionError(message) 59 60 return True 61 62 63def assertLessThan(left: Any, right: Any, message: str = "") -> Union[bool, None]: 64 """Assert that the left operand (First parameter) is less than the right operand (Second Parameter). 65 66 Args: 67 left (Any): Left Operand 68 right (Any): Right Operand 69 message (str, optional): User defind error messaage when left != right. 70 Defaults to "Left operand not equal to right operand". 71 72 Raises: 73 AssertionError: When left operand is greater than the right operands 74 """ 75 76 if left > right: 77 if message == "": 78 message = "Value is greater than not less than" 79 raise AssertionError(message) 80 81 return True 82 83 84def assertNotEqual(left: Any, right: Any, message: str = "") -> Union[bool, None]: 85 """Assert that the left operand (First parameter) is not equal to the right operand (Second Parameter). 86 87 Args: 88 left (Any): Left Operand 89 right (Any): Right Operand 90 message (str, optional): User defind error messaage when left != right. 91 Defaults to "Left operand not equal to right operand". 92 93 Raises: 94 AssertionError: When left operand type doesn't equal right operands type 95 AssertionError: When left operand doesn't equal right operand 96 """ 97 98 if left == right: 99 if message == "": 100 message = "Values are equal" 101 raise AssertionError(message) 102 103 return True 104 105 106def assertRaises( 107 function: Callable, exception: Exception = None, message: str = "" 108) -> Union[bool, None]: 109 """Assert that a exceptions is raised within a callable piece of code 110 111 Args: 112 exception (Exception, optional): Exception that is expected. Default None 113 function (Callable): Callable piece of code 114 message (str, optional): Error message. Defaults to None. 115 116 Raises: 117 AssertionError: Unexpected Exception 118 AssertionError: No Exceptions 119 120 Note: 121 If exception is not passed it will assume that it should expect any exception. 122 """ 123 124 if exception is not None: 125 try: 126 _ = function() 127 except exception: 128 return True 129 except Exception as error: 130 if message == "": 131 message = f"Unexpected exception {type(error).__name__}" 132 raise AssertionError(message) from error 133 else: 134 try: 135 _ = function() 136 except Exception: 137 return True 138 139 if message == "": 140 message = "No exception raised" 141 142 raise AssertionError(message) 143 144 145def assertWithin(search: Any, obj: Any, message: str = "") -> Union[bool, None]: 146 """Assert that a search value is contained within a certain string. 147 148 Args: 149 search (str, Any): Search value 150 obj (str, list): Object to identify if search is within 151 message (str, optional): Error message. Defaults to None. 152 153 Raises: 154 AssertionError: When the search is not contained within the object 155 156 Note: 157 This assert runs `value not in object` so it only works if object 158 implements __iter__. 159 """ 160 161 if message == "": 162 message = f"'{search}' is not within the given object" 163 164 try: 165 if search not in obj: 166 raise AssertionError(message) 167 except Exception as error: 168 raise AssertionError(message) from error 169 170 return True 171 172 173def assertNone(value: Any, message: str = "") -> Union[bool, None]: 174 """Assert that a value is None. 175 176 Args: 177 value (Any): Value that should be None 178 message (str, optional): Error message. Defaults to None. 179 180 Raises: 181 AssertionError: When the value is not None 182 """ 183 184 if message == "": 185 message = f"{type(value)} is not NoneType" 186 187 if value is not None: 188 raise AssertionError(message) 189 190 return True 191 192 193def assertNotNone(value: Any, message: str = "") -> Union[bool, None]: 194 """Assert that a value is not None. 195 196 Args: 197 value (Any): Value that shouldn't be None 198 message (str, optional): Error message. Defaults to None. 199 200 Raises: 201 AssertionError: When the value is None 202 """ 203 204 if message == "": 205 message = f"{value} is NoneType" 206 207 if value is None: 208 raise AssertionError(message) 209 210 return True
21def assertEqual(left: Any, right: Any, message: str = "") -> Union[bool, None]: 22 """Assert that the left operand (First parameter) is equal to the right operand (Second Parameter). 23 24 Args: 25 left (Any): Left Operand 26 right (Any): Right Operand 27 message (str, optional): User defind error messaage when left != right. 28 Defaults to "Left operand not equal to right operand". 29 30 Raises: 31 AssertionError: When left operand type doesn't equal right operands type 32 AssertionError: When left operand doesn't equal right operand 33 """ 34 35 if left != right: 36 if message == "": 37 message = "Values are not equal" 38 raise AssertionError(message) 39 40 return True
Assert that the left operand (First parameter) is equal to the right operand (Second Parameter).
Args: left (Any): Left Operand right (Any): Right Operand message (str, optional): User defind error messaage when left != right. Defaults to "Left operand not equal to right operand".
Raises: AssertionError: When left operand type doesn't equal right operands type AssertionError: When left operand doesn't equal right operand
85def assertNotEqual(left: Any, right: Any, message: str = "") -> Union[bool, None]: 86 """Assert that the left operand (First parameter) is not equal to the right operand (Second Parameter). 87 88 Args: 89 left (Any): Left Operand 90 right (Any): Right Operand 91 message (str, optional): User defind error messaage when left != right. 92 Defaults to "Left operand not equal to right operand". 93 94 Raises: 95 AssertionError: When left operand type doesn't equal right operands type 96 AssertionError: When left operand doesn't equal right operand 97 """ 98 99 if left == right: 100 if message == "": 101 message = "Values are equal" 102 raise AssertionError(message) 103 104 return True
Assert that the left operand (First parameter) is not equal to the right operand (Second Parameter).
Args: left (Any): Left Operand right (Any): Right Operand message (str, optional): User defind error messaage when left != right. Defaults to "Left operand not equal to right operand".
Raises: AssertionError: When left operand type doesn't equal right operands type AssertionError: When left operand doesn't equal right operand
107def assertRaises( 108 function: Callable, exception: Exception = None, message: str = "" 109) -> Union[bool, None]: 110 """Assert that a exceptions is raised within a callable piece of code 111 112 Args: 113 exception (Exception, optional): Exception that is expected. Default None 114 function (Callable): Callable piece of code 115 message (str, optional): Error message. Defaults to None. 116 117 Raises: 118 AssertionError: Unexpected Exception 119 AssertionError: No Exceptions 120 121 Note: 122 If exception is not passed it will assume that it should expect any exception. 123 """ 124 125 if exception is not None: 126 try: 127 _ = function() 128 except exception: 129 return True 130 except Exception as error: 131 if message == "": 132 message = f"Unexpected exception {type(error).__name__}" 133 raise AssertionError(message) from error 134 else: 135 try: 136 _ = function() 137 except Exception: 138 return True 139 140 if message == "": 141 message = "No exception raised" 142 143 raise AssertionError(message)
Assert that a exceptions is raised within a callable piece of code
Args: exception (Exception, optional): Exception that is expected. Default None function (Callable): Callable piece of code message (str, optional): Error message. Defaults to None.
Raises: AssertionError: Unexpected Exception AssertionError: No Exceptions
Note: If exception is not passed it will assume that it should expect any exception.
146def assertWithin(search: Any, obj: Any, message: str = "") -> Union[bool, None]: 147 """Assert that a search value is contained within a certain string. 148 149 Args: 150 search (str, Any): Search value 151 obj (str, list): Object to identify if search is within 152 message (str, optional): Error message. Defaults to None. 153 154 Raises: 155 AssertionError: When the search is not contained within the object 156 157 Note: 158 This assert runs `value not in object` so it only works if object 159 implements __iter__. 160 """ 161 162 if message == "": 163 message = f"'{search}' is not within the given object" 164 165 try: 166 if search not in obj: 167 raise AssertionError(message) 168 except Exception as error: 169 raise AssertionError(message) from error 170 171 return True
Assert that a search value is contained within a certain string.
Args: search (str, Any): Search value obj (str, list): Object to identify if search is within message (str, optional): Error message. Defaults to None.
Raises: AssertionError: When the search is not contained within the object
Note:
This assert runs value not in object
so it only works if object
implements __iter__.
174def assertNone(value: Any, message: str = "") -> Union[bool, None]: 175 """Assert that a value is None. 176 177 Args: 178 value (Any): Value that should be None 179 message (str, optional): Error message. Defaults to None. 180 181 Raises: 182 AssertionError: When the value is not None 183 """ 184 185 if message == "": 186 message = f"{type(value)} is not NoneType" 187 188 if value is not None: 189 raise AssertionError(message) 190 191 return True
Assert that a value is None.
Args: value (Any): Value that should be None message (str, optional): Error message. Defaults to None.
Raises: AssertionError: When the value is not None
194def assertNotNone(value: Any, message: str = "") -> Union[bool, None]: 195 """Assert that a value is not None. 196 197 Args: 198 value (Any): Value that shouldn't be None 199 message (str, optional): Error message. Defaults to None. 200 201 Raises: 202 AssertionError: When the value is None 203 """ 204 205 if message == "": 206 message = f"{value} is NoneType" 207 208 if value is None: 209 raise AssertionError(message) 210 211 return True
Assert that a value is not None.
Args: value (Any): Value that shouldn't be None message (str, optional): Error message. Defaults to None.
Raises: AssertionError: When the value is None