Example #1
0
def test_responder_worker_exc(mock_publish):

    message = Mock()
    message.properties = {"reply_to": ""}

    config = {AMQP_URI_CONFIG_KEY: ""}
    responder = Responder(config, message)

    # serialisable exception
    worker_exc = Exception("error")
    result, exc_info = responder.send_response(None, (Exception, worker_exc, "tb"))
    assert result is None
    assert exc_info == (Exception, worker_exc, "tb")

    expected_msg = {
        "result": None,
        "error": {
            "exc_path": "{}.Exception".format(EXCEPTION_MODULE),
            "value": "error",
            "exc_type": "Exception",
            "exc_args": ["error"],
        },
    }
    (msg,), _ = mock_publish.call_args
    assert msg == expected_msg
Example #2
0
def test_responder_unserializable_result(mock_publish, unserializable, serializer, exception_info_string):

    message = Mock()
    message.properties = {"reply_to": ""}

    config = {AMQP_URI_CONFIG_KEY: "", SERIALIZER_CONFIG_KEY: serializer}
    responder = Responder(config, message)

    # unserialisable result
    worker_result = unserializable
    result, exc_info = responder.send_response(worker_result, None)

    # responder will return the error from the serializer
    assert result is None
    # Different kombu versions return different exceptions, so
    # testing for the concrete exception is not feasible
    assert exc_info == (ANY, ANY, ANY)
    assert exception_info_string in str(exc_info[1])

    # and publish a dictionary-serialized UnserializableValueError
    # on worker_result
    expected_msg = {
        "result": None,
        "error": {
            "exc_path": "nameko.exceptions.UnserializableValueError",
            "value": "Unserializable value: `{}`".format(worker_result),
            "exc_type": "UnserializableValueError",
            "exc_args": [],
        },
    }
    (msg,), _ = mock_publish.call_args
    assert msg == expected_msg
Example #3
0
def test_responder_unserializable_exc(mock_publish):

    message = Mock()
    message.properties = {"reply_to": ""}

    container = Mock()
    container.config = {AMQP_URI_CONFIG_KEY: ""}

    responder = Responder(message)

    # unserialisable exception
    worker_exc = Exception(object())
    result, exc_info = responder.send_response(container, True, (Exception, worker_exc, "tb"))

    # responder will return the TypeError from json.dumps
    assert result is None
    assert exc_info == (TypeError, ANY, ANY)
    assert exc_info[1].message == ("{} is not JSON " "serializable".format(worker_exc.args[0]))

    # and publish a dictionary-serialized UnserializableValueError
    # (where the unserialisable value is a dictionary-serialized worker_exc)
    serialized_exc = serialize(worker_exc)
    expected_msg = {
        "result": None,
        "error": {
            "exc_path": "nameko.exceptions.UnserializableValueError",
            "value": "Unserializable value: `{}`".format(serialized_exc),
            "exc_type": "UnserializableValueError",
            "exc_args": (),
        },
    }
    (msg,), _ = mock_publish.call_args
    assert msg == expected_msg
Example #4
0
def test_responder_unserializable_result(mock_publish):

    message = Mock()
    message.properties = {"reply_to": ""}

    config = {AMQP_URI_CONFIG_KEY: ""}
    responder = Responder(config, message)

    # unserialisable result
    worker_result = object()
    result, exc_info = responder.send_response(worker_result, None)

    # responder will return the TypeError from json.dumps
    assert result is None
    assert exc_info == (TypeError, ANY, ANY)
    assert str(exc_info[1]) == "{} is not JSON serializable".format(worker_result)

    # and publish a dictionary-serialized UnserializableValueError
    # on worker_result
    expected_msg = {
        "result": None,
        "error": {
            "exc_path": "nameko.exceptions.UnserializableValueError",
            "value": "Unserializable value: `{}`".format(worker_result),
            "exc_type": "UnserializableValueError",
            "exc_args": [],
        },
    }
    (msg,), _ = mock_publish.call_args
    assert msg == expected_msg
    def test_populate_database_non_existent_model(self):
        # given
        item = Mock()
        item.type = "random"
        item.properties = [""]
        items = [item]

        # then
        with self.assertRaises(NonExistentModelException):
            populate_database(items)
    def test_populate_database_add_technology(self):
        # given
        item = Mock()
        item.type = "technology"
        item.properties = ["techno_txt"]
        items = [item]

        # when
        populate_database(items)

        # then
        self.assert_technology_with_name_exists_in_database(item.properties[0])
    def test_populate_database_add_status(self):
        # given
        item = Mock()
        item.type = "status"
        item.properties = ["status_txt"]
        items = [item]

        # when
        populate_database(items)

        # then
        self.assert_status_with_name_exists_in_database(item.properties[0])
    def test_populate_database_add_user(self):
        # given
        item = Mock()
        item.type = "user"
        item.properties = ["bob", "bob@bob.com", "bobpw"]
        items = [item]

        # when
        populate_database(items)

        # then
        self.assert_user_with_username_exists_in_database(item.properties[0])
    def test_populate_database_add_post(self):
        # given
        item = Mock()
        item.type = "post"
        item.properties = ["title", "filename", datetime.now(), "category", "author"]
        items = [item]

        # when
        populate_database(items)

        # then
        self.assert_post_with_title_exists_in_database(item.properties[0])
    def test_populate_database_add_project(self):
        # given
        item = Mock()
        item.type = "project"
        item.properties = ["title", "filename", "techno", "url", 1]
        items = [item]

        # when
        populate_database(items)

        # then
        self.assert_project_with_title_exists_in_database(item.properties[0])
Example #11
0
    def test_get_properties(self, find):
        service = Mock()
        service.name = "web"
        service.properties = properties.Properties(defaults={"prop1": 1})
        find.return_value = service
        r = web.ServiceResource()
        request = DummyRequest(["web", "properties"])
        d = _render(r, request)

        def rendered(ignored):
            self.assertEquals(request.responseCode, None)  # Gets treated as 200 OK
            self.assertEquals(json.loads("".join(request.written)), {"prop1": 1})

        d.addCallback(rendered)
        return d
Example #12
0
def test_responder(mock_publish):

    message = Mock()
    message.properties = {"reply_to": ""}

    config = {AMQP_URI_CONFIG_KEY: ""}
    responder = Responder(config, message)

    # serialisable result
    result, exc_info = responder.send_response(True, None)
    assert result is True
    assert exc_info is None

    expected_msg = {"result": True, "error": None}
    (msg,), _ = mock_publish.call_args
    assert msg == expected_msg
Example #13
0
def test_responder_cannot_repr_exc(mock_publish):

    message = Mock()
    message.properties = {"reply_to": ""}

    config = {AMQP_URI_CONFIG_KEY: ""}
    responder = Responder(config, message)

    class CannotRepr(object):
        def __repr__(self):
            raise Exception("error")

    # un-repr-able exception
    worker_exc = Exception(CannotRepr())

    # send_response should not throw
    responder.send_response(True, (Exception, worker_exc, "tb"))
Example #14
0
 def test_dump_message(self):
     m = Mock()
     m.body = "the quick brown fox"
     m.properties = {"a": 1}
     m.delivery_info = {"exchange": "bar"}
     self.assertTrue(dump_message(m))