def setUp(self):
     super().setUp()
     self.graph = GraphBuilder([
         get_mock_entity_model(),
         get_mock_metric_model(),
         get_mock_metric_gender_model(),
     ]).build_graph()
     self.scope = Scope(dict(company_id='10', project_id='10'))
Esempio n. 2
0
 def setUp(self) -> None:
     super().setUp()
     self._scope = Scope(dict(company_id='10', project_id='10'))
     self._properties = DataRequestProperties(
         dict(data_sources=[MOCK_DATA_SOURCE_NAME]))
     self._graph = GraphBuilder([
         get_mock_entity_model(),
         get_mock_metric_model(),
         get_mock_metric_gender_model()
     ]).build_graph()
    def test_company_scoped_models(self):
        entity_model = get_mock_entity_model()
        company_model = get_mock_metric_model(company_id='company-id')
        models = [entity_model, company_model]
        sorted_models = sort_models_with_heuristic(
            models,
            {TaxonSlugExpression('ad_id'),
             TaxonSlugExpression('impressions')})

        self.assertEqual(2, len(sorted_models))
        self.assertEqual([company_model, entity_model], sorted_models)
    def test_scope_filters(self, mock__get_taxons, mock__load_models):
        mock__load_models.return_value = [
            get_mock_entity_model(),
            get_mock_metric_model(),
        ]
        request = InternalDataRequest({
            'scope': {
                'project_id': 'project',
                'company_id': 'company',
                "preaggregation_filters": {
                    "type":
                    "group",
                    "logical_operator":
                    "AND",
                    "clauses": [
                        {
                            "type": "taxon_value",
                            "taxon": "account_id",
                            "operator": "=",
                            "value": "595126134331606"
                        },
                    ],
                    "negate":
                    False,
                },
            },
            'properties': {
                'data_sources': ['mock_data_source']
            },
            'taxons': ['account_id', 'ad_name'],
        })
        dataframe = QueryBuilder.build_query(
            SNOWFLAKE_HUSKY_CONTEXT,
            request,
            QueryInfo.create(request),
            preloaded_taxons=TAXON_MAP,
        )

        actual = compile_query(dataframe.query)
        self.write_test_expectations('query.sql', actual)
        expected = self.read_test_expectations('query.sql')
        assert expected == actual
        self.assertEqual({'mock_data_source.entity_model'},
                         dataframe.used_model_names)
 def test_simple_concat(self, mock__get_taxons, mock__load_models):
     mock__load_models.return_value = [
         get_mock_entity_model(),
         get_mock_metric_model(),
     ]
     request = InternalDataRequest({
         'scope': {
             'project_id': 'project-id',
             'company_id': 'company-id',
             "preaggregation_filters": {
                 "type": "taxon_value",
                 "taxon": "account_id",
                 "operator": "=",
                 "value": "abc",
             },
         },
         'properties': {
             'data_sources': ['mock_data_source']
         },
         'taxons': ['account_id', 'ad_name'],
     })
     dimension_templates = [
         SqlFormulaTemplate(SqlTemplate('''concat(${ad_name},'xx')'''),
                            '''__1''', MOCK_DATA_SOURCE_NAME, {'ad_name'})
     ]
     df = QueryBuilder.build_query(
         SNOWFLAKE_HUSKY_CONTEXT,
         request,
         QueryInfo.create(request),
         preloaded_taxons=TAXON_MAP,
         dimension_templates=dimension_templates,
     )
     actual = compile_query(df.query)
     self.write_test_expectations('query.sql', actual)
     expected = self.read_test_expectations('query.sql')
     assert expected == actual
     self.assertEqual({'mock_data_source.entity_model'},
                      df.used_model_names)
 def test_scope_filters(self):
     scope_filter = TaxonValueFilterClause({
         'type':
         FilterClauseType.TAXON_VALUE.value,
         'taxon':
         'account_id',
         'operator':
         SimpleFilterOperator.EQ.value,
         'value':
         '10',
     }).to_native()
     scope = Scope(
         dict(company_id='10',
              project_id='10',
              preaggregation_filters=scope_filter))
     model = get_mock_metric_model()
     query = select([literal_column('test')])
     model_info = TaxonModelInfo('acc_id_column', model.name,
                                 ValueQuantityType.scalar)
     new_query = ScopeGuard.add_scope_row_filters(
         SNOWFLAKE_HUSKY_CONTEXT, scope, query, dict(account_id=model_info))
     # Not global model, we are fine without scope filters
     assert compile_query(
         new_query) == "SELECT test \nWHERE acc_id_column = '10'"