def test_look_up_with_field_anchor(self):
        # given:
        schema_spec = {
            'user.sn_profiles.account_id': {
                'value_type': 'string',
                'multivalue': False
            },
            'user.sn_profiles': {
                'value_type': 'object',
                'multivalue': True
            }
        }
        schema_template = self._prepare_mock_schema_template(
            'user', 'user/user', schema_spec)

        # when:
        account_id_spec = column_specification.look_up(
            schema_template,
            'user.sn_profiles.account_id',
            'user',
            context='user.sn_profiles')
        account_id_user_spec = column_specification.look_up(
            schema_template,
            'user.sn_profiles.account_id',
            'user',
            context='user')

        # then:
        self.assertFalse(account_id_spec.is_field_of_list_element())
        self.assertEqual('string', account_id_spec.data_type)
        self.assertFalse(account_id_spec.multivalue)

        # and:
        self.assertTrue(account_id_user_spec.is_field_of_list_element())
    def test_look_up_linked_object_field(self):
        # given:
        schema_spec = {
            'manufacturer.organisation': {
                'value_type': 'string',
                'multivalue': False
            }
        }
        schema_template = self._prepare_mock_schema_template(
            'manufacturer', 'manufacturer/manufacturer', schema_spec)

        # when:
        organisation_spec = column_specification.look_up(
            schema_template, 'manufacturer.organisation', 'product')

        # then:
        self.assertEqual('manufacturer', organisation_spec.domain_type)
        self.assertEqual('product', organisation_spec.context_concrete_type)
    def test_look_up_multivalue_object_field(self):
        # given:
        schema_spec = {
            'product.remarks': {
                'value_type': 'string',
                'multivalue': True
            }
        }
        schema_template = self._prepare_mock_schema_template(
            'product', 'merchandise/product', schema_spec)

        # when:
        remarks_spec = column_specification.look_up(schema_template,
                                                    'product.remarks',
                                                    'product')

        # then:
        self.assertTrue(remarks_spec.multivalue)
        self.assertEqual('product.remarks', remarks_spec.field_name)
示例#4
0
    def create_row_template(self, ingest_worksheet: IngestWorksheet):
        concrete_type = self.get_concrete_type(ingest_worksheet.title)
        domain_type = self.get_domain_type(concrete_type)
        column_headers = ingest_worksheet.get_column_headers()
        cell_conversions = []

        context = self._determine_context(concrete_type, ingest_worksheet)
        header_counter = {}
        for header in column_headers:
            if not header_counter.get(header):
                header_counter[header] = 0
            header_counter[header] = header_counter[header] + 1

            column_spec = column_specification.look_up(self.template, header, concrete_type,
                                                       context=context,
                                                       order_of_occurrence=header_counter[header])
            strategy = conversion_strategy.determine_strategy(column_spec)
            cell_conversions.append(strategy)

        default_values = self._define_default_values(concrete_type)
        return RowTemplate(domain_type, concrete_type, cell_conversions,
                           default_values=default_values)
    def test_look_up_id_field(self):
        # given:
        schema_spec = {
            'product.id': {
                'value_type': 'integer',
                'multivalue': False,
                'identifiable': True
            }
        }
        schema_template = self._prepare_mock_schema_template(
            'product', 'merchandise/product', schema_spec)

        # when:
        id_spec = column_specification.look_up(schema_template, 'product.id',
                                               'product')

        # then:
        self.assertTrue(id_spec.is_identity())
        self.assertEqual('product', id_spec.context_concrete_type)
        self.assertEqual('merchandise', id_spec.domain_type)
        self.assertEqual('product.id', id_spec.field_name)
        self.assertEqual('integer', id_spec.data_type)
    def test_look_up_nested_object_field(self):
        # given:
        schema_spec = {
            'product.reviews.rating': {
                'value_type': 'integer',
                'multivalue': False
            },
            'product.reviews': {
                'value_type': 'object',
                'multivalue': True
            }
        }
        schema_template = self._prepare_mock_schema_template(
            'product', 'merchandise/product', schema_spec)

        # when:
        review_rating_spec = column_specification.look_up(
            schema_template, 'product.reviews.rating', 'product')

        # then:
        self.assertFalse(review_rating_spec.multivalue)
        self.assertTrue(review_rating_spec.multivalue_parent)
    def test_look_up_external_id(self):
        # given:
        schema_spec = {
            'product.manufacturer_id': {
                'value_type': 'integer',
                'multivalue': False,
                'identifiable': True,
                'external_reference': True
            }
        }
        schema_template = self._prepare_mock_schema_template(
            'product', 'merchandise/product', schema_spec)

        # when:
        manufacturer_id_spec = column_specification.look_up(
            schema_template, 'product.manufacturer_id', 'product')

        # then:
        self.assertTrue(manufacturer_id_spec.identity)
        self.assertTrue(manufacturer_id_spec.external_reference)
        self.assertEqual('product.manufacturer_id',
                         manufacturer_id_spec.field_name)
        self.assertEqual('integer', manufacturer_id_spec.data_type)
    def test_look_up_object_field(self):
        # given:
        schema_spec = {
            'product.name': {
                'value_type': 'string',
                'multivalue': False
            }
        }
        schema_template = self._prepare_mock_schema_template(
            'product', 'merchandise/product', schema_spec)

        # when:
        name_spec = column_specification.look_up(schema_template,
                                                 'product.name',
                                                 'product',
                                                 order_of_occurrence=7)

        # then:
        self.assertFalse(name_spec.multivalue)
        self.assertEqual('product.name', name_spec.field_name)
        self.assertEqual('product', name_spec.context_concrete_type)
        self.assertEqual('merchandise', name_spec.domain_type)
        self.assertEqual('string', name_spec.data_type)
        self.assertEqual(7, name_spec.order_of_occurrence)