Repository: CartoDB/cartoframes
Pull request #1723 (2021-03-10 16:36:38)

Filter only product entities in subscriptions


Patch:
diff --git a/cartoframes/data/observatory/catalog/repository/dataset_repo.py b/cartoframes/data/observatory/catalog/repository/dataset_repo.py
index 4bd26ade..cc9f906a 100644
--- a/cartoframes/data/observatory/catalog/repository/dataset_repo.py
+++ b/cartoframes/data/observatory/catalog/repository/dataset_repo.py
@@ -25,6 +25,7 @@ class DatasetRepository(EntityRepository):
 
         # Using user credentials to fetch entities
         self.client.set_user_credentials(credentials)
+        filters = self.client.get_entities_filters(filters)
         entities = self._get_filtered_entities(filters)
         self.client.reset_user_credentials()
         return entities
diff --git a/cartoframes/data/observatory/catalog/repository/geography_repo.py b/cartoframes/data/observatory/catalog/repository/geography_repo.py
index d23fc922..19d8f652 100644
--- a/cartoframes/data/observatory/catalog/repository/geography_repo.py
+++ b/cartoframes/data/observatory/catalog/repository/geography_repo.py
@@ -28,6 +28,7 @@ class GeographyRepository(EntityRepository):
 
         # Using user credentials to fetch entities
         self.client.set_user_credentials(credentials)
+        filters = self.client.get_entities_filters(filters)
         entities = self._get_filtered_entities(filters)
         self.client.reset_user_credentials()
         return entities
diff --git a/cartoframes/data/observatory/catalog/repository/repo_client.py b/cartoframes/data/observatory/catalog/repository/repo_client.py
index b828b4c9..6ef42d99 100644
--- a/cartoframes/data/observatory/catalog/repository/repo_client.py
+++ b/cartoframes/data/observatory/catalog/repository/repo_client.py
@@ -73,6 +73,13 @@ class RepoClient:
             entity = 'datasets/{0}/variables_groups'.format(filters.pop('dataset'))
             return self._fetch_entity(entity, filters)
 
+    def get_entities_filters(self, filters=None):
+        self.set_external_credentials()
+        if filters is not None:
+            filters['filter_catalog'] = False
+        entities = self._fetch_entity('entities', filters)
+        return {'id': [result['id'] for result in entities['results']]}
+
     def _get_filter_id(self, filters, use_slug=False):
         if isinstance(filters, dict):
             filter_id = filters.get('id')


