How to use recreate method in tox

Best Python code snippet using tox_python

test_se5.py

Source:test_se5.py Github

copy

Full Screen

1import sys2import os3import math4# Handle the fact that the test code may not5# be in the same directory as the solution code6sys.path.insert(0, os.getcwd())7import se58import numpy as np9MODULE = "se5"10# # #11#12# HELPER FUNCTIONS13#14# # #15def pretty_print_repr(x):16 """17 A version of repr with some special casing.18 """19 if isinstance(x, np.ndarray):20 return "np." + repr(x)21 return repr(x)22def gen_recreate_msg(module, function, *params, **kwparams):23 24 params_str = ", ".join([pretty_print_repr(p) for p in params])25 if len(kwparams) > 0:26 params_str += ", ".join(["{} = {}".format(k, repr(v)) for k, v in kwparams.items()])27 recreate_msg = "To recreate this test in ipython3 run:\n"28 recreate_msg += " {}.{}({})".format(module, function, params_str)29 return recreate_msg30def check_none(actual, recreate_msg=None):31 msg = "The function returned None."32 msg += " Did you forget to replace the placeholder value we provide?"33 if recreate_msg is not None:34 msg += "\n" + recreate_msg35 assert actual is not None, msg36def check_type(actual, expected, recreate_msg=None):37 actual_type = type(actual)38 expected_type = type(expected)39 msg = "The function returned a value of the wrong type.\n"40 msg += " Expected return type: {}.\n".format(expected_type.__name__)41 msg += " Actual return type: {}.".format(actual_type.__name__)42 if recreate_msg is not None:43 msg += "\n" + recreate_msg44 assert isinstance(actual, expected_type), msg45def check_dtype(array, expected, recreate_msg=None):46 actual_dtype = array.dtype47 expected_dtype = expected48 msg = "The function returned an array of the wrong dtype.\n"49 msg += " Expected return dtype: {}.\n".format(expected_dtype)50 msg += " Actual return dtype: {}.".format(actual_dtype)51 if recreate_msg is not None:52 msg += "\n" + recreate_msg53 assert actual_dtype == expected_dtype, msg54def check_array_equal(actual, expected, recreate_msg):55 msg = "The function returned the wrong array"56 msg += " Expected array: {}\n".format(expected)57 msg += " Actual returned array: {}\n".format(actual)58 if recreate_msg is not None:59 msg += "\n" + recreate_msg60 61 np.testing.assert_allclose(actual, expected,62 err_msg = msg, verbose=False)63 64 65 # # #66#67# TEST HELPERS68#69# # #70def check_is_ndarray(actual, recreate_msg=None):71 check_type(actual, np.zeros(1), recreate_msg)72 73def test_compute_matching_values():74 x = np.array([1, 2, 3, 4])75 y = np.array([1, 5, 3, 2])76 recreate_msg = gen_recreate_msg(MODULE, 'compute_matching',77 x, y)78 79 result = se5.compute_matching(x, y)80 check_none(result, recreate_msg)81 check_is_ndarray(result, recreate_msg)82 check_dtype(result, bool, recreate_msg)83 84 85 check_array_equal(result, np.array([True, False, True, False]),86 recreate_msg)87def test_compute_matching_indices():88 x = np.array([1, 2, 3, 4])89 y = np.array([1, 5, 3, 2])90 recreate_msg = gen_recreate_msg(MODULE, 'compute_matching_indices',91 x, y)92 93 result = se5.compute_matching_indices(x, y)94 check_none(result, recreate_msg)95 check_is_ndarray(result, recreate_msg)96 check_dtype(result, int, recreate_msg)97 98 99 check_array_equal(result, np.array([0, 2]),100 recreate_msg)101 102def test_powers():103 for p in [1, 2, 4]:104 for N in [1, 2, 5, 10]:105 recreate_msg = gen_recreate_msg(MODULE, 'powers',106 N, p)107 108 109 result = se5.powers(N, p)110 check_none(result, recreate_msg)111 check_is_ndarray(result, recreate_msg)112 check_array_equal(result,113 np.array([p**i for i in range(N)]),114 recreate_msg)115def test_clip_values():116 x = np.linspace(0, 2, 10)117 x_orig = x.copy()118 ### Check for modification of input array119 recreate_msg = gen_recreate_msg(MODULE, 'clip_values',120 min_val=1, max_val=1.8)121 122 result = se5.clip_values(x, min_val=1, max_val=1.8)123 124 check_none(result, recreate_msg)125 check_is_ndarray(result, recreate_msg)126 assert np.allclose(x, x_orig), \127 "\n Input array was modified.\n\n" + recreate_msg128 129 ### Check what happens with no specification130 recreate_msg = gen_recreate_msg(MODULE, 'clip_values')131 result = se5.clip_values(x)132 133 check_none(result, recreate_msg)134 check_is_ndarray(result, recreate_msg)135 assert np.max(result) == np.max(x), \136 "\n The max value of the array was modified when max_val=None\n\n" \137 + recreate_msg138 139 assert np.min(result) == np.min(x), \140 "\n The min value of the array was modified when min_val=None\n\n" \141 + recreate_msg142 143 144 ### Check minimum value145 recreate_msg = gen_recreate_msg(MODULE, 'clip_values', min_val =1)146 result = se5.clip_values(x, min_val=1)147 148 check_none(result, recreate_msg)149 check_is_ndarray(result, recreate_msg)150 assert np.min(result) == 1.0,\151 "\n The minimum value of the array is not 1.0\n\n" \152 + recreate_msg153 154 ### Check maximum value155 recreate_msg = gen_recreate_msg(MODULE, 'clip_values', max_val =1)156 result = se5.clip_values(x, max_val=1)157 158 check_none(result, recreate_msg)159 check_is_ndarray(result, recreate_msg)160 assert np.max(result) == 1.0, \161 "\n The maximum value of the array is not 1.0\n\n" \162 + recreate_msg163 164 ### Check Both165 recreate_msg = gen_recreate_msg(MODULE, 'clip_values', min_val =1.0, max_val=1.5)166 result = se5.clip_values(x, min_val=1.0, max_val=1.5)167 168 check_none(result, recreate_msg)169 check_is_ndarray(result, recreate_msg)170 assert np.max(result) == 1.5, \171 "\n The maximum value of the array is not 1.5\n\n"\172 + recreate_msg173 assert np.min(result) == 1.0, \174 "\n The minimum value of the array is not 1.0\n\n"\175 + recreate_msg176 177def test_find_closest_value():178 def manual_closest_value(x):179 closest_delta = 1e9180 181 closest_idx = None182 closest_val = None183 184 for i, x_item in enumerate(x):185 delta = abs(x_item - np.mean(x))186 if delta < closest_delta:187 closest_delta = delta188 closest_idx = i189 closest_val = x_item190 assert closest_idx is not None191 192 return closest_idx, closest_val193 194 195 x = np.array([1.0, 1.1, 1.2, 1.3, 1.4, 1.5])196 197 recreate_msg = gen_recreate_msg(MODULE, 'find_closest_value', 198 x)199 result = se5.find_closest_value(x)200 check_none(result, recreate_msg)201 expected_closest_val = manual_closest_value(x)202 assert expected_closest_val == result, \203 "\n function returned {} as closest val ".format(result) \204 + "but we expected {}\n\n".format(expected_closest_val) \205 + recreate_msg206def test_select_row_col():207 x = np.arange(20).reshape(4, 5)208 def custom_get_cols(x, cols):209 out = np.stack([x[:, r] for r in cols], -1)210 return out211 212 def custom_get_rows(x, rows):213 out = np.stack([x[r] for r in rows])214 return out215 216 def custom_get_rows_cols(x, rows, cols):217 row_out = np.stack([x[r] for r in rows])218 out = np.stack([row_out[:, r] for r in cols], -1)219 return out220 221 222 for tgt_cols in [[0], [1, 2, 3], [3, 2, 1], [2, 1, 3, 4, 0]]:223 recreate_msg = gen_recreate_msg(MODULE, 'select_row_col', x, None, tgt_cols)224 225 result = se5.select_row_col(x, None, tgt_cols)226 227 check_none(result, recreate_msg)228 check_is_ndarray(result, recreate_msg)229 expected_shape = (4, len(tgt_cols))230 assert result.shape == expected_shape, \231 "The shape of the returned array was {}, but".format(result.shape) \232 + " we expected {}\n\n".format(expected_shape) + recreate_msg233 expected_value = custom_get_cols(x, tgt_cols)234 check_array_equal(result, expected_value, 235 recreate_msg)236 237 for tgt_rows in [[0], [1, 2, 3], [3, 2, 1], [2, 1, 3, 0]]:238 recreate_msg = gen_recreate_msg(MODULE, 'select_row_col', x, tgt_rows, None)239 240 result = se5.select_row_col(x, tgt_rows, None)241 242 check_none(result, recreate_msg)243 check_is_ndarray(result, recreate_msg)244 expected_shape = (len(tgt_rows), 5)245 assert result.shape == expected_shape, \246 "The shape of the returned array was {}, but".format(result.shape) \247 + " we expected {}\n\n".format(expected_shape) + recreate_msg248 expected_value = custom_get_rows(x, tgt_rows)249 check_array_equal(result, expected_value, 250 recreate_msg)251 252 for tgt_rows, tgt_cols in [([0],[0]), ([1, 2, 3],[1, 3]), ([3, 1],[0,2])]:253 recreate_msg = gen_recreate_msg(MODULE, 'select_row_col', x, tgt_rows, tgt_cols)254 255 result = se5.select_row_col(x, tgt_rows, tgt_cols)256 257 check_none(result, recreate_msg)258 check_is_ndarray(result, recreate_msg)259 expected_shape = (len(tgt_rows), len(tgt_cols))260 assert result.shape == expected_shape, \261 "The shape of the returned array was {}, but".format(result.shape) \262 + " we expected {}\n\n".format(expected_shape) + recreate_msg263 expected_value = custom_get_rows_cols(x, tgt_rows, tgt_cols)264 check_array_equal(result, expected_value, 265 recreate_msg)266 267 268 for tgt_rows, tgt_cols in [(None, None)]:269 recreate_msg = gen_recreate_msg(MODULE, 'select_row_col', x, None, None)270 271 result = se5.select_row_col(x, None, None)272 273 check_none(result, recreate_msg)274 check_is_ndarray(result, recreate_msg)275 expected_shape = (x.shape[0], x.shape[1])276 assert result.shape == expected_shape, \277 "The shape of the returned array was {}, but".format(result.shape) \278 + " we expected {}\n\n".format(expected_shape) + recreate_msg279 check_array_equal(result, x, recreate_msg)280 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful