示例#1
0
def test_webhook_raises_error_on_get_flow_failure(sample_flow):
    webhook = Webhook(
        build_request_kwargs={"url": "https://content.dropboxapi.com/2/files"},
        build_request_http_method="POST",
        get_flow_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files"
        },
        get_flow_request_http_method="GET",
    )

    def _mock_failed_get(*args, **kwargs):
        return _MockResponse(status_code=500, json={})

    def _mock_successful_post(*args, **kwargs):
        return _MockResponse(status_code=200, json={"id": "abc"})

    webhook._method_to_function = {
        "GET": _mock_failed_get,
        "POST": _mock_successful_post,
    }
    webhook.add_flow(sample_flow)
    webhook.build()

    with pytest.raises(HTTPError, match="test-error-message"):
        webhook.get_flow()
示例#2
0
def test_webhook_throws_informative_error_if_flow_script_file_does_not_exist(
    sample_flow, ):

    nonexistent_file = "{}.py".format(str(uuid.uuid4()))
    webhook = Webhook(
        build_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files",
            "headers": {
                "Content-Type": "application/octet-stream"
            },
        },
        build_request_http_method="POST",
        get_flow_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files",
            "headers": {
                "Accept": "application/octet-stream"
            },
        },
        get_flow_request_http_method="GET",
        stored_as_script=True,
        flow_script_path=nonexistent_file,
    )

    webhook.add_flow(sample_flow)

    error_msg = "passed to flow_script_path does not exist"
    with pytest.raises(RuntimeError, match=error_msg):
        res = webhook.build()
        assert isinstance(res, Webhook)
示例#3
0
def test_webhook_throws_informative_error_if_flow_script_path_not_set(
        sample_flow):

    webhook = Webhook(
        build_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files",
            "headers": {
                "Content-Type": "application/octet-stream"
            },
        },
        build_request_http_method="POST",
        get_flow_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files",
            "headers": {
                "Accept": "application/octet-stream"
            },
        },
        get_flow_request_http_method="GET",
        stored_as_script=True,
    )

    webhook.add_flow(sample_flow)

    error_msg = "flow_script_path must be provided if stored_as_script=True"
    with pytest.raises(RuntimeError, match=error_msg):
        res = webhook.build()
        assert isinstance(res, Webhook)
示例#4
0
def test_webhook_raises_warning_if_data_in_build_request_kwargs(sample_flow):
    webhook = Webhook(
        build_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files",
            "data": cloudpickle.dumps(sample_flow),
        },
        build_request_http_method="POST",
        get_flow_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files"
        },
        get_flow_request_http_method="GET",
    )

    def _mock_successful_get(*args, **kwargs):
        return _MockResponse(status_code=200,
                             json={},
                             content=cloudpickle.dumps(sample_flow))

    def _mock_successful_post(*args, **kwargs):
        return _MockResponse(status_code=200, json={"id": "abc"})

    webhook._method_to_function = {
        "GET": _mock_successful_get,
        "POST": _mock_successful_post,
    }
    webhook.add_flow(sample_flow)

    with pytest.warns(RuntimeWarning,
                      match="flow content and should not be set directly"):
        res = webhook.build()
    assert isinstance(res, Webhook)
示例#5
0
def test_webhook_build_works_with_no_arguments(sample_flow):
    webhook = Webhook(
        build_request_kwargs={"url": "https://content.dropboxapi.com/2/files"},
        build_request_http_method="POST",
        get_flow_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files"
        },
        get_flow_request_http_method="GET",
    )

    def _mock_successful_get(*args, **kwargs):
        return _MockResponse(status_code=200,
                             json={},
                             content=cloudpickle.dumps(sample_flow))

    def _mock_successful_post(*args, **kwargs):
        return _MockResponse(status_code=200, json={"id": "abc"})

    webhook._method_to_function = {
        "GET": _mock_successful_get,
        "POST": _mock_successful_post,
    }
    webhook.add_flow(sample_flow)

    res = webhook.build()
    assert isinstance(res, Webhook)

    res = webhook.get_flow()
    assert isinstance(res, Flow)
示例#6
0
def test_webhook_works_with_file_storage(sample_flow, tmpdir):

    script_file = os.path.join(tmpdir, "{}.py".format(str(uuid.uuid4())))
    script_contents = """from prefect import Flow\nf=Flow('test-flow')"""
    with open(script_file, "w") as f:
        f.write(script_contents)

    webhook = Webhook(
        build_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files",
            "headers": {
                "Content-Type": "application/octet-stream"
            },
        },
        build_request_http_method="POST",
        get_flow_request_kwargs={
            "url": "https://content.dropboxapi.com/2/files",
            "headers": {
                "Accept": "application/octet-stream"
            },
        },
        get_flow_request_http_method="GET",
        stored_as_script=True,
        flow_script_path=script_file,
    )

    def _mock_successful_get(*args, **kwargs):
        file_contents = """from prefect import Flow\nf=Flow('test-flow')"""
        return _MockResponse(status_code=200,
                             json={},
                             content=file_contents.encode("utf-8"))

    def _mock_successful_post(*args, **kwargs):
        return _MockResponse(status_code=200, json={"id": "abc"})

    webhook._method_to_function = {
        "GET": _mock_successful_get,
        "POST": _mock_successful_post,
    }
    webhook.add_flow(sample_flow)

    res = webhook.build()
    assert isinstance(res, Webhook)

    res = webhook.get_flow()
    assert isinstance(res, Flow)
    assert res.name == "test-flow"