|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Base module for asserts on Test Suites""" |
| 3 | + |
| 4 | + |
| 5 | +import os |
| 6 | +import re |
| 7 | + |
| 8 | + |
| 9 | +ASSERT_MSG_DEFAULT = "Fails at '{}': actual={}, expected={}" |
| 10 | +ASSERT_REGEX_URL = r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+" # noqa: E501 |
| 11 | + |
| 12 | + |
| 13 | +class Assert(object): |
| 14 | + """Base class for inherit new Test classes""" |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def message(cls, assert_name, actual, expected, msg=None): |
| 18 | + """Generate assert message for method that calls for it |
| 19 | + Arguments: |
| 20 | + assert_name {str} -- Assert method name that call |
| 21 | + actual {any} -- Actual value to compare |
| 22 | + expected {any} -- Expected value to compare |
| 23 | + Keyword Arguments: |
| 24 | + msg {[type]} -- [description] (default: {None}) |
| 25 | + Returns: |
| 26 | + str -- Message to be use on Assert method |
| 27 | + """ |
| 28 | + if msg is not None: |
| 29 | + return msg |
| 30 | + return ASSERT_MSG_DEFAULT.format( |
| 31 | + assert_name, |
| 32 | + actual, |
| 33 | + expected) |
| 34 | + |
| 35 | + def equals(self, actual, expected, msg=None): |
| 36 | + """Allow to compare 2 values and check if 1st it's equals to |
| 37 | + 2nd value |
| 38 | + """ |
| 39 | + _msg = self.message("assert_equals", actual, expected, msg=msg) |
| 40 | + if actual != expected: |
| 41 | + raise AssertionError(actual, expected, _msg) |
| 42 | + return True |
| 43 | + |
| 44 | + def not_equals(self, actual, expected, msg=None): |
| 45 | + """Allow to compare 2 value to check if 1st isn't equals to |
| 46 | + 2nd value |
| 47 | + """ |
| 48 | + _msg = self.message("assert_not_equals", actual, expected, msg=msg) |
| 49 | + if actual == expected: |
| 50 | + raise AssertionError(actual, expected, _msg) |
| 51 | + return True |
| 52 | + |
| 53 | + def is_instance(self, instance, class_type, msg=None): |
| 54 | + """Allow to encapsulate method assertIsInstance(obj, cls, msg='')""" |
| 55 | + _msg = self.message( |
| 56 | + "assert_is_instance", instance, class_type, msg=msg) |
| 57 | + if not isinstance(class_type, type): |
| 58 | + class_type = type(class_type) |
| 59 | + if not isinstance(instance, class_type): |
| 60 | + raise AssertionError(instance, class_type, _msg) |
| 61 | + return True |
| 62 | + |
| 63 | + def greater(self, actual, greater, msg=None): |
| 64 | + """Allow to encapsulate method assertGreater(a, b, msg=msg)""" |
| 65 | + _msg = self.message("assert_greater", actual, greater, msg=msg) |
| 66 | + if actual < greater: |
| 67 | + raise AssertionError(actual, greater, _msg) |
| 68 | + return True |
| 69 | + |
| 70 | + def lower(self, actual, lower, msg=None): |
| 71 | + """Allow to encapsulate method assertLower(a, b, msg=msg)""" |
| 72 | + _msg = self.message("assert_lower", actual, lower, msg=msg) |
| 73 | + if actual > lower: |
| 74 | + raise AssertionError(actual, lower, _msg) |
| 75 | + return True |
| 76 | + |
| 77 | + def greater_or_equals(self, actual, greater, msg=None): |
| 78 | + """Allow to compare if A it's greater or equals than B""" |
| 79 | + _msg = self.message( |
| 80 | + "assert_greater_or_equals", actual, greater, msg=msg) |
| 81 | + if actual <= greater: |
| 82 | + raise AssertionError(actual, greater, _msg) |
| 83 | + return True |
| 84 | + |
| 85 | + def lower_or_equals(self, actual, lower, msg=None): |
| 86 | + """Allow to compare if A it's lower or equals than B""" |
| 87 | + _msg = self.message("assert_lower_or_equals", actual, lower, msg=msg) |
| 88 | + if actual >= lower: |
| 89 | + raise AssertionError(actual, lower, _msg) |
| 90 | + return True |
| 91 | + |
| 92 | + def in_list(self, actual, valid_values, msg=None): |
| 93 | + """Allow to compare if value it's in to 2nd list of values""" |
| 94 | + _msg = self.message("assert_in_list", actual, valid_values, msg=msg) |
| 95 | + if actual not in valid_values: |
| 96 | + raise AssertionError(actual, valid_values, _msg) |
| 97 | + return True |
| 98 | + |
| 99 | + def not_in_list(self, actual, invalid_values, msg=None): |
| 100 | + """Allow to compare if value it's not in to 2nd list of values""" |
| 101 | + _msg = self.message( |
| 102 | + "assert_not_in_list", actual, invalid_values, msg=msg) |
| 103 | + if actual in invalid_values: |
| 104 | + raise AssertionError(actual, invalid_values, _msg) |
| 105 | + return True |
| 106 | + |
| 107 | + def regex(self, actual, pattern, msg=None): |
| 108 | + """Allow to compare if value match pattern""" |
| 109 | + _msg = self.message("assert_regex", actual, pattern, msg=msg) |
| 110 | + is_match = re.match(pattern, actual) |
| 111 | + if not is_match: |
| 112 | + raise AssertionError(actual, pattern, _msg) |
| 113 | + return True |
| 114 | + |
| 115 | + def not_regex(self, actual, pattern, msg=None): |
| 116 | + """Allow to compare if value not match pattern""" |
| 117 | + _msg = self.message("assert_not_regex", actual, pattern, msg=msg) |
| 118 | + is_match = re.match(pattern, actual) |
| 119 | + if is_match: |
| 120 | + raise AssertionError(actual, pattern, _msg) |
| 121 | + return True |
| 122 | + |
| 123 | + def regex_url(self, actual, msg=None): |
| 124 | + """Allow to compare if value match url pattern, can use |
| 125 | + custom pattern |
| 126 | + """ |
| 127 | + return self.regex(actual, ASSERT_REGEX_URL, msg=msg) |
| 128 | + |
| 129 | + def path_exist(self, actual, msg=None): |
| 130 | + """Allow to check if path exist, can check if is_dir also""" |
| 131 | + _msg = self.message("assert_path_exist", actual, "", msg=msg) |
| 132 | + if not os.path.exists(actual): |
| 133 | + raise AssertionError(actual, "PATH_NOT_EXIST", _msg) |
| 134 | + _is_dir = os.path.isdir(actual) |
| 135 | + if not _is_dir: |
| 136 | + raise AssertionError(actual, "PATH_NOT_DIR", _msg) |
| 137 | + return True |
| 138 | + |
| 139 | + def path_not_exist(self, actual, msg=None): |
| 140 | + """Allow to check if path not exist, can check if is_dir also""" |
| 141 | + _msg = self.message("assert_path_not_exist", actual, "", msg=msg) |
| 142 | + if os.path.exists(actual): |
| 143 | + raise AssertionError(actual, "PATH_EXIST_AND_MUST_NOT", _msg) |
| 144 | + return True |
| 145 | + |
| 146 | + def true(self, actual, msg=None): |
| 147 | + """Allow to compare and check if value it's equals to 'True'""" |
| 148 | + return self.equals(actual, True, msg=msg) |
| 149 | + |
| 150 | + def false(self, actual, msg=None): |
| 151 | + """Allow to compare and check if value it's equals to 'False'""" |
| 152 | + return self.equals(actual, False, msg=msg) |
| 153 | + |
| 154 | + def none(self, actual, msg=None): |
| 155 | + """Allow to compare and check if value it's equals to 'None'""" |
| 156 | + return self.equals(actual, None, msg=msg) |
| 157 | + |
| 158 | + def not_none(self, actual, msg=None): |
| 159 | + """Allow to compare and check if value it's not equals to 'None'""" |
| 160 | + return self.not_equals(actual, None, msg=msg) |
0 commit comments