예제 #1
0
    def test_map_manipulation(self):
        self.data_dct["int_to_lst_ints_map"][1] = {}
        self.data_dct["int_to_lst_ints_map"].pop(2)

        self.data_dct["str_to_message_map"]["where_from"].pop("a_long")

        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        self.assertTrue(1 in getattr(msg, "int_to_lst_ints_map").keys())
        self.assertTrue(2 not in getattr(msg, "int_to_lst_ints_map").keys())
        self.assertTrue("first_key" in getattr(msg, "str_to_enum_map").keys())

        dct = protobuf_to_dict(msg)
        self.assertEqual(dct["int_to_lst_ints_map"][1], {"lst_ints": []})
        self.assertEqual(dct["int_to_lst_ints_map"][3],
                         self.data_dct["int_to_lst_ints_map"][3])
        self.assertEqual(dct["str_to_enum_map"]["first_key"], "first")
        self.assertEqual(dct["str_to_int_map"]["some_str"], 1)

        check_dct = {
            "a_str": "Kashmir",
            "lst_longs": [long(1), long(2), long(3)],
            "a_long": long(0)
        }
        self.assertEqual(dct["str_to_message_map"]["where_from"], check_dct)
예제 #2
0
    def test_timestamps(self):
        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        self.assertEqual(
            getattr(msg, "a_timestamp").seconds,
            _FIRST_JUNE_2018_SECONDS_FROM_EPOCH)
        self.assertEqual(
            getattr(msg, "lst_timestamps")[0].seconds,
            _FIRST_JUNE_2018_SECONDS_FROM_EPOCH + _SECONDS_IN_A_DAY)
        #dct["str_to_timestamp_map"] = {"some_timestamp": timestamp_4}
        self.assertEqual(
            getattr(msg, "str_to_timestamp_map")["some_timestamp"].seconds,
            _FIRST_JUNE_2018_SECONDS_FROM_EPOCH + (3 * _SECONDS_IN_A_DAY))

        dct = protobuf_to_dict(msg)

        self.assertEqual(dct["a_timestamp"].seconds,
                         _FIRST_JUNE_2018_SECONDS_FROM_EPOCH)
        self.assertEqual(
            dct["lst_timestamps"][0].seconds,
            _FIRST_JUNE_2018_SECONDS_FROM_EPOCH + _SECONDS_IN_A_DAY)
        self.assertEqual(
            dct["str_to_timestamp_map"]["some_timestamp"].seconds,
            _FIRST_JUNE_2018_SECONDS_FROM_EPOCH + (3 * _SECONDS_IN_A_DAY))
예제 #3
0
    def test_all_populated(self):
        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        self.assertTrue(
            self._are_fields_in_proto_msg(msg, self.data_dct.keys()))

        dct = protobuf_to_dict(msg)

        self.assertEqual(dct, self.data_dct)
예제 #4
0
    def test_message_manipulation(self):

        self.data_dct["sub_message"]["a_long"] = long(560103)

        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        self.assertTrue(self._are_fields_in_proto_msg(msg, ["sub_message"]))

        dct = protobuf_to_dict(msg)
        self.assertEqual(dct["sub_message"]["a_long"], long(560103))
예제 #5
0
    def test_serialising_data_which_is_not_part_of_schema(self):
        data_dct_copy = copy.deepcopy(self.data_dct)

        self.data_dct["new_int"] = 11
        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        self.assertFalse(self._are_fields_in_proto_msg(msg, ["new_int"]))

        dct = protobuf_to_dict(msg)

        self.assertEqual(dct, data_dct_copy)
예제 #6
0
    def test_basic_scalar_manipulation(self):
        self.data_dct.pop("a_str")
        self.data_dct.pop("an_int")

        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        self.assertFalse(
            self._are_fields_in_proto_msg(msg, ["a_str", "an_int"]))

        dct = protobuf_to_dict(msg)
        self.assertEqual(dct["a_str"], "")
        self.assertEqual(dct["an_int"], 0)
예제 #7
0
def get_dict_from_proto_message(message, proto_class):
    """
    Get parsed proto message.

    :param message : (bytes/str) proto message to be converted into dict.
    :param proto_class: (class) proto class from the source.
    :return: (dict)
    """
    proto_obj = proto_class()

    # Deserialize proto
    proto_obj.ParseFromString(message)

    # get a dict out
    return protobuf_to_dict(proto_obj)
예제 #8
0
    def test_all_populated_with_serialisation_deserialisation(self):
        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        self.assertTrue(
            self._are_fields_in_proto_msg(msg, self.data_dct.keys()))

        # Serialize the proto object
        proto_serialised_str = msg.SerializeToString()

        # Deserialise to proto object
        recv_message = MainMessage()
        recv_message.ParseFromString(proto_serialised_str)

        dct = protobuf_to_dict(recv_message)

        self.assertEqual(dct, self.data_dct)
예제 #9
0
    def test_list_manipulation(self):
        self.data_dct["lst_ints"] = []
        self.data_dct["lst_messages"].pop(1)
        self.data_dct["lst_enums"].append("second")
        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        self.assertFalse(self._are_fields_in_proto_msg(msg, ["lst_ints"]))
        self.assertTrue(
            self._are_fields_in_proto_msg(msg, ["lst_enums", "lst_messages"]))
        self.assertEqual(len(getattr(msg, "lst_enums")), 4)
        self.assertEqual(len(getattr(msg, "lst_messages")), 1)

        dct = protobuf_to_dict(msg)
        self.assertEqual(dct["lst_ints"], [])
        self.assertEqual(dct["lst_enums"],
                         ["first", "second", "first", "second"])
        self.assertEqual(dct["lst_messages"], self.data_dct["lst_messages"])
예제 #10
0
    def test_enum_manipulation(self):
        self.data_dct["an_enum"] = "first"
        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        # Am constant value 0 is not sent in enum even if we explicitly set it
        self.assertFalse(self._are_fields_in_proto_msg(msg, ["an_enum"]))

        dct = protobuf_to_dict(msg)
        self.assertEqual(dct["an_enum"], "first")
        self.assertEqual(dct["str_to_enum_map"]["first_key"], "first")

        # We check for the constant 1, which will be sent in the proto message
        self.data_dct["an_enum"] = "second"
        msg = MainMessage()
        dict_to_protobuf(self.data_dct, msg)

        # As constant value 0 is not sent in enum even if we explicitly set it
        self.assertTrue(self._are_fields_in_proto_msg(msg, ["an_enum"]))