Skip to content

Commit

Permalink
Merge b31274f into c755e7d
Browse files Browse the repository at this point in the history
  • Loading branch information
esloho committed Oct 14, 2019
2 parents c755e7d + b31274f commit 6a65a06
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 68 deletions.
4 changes: 2 additions & 2 deletions cartoframes/data/observatory/__init__.py
Expand Up @@ -3,7 +3,7 @@
from .catalog import Catalog
from .category import Category
from .country import Country
from .dataset import Dataset
from .dataset import CatalogDataset
from .geography import Geography
from .provider import Provider
from .variable import Variable
Expand All @@ -13,7 +13,7 @@
'Catalog',
'Category',
'Country',
'Dataset',
'CatalogDataset',
'Geography',
'Provider',
'Variable'
Expand Down
6 changes: 3 additions & 3 deletions cartoframes/data/observatory/catalog.py
Expand Up @@ -4,7 +4,7 @@
from .category import Category
from .country import Country
from .geography import Geography
from .dataset import Dataset
from .dataset import CatalogDataset
from .repository.constants import COUNTRY_FILTER, CATEGORY_FILTER, GEOGRAPHY_FILTER


Expand Down Expand Up @@ -45,7 +45,7 @@ def datasets(self):
"""

return Dataset.get_all(self.filters)
return CatalogDataset.get_all(self.filters)

@property
def geographies(self):
Expand Down Expand Up @@ -128,4 +128,4 @@ def purchased_datasets(self, credentials=None):
"""

return Dataset.get_all(self.filters, credentials)
return CatalogDataset.get_all(self.filters, credentials)
2 changes: 1 addition & 1 deletion cartoframes/data/observatory/dataset.py
Expand Up @@ -8,7 +8,7 @@
from .repository.constants import DATASET_FILTER


class Dataset(CatalogEntity):
class CatalogDataset(CatalogEntity):
entity_repo = get_dataset_repo()

@property
Expand Down
3 changes: 2 additions & 1 deletion cartoframes/data/observatory/entity.py
Expand Up @@ -21,6 +21,7 @@ class CatalogEntity(ABC):

id_field = 'id'
entity_repo = None
export_excluded_fields = ['summary_jsonb']

def __init__(self, data):
self.data = data
Expand Down Expand Up @@ -52,7 +53,7 @@ def to_series(self):
return pd.Series(self.data)

def to_dict(self):
return self.data
return {key: value for key, value in self.data.items() if key not in self.export_excluded_fields}

def __eq__(self, other):
return self.data == other.data
Expand Down
4 changes: 2 additions & 2 deletions cartoframes/data/observatory/repository/dataset_repo.py
Expand Up @@ -24,8 +24,8 @@ def get_all(self, filters=None, credentials=None):

@classmethod
def _get_entity_class(cls):
from cartoframes.data.observatory.dataset import Dataset
return Dataset
from cartoframes.data.observatory.dataset import CatalogDataset
return CatalogDataset

def _get_rows(self, filters=None):
return self.client.get_datasets(filters)
Expand Down
12 changes: 12 additions & 0 deletions cartoframes/data/observatory/variable.py
Expand Up @@ -6,6 +6,9 @@
from .repository.constants import VARIABLE_FILTER


_DESCRIPTION_LENGTH_LIMIT = 20


class Variable(CatalogEntity):

entity_repo = get_variable_repo()
Expand Down Expand Up @@ -64,3 +67,12 @@ def schema_name(self):
def dataset_name(self):
_, _, dataset, _ = self.id.split('.')
return dataset

def __repr__(self):
descr = self.description

if len(descr) > _DESCRIPTION_LENGTH_LIMIT:
descr = descr[0:_DESCRIPTION_LENGTH_LIMIT] + '...'

return "<{classname}('{entity_id}','{descr}')>"\
.format(classname=self.__class__.__name__, entity_id=self._get_print_id(), descr=descr)
8 changes: 4 additions & 4 deletions test/data/observatory/examples.py
@@ -1,5 +1,5 @@
from cartoframes.data.observatory.variable import Variable
from cartoframes.data.observatory.dataset import Dataset
from cartoframes.data.observatory.dataset import CatalogDataset
from cartoframes.data.observatory.category import Category
from cartoframes.data.observatory.geography import Geography
from cartoframes.data.observatory.country import Country
Expand Down Expand Up @@ -93,15 +93,15 @@
'is_public_data': False,
'summary_jsonb': {}
}
test_dataset1 = Dataset(db_dataset1)
test_dataset2 = Dataset(db_dataset2)
test_dataset1 = CatalogDataset(db_dataset1)
test_dataset2 = CatalogDataset(db_dataset2)
test_datasets = CatalogList([test_dataset1, test_dataset2])

