Ejemplo n.º 1
0
 def setUp(self):
     datatable = {
         'datatable':
         DatatableFactory.build(vendor_code='AUSBS', datatable_code='D')
     }
     self.datatable = Datatable(
         datatable['datatable']['vendor_code'] + '/' +
         datatable['datatable']['datatable_code'], datatable['datatable'])
     ApiConfig.api_key = 'api_token'
     ApiConfig.api_version = '2015-04-09'
Ejemplo n.º 2
0
    def test_datatable_calls_connection_with_params_for_get_request(
            self, mock):
        params = {
            'ticker': ['AAPL', 'MSFT'],
            'per_end_date': {
                'gte': '2015-01-01'
            },
            'qopts': {
                'columns': ['ticker', 'per_end_date']
            },
            'foo': 'bar',
            'baz': 4
        }

        expected_params = {
            'ticker[]': ['AAPL', 'MSFT'],
            'per_end_date.gte': '2015-01-01',
            'qopts.columns[]': ['ticker', 'per_end_date'],
            'foo': 'bar',
            'baz': 4
        }

        Datatable('ZACKS/FC').data(params=params)
        expected = call('get', 'datatables/ZACKS/FC', params=expected_params)
        self.assertEqual(mock.call_args, expected)
Ejemplo n.º 3
0
    def test_datatable_calls_connection_with_params_for_post_request(
            self, mock):
        RequestType.USE_GET_REQUEST = False
        params = {
            'ticker': ['AAPL', 'MSFT'],
            'per_end_date': {
                'gte': '2015-01-01'
            },
            'qopts': {
                'columns': ['ticker', 'per_end_date']
            },
            'foo': 'bar',
            'baz': 4
        }

        expected_params = {
            'ticker': ['AAPL', 'MSFT'],
            'per_end_date.gte': '2015-01-01',
            'qopts.columns': ['ticker', 'per_end_date'],
            'foo': 'bar',
            'baz': 4
        }

        Datatable('ZACKS/FC').data(params=params)
        expected = call('post', 'datatables/ZACKS/FC', json=expected_params)
        self.assertEqual(mock.call_args, expected)
 def test_pandas_dataframe_index_is_none(self, request_method):
     if request_method == 'POST':
         RequestType.USE_GET_REQUEST = False
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     df = results.to_pandas()
     self.assertEqual(df.index.name, 'None')
Ejemplo n.º 5
0
def get_table(datatable_code, **options):
    if 'paginate' in options.keys():
        paginate = options.pop('paginate')
    else:
        paginate = None

    data = None
    page_count = 0
    while True:
        next_options = copy.deepcopy(options)
        next_data = Datatable(datatable_code).data(params=next_options)

        if data is None:
            data = next_data
        else:
            data.extend(next_data)

        if page_count >= ApiConfig.page_limit:
            raise LimitExceededError(Message.WARN_DATA_LIMIT_EXCEEDED)

        next_cursor_id = next_data.meta['next_cursor_id']

        if next_cursor_id is None:
            break
        elif paginate is not True and next_cursor_id is not None:
            warnings.warn(Message.WARN_PAGE_LIMIT_EXCEEDED, UserWarning)
            break

        page_count = page_count + 1
        options['qopts.cursor_id'] = next_cursor_id
    return data.to_pandas()
 def test_to_numpy_returns_numpy_object(self, request_method):
     if request_method == 'POST':
         RequestType.USE_GET_REQUEST = False
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     data = results.to_numpy()
     self.assertIsInstance(data, numpy.core.records.recarray)
 def test_to_pandas_returns_pandas_dataframe_object(self, request_method):
     if request_method == 'POST':
         RequestType.USE_GET_REQUEST = False
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     df = results.to_pandas()
     self.assertIsInstance(df, pandas.core.frame.DataFrame)
 def test_values_and_meta_exist(self, request_method):
     if request_method == 'POST':
         RequestType.USE_GET_REQUEST = False
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     self.assertIsNotNone(results.values)
     self.assertIsNotNone(results.meta)
