def test_without_logging_configuration_file(responses: RequestsMock, tmpdir): """ This test case assert that pyxelrest can be loaded without logging configuration """ responses.add( responses.GET, url="http://localhost:8943/", json={ "swagger": "2.0", "paths": { "/date": { "get": { "operationId": "get_date", "responses": { "200": { "description": "return value", "schema": { "type": "array", "items": { "type": "string", "format": "date" }, }, } }, } } }, }, match_querystring=True, ) this_dir = os.path.abspath(os.path.dirname(__file__)) pyxelrest.LOGGING_CONFIGURATION_FILE_PATH = os.path.join( this_dir, "non_existing_configuration.yml") loader.load( tmpdir, { "usual_parameters": { "open_api": { "definition": "http://localhost:8943/" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, )
def test_post_form_parameter(responses: RequestsMock, form_parameter_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "form_parameter": { "open_api": { "definition": "http://localhost:8952/" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) responses.add( responses.POST, url="http://localhost:8952/form", json={}, match_querystring=True, ) assert pyxelrestgenerator.form_parameter_post_form( "sent string form data") == [[""]] assert (_get_request(responses, "http://localhost:8952/form").body == b'{"form_string": "sent string form data"}')
def test_delete_without_parameter(responses: RequestsMock, without_parameter_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "without_parameter": { "open_api": { "definition": "http://localhost:8950/" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) responses.add( responses.DELETE, url="http://localhost:8950/without_parameter", json={}, match_querystring=True, ) assert pyxelrestgenerator.without_parameter_delete_without_parameter() == [ [""] ]
def test_get_header_parameter(responses: RequestsMock, header_parameter_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "header_parameter": { "open_api": { "definition": "http://localhost:8951/" }, "udf": { "return_types": ["vba_compatible", "sync_auto_expand"], "shift_result": False, }, } }, ) responses.add( responses.GET, url="http://localhost:8951/header", json={}, match_querystring=True, ) assert pyxelrestgenerator.header_parameter_get_header("sent header") == [[ "" ]] assert (_get_request( responses, "http://localhost:8951/header").headers["header_string"] == "sent header")
def test_string_pipes_array_parameter(responses: RequestsMock, array_parameter_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "array_parameter": { "open_api": { "definition": "http://localhost:8953/" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) responses.add( responses.GET, url= "http://localhost:8953/string_pipes_array_parameter?string_array=str1|str2", json=[], match_querystring=True, ) assert pyxelrestgenerator.array_parameter_get_string_pipes_array_parameter( ["str1", "str2"]) == [[""]]
def test_service_only_sync_does_not_have_vba_prefix(header_parameter_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "header_advanced_configuration": { "open_api": { "definition": "http://localhost:8951/" }, "udf": { "return_types": ["vba_compatible"], "shift_result": False }, "headers": { "X-PXL-CUSTOM": "MyCustomValue", "X-PXL-OTHER": "MyOtherValue", "X-PXL-ENVVAR": "%USERNAME%", }, } }, ) with pytest.raises(AttributeError) as exception_info: pyxelrestgenerator.vba_header_advanced_configuration_get_header( "sent header") assert ( str(exception_info.value) == "module 'pyxelrest.pyxelrestgenerator' has no attribute 'vba_header_advanced_configuration_get_header'" )
def test_swagger_version_is_mandatory(responses: RequestsMock, tmpdir): responses.add( responses.GET, url="http://localhost:8948/swagger_version_not_provided", json={ "paths": { "/should_not_be_available": { "get": { "operationId": "get_should_not_be_available", "responses": {200: {"description": "successful operation"}}, } } } }, match_querystring=True, ) pyxelrestgenerator = loader.load( tmpdir, { "not_provided": { "open_api": { "definition": "http://localhost:8948/swagger_version_not_provided" }, "udf": {"return_types": ["sync_auto_expand"], "shift_result": False}, } }, ) assert not hasattr(pyxelrestgenerator, "not_provided_get_should_not_be_available")
def test_delete_custom_url_sync(responses: RequestsMock, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "pyxelrest": { "udf": { "return_types": ["vba_compatible", "sync_auto_expand"], "shift_result": False, } } }, ) responses.add( responses.DELETE, url="http://localhost:8958/unlisted", json={}, match_querystring=True, ) assert pyxelrestgenerator.vba_pyxelrest_delete_url( "http://localhost:8958/unlisted", extra_headers=[ ["X-Custom-Header1", "custom1"], ["X-Custom-Header2", "custom2"], ], ) == [[""]] headers = _get_request(responses, "http://localhost:8958/unlisted").headers assert headers["X-Custom-Header1"] == "custom1" assert headers["X-Custom-Header2"] == "custom2"
def test_get_async_url(responses: RequestsMock, async_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "async": { "open_api": { "definition": "http://localhost:8958/" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) responses.add( responses.GET, url="http://localhost:8958/async", json={}, headers={"location": "http://localhost:8958/async/status"}, status=202, match_querystring=True, ) assert pyxelrestgenerator.async_get_async() == [ ["Status URL"], ["http://localhost:8958/async/status"], ]
def test_get_http_method(responses: RequestsMock, http_methods_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "http_methods": { "open_api": { "definition": "http://localhost:8955/" }, "methods": ["get", "post", "put", "delete", "patch", "options", "head"], "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) responses.add( responses.GET, url="http://localhost:8955/http_methods", json={}, match_querystring=True, ) assert pyxelrestgenerator.http_methods_get_http_methods() == [[""]]
def test_put_custom_url_dict_sync(responses: RequestsMock, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "pyxelrest": { "udf": { "return_types": ["vba_compatible", "sync_auto_expand"], "shift_result": False, } } }, ) responses.add(responses.PUT, url="http://localhost:8958/dict", json={}, match_querystring=True) assert pyxelrestgenerator.vba_pyxelrest_put_url( "http://localhost:8958/dict", [["key1", "key2", "key3"], ["value1", 1, "value3"]], extra_headers=[["Content-Type", "application/json"]], parse_body_as="dict", ) == [[""]] request = _get_request(responses, "http://localhost:8958/dict") assert request.headers["Content-Type"] == "application/json" assert request.body == b'{"key1": "value1", "key2": 1, "key3": "value3"}'
def test_get_compare_output_order( responses: RequestsMock, output_order_service, tmpdir ): pyxelrestgenerator = loader.load( tmpdir, { "output_order": { "open_api": {"definition": "http://localhost:8946/"}, "udf": {"return_types": ["sync_auto_expand"], "shift_result": False}, } }, ) responses.add( responses.GET, url="http://localhost:8946/price/unordered", json=[ {"ts": None, "curve": "PW_FR", "date": "2017-04-05", "mat": "H01"}, { "ts": "2017-04-05 12:03:15", "curve": "PW_FR", "date": "2017-04-05", "mat": "H02", }, {"ts": None, "curve": "PW_FR", "date": "2017-04-05", "mat": "H03"}, ], match_querystring=True, ) assert pyxelrestgenerator.output_order_get_price_unordered() == [ ["ts", "curve", "date", "mat"], ["", "PW_FR", datetime.datetime(2017, 4, 5, 0, 0), "H01"], ["2017-04-05 12:03:15", "PW_FR", datetime.datetime(2017, 4, 5, 0, 0), "H02"], ["", "PW_FR", datetime.datetime(2017, 4, 5, 0, 0), "H03"], ]
def test_delete_with_excluded_operation_ids(responses: RequestsMock, filtered_tags_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "excluded_operation_ids": { "open_api": { "definition": "http://localhost:8944/", "excluded_operation_ids": ["get_tags", "post_tags", "put_tags"], }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) responses.add( responses.DELETE, url="http://localhost:8944/tags", json={}, match_querystring=True, ) assert pyxelrestgenerator.excluded_operation_ids_delete_tags() == [[""]]
def test_post_with_selected_tags(responses: RequestsMock, filtered_tags_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "selected_tags": { "open_api": { "definition": "http://localhost:8944/", "selected_tags": ["tag 1", "tag 2"], }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) responses.add( responses.POST, url="http://localhost:8944/tags", json={}, match_querystring=True, ) # All tags are accepted assert pyxelrestgenerator.selected_tags_post_tags() == [[""]]
def test_get_with_empty_dictionary(responses: RequestsMock, values_false_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "values_false": { "open_api": { "definition": "http://localhost:8945/" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) responses.add( responses.GET, url="http://localhost:8945/with/empty/dictionary", json=[{ "empty_dictionary": {} }], match_querystring=True, ) assert pyxelrestgenerator.values_false_get_with_empty_dictionary() == [ ["empty_dictionary"], [""], ]
def test_put_cached(caching_service, responses: RequestsMock, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "caching": { "open_api": { "definition": "http://localhost:8949/" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, "caching": { "result_caching_time": 5 }, } }, ) responses.add( responses.PUT, url="http://localhost:8949/cached?test1=1&test2=2", json={}, match_querystring=True, ) assert pyxelrestgenerator.caching_put_cached(test1="1", test2="2") == [[""]] assert _get_request(responses, "http://localhost:8949/cached?test1=1&test2=2") responses.add( responses.PUT, url="http://localhost:8949/cached?test1=1&test2=3", json={}, match_querystring=True, ) assert pyxelrestgenerator.caching_put_cached(test1="1", test2="3") == [[""]] assert _get_request(responses, "http://localhost:8949/cached?test1=1&test2=3") assert pyxelrestgenerator.caching_put_cached(test1="1", test2="2") == [[""]] assert _get_request(responses, "http://localhost:8949/cached?test1=1&test2=2") time.sleep(5) assert pyxelrestgenerator.caching_put_cached(test1="1", test2="2") == [[""]] assert _get_request(responses, "http://localhost:8949/cached?test1=1&test2=2") assert pyxelrestgenerator.caching_put_cached(test1="1", test2="2") == [[""]] assert _get_request(responses, "http://localhost:8949/cached?test1=1&test2=2")
def test_get_static_open_api_definition(responses: RequestsMock, tmpdir): open_api_definition_file_path = os.path.join( tmpdir, "static_open_api_definition.json" ) with open(open_api_definition_file_path, "wt") as open_api_definition_file: open_api_definition_file.write( """{ "swagger": "2.0", "schemes": [ "http" ], "host": "localhost:8954", "basePath": "/sub", "paths": { "/static/file/call": { "get": { "operationId": "get_static_file_call", "responses": { "200": { "description": "" } } } } } } """ ) pyxelrestgenerator = loader.load( tmpdir, { "open_api_definition_loaded_from_file": { "open_api": {"definition": f"file:///{open_api_definition_file_path}"}, "udf": {"return_types": ["sync_auto_expand"], "shift_result": False}, } }, ) responses.add( responses.GET, url="http://localhost:8954/sub/static/file/call", json={}, match_querystring=True, ) assert pyxelrestgenerator.open_api_definition_loaded_from_file_get_static_file_call() == [ [""] ]
def test_get_plain_text_with_service_down(without_parameter_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "without_parameter": { "open_api": {"definition": "http://localhost:8950/"}, "udf": {"return_types": ["sync_auto_expand"], "shift_result": False}, } }, ) assert ( pyxelrestgenerator.without_parameter_get_plain_text_without_parameter() == "Cannot connect to service. Please retry once connection is re-established." )
def test_get_with_empty_string(responses: RequestsMock, values_false_service, tmpdir, service_config): pyxelrestgenerator = loader.load(tmpdir, service_config) responses.add( responses.GET, url="http://localhost:8945/with/empty/string", json=[{ "empty_string": "" }], match_querystring=True, ) assert pyxelrestgenerator.values_false_get_with_empty_string() == [ ["empty_string"], [""], ]
def test_get_with_false_boolean(responses: RequestsMock, values_false_service, tmpdir, service_config): pyxelrestgenerator = loader.load(tmpdir, service_config) responses.add( responses.GET, url="http://localhost:8945/with/false/boolean", json=[{ "false_boolean": False }], match_querystring=True, ) assert pyxelrestgenerator.values_false_get_with_false_boolean() == [ ["false_boolean"], [False], ]
def test_get_with_zero_float(responses: RequestsMock, values_false_service, tmpdir, service_config): pyxelrestgenerator = loader.load(tmpdir, service_config) responses.add( responses.GET, url="http://localhost:8945/with/zero/float", json=[{ "zero_float": 0.0 }], match_querystring=True, ) assert pyxelrestgenerator.values_false_get_with_zero_float() == [ ["zero_float"], [0.0], ]
def test_mixed_operation_id(responses: RequestsMock, operation_id_services, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "operation_id_not_provided": { "open_api": { "definition": "http://localhost:8948/operation_id_not_provided" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, }, "operation_id_not_always_provided": { "open_api": { "definition": "http://localhost:8948/operation_id_not_always_provided" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, }, }, ) responses.add( responses.GET, url="http://localhost:8948/without_operationId", json=["first"], match_querystring=True, ) assert pyxelrestgenerator.operation_id_not_always_provided_get_without_operationId( ) == [["first"]] responses.add( responses.GET, url="http://localhost:8948/with_operationId", json=["second"], match_querystring=True, ) assert pyxelrestgenerator.operation_id_not_always_provided_duplicated_get_without_operationId( ) == [["second"]]
def test_put_with_excluded_tags(filtered_tags_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "excluded_tags": { "open_api": { "definition": "http://localhost:8944/", "excluded_tags": ["tag 1", "tag 2"], }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) assert not hasattr(pyxelrestgenerator, "excluded_tags_put_tags")
def test_get_header_advanced_configuration(responses: RequestsMock, header_parameter_service, monkeypatch, tmpdir): import pyxelrest.session pyxelrest.session.sessions.clear() monkeypatch.setattr(pyxelrest.session, "datetime", DateTimeModuleMock) pyxelrestgenerator = loader.load( tmpdir, { "header_advanced_configuration": { "open_api": { "definition": "http://localhost:8951/" }, "udf": { "return_types": ["vba_compatible"], "shift_result": False }, "headers": { "X-PXL-CUSTOM": "MyCustomValue", "X-PXL-OTHER": "MyOtherValue", "X-PXL-ENVVAR": "%USERNAME%", }, } }, ) responses.add( responses.GET, url="http://localhost:8951/header", json={}, match_querystring=True, ) assert pyxelrestgenerator.header_advanced_configuration_get_header( "sent header") == [[""]] headers = _get_request(responses, "http://localhost:8951/header").headers assert headers["X-Pxl-Custom"] == "MyCustomValue" assert headers["X-Pxl-Other"] == "MyOtherValue" assert headers["X-Pxl-Envvar"] == os.environ["USERNAME"] assert headers["X-Pxl-Request"] assert headers["User-Agent"] == "PyxelRest v1.0.0a1" assert headers["X-Pxl-Cell"] == "Python" assert headers["X-Pxl-Session"] == "2018-10-11T15:05:05.663979"
def test_plain_without_parameter(responses: RequestsMock, without_parameter_service, tmpdir, service_config: dict): pyxelrestgenerator = loader.load(tmpdir, service_config) responses.add( responses.GET, url="http://localhost:8950/plain_text_without_parameter", body= "string value returned should be truncated so that the following information cannot be seen by user, " "because of the fact that Excel does not allow more than 255 characters in a cell. " "Only the 255 characters will be returned by the user defined functions: YOU CANNOT RECEIVE THIS!!!!!!", match_querystring=True, ) assert ( pyxelrestgenerator.without_parameter_get_plain_text_without_parameter( ) == "string value returned should be truncated so that the following information cannot be seen by user, because of the fact that Excel does not allow more than 255 characters in a cell. Only the 255 characters will be returned by the user defined functions: " )
def test_json_content_type(responses: RequestsMock, content_type_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "content_type": { "open_api": {"definition": "http://localhost:8956/"}, "udf": {"return_types": ["sync_auto_expand"], "shift_result": False}, } }, ) responses.add( responses.GET, url="http://localhost:8956/json", json={}, match_querystring=True ) assert pyxelrestgenerator.content_type_get_json() == [[""]] assert ( _get_request(responses, "http://localhost:8956/json").headers["Accept"] == "application/json" )
def test_post_base_path_ending_with_slash( responses: RequestsMock, base_path_ending_with_slash_service, tmpdir ): pyxelrestgenerator = loader.load( tmpdir, { "base_path_ending_with_slash": { "open_api": {"definition": "http://localhost:8957/"}, "udf": {"return_types": ["sync_auto_expand"], "shift_result": False}, } }, ) responses.add( responses.POST, url="http://localhost:8957/method", json={}, match_querystring=True, ) assert pyxelrestgenerator.base_path_ending_with_slash_post_method() == [[""]]
def test_delete_with_selected_operation_ids(filtered_tags_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "selected_operation_ids": { "open_api": { "definition": "http://localhost:8944/", "selected_operation_ids": ["get_tags", "post_tags", "put_tags"], }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) assert not hasattr(pyxelrestgenerator, "selected_operation_ids_delete_tags")
def test_files_parameter(files_service, responses: RequestsMock, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "files": { "open_api": { "definition": "http://localhost:8959/" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) with tempfile.TemporaryDirectory() as temp_dir: with open(os.path.join(temp_dir, "temp_file"), "wb") as temp_file: temp_file.write(b"This is the content of the temporary file.") responses.add( responses.POST, url="http://localhost:8959/files", json={}, match_querystring=True, ) assert pyxelrestgenerator.files_post_files( mandatory_file="This is the content of the mandatory file.", optional_file=temp_file.name, ) == [[""]] actual_body = _get_request(responses, "http://localhost:8959/files").body assert ( b'\r\nContent-Disposition: form-data; name="mandatory_file"; filename="mandatory_file"\r\n\r\nThis is the content of the mandatory file.\r\n' in actual_body) assert ( b'\r\nContent-Disposition: form-data; name="optional_file"; filename="temp_file"\r\n\r\nThis is the content of the temporary file.\r\n' in actual_body)
def test_service_without_sync_does_not_have_sync(without_parameter_service, tmpdir): pyxelrestgenerator = loader.load( tmpdir, { "without_parameter": { "open_api": { "definition": "http://localhost:8950/" }, "udf": { "return_types": ["sync_auto_expand"], "shift_result": False }, } }, ) with pytest.raises(AttributeError) as exception_info: pyxelrestgenerator.vba_without_parameter_delete_without_parameter() assert ( str(exception_info.value) == "module 'pyxelrest.pyxelrestgenerator' has no attribute 'vba_without_parameter_delete_without_parameter'" )