def test_get_all_only_uses_allowed_filters(self, mocked_repo):
        # Given
        mocked_repo.return_value = [db_geography1, db_geography2]
        repo = GeographyRepository()
        filters = {
            COUNTRY_FILTER: 'usa',
            DATASET_FILTER: 'carto-do.project.census2011',
            CATEGORY_FILTER: 'demographics',
            VARIABLE_FILTER: 'population',
            GEOGRAPHY_FILTER: 'census-geo',
            VARIABLE_GROUP_FILTER: 'var-group',
            PROVIDER_FILTER: 'open_data',
            'fake_field_id': 'fake_value'
        }

        # When
        geographies = repo.get_all(filters)

        # Then
        mocked_repo.assert_called_once_with({
            COUNTRY_FILTER: 'usa',
            CATEGORY_FILTER: 'demographics',
            PROVIDER_FILTER: 'open_data'
        })
        assert geographies == test_geographies
    def test_missing_fields_are_mapped_as_None(self, mocked_repo):
        # Given
        mocked_repo.return_value = [{'id': 'geography1'}]
        repo = GeographyRepository()

        expected_geographies = CatalogList([Geography({
            'id': 'geography1',
            'slug': None,
            'name': None,
            'description': None,
            'provider_id': None,
            'provider_name': None,
            'country_id': None,
            'lang': None,
            'geom_coverage': None,
            'geom_type': None,
            'update_frequency': None,
            'version': None,
            'is_public_data': None,
            'summary_json': None
        })])

        # When
        geographies = repo.get_all()

        # Then
        assert geographies == expected_geographies
    def test_get_all_only_uses_allowed_filters(self, mocked_repo):
        # Given
        mocked_repo.return_value = [db_geography1, db_geography2]
        repo = GeographyRepository()
        filters = {
            'country_id': 'usa',
            'dataset_id': 'carto-do.project.census2011',
            'category_id': 'demographics',
            'variable_id': 'population',
            'geography_id': 'census-geo',
            'variable_group_id': 'var-group',
            'provider_id': 'open_data',
            'fake_field_id': 'fake_value'
        }

        # When
        geographies = repo.get_all(filters)

        # Then
        mocked_repo.assert_called_once_with({
            'country_id': 'usa',
            'category_id': 'demographics',
            'provider_id': 'open_data'
        })
        assert geographies == test_geographies
    def test_get_all_when_empty(self, mocked_repo):
        # Given
        mocked_repo.return_value = []
        repo = GeographyRepository()

        # When
        geographies = repo.get_all()

        # Then
        mocked_repo.assert_called_once_with(None)
        assert geographies == []
    def test_get_all(self, mocked_repo):
        # Given
        mocked_repo.return_value = [db_geography1, db_geography2]
        repo = GeographyRepository()

        # When
        geographies = repo.get_all()

        # Then
        mocked_repo.assert_called_once_with(None)
        assert isinstance(geographies, CatalogList)
        assert geographies == test_geographies
    def test_get_all_with_join_filters(self, mocked_repo):
        # Given
        mocked_repo.return_value = [db_geography1, db_geography2]
        repo = GeographyRepository()

        # When
        geographies = repo.get_all({CATEGORY_FILTER: 'demographics'})

        # Then
        mocked_repo.assert_called_once_with({CATEGORY_FILTER: 'demographics'})
        assert isinstance(geographies, CatalogList)
        assert geographies == test_geographies
    def test_get_all_credentials(self, mocked_get_subscription_ids, mocked_get_geographies):
        # Given
        mocked_get_subscription_ids.return_value = [db_geography1['id'], db_geography2['id']]
        mocked_get_geographies.return_value = [db_geography1, db_geography2]
        credentials = Credentials('user', '1234')
        repo = GeographyRepository()

        # When
        geographies = repo.get_all(credentials=credentials)

        # Then
        mocked_get_geographies.assert_called_once_with({'id': [db_geography1['id'], db_geography2['id']]})
        assert isinstance(geographies, CatalogList)
        assert geographies == test_geographies
    def test_get_all_credentials(self, mocked_set_user_credentials,
                                 mocked_get_geographies):
        # Given
        mocked_get_geographies.return_value = [db_geography1, db_geography2]
        credentials = Credentials('user', '1234')
        repo = GeographyRepository()

        # When
        geographies = repo.get_all(credentials=credentials)

        # Then
        mocked_set_user_credentials.assert_has_calls(
            [call(credentials), call(None)])
        mocked_get_geographies.assert_called_once_with(None)
        assert isinstance(geographies, CatalogList)
        assert geographies == test_geographies