Ejemplo n.º 9
0
def get_table(datatable_code, **options):
    """Similar to quandl.get(), except this function returns a pandas DataFrame object.
    See the quandl.get docstring for information about the **options."""
    if 'paginate' in options.keys():
        paginate = options.pop('paginate')
    else:
        paginate = None

    data = None
    page_count = 0
    while True:
        next_options = copy.deepcopy(options)
        next_data = Datatable(datatable_code).data(params=next_options)

        if data is None:
            data = next_data
        else:
            data.extend(next_data)

        if page_count >= ApiConfig.page_limit:
            raise LimitExceededError(Message.WARN_DATA_LIMIT_EXCEEDED %
                                     (datatable_code, ApiConfig.api_key))

        next_cursor_id = next_data.meta['next_cursor_id']

        if next_cursor_id is None:
            break
        elif paginate is not True and next_cursor_id is not None:
            warnings.warn(Message.WARN_PAGE_LIMIT_EXCEEDED, UserWarning)
            break

        page_count = page_count + 1
        options['qopts.cursor_id'] = next_cursor_id
    return data.to_pandas()
 def test_pandas_dataframe_date_field_is_datetime(self):
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     df = results.to_pandas()
     self.assertIsInstance(df['per_end_date'][0], pandas.datetime)
     self.assertIsInstance(df['per_end_date'][1], pandas.datetime)
     self.assertIsInstance(df['per_end_date'][2], pandas.datetime)
     self.assertIsInstance(df['per_end_date'][3], pandas.datetime)
 def test_pandas_dataframe_date_field_is_datetime(self, request_method):
     if request_method == 'POST':
         RequestType.USE_GET_REQUEST = False
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     df = results.to_pandas()
     self.assertIsInstance(df['per_end_date'][0], pandas.datetime)
     self.assertIsInstance(df['per_end_date'][1], pandas.datetime)
     self.assertIsInstance(df['per_end_date'][2], pandas.datetime)
     self.assertIsInstance(df['per_end_date'][3], pandas.datetime)
 def test_to_csv_returns_expected_csv(self):
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     data = results.to_csv()
     expected = "None,per_end_date,ticker,tot_oper_exp\n" + \
                "0,2015-07-11,AAPL,456.9\n" + \
                "1,2015-07-13,433.3,\n" + \
                "2,2015-07-14,AAPL,419.1\n" + \
                "3,2015-07-15,476.5,\n"
     self.assertEqual(data, expected)
Ejemplo n.º 13
0
 def setUpClass(cls):
     httpretty.enable()
     datatable = {
         'datatable':
         DatatableFactory.build(vendor_code='AUSBS', datatable_code='D')
     }
     httpretty.register_uri(
         httpretty.GET,
         re.compile('https://www.quandl.com/api/v3/datatables/*'),
         body=json.dumps(datatable))
     cls.datatable_instance = Datatable(datatable['datatable'])
 def test_to_csv_returns_expected_csv(self, request_method):
     if request_method == 'POST':
         RequestType.USE_GET_REQUEST = False
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     data = results.to_csv()
     expected = "None,per_end_date,ticker,tot_oper_exp\n" + \
                "0,2015-07-11,AAPL,456.9\n" + \
                "1,2015-07-13,433.3,\n" + \
                "2,2015-07-14,AAPL,419.1\n" + \
                "3,2015-07-15,476.5,\n"
     self.assertEqual(data, expected)
Ejemplo n.º 15
0
 def setUpClass(cls):
     httpretty.enable()
     datatable = {
         'datatable':
         DatatableFactory.build(vendor_code='ZACKS', datatable_code='FC')
     }
     data = DatatableDataFactory.build()
     datatable['datatable'].update(data)
     meta = {'meta': DatatableMetaFactory.build()}
     datatable.update(meta)
     httpretty.register_uri(
         httpretty.GET,
         re.compile('https://www.quandl.com/api/v3/datatables*'),
         body=json.dumps(datatable))
     cls.datatable_instance = Datatable(datatable['datatable'])
 def test_data_calls_connection_get(self, mock):
     datatable = Datatable('ZACKS/FC')
     Data.page(datatable,
               params={
                   'ticker': ['AAPL', 'MSFT'],
                   'per_end_date': {
                       'gte': '2015-01-01'
                   },
                   'qopts': {
                       'columns': ['ticker', 'per_end_date']
                   }
               })
     expected = call('get',
                     'datatables/ZACKS/FC',
                     params={
                         'ticker[]': ['AAPL', 'MSFT'],
                         'per_end_date.gte': '2015-01-01',
                         'qopts.columns[]': ['ticker', 'per_end_date']
                     })
     self.assertEqual(mock.call_args, expected)
 def test_data_calls_connection_post(self, mock):
     RequestType.USE_GET_REQUEST = False
     datatable = Datatable('ZACKS/FC')
     Data.page(datatable,
               params={
                   'ticker': ['AAPL', 'MSFT'],
                   'per_end_date': {
                       'gte': '2015-01-01'
                   },
                   'qopts': {
                       'columns': ['ticker', 'per_end_date']
                   }
               })
     expected = call('post',
                     'datatables/ZACKS/FC',
                     json={
                         'ticker': ['AAPL', 'MSFT'],
                         'per_end_date.gte': '2015-01-01',
                         'qopts.columns': ['ticker', 'per_end_date']
                     })
     self.assertEqual(mock.call_args, expected)
 def test_to_numpy_returns_numpy_object(self):
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     data = results.to_numpy()
     self.assertIsInstance(data, numpy.core.records.recarray)
 def test_values_and_meta_exist(self):
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     self.assertIsNotNone(results.values)
     self.assertIsNotNone(results.meta)
 def test_to_pandas_returns_pandas_dataframe_object(self):
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     df = results.to_pandas()
     self.assertIsInstance(df, pandas.core.frame.DataFrame)
