Пример #1
0
    def read_template(message: IpapMessage,
                      template_type: TemplateType) -> IpapTemplate:
        """
        Reads a template type from a message.

        :return: Ipap Template
        """
        temp_list = message.get_template_list()
        for id_template in temp_list:
            template = message.get_template_object(id_template)
            if template.get_type() == template_type:
                return template

        raise ValueError("Template not found")
Пример #2
0
class IpapMessageTest(unittest.TestCase):
    """
    IpapMessageTest
    """
    def setUp(self):
        self.ipap_message = IpapMessage(1, 1, False)
        self.ipap_message2 = IpapMessage(1, 1, True)

    def test_new_data_template(self):
        val = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.assertEqual(val, 256)

    def test_add_field(self):
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)

        # test adding an invalid field
        with self.assertRaises(ValueError):
            self.ipap_message.add_field(template_id, 0, 3000)

        # test adding an invalid template id.
        with self.assertRaises(ValueError):
            self.ipap_message.add_field(2, 0, 30)

    def test_delete_template(self):
        print('start test_delete_template')
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)
        self.ipap_message.delete_template(template_id)

        lst = self.ipap_message.get_template_list()
        self.assertEqual(len(lst), 0)

    def test_delete_all_templates(self):
        print('start test_delete_all_templates')
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)
        self.ipap_message.delete_all_templates()
        lst = self.ipap_message.get_template_list()
        self.assertEqual(len(lst), 0)

    def test_get_template_list(self):
        print('start test_get_template_list')
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)
        lst = self.ipap_message.get_template_list()
        self.assertEqual(lst[0], 256)

    def test_get_template_object(self):
        print('start test_get_template_object')
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)

        template = self.ipap_message.get_template_object(template_id)
        template_type = template.get_type()
        self.assertEqual(template_type,
                         TemplateType.IPAP_SETID_AUCTION_TEMPLATE)

        lst = template.get_fields()
        print('num fields:', len(lst))

        with self.assertRaises(ValueError):
            template = self.ipap_message.get_template_object(4)

    def test_include_data(self):
        print('in test_include_data')
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)

        template = self.ipap_message.get_template_object(template_id)
        lst = template.get_fields()

        ipap_data_record = IpapDataRecord(templ_id=template_id)
        ipap_field_value1 = IpapValueField()
        value = 12231213
        ipap_field_value1.set_value_uint64(value)

        template = self.ipap_message.get_template_object(template_id)
        lst = template.get_fields()

        # Replace the value
        ipap_data_record.insert_field(0, 30, ipap_field_value1)
        self.ipap_message.include_data(template_id, ipap_data_record)

        record_size = self.ipap_message.get_data_record_size()
        self.assertEqual(record_size, 1)

    def test_get_data_record_at_pos(self):
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)

        ipap_data_record = IpapDataRecord(templ_id=template_id)
        ipap_field_value1 = IpapValueField()
        value = 12231213
        ipap_field_value1.set_value_uint64(value)

        # Replace the value
        ipap_data_record.insert_field(0, 30, ipap_field_value1)
        self.ipap_message.include_data(template_id, ipap_data_record)
        ipap_data_record2 = self.ipap_message.get_data_record_at_pos(0)

    def test_import(self):
        print('In test import')
        self.ipap_message2.set_syn(True)
        self.ipap_message2.set_seqno(300)
        self.ipap_message2.output()

        str_msg = self.ipap_message2.get_message()
        ipap_message3 = IpapMessage(1, 1, True, str_msg)

        str_msg = 'aqui estoy'
        with self.assertRaises(ValueError):
            ipap_message4 = IpapMessage(1, 1, True, str_msg)
Пример #3
0
    def split(self, ipap_message: IpapMessage) -> (dict, dict, dict):
        """
        parse the ipap message by splitting message data by object key ( Auctions, Bids, Allocations).

        :param ipap_message: message to parse
        :return:
        """
        data_record_count = ipap_message.get_data_record_size()
        templates_included = {}
        object_templates = {}
        object_data_records = {}
        templates_not_related = {}

        for i in range(0, data_record_count):
            data_record = ipap_message.get_data_record_at_pos(i)
            template_id = data_record.get_template_id()
            try:
                template = ipap_message.get_template_object(template_id)

            except ValueError:
                raise ValueError(
                    "required template not included in the message")

            templates_included[template_id] = template_id
            templ_type = template.get_type()

            try:
                # Obtain template keys
                data_key = ''
                key_fields = template.get_template_type_key_field(templ_type)
                for key_field in key_fields:
                    field = template.get_field(key_field.get_eno(),
                                               key_field.get_ftype())
                    value = data_record.get_field(key_field.get_eno(),
                                                  key_field.get_ftype())
                    data_key = data_key + field.write_value(value)

                object_type = template.get_object_type(templ_type)
                ipap_object_key = IpapObjectKey(object_type, data_key)

                if ipap_object_key not in object_templates.keys():
                    object_templates[ipap_object_key] = []

                object_templates[ipap_object_key].append(template)

                if ipap_object_key not in object_data_records:
                    object_data_records[ipap_object_key] = []

                object_data_records[ipap_object_key].append(data_record)

            except ValueError as e:
                raise ValueError(
                    "error while reading data record - error: {0}", str(e))

        # Copy templates from message that are not related with a record data
        templates = ipap_message.get_template_list()
        for template_id in templates:
            if template_id not in templates_included:
                templates_not_related[
                    template_id] = ipap_message.get_template_object(
                        template_id)

        return object_templates, object_data_records, templates_not_related