Best Python code snippet using pyresttest_python
test_data.py
Source:test_data.py
1import numpy as np2from gradientascent.ascender import Ascender, Coordinate3from pytest import param4from test_helpers import create_ascender5def test_get_bordering_coordinates_success():6 test_variables = "array, current_coordinate, expected_result"7 test_data = [8 param(9 np.zeros((3, 3)),10 Coordinate(0, 0),11 [Coordinate(1, 0), Coordinate(0, 1)],12 id='top_left'13 ),14 param(15 np.zeros((3, 3)),16 Coordinate(1, 1),17 [Coordinate(0, 1), Coordinate(2, 1), Coordinate(1, 2), Coordinate(1, 0)],18 id='centre'19 ),20 param(21 np.zeros((3, 3)),22 Coordinate(2, 2),23 [Coordinate(1, 2), Coordinate(2, 1)],24 id='bottom_right'25 ),26 param(27 np.zeros((3, 3)),28 Coordinate(1, 0),29 [Coordinate(0, 0), Coordinate(2, 0), Coordinate(1, 1)],30 id='central_top'31 )32 ]33 return test_variables, test_data34def test_calculate_steepest_ascent_coordinate_success():35 test_variables = "array, current_coordinate, expected_result"36 test_data = [37 param(38 np.array([[1, 2, 3],39 [4, 5, 6],40 [6, 8, 9]]),41 Coordinate(1, 1),42 Coordinate(1, 2),43 id='move_south'44 ),45 param(46 np.array([[1, 2, 3],47 [4, 9, 6],48 [6, 8, 5]]),49 Coordinate(2, 2),50 Coordinate(1, 2),51 id='move_west'52 ),53 param(54 np.array([[1, 2, 3],55 [4, 5, 6],56 [6, 8, 9]]),57 Coordinate(2, 2),58 Coordinate(2, 2),59 id='stay_stationary'60 ),61 param(62 np.array([[0, 0, 0],63 [1, 2, 3],64 [0, 0, 0]]),65 Coordinate(1, 1),66 Coordinate(2, 1),67 id='move_west_from_centre'68 )69 ]70 return test_variables, test_data71def test_generate_ascent_route_success():72 test_variables = "array, start_coordinate, expected_result"73 test_data = [74 param(75 np.array([[0, 1, 0],76 [0, 0, 0],77 [0, 0, 0]]),78 Coordinate(0, 0),79 [Coordinate(0, 0), Coordinate(1, 0)],80 id='single_step'81 ),82 param(83 np.array([[1, 2, 3],84 [0, 0, 4],85 [0, 0, 5]]),86 Coordinate(0, 0),87 [Coordinate(0, 0), Coordinate(1, 0), Coordinate(2, 0),88 Coordinate(2, 1), Coordinate(2, 2)],89 id='north_east_pass'90 )91 # param(92 # np.array([[1, 2, 3],93 # [2, 2, 4],94 # [1, 1, 5]]),95 # Coordinate(0, 0),96 # [Coordinate(0, 0), Coordinate(1, 0), Coordinate(2, 0),97 # Coordinate(2, 1), Coordinate(2, 2)],98 # id='north_east_pass'99 # ),100 ]101 return test_variables, test_data102def test_create_summit_heatmap_success():103 test_variables = "ascender, expected_result"104 test_data = [105 param(106 create_ascender(np.array([[1, 2, 1],107 [2, 3, 4],108 [1, 2, 5]])),109 np.array([[1, 1, 1],110 [1, 1, 1],111 [1, 1, 1]]),112 id='all_reach_top_3x3'113 )114 ]115 return test_variables, test_data116def test_is_visited_success():117 test_variables = "ascender, summit_heatmap, coordinate, expected_result"118 test_data = [119 param(120 create_ascender(np.array([[1, 2, 3],121 [2, 2, 4],122 [1, 1, 5]])),123 np.array([[-1, -1, -1],124 [-1, -1, -1],125 [0, -1, -1]]),126 Coordinate(0, 2),127 True,128 id='bottom_left_visited'129 ),130 param(131 create_ascender(np.array([[1, 2, 3],132 [2, 2, 4],133 [1, 1, 5]])),134 np.array([[-1, -1, -1],135 [-1, -1, -1],136 [0, -1, -1]]),137 Coordinate(2, 2),138 False,139 id='bottom_right_unvisited'140 )141 ]142 return test_variables, test_data143def test_get_summit_coord_success():144 test_variables = "ascender, expected_result"145 test_data = [146 param(147 create_ascender(np.array([[1, 2, 3],148 [2, 2, 4],149 [8, 1, 5]])),150 Coordinate(0, 2),151 id='summit_bottom_left'152 )153 ]...
upstream_pipeline_test.py
Source:upstream_pipeline_test.py
1import os2import tempfile3from oais_utils.validate import validate_sip4from .. import main5"""6All these tests cover different supported pipelines.7Based on the parameters given at the test_variables,8each function calls the pipeline_results() which runs bagit-create and9validates the result using the oais_utils.validate method and returns True or False.10"""11def test_codimd_pipeline():12 test_variables = {13 "source": "codimd",14 # This note should be set as EDITABLE and be PUBLISHED or it15 # won't be accessible without a token16 "recid": "tBD632vFt",17 "dry_run": False,18 }19 valid = pipeline_results(20 test_variables["source"],21 test_variables["recid"],22 test_variables["dry_run"],23 )24 assert valid is True25def test_indico_pipeline():26 # So we can keep INDICO_KEY uncommitted and read it from an environment variable set for the ci/cd job27 token = os.environ["INDICO_KEY"]28 test_variables = {29 "source": "indico",30 "recid": 1024767,31 "dry_run": False,32 "token": token,33 }34 valid = pipeline_results(35 test_variables["source"],36 test_variables["recid"],37 test_variables["dry_run"],38 test_variables["token"],39 )40 assert valid is True41def test_cds_pipeline():42 test_variables = {"source": "cds", "recid": 2728246, "dry_run": True}43 valid = pipeline_results(44 test_variables["source"], test_variables["recid"], test_variables["dry_run"]45 )46 assert valid is True47"""48def test_cds_pipeline():49 test_variables = {"source": "ilcdoc", "recid": 62959, "dry_run": True}50 valid = pipeline_results(51 test_variables["source"], test_variables["recid"], test_variables["dry_run"]52 )53 assert valid == True54"""55def test_zenodo_pipeline():56 test_variables = {"source": "zenodo", "recid": 3911261, "dry_run": True}57 valid = pipeline_results(58 test_variables["source"], test_variables["recid"], test_variables["dry_run"]59 )60 assert valid is True61def test_cod_pipeline():62 test_variables = {"source": "cod", "recid": 10101, "dry_run": True}63 valid = pipeline_results(64 test_variables["source"], test_variables["recid"], test_variables["dry_run"]65 )66 assert valid is True67"""68def test_inveniordm_pipeline():69 test_variables = {"source": "inveniordm", "recid": "v3vqp-bfg07", "dry_run": True}70 valid = pipeline_results(71 test_variables["source"], test_variables["recid"], test_variables["dry_run"]72 )73 assert valid == True74"""75def pipeline_results(source, recid, dry_run, token=None):76 # Prepare a temporary folder to save the results77 with tempfile.TemporaryDirectory() as tmpdir1:78 # Run Bagit Create with the following parameters:79 # Save the results to tmpdir180 main.process(81 recid=recid,82 source=source,83 loglevel=0,84 target=tmpdir1,85 dry_run=dry_run,86 token=token,87 )88 # Check inside the tmpdir1 for any folders. If it finds one, this will be the folder created by Bagit Create.89 target_sip_list = os.listdir(tmpdir1)90 # Run validate_sip on the folder that was found91 valid_structure = validate_sip(os.path.join(tmpdir1, target_sip_list[0]))...
test2.py
Source:test2.py
1import json2import timeit3from threadpoolctl import threadpool_limits4import numpy as np5import cupy as cp6import os7numpydot = True8discrete = True9threads = 810use_cuda = False11#os.environ["MKL_DYNAMIC"] = "FALSE"12#os.environ["OMP_DYNAMIC"] = "FALSE"13#os.environ["MKL_NUM_THREADS"] = str(threads)14#os.environ["OMP_NUM_THREADS"] = str(threads)15#os.environ["MKL_DOMAIN_NUM_THREADS"] = "MKL_BLAS=4"16f = open('./test_variables.json', "r")17test_variables = json.loads(f.read())18if use_cuda:19 A = cp.array(test_variables['A_d'])20 B = cp.array(test_variables['B_d'])21 C = cp.array(test_variables['C'])22 D = cp.array(test_variables['D'])23 u_fix = cp.array(test_variables['u_fix'])24 Ad = cp.array(test_variables['A_d_foh'])25 Bd1 = cp.array(test_variables['B1_d_foh'])26 Bd0 = cp.array(test_variables['B0_d_foh'])27else:28 A = np.array(test_variables['A_d'])29 B = np.array(test_variables['B_d'])30 C = np.array(test_variables['C'])31 D = np.array(test_variables['D'])32 u_fix = np.array(test_variables['u_fix'])33 Ad = np.array(test_variables['A_d_foh'])34 Bd1 = np.array(test_variables['B1_d_foh'])35 Bd0 = np.array(test_variables['B0_d_foh'])36n_states = A.shape[0]37n_inputs = B.shape[1]38n_outputs = C.shape[0]39n_steps = len(u_fix[0, :])40if use_cuda:41 xout = cp.zeros((n_states, n_steps))42 yout = cp.zeros((n_outputs, n_steps))43else:44 xout = np.zeros((n_states, n_steps))45 yout = np.zeros((n_outputs, n_steps))46limit = 1047def sim():48 for n in range(0, limit):49 # print(str(100*n/limit) + '%')50 if discrete:51 if use_cuda:52 for i in range(0, n_steps - 1):53 global yout54 xout[:, i + 1] = cp.dot(A, xout[:, i]) + cp.dot(B, u_fix[:, i])55 yout[:, i] = cp.dot(C, xout[:, i]) + cp.dot(D, u_fix[:, i])56 elif numpydot:57 for i in range(0, n_steps - 1):58 xout[:, i + 1] = np.dot(A, xout[:, i]) + np.dot(B, u_fix[:, i])59 yout[:, i] = np.dot(C, xout[:, i]) + np.dot(D, u_fix[:, i])60 else:61 for i in range(0, len(u_fix[0, :]) - 1):62 xout[:, i + 1] = A @ xout[:, i] + B @ u_fix[:, i]63 yout[:, i] = C @ xout[:, i] + D @ u_fix[:, i]64 else:65 if use_cuda:66 for i in range(1, n_steps):67 xout[:, i] = cp.dot(Ad, xout[:, i - 1]) + cp.dot(Bd0, u_fix[:, i - 1]) + cp.dot(Bd1, u_fix[:, i])68 yout = cp.dot(C, xout) + cp.dot(D, u_fix)69 elif numpydot:70 for i in range(1, n_steps):71 xout[:, i] = np.dot(Ad, xout[:, i - 1]) + np.dot(Bd0, u_fix[:, i - 1]) + np.dot(Bd1, u_fix[:, i])72 yout = np.dot(C, xout) + np.dot(D, u_fix)73 else:74 for i in range(1, n_steps):75 xout[:, i] = (Ad @ xout[:, i - 1] + Bd0 @ u_fix[:, i - 1] + Bd1 @ u_fix[:, i])76 yout = C @ xout + D @ u_fix77 if use_cuda:78 cp.cuda.Device().synchronize()79with threadpool_limits(limits=threads):80 res = timeit.timeit(lambda: sim(), number=5)81 #sim()...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!