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_builder_dimensions_as_arg_with_one_dimension( self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() dimensions = [mock_dataset.fields.state] mock_dataset.query.widget(mock_widget).dimension(*dimensions).fetch() mock_fetch_data.assert_called_once_with(ANY, ANY, FieldMatcher(*dimensions), ANY, ANY)
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_call_transform_on_widget(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() # Need to keep widget the last call in the chain otherwise the object gets cloned and the assertion won't work mock_dataset.query.dimension( mock_dataset.fields.timestamp).widget(mock_widget).fetch() mock_widget.transform.assert_called_once_with( mock_paginate.return_value, mock_dataset, FieldMatcher(mock_dataset.fields.timestamp), [], )
def test_builder_dimensions_as_arg_with_a_non_replaced_set_dimension( self, mock_fetch_data: Mock, *args ): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() dimensions = [mock_dataset.fields.state] set_filter = f.ResultSet(dimensions[0]=='On', will_replace_referenced_dimension=False) mock_dataset.query.widget(mock_widget).dimension(*dimensions).filter(set_filter).fetch() set_dimension = _make_set_dimension(set_filter, mock_dataset) mock_fetch_data.assert_called_once_with( ANY, ANY, FieldMatcher(set_dimension, dimensions[0]), ANY, ANY )
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_find_share_dimensions_with_a_single_share_operation( self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget( Share(mock_dataset.fields.votes, over=mock_dataset.fields.state)) mock_widget.transform = Mock() dimensions = ( mock_dataset.fields.timestamp, mock_dataset.fields.state, mock_dataset.fields.political_party, ) mock_dataset.query.widget(mock_widget).dimension(*dimensions).fetch() mock_fetch_data.assert_called_once_with( ANY, ANY, ANY, FieldMatcher(mock_dataset.fields.state), ANY)
def test_builder_dimensions_as_arg_with_multiple_dimensions( self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() dimensions = ( mock_dataset.fields.timestamp, mock_dataset.fields.state, mock_dataset.fields.political_party, ) mock_dataset.query.widget(mock_widget).dimension(*dimensions).fetch() mock_fetch_data.assert_called_once_with(ANY, ANY, FieldMatcher(*dimensions), 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_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_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_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_find_share_dimensions_with_a_multiple_share_operations_over_different_dimensions( self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget( Share(mock_dataset.fields.votes, over=mock_dataset.fields.state), Share(mock_dataset.fields.wins, over=mock_dataset.fields.political_party), ) mock_widget.transform = Mock() dimensions = ( mock_dataset.fields.timestamp, mock_dataset.fields.state, mock_dataset.fields.political_party, ) mock_dataset.query.widget(mock_widget).dimension(*dimensions).fetch() expected = FieldMatcher(mock_dataset.fields.state, mock_dataset.fields.political_party) mock_fetch_data.assert_called_once_with(ANY, ANY, ANY, expected, ANY)
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), )