def test_message_encode_full_message(): a_payload = scarab.ParamValue(99) messages = [] messages.append(_dripline.core.MsgRequest.create(payload=a_payload)) messages.append(_dripline.core.MsgReply.create(payload=a_payload)) messages.append(_dripline.core.MsgAlert.create(payload=a_payload)) for i in range(3): a_full_message = messages[i].encode_full_message() assert (type(a_full_message) == str) assert (len(a_full_message) > 0) assert (type(eval(a_full_message) == dict))
def test_submit_request_message(): a_name = "an_endpoint" an_endpoint = dripline.core.Endpoint(a_name) a_request = dripline.core.MsgRequest.create(scarab.ParamValue(5), dripline.core.op_t.get, "hey", "name", "a_receiver") a_reply = an_endpoint.submit_request_message(a_request) assert (isinstance(a_reply, dripline.core.MsgReply)) assert (a_reply.return_code == 0) assert (a_reply.correlation_id == a_request.correlation_id) a_reply.payload.to_python()['values'] == [a_name]
def test_request_reply_nondefault(): a_return_code = 788 a_return_message = "a return message." a_payload = scarab.ParamValue(99) a_request = _dripline.core.MsgRequest.create(reply_to="a_receiver") a_reply = a_request.reply(a_return_code, a_return_message, a_payload) assert (isinstance(a_reply, _dripline.core.MsgReply)) assert (a_reply.return_code == a_return_code) assert (a_reply.return_message == a_return_message) assert (a_reply.payload.is_value()) assert (a_reply.routing_key == a_request.reply_to) assert (a_reply.correlation_id == a_request.correlation_id)
def test_do_get_request_invalid_specifier(): an_endpoint = dripline.core.Endpoint("an_endpoint") a_get_request = dripline.core.MsgRequest.create(scarab.ParamValue(5), dripline.core.op_t.get, "hey", "namee", "a_receiver") correct_error = False try: a_reply = an_endpoint.do_get_request(a_get_request) except dripline.core.ThrowReply as e: correct_error = True assert (correct_error)
def test_do_cmd_request_valid_specifier(): class AnotherEndpoint(dripline.core.Endpoint): def __init__(self, name): dripline.core.Endpoint.__init__(self, name) def a_method(self, n1, n2): return n1 + n2 an_endpoint = AnotherEndpoint("an_endpoint") the_node = scarab.ParamNode() the_node["values"] = scarab.ParamArray() n1, n2 = 10, 13 the_node["values"].push_back(scarab.ParamValue(n1)) the_node["values"].push_back(scarab.ParamValue(n2)) a_cmd_request = dripline.core.MsgRequest.create(the_node, dripline.core.op_t.cmd, "hey", "a_method", "a_receiver") a_reply = an_endpoint.do_cmd_request(a_cmd_request) assert (isinstance(a_reply, dripline.core.MsgReply)) assert (a_reply.return_code == 0) assert (a_reply.correlation_id == a_cmd_request.correlation_id) assert (a_reply.payload.to_python() == n1 + n2)
def test_request_create_nondefault(): a_payload = scarab.ParamNode() a_payload.add("p1", scarab.ParamValue(1)) a_payload.add("p2", scarab.ParamValue(2)) a_msg_op = _dripline.core.op_t.get a_routing_key = "hey" a_specifier = "heyy.1" a_reply_to = "a_receiver" a_request = _dripline.core.MsgRequest.create(a_payload, a_msg_op, a_routing_key, a_specifier, a_reply_to) assert (isinstance(a_request, _dripline.core.MsgRequest)) assert (a_request.is_request()) assert (not a_request.is_reply()) assert (not a_request.is_alert()) assert (a_request.lockout_key_valid) assert (a_request.payload.is_node()) assert (a_request.payload.count("p1") == 1) assert (a_request.message_operation == a_msg_op) assert (a_request.routing_key == a_routing_key) assert (a_request.specifier.to_string() == a_specifier) assert (a_request.reply_to == a_reply_to) assert (a_request.encoding.name == "json") assert (len(a_request.correlation_id) == 36)
def test_alert_create_nondefault(): a_payload = scarab.ParamValue(3) a_routing_key = "hey" a_specifier = "heyy.1" an_alert = _dripline.core.MsgAlert.create(a_payload, a_routing_key, a_specifier) assert (isinstance(an_alert, _dripline.core.MsgAlert)) assert (an_alert.is_alert()) assert (not an_alert.is_reply()) assert (not an_alert.is_request()) assert (an_alert.payload.is_value()) assert (an_alert.routing_key == a_routing_key) assert (an_alert.specifier.to_string() == a_specifier) assert (an_alert.encoding.name == "json") assert (len(an_alert.correlation_id) == 36)
def test_do_set_request_no_specifier(): print("start test") an_endpoint = dripline.core.Endpoint("hello") the_node = scarab.ParamNode() the_node["values"] = scarab.ParamArray() the_node["values"].push_back(scarab.ParamValue("a_better_endpoint")) a_set_request = dripline.core.MsgRequest.create(the_node, dripline.core.op_t.set, "hey") correct_error = False try: a_reply = an_endpoint.do_set_request(a_set_request) except dripline.core.ThrowReply as e: correct_error = True assert (correct_error)
def test_do_set_request_invalid_specifier(): an_endpoint = dripline.core.Endpoint("an_endpoint") the_node = scarab.ParamNode() the_node["values"] = scarab.ParamArray() the_node["values"].push_back(scarab.ParamValue("a_better_endpoint")) a_set_request = dripline.core.MsgRequest.create(the_node, dripline.core.op_t.set, "hey", "namee", "a_receiver") correct_error = False try: a_reply = an_endpoint.do_set_request(a_set_request) except dripline.core.ThrowReply as e: correct_error = True print('content of error:\n{}'.format(dir(e))) assert (correct_error)
def test_reply_create2_nondefault(): a_retcode = 2005 a_message = "another message!" a_payload = scarab.ParamValue(87) a_request = _dripline.core.MsgRequest.create(reply_to="a_receiver") a_reply = _dripline.core.MsgReply.create(a_retcode, a_message, a_payload, a_request) assert (isinstance(a_reply, _dripline.core.MsgReply)) assert (a_reply.is_reply()) assert (not a_reply.is_request()) assert (not a_reply.is_alert()) assert (a_reply.return_code == a_retcode) assert (a_reply.return_message == a_message) assert (a_reply.payload.is_value()) assert (a_reply.routing_key == a_request.reply_to) assert (a_reply.specifier.to_string() == "") assert (a_reply.encoding.name == "json") assert (a_reply.correlation_id == a_request.correlation_id)
def do_get_request(self, a_request_message): logger.info("in get_request") a_specifier = a_request_message.specifier.to_string() if (a_specifier): logger.debug("has specifier") try: logger.debug(f"specifier is: {a_specifier}") an_attribute = getattr(self, a_specifier) logger.debug(f"attribute '{a_specifier}' value is [{an_attribute}]") the_node = scarab.ParamNode() the_node["values"] = scarab.ParamArray() the_node["values"].push_back(scarab.ParamValue(an_attribute)) return a_request_message.reply(payload=the_node) except AttributeError as this_error: raise ThrowReply('service_error_invalid_specifier', f"endpoint {self.name} has no attribute {a_specifier}, unable to get") else: logger.debug('no specifier') the_value = self.on_get() return a_request_message.reply(payload=scarab.to_param(the_value))
def test_reply_create1_nondefault(): a_retcode = 1005 a_message = "a message!" a_payload = scarab.ParamValue(87) a_routing_key = "hey" a_specifier = "heyy.11" a_reply = _dripline.core.MsgReply.create(a_retcode, a_message, a_payload, a_routing_key, a_specifier) assert (isinstance(a_reply, _dripline.core.MsgReply)) assert (a_reply.is_reply()) assert (not a_reply.is_request()) assert (not a_reply.is_alert()) assert (a_reply.return_code == a_retcode) assert (a_reply.return_message == a_message) assert (a_reply.payload.is_value()) assert (a_reply.routing_key == a_routing_key) assert (a_reply.specifier.to_string() == a_specifier) assert (a_reply.encoding.name == "json") assert (a_reply.correlation_id == "")
def test_do_set_request_valid_specifier(): value1 = "an_endpoint" value2 = "a_better_endpoint" ## the endpoint base class doesn't have any settable members, create one: class EndpointWithMember(dripline.core.Endpoint): a_value = value1 an_endpoint = EndpointWithMember("an_endpoint") the_node = scarab.ParamNode() the_node["values"] = scarab.ParamArray() the_node["values"].push_back(scarab.ParamValue(value2)) a_set_request = dripline.core.MsgRequest.create(the_node, dripline.core.op_t.set, "hey", "a_value", "a_receiver") a_reply = an_endpoint.do_set_request(a_set_request) assert (isinstance(a_reply, dripline.core.MsgReply)) assert (a_reply.return_code == 0) assert (a_reply.correlation_id == a_set_request.correlation_id) print(an_endpoint.name) assert (an_endpoint.a_value == value2)
def test_type_checks(): a_param = scarab.ParamValue(3) assert isinstance(a_param, scarab.Param)