コード例 #1
0
def test_ping():
    app = _worker.Worker(transform_fn=MagicMock(), module_name='test_module')

    with app.test_client() as client:
        for _ in range(9):
            response = client.get('/ping')
            assert response.status_code == _status_codes.OK
            assert response.mimetype == _content_types.JSON
コード例 #2
0
def test_worker_with_initialize(module_name, expected_name):
    mock = MagicMock()
    app = _worker.Worker(transform_fn=mock.transform,
                         initialize_fn=mock.initialize,
                         module_name=module_name)
    assert app.import_name == expected_name
    assert app.before_first_request_funcs == [mock.initialize]
    assert app.request_class == _worker.Request
コード例 #3
0
def test_worker_with_custom_ping():
    transformer = Transformer()

    def custom_ping():
        return 'ping', _status_codes.ACCEPTED

    with _worker.Worker(transform_fn=transformer.transform,
                        healthcheck_fn=custom_ping,
                        module_name='custom_ping').test_client() as client:
        response = client.get('/ping')
        assert response.status_code == _status_codes.ACCEPTED
        assert response.get_data(as_text=True) == 'ping'
コード例 #4
0
def test_invocations(content_type):
    def transform_fn():
        return _worker.Response(response="fake data", mimetype=content_type)

    app = _worker.Worker(transform_fn=transform_fn, module_name="test_module")

    with app.test_client() as client:
        for _ in range(9):
            response = client.post("/invocations")
            assert response.status_code == http_client.OK
            assert response.get_data().decode("utf-8") == "fake data"
            assert response.mimetype == content_type
コード例 #5
0
def test_response_accept_deprecated():
    def transform_fn():
        return _worker.Response(response="fake data",
                                accept="deprecated accept arg")

    app = _worker.Worker(transform_fn=transform_fn, module_name="test_module")
    with app.test_client() as client:
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            client.post("/invocations")
            assert len(w) == 1
            assert issubclass(w[0].category, DeprecationWarning)
コード例 #6
0
def test_invocations():
    def transform_fn():
        return _worker.Response(response='fake data', accept=_content_types.JSON)

    app = _worker.Worker(transform_fn=transform_fn, module_name='test_module')

    with app.test_client() as client:
        for _ in range(9):
            response = client.post('/invocations')
            assert response.status_code == http_client.OK
            assert response.get_data().decode('utf-8') == 'fake data'
            assert response.mimetype == _content_types.JSON
コード例 #7
0
def test_worker_with_custom_ping():
    transformer = Transformer()

    def custom_ping():
        return "ping", http_client.ACCEPTED

    with _worker.Worker(
        transform_fn=transformer.transform, healthcheck_fn=custom_ping, module_name="custom_ping"
    ).test_client() as client:
        response = client.get("/ping")
        assert response.status_code == http_client.ACCEPTED
        assert response.get_data(as_text=True) == "ping"
コード例 #8
0
def test_worker(module_name, expected_name):
    transformer = Transformer()

    with _worker.Worker(transform_fn=transformer.transform,
                        module_name=module_name).test_client() as client:
        assert client.application.import_name == expected_name

        assert client.get('/ping').status_code == http_client.OK

        for _ in range(9):
            response = client.post('/invocations')
            assert response.status_code == http_client.OK

        response = client.post('/invocations')
        assert response.mimetype == _content_types.JSON
        assert json.loads(response.get_data(as_text=True)) == dict(initialize=0, transform=10)
コード例 #9
0
def test_user_execution_parameters_fn():
    expected_json = {"some-json": 1}
    expected_response_string = json.dumps(expected_json)
    test_execution_parameters_fn = MagicMock(
        return_value=expected_response_string)

    app = _worker.Worker(
        transform_fn=MagicMock(),
        module_name="test_module",
        execution_parameters_fn=test_execution_parameters_fn,
    )

    with app.test_client() as client:
        response = client.get("/execution-parameters")
        assert response.status_code == http_client.OK
        assert response.get_data().decode("utf-8") == expected_response_string
        assert json.loads(response.get_data().decode("utf-8")) == expected_json
コード例 #10
0
def test_worker_with_initialize():
    transformer = Transformer()

    with _worker.Worker(
            transform_fn=transformer.transform,
            initialize_fn=transformer.initialize,
            module_name='worker_with_initialize').test_client() as client:
        assert client.application.import_name == 'worker_with_initialize'

        assert client.get('/ping').status_code == _status_codes.OK

        for _ in range(9):
            response = client.post('/invocations')
            assert response.status_code == _status_codes.OK

        response = client.post('/invocations')
        assert json.loads(response.get_data(as_text=True)) == dict(
            initialize=1, transform=10)
        assert response.mimetype == _content_types.JSON
コード例 #11
0
def test_worker(module_name, expected_name):
    app = _worker.Worker(transform_fn=MagicMock().transform,
                         module_name=module_name)
    assert app.import_name == expected_name
    assert app.before_first_request_funcs == []
    assert app.request_class == _worker.Request
コード例 #12
0
def test_no_execution_parameters_fn():
    app = _worker.Worker(transform_fn=MagicMock(), module_name="test_module")
    with app.test_client() as client:
        response = client.get("/execution-parameters")
        assert response.status_code == http_client.NOT_FOUND