Repository: CartoDB/cartoframes
Pull request #1625 (2020-04-28 07:09:42)

Add providers to Catalog
https://app.clubhouse.io/cartoteam/story/72147/fix-providers-in-catalog


Patch:
diff --git a/cartoframes/data/observatory/catalog/catalog.py b/cartoframes/data/observatory/catalog/catalog.py
index d8659c43..918dd138 100644
--- a/cartoframes/data/observatory/catalog/catalog.py
+++ b/cartoframes/data/observatory/catalog/catalog.py
@@ -1,7 +1,8 @@
-from .dataset import Dataset
 from .entity import is_slug_value
-from .category import Category
 from .country import Country
+from .category import Category
+from .provider import Provider
+from .dataset import Dataset
 from .geography import Geography
 from .subscriptions import Subscriptions
 from .repository.constants import COUNTRY_FILTER, CATEGORY_FILTER, GEOGRAPHY_FILTER, PROVIDER_FILTER, PUBLIC_FILTER
@@ -143,6 +144,19 @@ class Catalog:
         """
         return Category.get_all(self.filters)
 
+    @property
+    def providers(self):
+        """Get all the providers in the Catalog.
+
+        Returns:
+            :py:class:`CatalogList <cartoframes.data.observatory.entity.CatalogList>`
+
+        Raises:
+            CatalogError: if there's a problem when connecting to the catalog or no datasets are found.
+
+        """
+        return Provider.get_all(self.filters)
+
     @property
     def datasets(self):
         """Get all the datasets in the Catalog.
diff --git a/cartoframes/data/observatory/catalog/repository/category_repo.py b/cartoframes/data/observatory/catalog/repository/category_repo.py
index 624f97a5..0a011400 100644
--- a/cartoframes/data/observatory/catalog/repository/category_repo.py
+++ b/cartoframes/data/observatory/catalog/repository/category_repo.py
@@ -1,9 +1,9 @@
-from .constants import COUNTRY_FILTER
+from .constants import COUNTRY_FILTER, PROVIDER_FILTER
 from .entity_repo import EntityRepository
 
 
 _CATEGORY_ID_FIELD = 'id'
-_ALLOWED_FILTERS = [COUNTRY_FILTER]
+_ALLOWED_FILTERS = [COUNTRY_FILTER, PROVIDER_FILTER]
 
 
 def get_category_repo():
diff --git a/cartoframes/data/observatory/catalog/repository/country_repo.py b/cartoframes/data/observatory/catalog/repository/country_repo.py
index 84d885cb..1717f489 100644
--- a/cartoframes/data/observatory/catalog/repository/country_repo.py
+++ b/cartoframes/data/observatory/catalog/repository/country_repo.py
@@ -1,9 +1,9 @@
-from .constants import CATEGORY_FILTER
+from .constants import CATEGORY_FILTER, PROVIDER_FILTER
 from .entity_repo import EntityRepository
 
 
 _COUNTRY_ID_FIELD = 'id'
-_ALLOWED_FILTERS = [CATEGORY_FILTER]
+_ALLOWED_FILTERS = [CATEGORY_FILTER, PROVIDER_FILTER]
 
 
 def get_country_repo():
diff --git a/cartoframes/data/observatory/catalog/repository/provider_repo.py b/cartoframes/data/observatory/catalog/repository/provider_repo.py
index 72216586..acdc9bcb 100644
--- a/cartoframes/data/observatory/catalog/repository/provider_repo.py
+++ b/cartoframes/data/observatory/catalog/repository/provider_repo.py
@@ -1,7 +1,9 @@
+from .constants import CATEGORY_FILTER, COUNTRY_FILTER
 from .entity_repo import EntityRepository
 
 
 _PROVIDER_ID_FIELD = 'id'
+_ALLOWED_FILTERS = [CATEGORY_FILTER, COUNTRY_FILTER]
 
 
 def get_provider_repo():
@@ -11,7 +13,7 @@ def get_provider_repo():
 class ProviderRepository(EntityRepository):
 
     def __init__(self):
-        super(ProviderRepository, self).__init__(_PROVIDER_ID_FIELD, [])
+        super(ProviderRepository, self).__init__(_PROVIDER_ID_FIELD, _ALLOWED_FILTERS)
 
     @classmethod
     def _get_entity_class(cls):


Test patch:
diff --git a/tests/unit/data/observatory/catalog/repository/test_category_repo.py b/tests/unit/data/observatory/catalog/repository/test_category_repo.py
index 6c7bae28..5a3a138c 100644
--- a/tests/unit/data/observatory/catalog/repository/test_category_repo.py
+++ b/tests/unit/data/observatory/catalog/repository/test_category_repo.py
@@ -63,7 +63,8 @@ class TestCategoryRepo(object):
 
         # Then
         mocked_repo.assert_called_once_with({
-            COUNTRY_FILTER: 'usa'
+            COUNTRY_FILTER: 'usa',
+            PROVIDER_FILTER: 'open_data'
         })
         assert categories == test_categories
 
