def test_prepare_variables_works_with_private_and_subscribed(self, get_mock, entity_repo, get_all_mock):
        dataset = Dataset({
            'id': 'id',
            'slug': 'slug',
            'name': 'name',
            'description': 'description',
            'available_in': ['bq'],
            'geography_id': 'geography',
            'is_public_data': False
        })

        # mock dataset
        entity_repo.return_value = dataset

        # mock subscriptions
        get_all_mock.return_value = [dataset]

        variable = Variable({
            'id': 'id',
            'column_name': 'column',
            'dataset_id': 'fake_name',
            'slug': 'slug'
        })

        get_mock.return_value = variable

        credentials = Credentials('fake_user', '1234')

        result = prepare_variables(variable, credentials)
        assert result == [variable]
    def test_prepare_variables_raises_if_not_available_in_bq(self, get_mock, entity_repo, get_all_mock):
        dataset = Dataset({
            'id': 'id',
            'slug': 'slug',
            'name': 'name',
            'description': 'description',
            'available_in': [],
            'geography_id': 'geography',
            'is_public_data': False
        })

        # mock dataset
        entity_repo.return_value = dataset

        # mock subscriptions
        get_all_mock.return_value = [dataset]

        variable = Variable({
            'id': 'id',
            'column_name': 'column',
            'dataset_id': 'fake_name',
            'slug': 'slug'
        })

        get_mock.return_value = variable

        credentials = Credentials('fake_user', '1234')

        with pytest.raises(EnrichmentError) as e:
            prepare_variables(variable, credentials)

        error = """
            The Dataset '{}' is not ready for Enrichment. Please, contact us for more information.
        """.format(dataset)
        assert str(e.value) == error
    def test_prepare_variables_fails_with_private(self, get_mock, entity_repo, get_all_mock):
        dataset = Dataset({
            'id': 'id',
            'slug': 'slug',
            'name': 'name',
            'description': 'description',
            'available_in': ['bq'],
            'geography_id': 'geography',
            'is_public_data': False
        })

        # mock dataset
        entity_repo.return_value = dataset

        # mock subscriptions
        get_all_mock.return_value = []

        variable = Variable({
            'id': 'id',
            'column_name': 'column',
            'dataset_id': 'fake_name',
            'slug': 'slug'
        })

        get_mock.return_value = variable

        credentials = Credentials('fake_user', '1234')

        with pytest.raises(EnrichmentError) as e:
            prepare_variables(variable, credentials)

        error = """
            You are not subscribed to the Dataset '{}' yet. Please, use the subscribe method first.
        """.format(dataset.id)
        assert str(e.value) == error
示例#4
0
import os
import json
import pandas
import pytest
from pathlib import Path

from cartoframes.auth import Credentials
from cartoframes.data.observatory import Dataset, Geography


def file_path(path):
    return '{}/{}'.format(Path(__file__).parent.absolute(), path)


public = 'carto-do-public-data.usa_acs.demographics_sociodemographics_usa_censustract_2015_5yrs_20132017'
public_dataset = Dataset.get(public)
public_geography = Geography.get(public_dataset.geography)

private = 'carto-do.ags.demographics_retailpotential_usa_blockgroup_2015_yearly_2019'
private_dataset = Dataset.get(private)
private_geography = Geography.get(private_dataset.geography)

PUBLIC_LIMIT = 2
PRIVATE_LIMIT = 1


def clean_df(df):
    return df.sort_index(axis=1).round(5).reset_index(drop=True)


class TestDownload(object):