Ejemplo n.º 1
0
def test_detects_many_to_one():
    adset_id_attr = FdqModelAttribute(data_reference='"adset_id"', field_map=['adset_id'])
    ad_id_attr = FdqModelAttribute(data_reference='"ad_id"', field_map=['ad_id'])
    spend_attr = FdqModelAttribute(data_reference='"spend"', field_map=['spend'])
    impressions_attr = FdqModelAttribute(data_reference='"impressions"', field_map=['impressions'])

    adset_model = create_temp_internal_from_api_model(
        name='model_1',
        data_source='source.db.schema.table',
        fields=[adset_id_attr, spend_attr],
        identifiers=['adset_id'],
    )
    ad_model = create_temp_internal_from_api_model(
        name='model_2',
        data_source='source.db.schema.second_table',
        fields=[ad_id_attr, adset_id_attr, spend_attr, impressions_attr],
        identifiers=['adset_id', 'ad_id'],
    )
    models = [adset_model, ad_model]

    result = detect_joins(models)

    assert result == {
        ad_model.model_name: [
            FdqModelJoin(
                to_model=adset_model.model_name,
                relationship=FdqModelJoinRelationship.many_to_one,
                fields=['adset_id'],
                join_type=JoinType.left,
            ).dict(by_alias=True)
        ]
    }
Ejemplo n.º 2
0
def test_does_not_find_joins_with_no_matching_ids():
    some_id_attr = FdqModelAttribute(data_reference='"some_id"', field_map=['some_id'])
    another_id_attr = FdqModelAttribute(data_reference='"another_id"', field_map=['another_id'])
    spend_attr = FdqModelAttribute(data_reference='"spend"', field_map=['spend'])

    model_one = create_temp_internal_from_api_model(
        name='model_1', data_source='source.db.schema.table', fields=[some_id_attr, spend_attr], identifiers=['some_id']
    )

    model_two = create_temp_internal_from_api_model(
        name='model_2',
        data_source='source.db.schema.table',
        fields=[another_id_attr, spend_attr],
        identifiers=['another_id'],
    )

    assert detect_joins(models=[model_one, model_two]) == {}
Ejemplo n.º 3
0
 def from_internal(
     cls,
     transformation: str,
     model_attributes: List[ModelAttribute],
     virtual_data_source: str,
 ) -> FdqModelAttribute:
     """
     Initialize model attribute for API model from Husky model attribute
     and cutoff virtual data source from taxon slug, if needed
     """
     # type is same across all attributes
     return FdqModelAttribute.construct(
         field_map=[
             remove_virtual_data_source_prefix(virtual_data_source,
                                               attr.taxon)
             for attr in model_attributes
         ],
         # for backward compatibility, we reuse column_name for a bit
         data_reference=transformation,
     )
Ejemplo n.º 4
0
import pytest

from panoramic.cli.husky.core.federated.model.models import FdqModelAttribute
from panoramic.cli.husky.core.federated.model.tel.data_structures import (
    AttributeValidationTelVisitorParams, )
from panoramic.cli.husky.core.federated.model.tel.visitor import (
    AttributeValidationTelVisitor, )
from panoramic.cli.husky.core.tel.exceptions import TelExpressionException
from panoramic.cli.husky.core.tel.tel_dialect import ModelTelDialect

_TMP_MODEL_ATTRIBUTES = [
    FdqModelAttribute(data_reference='"column_0"', field_map=['source_taxon']),
    FdqModelAttribute(data_reference='"column_1"', field_map=['gender']),
    FdqModelAttribute(data_reference='"column_2"', field_map=['ad_id']),
    FdqModelAttribute(data_reference='"column_3"', field_map=['spend']),
    FdqModelAttribute(data_reference='spend + 10', field_map=['spend_2']),
]


@pytest.mark.parametrize(
    ['inp', 'exp', 'exp_used_taxons', 'exp_used_columns'],
    [
        ('1+10', '1+10', set(), set()),
        ('1+10/spend', '1+10/"column_3"', {'spend'}, {'column_3'}),
        ('1+10/spend_2', '1+10/"column_3"+10', {'spend_2', 'spend'
                                                }, {'column_3'}),
        (
            '1+10/spend_2 * unknown_taxon',
            '1+10/"column_3"+10*unknown_taxon',
            {'spend_2', 'spend', 'unknown_taxon'},
            {'column_3'},
Ejemplo n.º 5
0
                                                 _VIRTUAL_DATA_SOURCE)

    assert attr.dict(by_alias=True) == {
        'data_reference':
        transformation,
        'field_map': [
            remove_virtual_data_source_prefix(_VIRTUAL_DATA_SOURCE,
                                              model_attrs[0].taxon)
        ],
    }


@pytest.mark.parametrize(
    'model_attr',
    [
        FdqModelAttribute(data_reference='"first_col"',
                          field_map=['taxon_slug']),
        FdqModelAttribute(data_reference='"col_name"',
                          field_map=['taxon_slug_2']),
    ],
)
def test_api_model_attribute_to_internal(model_attr):
    identifiers = {'taxon_slug'}
    attrs = FdqModelAttributeMapper.to_internal(model_attr,
                                                _VIRTUAL_DATA_SOURCE,
                                                identifiers)

    assert [attr.to_primitive() for attr in attrs] == [{
        'tel_transformation':
        model_attr.data_reference,
        'taxon':
        prefix_with_virtual_data_source(_VIRTUAL_DATA_SOURCE,