Best Python code snippet using autotest_python
test_regression.py
Source:test_regression.py
1import pytest2from datarobot_batch_scoring.batch_scoring import run_batch_predictions3from utils import PickableMock4from utils import read_logs5def test_regression(live_server, tmpdir, ui, keep_cols=None,6 in_fixture='tests/fixtures/regression_predict.csv',7 out_fixture='tests/fixtures/regression_output.csv',8 fast_mode=False, skip_row_id=False, output_delimiter=None,9 skip_dialect=False,10 n_samples=500,11 max_batch_size=None,12 expected_ret=None):13 # train one model in project14 out = tmpdir.join('out.csv')15 base_url = '{webhost}/predApi/v1.0/'.format(webhost=live_server.url())16 ret = run_batch_predictions(17 base_url=base_url,18 base_headers={},19 user='username',20 pwd='password',21 api_token=None,22 create_api_token=False,23 pid='56dd9570018e213242dfa93c',24 lid='56dd9570018e213242dfa93e',25 import_id=None,26 n_retry=3,27 concurrent=1,28 resume=False,29 n_samples=n_samples,30 out_file=str(out),31 keep_cols=keep_cols,32 delimiter=None,33 dataset=in_fixture,34 pred_name=None,35 timeout=None,36 ui=ui,37 auto_sample=False,38 fast_mode=fast_mode,39 dry_run=False,40 encoding='',41 skip_dialect=skip_dialect,42 skip_row_id=skip_row_id,43 output_delimiter=output_delimiter,44 max_batch_size=max_batch_size45 )46 assert ret is expected_ret47 if out_fixture:48 actual = out.read_text('utf-8')49 with open(out_fixture, 'rU') as f:50 expected = f.read()51 print(len(actual), len(expected))52 assert actual == expected53def test_regression_rename(live_server, tmpdir):54 # train one model in project55 out = tmpdir.join('out.csv')56 ui = PickableMock()57 base_url = '{webhost}/predApi/v1.0/'.format(webhost=live_server.url())58 ret = run_batch_predictions(59 base_url=base_url,60 base_headers={},61 user='username',62 pwd='password',63 api_token=None,64 create_api_token=False,65 pid='56dd9570018e213242dfa93c',66 lid='56dd9570018e213242dfa93e',67 import_id=None,68 n_retry=3,69 concurrent=1,70 resume=False,71 n_samples=10,72 out_file=str(out),73 keep_cols=None,74 delimiter=None,75 dataset='tests/fixtures/regression_predict.csv',76 pred_name='new_name',77 timeout=None,78 ui=ui,79 auto_sample=False,80 fast_mode=False,81 dry_run=False,82 encoding='',83 skip_dialect=False84 )85 assert ret is None86 actual = out.read_text('utf-8')87 with open('tests/fixtures/regression_output_rename.csv', 'rU') as f:88 assert actual == f.read()89def test_regression_rename_fast(live_server, tmpdir):90 # train one model in project91 out = tmpdir.join('out.csv')92 ui = PickableMock()93 base_url = '{webhost}/predApi/v1.0/'.format(webhost=live_server.url())94 ret = run_batch_predictions(95 base_url=base_url,96 base_headers={},97 user='username',98 pwd='password',99 api_token=None,100 create_api_token=False,101 pid='56dd9570018e213242dfa93c',102 lid='56dd9570018e213242dfa93e',103 import_id=None,104 n_retry=3,105 concurrent=1,106 resume=False,107 n_samples=10,108 out_file=str(out),109 keep_cols=None,110 delimiter=None,111 dataset='tests/fixtures/regression_predict.csv',112 pred_name='new_name',113 timeout=None,114 ui=ui,115 auto_sample=False,116 fast_mode=True,117 dry_run=False,118 encoding='',119 skip_dialect=False120 )121 assert ret is None122 actual = out.read_text('utf-8')123 with open('tests/fixtures/regression_output_rename.csv', 'rU') as f:124 assert actual == f.read()125def check_regression_jp(live_server, tmpdir, fast_mode, gzipped):126 """Use utf8 encoded input data.127 """128 if fast_mode:129 out_fname = 'out_fast.csv'130 else:131 out_fname = 'out.csv'132 out = tmpdir.join(out_fname)133 dataset_suffix = '.gz' if gzipped else ''134 ui = PickableMock()135 base_url = '{webhost}/predApi/v1.0/'.format(webhost=live_server.url())136 ret = run_batch_predictions(137 base_url=base_url,138 base_headers={},139 user='username',140 pwd='password',141 api_token=None,142 create_api_token=False,143 pid='56dd9570018e213242dfa93c',144 lid='56dd9570018e213242dfa93e',145 import_id=None,146 n_retry=3,147 concurrent=1,148 resume=False,149 n_samples=500,150 out_file=str(out),151 keep_cols=None,152 delimiter=None,153 dataset='tests/fixtures/regression_jp.csv' + dataset_suffix,154 pred_name='new_name',155 timeout=None,156 ui=ui,157 auto_sample=False,158 fast_mode=fast_mode,159 dry_run=False,160 encoding='',161 skip_dialect=False,162 compression=True163 )164 assert ret is None165 actual = out.read_text('utf-8')166 with open('tests/fixtures/regression_output_jp.csv', 'rU') as f:167 assert actual == f.read()168def test_fast_mode_regression_jp(live_server, tmpdir):169 check_regression_jp(live_server, tmpdir, True, False)170def test_wo_fast_mode_regression_jp(live_server, tmpdir):171 check_regression_jp(live_server, tmpdir, False, False)172def test_fast_mode_gzipped_regression_jp(live_server, tmpdir):173 check_regression_jp(live_server, tmpdir, True, True)174def test_wo_fast_mode_gzipped_regression_jp(live_server, tmpdir):175 check_regression_jp(live_server, tmpdir, False, True)176def test_regression_keep_cols(live_server, tmpdir, ui):177 test_regression(live_server, tmpdir, ui, keep_cols=['x'],178 in_fixture='tests/fixtures/regression.csv',179 out_fixture='tests/fixtures/regression_output_x.csv')180def test_regression_keep_cols_multi(live_server, tmpdir, ui):181 test_regression(live_server, tmpdir, ui, keep_cols=['y', 'x'],182 in_fixture='tests/fixtures/regression.csv',183 out_fixture='tests/fixtures/regression_output_yx.csv')184def test_regression_keep_cols_fast(live_server, tmpdir, ui):185 test_regression(live_server, tmpdir, ui, keep_cols=['x'],186 in_fixture='tests/fixtures/regression.csv',187 out_fixture='tests/fixtures/regression_output_x.csv',188 fast_mode=True)189def test_regression_keep_cols_multi_fast(live_server, tmpdir, ui):190 test_regression(live_server, tmpdir, ui, keep_cols=['y', 'x'],191 in_fixture='tests/fixtures/regression.csv',192 out_fixture='tests/fixtures/regression_output_yx.csv',193 fast_mode=True)194def test_regression_keep_cols_multi_fast_max_batch(live_server, tmpdir, ui):195 test_regression(live_server, tmpdir, ui, keep_cols=['y', 'x'],196 in_fixture='tests/fixtures/regression.csv',197 out_fixture='tests/fixtures/regression_output_yx.csv',198 fast_mode=True,199 max_batch_size=100)200 logs = read_logs()201 assert "bytes, splitting" in logs202def test_regression_bad_csv(live_server, tmpdir, ui):203 test_regression(live_server, tmpdir, ui,204 in_fixture='tests/fixtures/regression_bad.csv',205 out_fixture=None,206 fast_mode=False,207 expected_ret=1)208 logs = read_logs()209 assert "Error parsing CSV file after line 1000, error: " in logs210def test_regression_bad2_csv(live_server, tmpdir, monkeypatch, ui):211 def sys_exit(code):212 raise RuntimeError213 monkeypatch.setattr("sys.exit", sys_exit)214 with pytest.raises(RuntimeError):215 test_regression(live_server, tmpdir, ui,216 in_fixture='tests/fixtures/regression_bad2.csv',217 out_fixture=None,218 fast_mode=True,219 expected_ret=1)220def test_regression_keep_cols_wo_row_id(live_server, tmpdir, ui):221 test_regression(live_server, tmpdir, ui, keep_cols=['x'],222 in_fixture='tests/fixtures/regression.csv',223 out_fixture='tests/fixtures/regression_output_x_rid.csv',224 skip_row_id=True)225def test_regression_keep_cols_multi_wo_row_id(live_server, tmpdir, ui):226 test_regression(live_server, tmpdir, ui, keep_cols=['y', 'x'],227 in_fixture='tests/fixtures/regression.csv',228 out_fixture='tests/fixtures/regression_output_yx_rid.csv',229 skip_row_id=True)230def test_regression_keep_cols_fast_wo_row_id(live_server, tmpdir, ui):231 test_regression(live_server, tmpdir, ui, keep_cols=['x'],232 in_fixture='tests/fixtures/regression.csv',233 out_fixture='tests/fixtures/regression_output_x_rid.csv',234 fast_mode=True,235 skip_row_id=True)236def test_regression_keep_cols_multi_fast_wo_row_id(live_server, tmpdir, ui):237 test_regression(live_server, tmpdir, ui, keep_cols=['y', 'x'],238 in_fixture='tests/fixtures/regression.csv',239 out_fixture='tests/fixtures/regression_output_yx_rid.csv',240 fast_mode=True,241 skip_row_id=True)242def test_regression_fast_wo_row_id(live_server, tmpdir, ui):243 test_regression(live_server, tmpdir, ui,244 in_fixture='tests/fixtures/regression.csv',245 out_fixture='tests/fixtures/regression_output_rid.csv',246 fast_mode=True,247 skip_row_id=True)248def test_regression_wo_row_id(live_server, tmpdir, ui):249 test_regression(live_server, tmpdir, ui,250 in_fixture='tests/fixtures/regression.csv',251 out_fixture='tests/fixtures/regression_output_rid.csv',252 fast_mode=False,253 skip_row_id=True)254def test_regression_keep_cols_multi_output(live_server, tmpdir, ui):255 test_regression(256 live_server, tmpdir, ui, keep_cols=['y', 'x'],257 in_fixture='tests/fixtures/regression.csv',258 out_fixture='tests/fixtures/regression_output_yx_output.csv',259 output_delimiter='|')260def test_regression_keep_cols_multi_output_skip_dialect(live_server,261 tmpdir, ui):262 test_regression(263 live_server, tmpdir, ui, keep_cols=['y', 'x'],264 skip_dialect=True,265 in_fixture='tests/fixtures/regression.csv',266 out_fixture='tests/fixtures/regression_output_yx_output.csv',267 output_delimiter='|')268def test_regression_keep_cols_multi_skip_dialect(live_server, tmpdir, ui):269 test_regression(270 live_server, tmpdir, ui, keep_cols=['y', 'x'],271 skip_dialect=True,272 in_fixture='tests/fixtures/regression.csv',...
test_serialization.py
Source:test_serialization.py
1import pytest2import capnp3import os4import platform5import test_regression6import tempfile7import pickle8import mmap9import sys10this_dir = os.path.dirname(__file__)11@pytest.fixture12def all_types():13 return capnp.load(os.path.join(this_dir, 'all_types.capnp'))14def test_roundtrip_file(all_types):15 f = tempfile.TemporaryFile()16 msg = all_types.TestAllTypes.new_message()17 test_regression.init_all_types(msg)18 msg.write(f)19 f.seek(0)20 msg = all_types.TestAllTypes.read(f)21 test_regression.check_all_types(msg)22def test_roundtrip_file_packed(all_types):23 f = tempfile.TemporaryFile()24 msg = all_types.TestAllTypes.new_message()25 test_regression.init_all_types(msg)26 msg.write_packed(f)27 f.seek(0)28 msg = all_types.TestAllTypes.read_packed(f)29 test_regression.check_all_types(msg)30def test_roundtrip_bytes(all_types):31 msg = all_types.TestAllTypes.new_message()32 test_regression.init_all_types(msg)33 message_bytes = msg.to_bytes()34 msg = all_types.TestAllTypes.from_bytes(message_bytes)35 test_regression.check_all_types(msg)36@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="TODO: Investigate why this works on CPython but fails on PyPy.")37def test_roundtrip_segments(all_types):38 msg = all_types.TestAllTypes.new_message()39 test_regression.init_all_types(msg)40 segments = msg.to_segments()41 msg = all_types.TestAllTypes.from_segments(segments)42 test_regression.check_all_types(msg)43@pytest.mark.skipif(sys.version_info[0] < 3, reason="mmap doesn't implement the buffer interface under python 2.")44def test_roundtrip_bytes_mmap(all_types):45 msg = all_types.TestAllTypes.new_message()46 test_regression.init_all_types(msg)47 with tempfile.TemporaryFile() as f:48 msg.write(f)49 length = f.tell()50 f.seek(0)51 memory = mmap.mmap(f.fileno(), length)52 msg = all_types.TestAllTypes.from_bytes(memory)53 test_regression.check_all_types(msg)54@pytest.mark.skipif(sys.version_info[0] < 3, reason="memoryview is a builtin on Python 3")55def test_roundtrip_bytes_buffer(all_types):56 msg = all_types.TestAllTypes.new_message()57 test_regression.init_all_types(msg)58 b = msg.to_bytes()59 v = memoryview(b)60 msg = all_types.TestAllTypes.from_bytes(v)61 test_regression.check_all_types(msg)62def test_roundtrip_bytes_fail(all_types):63 with pytest.raises(TypeError):64 all_types.TestAllTypes.from_bytes(42)65@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="This works in PyPy 4.0.1 but travisci's version of PyPy has some bug that fails this test.")66def test_roundtrip_bytes_packed(all_types):67 msg = all_types.TestAllTypes.new_message()68 test_regression.init_all_types(msg)69 message_bytes = msg.to_bytes_packed()70 msg = all_types.TestAllTypes.from_bytes_packed(message_bytes)71 test_regression.check_all_types(msg)72def test_roundtrip_file_multiple(all_types):73 f = tempfile.TemporaryFile()74 msg = all_types.TestAllTypes.new_message()75 test_regression.init_all_types(msg)76 msg.write(f)77 msg.write(f)78 msg.write(f)79 f.seek(0)80 i = 081 for msg in all_types.TestAllTypes.read_multiple(f):82 test_regression.check_all_types(msg)83 i += 184 assert i == 385def test_roundtrip_bytes_multiple(all_types):86 msg = all_types.TestAllTypes.new_message()87 test_regression.init_all_types(msg)88 msgs = msg.to_bytes()89 msgs += msg.to_bytes()90 msgs += msg.to_bytes()91 i = 092 for msg in all_types.TestAllTypes.read_multiple_bytes(msgs):93 test_regression.check_all_types(msg)94 i += 195 assert i == 396def test_roundtrip_file_multiple_packed(all_types):97 f = tempfile.TemporaryFile()98 msg = all_types.TestAllTypes.new_message()99 test_regression.init_all_types(msg)100 msg.write_packed(f)101 msg.write_packed(f)102 msg.write_packed(f)103 f.seek(0)104 i = 0105 for msg in all_types.TestAllTypes.read_multiple_packed(f):106 test_regression.check_all_types(msg)107 i += 1108 assert i == 3109def test_roundtrip_bytes_multiple_packed(all_types):110 msg = all_types.TestAllTypes.new_message()111 test_regression.init_all_types(msg)112 msgs = msg.to_bytes_packed()113 msgs += msg.to_bytes_packed()114 msgs += msg.to_bytes_packed()115 i = 0116 for msg in all_types.TestAllTypes.read_multiple_bytes_packed(msgs):117 test_regression.check_all_types(msg)118 i += 1119 assert i == 3120@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason="This works on my local PyPy v2.5.0, but is for some reason broken on TravisCI. Skip for now.")121def test_roundtrip_dict(all_types):122 msg = all_types.TestAllTypes.new_message()123 test_regression.init_all_types(msg)124 d = msg.to_dict()125 msg = all_types.TestAllTypes.from_dict(d)126 test_regression.check_all_types(msg)127def test_file_and_bytes(all_types):128 f = tempfile.TemporaryFile()129 msg = all_types.TestAllTypes.new_message()130 test_regression.init_all_types(msg)131 msg.write(f)132 f.seek(0)133 assert f.read() == msg.to_bytes()134def test_file_and_bytes_packed(all_types):135 f = tempfile.TemporaryFile()136 msg = all_types.TestAllTypes.new_message()137 test_regression.init_all_types(msg)138 msg.write_packed(f)139 f.seek(0)140 assert f.read() == msg.to_bytes_packed()141def test_pickle(all_types):142 msg = all_types.TestAllTypes.new_message()143 test_regression.init_all_types(msg)144 data = pickle.dumps(msg)145 msg2 = pickle.loads(data)146 test_regression.check_all_types(msg2)147def test_from_bytes_traversal_limit(all_types):148 size = 1024149 bld = all_types.TestAllTypes.new_message()150 bld.init("structList", size)151 data = bld.to_bytes()152 msg = all_types.TestAllTypes.from_bytes(data)153 with pytest.raises(capnp.KjException):154 for i in range(0, size):155 msg.structList[i].uInt8Field == 0156 msg = all_types.TestAllTypes.from_bytes(data,157 traversal_limit_in_words=2**62)158 for i in range(0, size):159 assert msg.structList[i].uInt8Field == 0160def test_from_bytes_packed_traversal_limit(all_types):161 size = 1024162 bld = all_types.TestAllTypes.new_message()163 bld.init("structList", size)164 data = bld.to_bytes_packed()165 msg = all_types.TestAllTypes.from_bytes_packed(data)166 with pytest.raises(capnp.KjException):167 for i in range(0, size):168 msg.structList[i].uInt8Field == 0169 msg = all_types.TestAllTypes.from_bytes_packed(data,170 traversal_limit_in_words=2**62)171 for i in range(0, size):...
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!!