Ejemplo n.º 21
0
class ExportDataTableTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        httpretty.enable()
        datatable = {
            'datatable':
            DatatableFactory.build(vendor_code='AUSBS', datatable_code='D')
        }
        httpretty.register_uri(
            httpretty.GET,
            re.compile('https://www.quandl.com/api/v3/datatables/*'),
            body=json.dumps(datatable))
        cls.datatable_instance = Datatable(datatable['datatable'])

    @classmethod
    def tearDownClass(cls):
        httpretty.disable()
        httpretty.reset()

    def setUp(self):
        datatable = {
            'datatable':
            DatatableFactory.build(vendor_code='AUSBS', datatable_code='D')
        }
        self.datatable = Datatable(
            datatable['datatable']['vendor_code'] + '/' +
            datatable['datatable']['datatable_code'], datatable['datatable'])
        ApiConfig.api_key = 'api_token'
        ApiConfig.api_version = '2015-04-09'

    def test_download_get_file_info(self):
        url = self.datatable._download_request_path()
        parsed_url = urlparse(url)
        self.assertEqual(parsed_url.path, 'datatables/AUSBS/D.json')

    def test_download_generated_file(self):
        m = mock_open()

        httpretty.register_uri(
            httpretty.GET,
            re.compile('https://www.quandl.com/api/v3/datatables/*'),
            body=json.dumps({
                'datatable_bulk_download': {
                    'file': {
                        'status': 'fresh',
                        'link': 'https://www.blah.com/download/db.zip'
                    }
                }
            }),
            status=200)

        with patch('quandl.model.datatable.urlopen', m, create=True):
            self.datatable.download_file('.')

        self.assertEqual(m.call_count, 1)

    def test_bulk_download_raises_exception_when_no_path(self):
        self.assertRaises(QuandlError,
                          lambda: self.datatable.download_file(None))

    def test_bulk_download_table_raises_exception_when_error_response(self):
        httpretty.register_uri(
            httpretty.GET,
            re.compile('https://www.quandl.com/api/v3/datatables/*'),
            body=json.dumps({
                'quandl_error': {
                    'code': 'QEMx01',
                    'message': 'something went wrong'
                }
            }),
            status=500)
        self.assertRaises(InternalServerError,
                          lambda: self.datatable.download_file('.'))
Ejemplo n.º 22
0
 def test_dataset_column_names_match_expected(self):
     metadata = Datatable('ZACKS/FC').data_fields()
     six.assertCountEqual(
         self, metadata,
         [u'datatable_code', u'id', u'name', u'vendor_code'])
Ejemplo n.º 23
0
 def test_datatable_returns_datatable_object(self):
     datatable = Datatable('ZACKS/FC')
     self.assertIsInstance(datatable, Datatable)
     self.assertEqual(datatable.vendor_code, 'ZACKS')
     self.assertEqual(datatable.datatable_code, 'FC')
Ejemplo n.º 24
0
 def test_datatable_data_calls_connection(self, mock):
     Datatable('ZACKS/FC').data()
     expected = call('get', 'datatables/ZACKS/FC', params={})
     self.assertEqual(mock.call_args, expected)
 def test_pandas_dataframe_index_is_none(self):
     datatable = Datatable('ZACKS/FC')
     results = Data.page(datatable, params={})
     df = results.to_pandas()
     self.assertEqual(df.index.name, 'None')
Ejemplo n.º 26
0
 def test_datatable_data_calls_connection_with_no_params_for_post_request(
         self, mock):
     RequestType.USE_GET_REQUEST = False
     Datatable('ZACKS/FC').data()
     expected = call('post', 'datatables/ZACKS/FC', json={})
     self.assertEqual(mock.call_args, expected)