How to use assert_dict_eq method in pyresttest

Best Python code snippet using pyresttest_python

data.py

Source:data.py Github

copy

Full Screen

...86 logging.debug(f"match_stdout_to_json_file: stdout={stdout!r}")87 logging.debug(f"match_stdout_to_json_file: file={obj!r}")88 if isinstance(obj, dict):89 stdout = {key: value for key, value in stdout.items() if key in obj}90 assert_dict_eq(obj, stdout)91 elif isinstance(obj, list):92 obj = {"key": obj}93 stdout = {"key": stdout}94 assert_dict_eq(obj, stdout)95 assert_dict_eq(obj, stdout)96 else:97 assert_eq(obj, stdout)98def match_stdout_to_json_file_exactly(ctx, filename=None):99 runcmd_get_stdout = globals()["runcmd_get_stdout"]100 assert_eq = globals()["assert_eq"]101 assert_dict_eq = globals()["assert_dict_eq"]102 stdout = runcmd_get_stdout(ctx)103 stdout = json.loads(stdout.strip())104 obj = json.load(open(filename))105 logging.debug(f"match_stdout_to_json_file: stdout={stdout!r}")106 logging.debug(f"match_stdout_to_json_file: file={obj!r}")107 if isinstance(obj, list):108 obj = {"key": obj}109 stdout = {"key": stdout}110 assert_dict_eq(obj, stdout)111 elif isinstance(obj, dict):112 assert_dict_eq(obj, stdout)113 else:114 assert_eq(obj, stdout)115def manifests_match(ctx, expected=None, actual=None):116 assert_eq = globals()["assert_eq"]117 assert_dict_eq = globals()["assert_dict_eq"]118 logging.debug(f"comparing manifests {expected} and {actual}")119 expected_objs = list(yaml.safe_load_all(open(expected)))120 actual_objs = list(yaml.safe_load_all(open(actual)))121 logging.debug(f"there are {len(expected_objs)} and {len(actual_objs)} objects")122 i = 0123 while expected_objs and actual_objs:124 e = expected_objs.pop(0)125 a = actual_objs.pop(0)126 logging.debug(f"comparing manifest objects at index {i}:")127 logging.debug(f" expected: {e}")128 logging.debug(f" actual : {a}")129 assert_dict_eq(e, a)130 i += 1131 logging.debug(f"remaining expected objecvts: {expected_objs}")132 logging.debug(f"remaining actual objecvts : {actual_objs}")133 assert_eq(expected_objs, [])134 assert_eq(actual_objs, [])135 logging.debug(f"manifests {expected} and {actual} match")136def file_is_readable_by_owner(ctx, filename=None):137 assert_eq = globals()["assert_eq"]138 st = os.lstat(filename)139 mode = stat.S_IMODE(st.st_mode)140 logging.debug("file mode: %o", mode)141 assert_eq(mode, 0o400)142def file_does_not_contain(ctx, filename=None, pattern=None):143 data = open(filename).read()...

Full Screen

Full Screen

kmeans_tests.py

Source:kmeans_tests.py Github

copy

Full Screen

