def test_fetch_annotation_invalid_filter(self, mock_fetch_data: Mock): dims = [ mock_date_annotation_dataset.fields.timestamp, mock_date_annotation_dataset.fields.political_party, ] mock_date_annotation_dataset.query.widget(self.mock_widget).dimension( *dims ).filter( mock_date_annotation_dataset.fields.political_party == "Democrat" ).fetch() fetch_annotation_args, fetch_data_args = self.get_fetch_call_args( mock_fetch_data ) self.assertEqual( ( mock_date_annotation_dataset.database, [ PypikaQueryMatcher( "SELECT " '"timestamp2" "$timestamp2",' '"district_name" "$district-name" ' 'FROM "politics"."annotations" ' 'GROUP BY "$timestamp2","$district-name"' ) ], FieldMatcher(mock_date_annotation_dataset.annotation.alignment_field), ), fetch_annotation_args, ) self.assertEqual( ( mock_date_annotation_dataset.database, [ PypikaQueryMatcher( "SELECT " '"timestamp" "$timestamp",' '"political_party" "$political_party",' 'SUM("votes") "$votes" ' 'FROM "politics"."politician" ' "WHERE \"political_party\"='Democrat' " 'GROUP BY "$timestamp","$political_party" ' 'ORDER BY "$timestamp","$political_party" ' 'LIMIT 200000' ) ], FieldMatcher(*dims), [], [], ), fetch_data_args, )
def test_fetch_annotation_single_category_dimension(self, mock_fetch_data: Mock): dims = [mock_category_annotation_dataset.fields.political_party] widget = f.Widget(mock_category_annotation_dataset.fields.votes) widget.transform = Mock() mock_category_annotation_dataset.query.widget(widget).dimension(*dims).fetch() fetch_annotation_args, fetch_data_args = self.get_fetch_call_args( mock_fetch_data ) self.assertEqual( ( mock_date_annotation_dataset.database, [ PypikaQueryMatcher( "SELECT " '"political_party" "$political_party",' '"district_name" "$district-name" ' 'FROM "politics"."annotations" ' 'GROUP BY "$political_party","$district-name"' ) ], FieldMatcher( mock_category_annotation_dataset.annotation.alignment_field ), ), fetch_annotation_args, ) self.assertEqual( ( mock_category_annotation_dataset.database, [ PypikaQueryMatcher( "SELECT " '"political_party" "$political_party",' 'SUM("votes") "$votes" ' 'FROM "politics"."politician" ' 'GROUP BY "$political_party" ' 'ORDER BY "$political_party" ' 'LIMIT 200000' ) ], FieldMatcher(*dims), [], [], ), fetch_data_args, )
def test_query_choices_for_join_dimension_with_filter_from_join( self, mock_get_column_definitions: Mock, mock_fetch_data: Mock ): mock_hint_dataset.fields["district-name"].choices.filter( mock_hint_dataset.fields["district-name"].isin(["Manhattan"]) ).fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher( "SELECT " '"hints"."district_name" "$district-name" ' 'FROM "politics"."hints" ' 'FULL OUTER JOIN "locations"."district" ON ' '"hints"."district_id"="district"."id" ' 'WHERE "district"."district_name" IN (' "'Manhattan') " 'AND NOT "hints"."district_name" IS NULL ' 'GROUP BY "$district-name" ' 'ORDER BY "$district-name"' ) ], FieldMatcher(mock_hint_dataset.fields["district-name"]), )
def test_query_choices_for_field(self, mock_fetch_data: Mock): mock_dataset.fields.political_party.choices.fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher("SELECT " '"political_party" "$political_party" ' 'FROM "politics"."politician" ' 'WHERE NOT "political_party" IS NULL ' 'GROUP BY "$political_party" ' 'ORDER BY "$political_party"') ], FieldMatcher(mock_dataset.fields.political_party), )
def test_query_choices_for_join_dimension( self, mock_get_column_definitions: Mock, mock_fetch_data: Mock): mock_hint_dataset.fields["district-name"].choices.fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher("SELECT " '"district_name" "$district-name" ' 'FROM "politics"."hints" ' 'WHERE NOT "district_name" IS NULL ' 'GROUP BY "$district-name" ' 'ORDER BY "$district-name"') ], FieldMatcher(mock_hint_dataset.fields["district-name"]), )
def test_pass_query_from_builder_as_arg(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() mock_dataset.query.widget(mock_widget).fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher('SELECT SUM("votes") "$votes" ' 'FROM "politics"."politician"') ], ANY, ANY, ANY, )
def test_fetch_annotation_no_dimension(self, mock_fetch_data: Mock): dims = [] mock_date_annotation_dataset.query.widget( self.mock_widget).dimension(*dims).fetch() mock_fetch_data.assert_called_once_with( mock_date_annotation_dataset.database, [ PypikaQueryMatcher( 'SELECT SUM("votes") "$votes" FROM "politics"."politician" ORDER BY 1 LIMIT 200000' ) ], FieldMatcher(*dims), [], [], )
def test_query_choices_for_field_with_display_hint_table( self, mock_get_column_definitions: Mock, mock_fetch_data: Mock): mock_hint_dataset.fields.candidate_name.choices.fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher("SELECT " '"candidate_name" "$candidate_name",' '"candidate_name_display" ' '"$candidate_name_display" ' 'FROM "politics"."hints" ' 'WHERE NOT "candidate_name" IS NULL ' 'GROUP BY "$candidate_name",' '"$candidate_name_display" ' 'ORDER BY "$candidate_name"') ], FieldMatcher(mock_hint_dataset.fields.candidate_name), )
def test_query_choices_for_case_filter(self, mock_get_column_definitions: Mock, mock_fetch_data: Mock): mock_hint_dataset.fields.political_party.choices.filter( mock_hint_dataset.fields.political_party_case.isin( ["Democrat", "Bill Clinton"])).fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher("SELECT " '"political_party" "$political_party" ' 'FROM "politics"."hints" ' 'WHERE NOT "political_party" IS NULL ' 'GROUP BY "$political_party" ' 'ORDER BY "$political_party"') ], FieldMatcher(mock_hint_dataset.fields.political_party), )
def test_query_choices_for_join_dimension_with_filter_from_base( self, mock_get_column_definitions: Mock, mock_fetch_data: Mock): mock_hint_dataset.fields["district-name"].choices.filter( mock_hint_dataset.fields.candidate_name.isin(["Bill Clinton" ])).fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher( "SELECT " '"district_name" "$district-name" ' 'FROM "politics"."hints" ' "WHERE \"candidate_name\" IN ('Bill Clinton') " 'AND NOT "district_name" IS NULL ' 'GROUP BY "$district-name" ' 'ORDER BY "$district-name"') ], FieldMatcher(mock_hint_dataset.fields["district-name"]), )
def test_query_choices_for_filters_from_base( self, mock_get_column_definitions: Mock, mock_fetch_data: Mock ): mock_hint_dataset.fields.political_party.choices.filter( mock_hint_dataset.fields.candidate_name.isin(["Bill Clinton"]) ).filter(mock_hint_dataset.fields["election-year"].isin([1992])).fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher( "SELECT " '"political_party" "$political_party" ' 'FROM "politics"."hints" ' "WHERE \"candidate_name\" IN ('Bill Clinton') " 'AND NOT "political_party" IS NULL ' 'GROUP BY "$political_party" ' 'ORDER BY "$political_party"' ) ], FieldMatcher(mock_hint_dataset.fields.political_party), )
def test_fetch_annotation_invalid_first_dimension(self, mock_fetch_data: Mock): dims = [mock_date_annotation_dataset.fields.political_party] mock_date_annotation_dataset.query.widget( self.mock_widget).dimension(*dims).fetch() mock_fetch_data.assert_called_once_with( mock_date_annotation_dataset.database, [ PypikaQueryMatcher("SELECT " '"political_party" "$political_party",' 'SUM("votes") "$votes" ' 'FROM "politics"."politician" ' 'GROUP BY "$political_party" ' 'ORDER BY "$political_party" ' 'LIMIT 200000') ], FieldMatcher(*dims), [], [], )
def test_query_choices_for_filters_from_joins( self, mock_get_column_definitions: Mock, mock_fetch_data: Mock ): mock_hint_dataset.fields.political_party.choices.filter( mock_hint_dataset.fields["district-name"].isin(["Manhattan"]) ).filter(mock_hint_dataset.fields["state"].isin(["Texas"])).fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher( "SELECT " '"hints"."political_party" "$political_party" ' 'FROM "politics"."hints" ' 'JOIN "locations"."state" ON ' '"hints"."state_id"="state"."id" ' 'WHERE "state"."state_name" IN (\'Texas\') ' 'AND NOT "hints"."political_party" IS NULL ' 'GROUP BY "$political_party" ' 'ORDER BY "$political_party"' ) ], FieldMatcher(mock_hint_dataset.fields.political_party), )