def test_nan_insertion(self): '''when the external data source returns a subset of the requested dates NaNs are inserted into the database for the dates not returned. ''' connection = SQLiteTimeseries.connect(':memory:') driver = SQLiteTimeseries(connection=connection, table='price', metric='Adj Close') symbol = 'MSFT' missing_date = datetime.datetime(2012, 12, 4, tzinfo=pytz.UTC) missing_dates = {missing_date} returned_dates = { datetime.datetime(2012, 12, 1, tzinfo=pytz.UTC), datetime.datetime(2012, 12, 2, tzinfo=pytz.UTC), datetime.datetime(2012, 12, 3, tzinfo=pytz.UTC), } requested_dates = missing_dates | returned_dates mock_yahoo = mock.Mock() mock_yahoo.return_value = ((date, 10.) for date in returned_dates) cache = FinancialDataTimeSeriesCache(gets_data=mock_yahoo, database=driver) cached_values = list( cache.get(symbol=symbol, dates=list(requested_dates))) db_val = connection.execute( "SELECT value FROM price WHERE date = '{}'".format( missing_date)).next() self.assertEqual(db_val['value'], 'NaN') cache_value_dict = {date: value for date, value in cached_values} assert np.isnan(cache_value_dict[missing_date])
def build_sqlite_price_cache(cls, sqlite_file_path, table='prices', metric='Adj Close'): connection = SQLiteTimeseries.connect(sqlite_file_path) db = SQLiteTimeseries(connection=connection, table=table, metric=metric) cache = cls(gets_data=prices.get_prices_from_yahoo, database=db) return cache
def test_nan_insertion(self): '''when the external data source returns a subset of the requested dates NaNs are inserted into the database for the dates not returned. ''' connection = SQLiteTimeseries.connect(':memory:') driver = SQLiteTimeseries(connection=connection, table='price', metric='Adj Close') symbol = 'MSFT' missing_date = datetime.datetime(2012, 12, 4, tzinfo=pytz.UTC) missing_dates = {missing_date} returned_dates = {datetime.datetime(2012, 12, 1, tzinfo=pytz.UTC), datetime.datetime(2012, 12, 2, tzinfo=pytz.UTC), datetime.datetime(2012, 12, 3, tzinfo=pytz.UTC), } requested_dates = missing_dates | returned_dates mock_yahoo = mock.Mock() mock_yahoo.return_value = ((date, 10.) for date in returned_dates) cache = FinancialDataTimeSeriesCache(gets_data=mock_yahoo, database=driver) cached_values = list(cache.get(symbol=symbol, dates=list(requested_dates))) db_val = connection.execute("SELECT value FROM price WHERE date = '{}'".format(missing_date)).next() self.assertEqual(db_val['value'], 'NaN') cache_value_dict = {date : value for date, value in cached_values} assert np.isnan(cache_value_dict[missing_date])