def test_python_builder_flask_distr_runnable(tmpdir, python_builder: PythonBuilder, pandas_data): args, env = _prepare_distribution(tmpdir, python_builder) from setup import setup_args _check_requirements( tmpdir, { *setup_args['install_requires'], 'flasgger==0.9.3', # server reqs 'pandas==0.25.1', 'scikit-learn==0.21.3', 'numpy==1.17.3' }) # model reqs # TODO make ModelLoader.load cwd-independent server = subprocess.Popen(args, env=env, cwd=tmpdir) with pytest.raises(subprocess.TimeoutExpired): # we hope that 5 seconds is enough to determine that server didn't crash server.wait(5) try: client = HTTPClient() predictions = client.predict(pandas_data) np.testing.assert_array_almost_equal(predictions, [1, 0]) finally: # our server runs for eternity, thus we should kill it to clean up # `server.kill` kills just shell script, Python subprocess still alive parent = psutil.Process(server.pid) for child in parent.children(recursive=True): child.kill() parent.kill()
def test_http_client__no_interface(): responses.add(responses.GET, 'http://localhost:9000/interface.json', json='', status=404) with pytest.raises(HTTPError): HTTPClient().go()
def main(): try: value_a, value_b = float(sys.argv[1]), float(sys.argv[2]) except (IndexError, ValueError): print(f'Usage: {sys.argv[0]} [float] [float]') return client = HTTPClient() df = pd.DataFrame({'a': [value_a], 'b': [value_b]}) pred = client.predict(df) # pred is numpy array of shape (1,) print(pred.item()) proba = client.predict_proba(df) # proba is numpy array of shape (1, 2) print(list(proba[0]))
def test_python_build_context_flask_distr_runnable(tmpdir, python_build_context, pandas_data, server_reqs, request): python_build_context: PythonBuildContext = request.getfixturevalue( python_build_context) args, env = _prepare_distribution(tmpdir, python_build_context) from setup import setup_args _check_requirements( tmpdir, { *setup_args['install_requires'], *server_reqs, 'pandas==1.0.3', 'scikit-learn==0.22.2', 'numpy==1.18.2' }) # model reqs check_ebonite_port_free() # TODO make ModelLoader.load cwd-independent server = subprocess.Popen(args, env=env, cwd=tmpdir) with pytest.raises(subprocess.TimeoutExpired): # we hope that 5 seconds is enough to determine that server didn't crash server.wait(5) try: client = HTTPClient() predictions = client.predict(pandas_data) np.testing.assert_array_almost_equal(predictions, [1, 0]) probas = client.predict_proba(pandas_data) np.testing.assert_array_almost_equal(np.argmax(probas, axis=1), [1, 0]) finally: # our server runs for eternity, thus we should kill it to clean up # `server.kill` kills just shell script, Python subprocess still alive parent = psutil.Process(server.pid) for child in parent.children(recursive=True): child.kill() parent.kill()
def test_http_client__wrong_arg_type(): _mock_interface_json() # pyjackson serializers raise unpredictable exceptions with pytest.raises(Exception): HTTPClient().predict(1)
def test_http_client__excessive_args(): _mock_interface_json() with pytest.raises(ValueError): HTTPClient().predict(1, 1)
def test_http_client__wrong_args(): _mock_interface_json() with pytest.raises(ValueError): HTTPClient().predict(k=1)
def test_http_client__unknown_method(): _mock_interface_json() with pytest.raises(KeyError): HTTPClient().go()
def test_http_client__kwarg_ok(data_frame, ndarray): _mock_interface_json() _mock_predict() assert np.array_equal(HTTPClient().predict(vector=data_frame), ndarray)