2 assign_data_to_closest_centroid, update_assignment,\3 mean_of_points, update_centroids4import numpy as np5# help functions6def assert_dict_eq(dict1, dict2):7 assert type(dict1) is dict8 assert type(dict2) is dict9 # keys10 assert dict1.keys() == dict2.keys()11 # values12 for k, v in dict2.items():13 matrix2 = np.array(v)14 matrix1 = np.array(dict1[k])15 assert np.allclose(np.sort(matrix1, axis=0), np.sort(matrix2, axis=0))16def setup_data_centroids():17 data = [18 [-1.01714716, 0.95954521, 1.20493919, 0.34804443],19 [-1.36639346, -0.38664658, -1.02232584, -1.05902604],20 [1.13659605, -2.47109085, -0.83996912, -0.24579457],21 [-1.48090019, -1.47491857, -0.6221167, 1.79055006],22 [-0.31237952, 0.73762417, 0.39042814, -1.1308523],23 [-0.83095884, -1.73002213, -0.01361636, -0.32652741],24 [-0.78645408, 1.98342914, 0.31944446, -0.41656898],25 [-1.06190687, 0.34481172, -0.70359847, -0.27828666],26 [-2.01157677, 2.93965872, 0.32334723, -0.1659333],27 [-0.56669023, -0.06943413, 1.46053764, 0.01723844]28 ]29 random_centroids = {30 "centroid1": [0.1839742, -0.45809263, -1.91311585, -1.48341843],31 "centroid2": [-0.71767545, 1.2309971, -1.00348728, -0.38204247],32 }33 bad_centroids = {34 "centroid1": [0.1839742, -0.45809263, -1.91311585, -1.48341843],35 "centroid2": [10, 10, 10, 10],36 }37 return data, random_centroids, bad_centroids38# tests begin39def test_eucliean_distance():40 # int41 data1 = [0, 0, 0, 0]42 data2 = [1, 1, 1, 1]43 assert euclidean_distance_between_data(data1, data2) == 244 # random45 data1 = np.random.randn(100)46 data2 = np.random.randn(100)47 assert np.allclose(np.array(euclidean_distance_between_data(data1.tolist(),48 data2.tolist())),49 np.linalg.norm(data1 - data2).tolist())50 print("test_eucliean_distance passed.")51def test_assign_data():52 # set up53 data_empty = [0, 0, 0, 0]54 data_random = [1.1, 5.3, 55, -12.1]55 centroids = {"centroid1": [1, 1, 1, 1],56 "centroid2": [-10.1, 1, 23.2, 5.099]}57 assert assign_data_to_closest_centroid(data_empty, centroids) \58 == "centroid1"59 assert assign_data_to_closest_centroid(data_random, centroids) \60 == "centroid2"61 print("test_assign_data passed.")62def test_update_assignment():63 # set up64 data, random_centroids, bad_centroids = setup_data_centroids()65 # random66 rtn = update_assignment(data, random_centroids)67 answer = {68 "centroid1": [[-1.36639346, -0.38664658, -1.02232584, -1.05902604],69 [1.13659605, -2.47109085, -0.83996912, -0.24579457],70 [-0.83095884, -1.73002213, -0.01361636, -0.3265274]],71 "centroid2": [[-1.01714716, 0.95954521, 1.20493919, 0.34804443],72 [-1.48090019, -1.47491857, -0.6221167, 1.79055006],73 [-0.31237952, 0.73762417, 0.39042814, -1.1308523],74 [-0.78645408, 1.98342914, 0.31944446, -0.41656898],75 [-1.06190687, 0.34481172, -0.70359847, -0.27828666],76 [-2.01157677, 2.93965872, 0.32334723, -0.1659333],77 [-0.56669023, -0.06943413, 1.46053764, 0.01723844]]78 }79 assert_dict_eq(rtn, answer)80 # bad81 rtn = update_assignment(data, bad_centroids)82 answer = {83 "centroid1": [[-1.36639346, -0.38664658, -1.02232584, -1.05902604],84 [1.13659605, -2.47109085, -0.83996912, -0.24579457],85 [-0.83095884, -1.73002213, -0.01361636, -0.3265274],86 [-1.01714716, 0.95954521, 1.20493919, 0.34804443],87 [-1.48090019, -1.47491857, -0.6221167, 1.79055006],88 [-0.31237952, 0.73762417, 0.39042814, -1.1308523],89 [-0.78645408, 1.98342914, 0.31944446, -0.41656898],90 [-1.06190687, 0.34481172, -0.70359847, -0.27828666],91 [-2.01157677, 2.93965872, 0.32334723, -0.1659333],92 [-0.56669023, -0.06943413, 1.46053764, 0.01723844]]93 }94 assert_dict_eq(rtn, answer)95 print("test_update_assignment passed.")96def test_mean_of_points():97 # empty98 data = [99 [0, 0, 0, 0],100 [0, 0, 0, 0],101 ]102 assert mean_of_points(data) == [0, 0, 0, 0]103 # random104 data = np.random.randn(10, 4)105 assert np.allclose(np.array(mean_of_points(data.tolist())),106 data.mean(axis=0))107 print("test_mean_of_points passed.")108def test_update_centroids():109 # set up110 data, random_centroids, bad_centroids = setup_data_centroids()111 # random112 assignment_dict = update_assignment(data, random_centroids)113 answer = {114 'centroid2': [-1.03386497, 0.774388037, 0.33899735, 0.023455955],115 'centroid1': [-0.35358541, -1.529253186, -0.62530377, -0.543782673]116 }117 rtn = update_centroids(assignment_dict)118 assert_dict_eq(rtn, answer)119 # bad120 assignment_dict = update_assignment(data, bad_centroids)121 answer = {122 'centroid1': [-0.82978110, 0.08329567, 0.04970701, -0.146715632]123 }124 rtn = update_centroids(assignment_dict)125 assert_dict_eq(rtn, answer)126 print("test_update_centroids passed.")127if __name__ == '__main__':128 test_eucliean_distance()129 test_assign_data()130 test_update_assignment()131 test_mean_of_points()132 test_update_centroids()...

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 pyresttest 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