diff --git a/tests/unit/data/observatory/catalog/repository/test_country_repo.py b/tests/unit/data/observatory/catalog/repository/test_country_repo.py
index f6adf2d3..7fd71e2c 100644
--- a/tests/unit/data/observatory/catalog/repository/test_country_repo.py
+++ b/tests/unit/data/observatory/catalog/repository/test_country_repo.py
@@ -62,7 +62,8 @@ class TestCountryRepo(object):
 
         # Then
         mocked_repo.assert_called_once_with({
-            CATEGORY_FILTER: 'demographics'
+            CATEGORY_FILTER: 'demographics',
+            PROVIDER_FILTER: 'open_data'
         })
         assert countries == test_countries
 
diff --git a/tests/unit/data/observatory/catalog/repository/test_provider_repo.py b/tests/unit/data/observatory/catalog/repository/test_provider_repo.py
index 828c8c65..709d6aa3 100644
--- a/tests/unit/data/observatory/catalog/repository/test_provider_repo.py
+++ b/tests/unit/data/observatory/catalog/repository/test_provider_repo.py
@@ -7,6 +7,10 @@ from cartoframes.data.observatory.catalog.entity import CatalogList
 from cartoframes.data.observatory.catalog.provider import Provider
 from cartoframes.data.observatory.catalog.repository.provider_repo import ProviderRepository
 from cartoframes.data.observatory.catalog.repository.repo_client import RepoClient
+from cartoframes.data.observatory.catalog.repository.constants import (
+    CATEGORY_FILTER, COUNTRY_FILTER, DATASET_FILTER, GEOGRAPHY_FILTER, VARIABLE_FILTER,
+    VARIABLE_GROUP_FILTER
+)
 from ..examples import test_provider1, test_providers, db_provider1, db_provider2
 
 
@@ -39,6 +43,31 @@ class TestProviderRepo(object):
         mocked_repo.assert_called_once_with(None)
         assert providers is None
 
+    @patch.object(RepoClient, 'get_providers')
+    def test_get_all_only_uses_allowed_filters(self, mocked_repo):
+        # Given
+        mocked_repo.return_value = [db_provider1, db_provider2]
+        repo = ProviderRepository()
+        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',
+            'fake_field_id': 'fake_value'
+        }
+
+        # When
+        providers = repo.get_all(filters)
+
+        # Then
+        mocked_repo.assert_called_once_with({
+            COUNTRY_FILTER: 'usa',
+            CATEGORY_FILTER: 'demographics'
+        })
+        assert providers == test_providers
+
     @patch.object(RepoClient, 'get_providers')
     def test_get_by_id(self, mocked_repo):
         # Given
diff --git a/tests/unit/data/observatory/catalog/test_catalog.py b/tests/unit/data/observatory/catalog/test_catalog.py
index 087ae50b..6fca9a74 100644
--- a/tests/unit/data/observatory/catalog/test_catalog.py
+++ b/tests/unit/data/observatory/catalog/test_catalog.py
@@ -7,6 +7,7 @@ from cartoframes.data.observatory.catalog.dataset import Dataset
 from cartoframes.data.observatory.catalog.geography import Geography
 from cartoframes.data.observatory.catalog.country import Country
 from cartoframes.data.observatory.catalog.category import Category
+from cartoframes.data.observatory.catalog.provider import Provider
 from cartoframes.data.observatory.catalog.catalog import Catalog
 from cartoframes.data.observatory.catalog.subscriptions import Subscriptions
 from cartoframes.data.observatory.catalog.repository.geography_repo import GeographyRepository
@@ -15,7 +16,8 @@ from cartoframes.data.observatory.catalog.repository.constants import (
 )
 from .examples import (
     test_country2, test_country1, test_category1, test_category2, test_dataset1, test_dataset2,
-    test_geographies, test_datasets, test_categories, test_countries, test_geography1, test_geography2
+    test_geographies, test_datasets, test_categories, test_countries, test_geography1, test_geography2,
+    test_provider1, test_provider2
 )
 
 
@@ -47,6 +49,19 @@ class TestCatalog(object):
         # Then
         assert categories == expected_categories
 
+    @patch.object(Provider, 'get_all')
+    def test_providers(self, mocked_providers):
+        # Given
+        expected_providers = [test_provider1, test_provider2]
+        mocked_providers.return_value = expected_providers
+        catalog = Catalog()
+
+        # When
+        providers = catalog.providers
+
+        # Then
+        assert providers == expected_providers
+
     @patch.object(Dataset, 'get_all')
     def test_datasets(self, mocked_datasets):
         # Given