Test patch:
diff --git a/tests/unit/data/observatory/catalog/repository/test_dataset_repo.py b/tests/unit/data/observatory/catalog/repository/test_dataset_repo.py
index 20471a21..cc92d742 100644
--- a/tests/unit/data/observatory/catalog/repository/test_dataset_repo.py
+++ b/tests/unit/data/observatory/catalog/repository/test_dataset_repo.py
@@ -18,9 +18,11 @@ from ..examples import test_dataset1, test_datasets, db_dataset1, db_dataset2
 class TestDatasetRepo(object):
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_get_all(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_all(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_dataset1, db_dataset2]
+        mocked_filters.side_effect = lambda f: f
         repo = DatasetRepository()
 
         # When
@@ -32,10 +34,12 @@ class TestDatasetRepo(object):
         assert datasets == test_datasets
 
     @patch.object(RepoClient, 'get_datasets')
+    @patch.object(RepoClient, 'get_entities_filters')
     @patch('cartoframes.data.observatory.catalog.repository.entity_repo.get_subscription_ids')
-    def test_get_all_credentials(self, mocked_get_subscription_ids, mocked_get_datasets):
+    def test_get_all_credentials(self, mocked_get_subscription_ids, mocked_filters, mocked_get_datasets):
         # Given
         mocked_get_subscription_ids.return_value = [db_dataset1['id'], db_dataset2['id']]
+        mocked_filters.side_effect = lambda f: f
         mocked_get_datasets.return_value = [db_dataset1, db_dataset2]
         credentials = Credentials('user', '1234')
         repo = DatasetRepository()
@@ -49,9 +53,11 @@ class TestDatasetRepo(object):
         assert datasets == test_datasets
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_get_all_when_empty(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_all_when_empty(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = []
+        mocked_filters.side_effect = lambda f: f
         repo = DatasetRepository()
 
         # When
@@ -62,9 +68,11 @@ class TestDatasetRepo(object):
         assert datasets == []
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_get_all_only_uses_allowed_filters(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_all_only_uses_allowed_filters(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_dataset1, db_dataset2]
+        mocked_filters.side_effect = lambda f: f
         repo = DatasetRepository()
         filters = {
             COUNTRY_FILTER: 'usa',
@@ -90,9 +98,11 @@ class TestDatasetRepo(object):
         assert datasets == test_datasets
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_get_by_id(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_id(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_dataset1]
+        mocked_filters.side_effect = lambda f: f
         requested_id = db_dataset1['id']
         repo = DatasetRepository()
 
@@ -104,9 +114,11 @@ class TestDatasetRepo(object):
         assert dataset == test_dataset1
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_get_by_id_unknown_fails(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_id_unknown_fails(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = []
+        mocked_filters.side_effect = lambda f: f
         requested_id = 'unknown_id'
         repo = DatasetRepository()
 
@@ -115,9 +127,11 @@ class TestDatasetRepo(object):
             repo.get_by_id(requested_id)
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_get_by_slug(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_slug(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_dataset1]
+        mocked_filters.side_effect = lambda f: f
         requested_slug = db_dataset1['slug']
         repo = DatasetRepository()
 
@@ -129,9 +143,11 @@ class TestDatasetRepo(object):
         assert dataset == test_dataset1
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_get_by_id_list(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_id_list(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_dataset1, db_dataset2]
+        mocked_filters.side_effect = lambda f: f
         repo = DatasetRepository()
 
         # When
@@ -143,9 +159,11 @@ class TestDatasetRepo(object):
         assert datasets == test_datasets
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_get_by_slug_list(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_slug_list(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_dataset1, db_dataset2]
+        mocked_filters.side_effect = lambda f: f
         repo = DatasetRepository()
 
         # When
@@ -157,9 +175,11 @@ class TestDatasetRepo(object):
         assert datasets == test_datasets
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_get_by_slug_and_id_list(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_slug_and_id_list(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_dataset1, db_dataset2]
+        mocked_filters.side_effect = lambda f: f
         repo = DatasetRepository()
 
         # When
@@ -171,9 +191,11 @@ class TestDatasetRepo(object):
         assert datasets == test_datasets
 
     @patch.object(RepoClient, 'get_datasets')
-    def test_missing_fields_are_mapped_as_None(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_missing_fields_are_mapped_as_None(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [{'id': 'dataset1'}]
+        mocked_filters.side_effect = lambda f: f
         repo = DatasetRepository()
 
         expected_datasets = CatalogList([Dataset({
diff --git a/tests/unit/data/observatory/catalog/repository/test_geography_repo.py b/tests/unit/data/observatory/catalog/repository/test_geography_repo.py
index 5a1ed001..2a4efc61 100644
--- a/tests/unit/data/observatory/catalog/repository/test_geography_repo.py
+++ b/tests/unit/data/observatory/catalog/repository/test_geography_repo.py
@@ -18,9 +18,11 @@ from ..examples import test_geography1, test_geographies, db_geography1, db_geog
 class TestGeographyRepo(object):
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_all(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_all(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_geography1, db_geography2]
+        mocked_filters.side_effect = lambda f: f
         repo = GeographyRepository()
 
         # When
@@ -32,10 +34,12 @@ class TestGeographyRepo(object):
         assert geographies == test_geographies
 
     @patch.object(RepoClient, 'get_geographies')
+    @patch.object(RepoClient, 'get_entities_filters')
     @patch('cartoframes.data.observatory.catalog.repository.entity_repo.get_subscription_ids')
-    def test_get_all_credentials(self, mocked_get_subscription_ids, mocked_get_geographies):
+    def test_get_all_credentials(self, mocked_get_subscription_ids, mocked_filters, mocked_get_geographies):
         # Given
         mocked_get_subscription_ids.return_value = [db_geography1['id'], db_geography2['id']]
+        mocked_filters.side_effect = lambda f: f
         mocked_get_geographies.return_value = [db_geography1, db_geography2]
         credentials = Credentials('user', '1234')
         repo = GeographyRepository()
@@ -49,9 +53,11 @@ class TestGeographyRepo(object):
         assert geographies == test_geographies
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_all_when_empty(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_all_when_empty(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = []
+        mocked_filters.side_effect = lambda f: f
         repo = GeographyRepository()
 
         # When
@@ -62,9 +68,11 @@ class TestGeographyRepo(object):
         assert geographies == []
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_all_only_uses_allowed_filters(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_all_only_uses_allowed_filters(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_geography1, db_geography2]
+        mocked_filters.side_effect = lambda f: f
         repo = GeographyRepository()
         filters = {
             COUNTRY_FILTER: 'usa',
@@ -89,9 +97,11 @@ class TestGeographyRepo(object):
         assert geographies == test_geographies
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_by_id(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_id(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_geography1]
+        mocked_filters.side_effect = lambda f: f
         requested_id = db_geography1['id']
         repo = GeographyRepository()
 
@@ -104,9 +114,11 @@ class TestGeographyRepo(object):
         assert geography == test_geography1
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_by_id_unknown_fails(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_id_unknown_fails(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = []
+        mocked_filters.side_effect = lambda f: f
         requested_id = 'unknown_id'
         repo = GeographyRepository()
 
@@ -115,9 +127,11 @@ class TestGeographyRepo(object):
             repo.get_by_id(requested_id)
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_by_slug(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_slug(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_geography1]
+        mocked_filters.side_effect = lambda f: f
         requested_slug = db_geography1['slug']
         repo = GeographyRepository()
 
@@ -129,9 +143,11 @@ class TestGeographyRepo(object):
         assert geography == test_geography1
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_by_id_list(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_id_list(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_geography1, db_geography2]
+        mocked_filters.side_effect = lambda f: f
         repo = GeographyRepository()
 
         # When
@@ -143,9 +159,11 @@ class TestGeographyRepo(object):
         assert geographies == test_geographies
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_by_slug_list(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_slug_list(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_geography1, db_geography2]
+        mocked_filters.side_effect = lambda f: f
         repo = GeographyRepository()
 
         # When
@@ -157,9 +175,11 @@ class TestGeographyRepo(object):
         assert geographies == test_geographies
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_by_slug_and_id_list(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_by_slug_and_id_list(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_geography1, db_geography2]
+        mocked_filters.side_effect = lambda f: f
         repo = GeographyRepository()
 
         # When
@@ -171,9 +191,11 @@ class TestGeographyRepo(object):
         assert geographies == test_geographies
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_get_all_with_join_filters(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_get_all_with_join_filters(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [db_geography1, db_geography2]
+        mocked_filters.side_effect = lambda f: f
         repo = GeographyRepository()
 
         # When
@@ -185,9 +207,11 @@ class TestGeographyRepo(object):
         assert geographies == test_geographies
 
     @patch.object(RepoClient, 'get_geographies')
-    def test_missing_fields_are_mapped_as_None(self, mocked_repo):
+    @patch.object(RepoClient, 'get_entities_filters')
+    def test_missing_fields_are_mapped_as_None(self, mocked_filters, mocked_repo):
         # Given
         mocked_repo.return_value = [{'id': 'geography1'}]
+        mocked_filters.side_effect = lambda f: f
         repo = GeographyRepository()
 
         expected_geographies = CatalogList([Geography({