db_variable1 = {
'id': 'carto-do.variable.var1',
'slug': 'var1',
'name': 'Population',
'description': 'The number of people within each geography',
'description': 'Number of people',
'column_name': 'pop',
'db_type': 'Numeric',
'dataset_id': 'dataset1',
Expand Down
4 changes: 2 additions & 2 deletions test/data/observatory/repository/test_dataset_repo.py
@@ -1,7 +1,7 @@
import unittest

from cartoframes.auth import Credentials
from cartoframes.data.observatory.dataset import Dataset
from cartoframes.data.observatory.dataset import CatalogDataset

from cartoframes.exceptions import DiscoveryException
from cartoframes.data.observatory.entity import CatalogList
Expand Down Expand Up @@ -176,7 +176,7 @@ def test_missing_fields_are_mapped_as_None(self, mocked_repo):
mocked_repo.return_value = [{'id': 'dataset1'}]
repo = DatasetRepository()

expected_datasets = CatalogList([Dataset({
expected_datasets = CatalogList([CatalogDataset({
'id': 'dataset1',
'slug': None,
'name': None,
Expand Down
12 changes: 6 additions & 6 deletions test/data/observatory/test_catalog.py
Expand Up @@ -5,7 +5,7 @@
from cartoframes.data.observatory.geography import Geography
from cartoframes.data.observatory.country import Country
from cartoframes.data.observatory.category import Category
from cartoframes.data.observatory.dataset import Dataset
from cartoframes.data.observatory.dataset import CatalogDataset
from cartoframes.data.observatory.catalog import Catalog
from cartoframes.data.observatory.repository.geography_repo import GeographyRepository
from .examples import test_country2, test_country1, test_category1, test_category2, test_dataset1, test_dataset2, \
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_categories(self, mocked_categories):
# Then
assert categories == expected_categories

@patch.object(Dataset, 'get_all')
@patch.object(CatalogDataset, 'get_all')
def test_datasets(self, mocked_datasets):
# Given
expected_datasets = [test_dataset1, test_dataset2]
Expand Down Expand Up @@ -84,7 +84,7 @@ def test_filters_on_categories(self, mocked_categories):
mocked_categories.called_once_with({'country_id': 'usa'})
assert categories == test_categories

@patch.object(Dataset, 'get_all')
@patch.object(CatalogDataset, 'get_all')
def test_filters_on_datasets(self, mocked_datasets):
# Given
mocked_datasets.return_value = test_datasets
Expand All @@ -110,7 +110,7 @@ def test_filters_on_geographies(self, mocked_geographies):
mocked_geographies.called_once_with({'country_id': 'usa', 'category_id': 'demographics'})
assert geographies == test_geographies

@patch.object(Dataset, 'get_all')
@patch.object(CatalogDataset, 'get_all')
def test_all_filters(self, mocked_datasets):
# Given
mocked_datasets.return_value = test_datasets
Expand All @@ -128,7 +128,7 @@ def test_all_filters(self, mocked_datasets):

assert datasets == test_datasets

@patch.object(Dataset, 'get_all')
@patch.object(CatalogDataset, 'get_all')
@patch.object(GeographyRepository, 'get_by_id')
def test_geography_filter_by_slug(self, mocked_repo, mocked_datasets):
# Given
Expand All @@ -145,7 +145,7 @@ def test_geography_filter_by_slug(self, mocked_repo, mocked_datasets):
mocked_datasets.assert_called_once_with({'geography_id': test_geography1.id})
assert datasets == test_datasets

@patch.object(Dataset, 'get_all')
@patch.object(CatalogDataset, 'get_all')
def test_purchased_datasets(self, mocked_purchased_datasets):
# Given
expected_datasets = [test_dataset1, test_dataset2]
Expand Down
6 changes: 3 additions & 3 deletions test/data/observatory/test_category.py
Expand Up @@ -104,7 +104,7 @@ def test_category_is_exported_as_dict(self):
assert isinstance(category_dict, dict)
assert category_dict == db_category1

def test_category_is_represented_with_id(self):
def test_category_is_represented_with_classname_and_id(self):
# Given
category = Category(db_category1)

Expand Down Expand Up @@ -137,7 +137,7 @@ def test_get_all(self, mocked_repo):
assert isinstance(categories, CatalogList)
assert categories == test_categories

def test_category_list_is_printed_with_classname(self):
def test_category_list_is_printed_with_classname_and_ids(self):
# Given
categories = CatalogList([test_category1, test_category2])

Expand All @@ -148,7 +148,7 @@ def test_category_list_is_printed_with_classname(self):
assert categories_str == "[<Category('{id1}')>, <Category('{id2}')>]" \
.format(id1=db_category1['id'], id2=db_category2['id'])

def test_category_list_is_represented_with_ids(self):
def test_category_list_is_represented_with_classname_and_ids(self):
# Given
categories = CatalogList([test_category1, test_category2])

Expand Down
6 changes: 3 additions & 3 deletions test/data/observatory/test_country.py
Expand Up @@ -117,7 +117,7 @@ def test_country_is_exported_as_dict(self):
assert isinstance(country_dict, dict)
assert country_dict == db_country1

def test_country_is_represented_with_id(self):
def test_country_is_represented_with_classname_and_id(self):
# Given
country = Country(db_country1)

Expand Down Expand Up @@ -150,7 +150,7 @@ def test_get_all_countries(self, mocked_repo):
assert isinstance(countries, CatalogList)
assert countries == test_countries

def test_country_list_is_printed_with_classname(self):
def test_country_list_is_printed_with_classname_and_ids(self):
# Given
countries = CatalogList([test_country1, test_country2])

Expand All @@ -161,7 +161,7 @@ def test_country_list_is_printed_with_classname(self):
assert countries_str == "[<Country('{id1}')>, <Country('{id2}')>]" \
.format(id1=db_country1['id'], id2=db_country2['id'])

def test_country_list_is_represented_with_ids(self):
def test_country_list_is_represented_with_classname_and_ids(self):
# Given
countries = CatalogList([test_country1, test_country2])

Expand Down

0 comments on commit 6a65a06

Please sign in to comment.