Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert_array_equal(df['b'].values, np.array([3., 5., 7., 9.]))
assert mongo_count(tickstore_lib._collection, filter=f.call_args_list[-1][0][0]) == 2
df = tickstore_lib.read('SYM', date_range=DateRange(20130103, 20130104), columns=None)
assert_array_equal(df['b'].values, np.array([5., 7.]))
assert mongo_count(tickstore_lib._collection, filter=f.call_args_list[-1][0][0]) == 2
df = tickstore_lib.read('SYM', date_range=DateRange(20130103, 20130105), columns=None)
assert_array_equal(df['b'].values, np.array([5., 7., 9.]))
assert mongo_count(tickstore_lib._collection, filter=f.call_args_list[-1][0][0]) == 2
df = tickstore_lib.read('SYM', date_range=DateRange(20130104, 20130105), columns=None)
assert_array_equal(df['b'].values, np.array([7., 9.]))
assert mongo_count(tickstore_lib._collection, filter=f.call_args_list[-1][0][0]) == 1
# Test the different open-closed behaviours
df = tickstore_lib.read('SYM', date_range=DateRange(20130104, 20130105, CLOSED_CLOSED), columns=None)
assert_array_equal(df['b'].values, np.array([7., 9.]))
df = tickstore_lib.read('SYM', date_range=DateRange(20130104, 20130105, CLOSED_OPEN), columns=None)
assert_array_equal(df['b'].values, np.array([7.]))
df = tickstore_lib.read('SYM', date_range=DateRange(20130104, 20130105, OPEN_CLOSED), columns=None)
assert_array_equal(df['b'].values, np.array([9.]))
df = tickstore_lib.read('SYM', date_range=DateRange(20130104, 20130105, OPEN_OPEN), columns=None)
assert_array_equal(df['b'].values, np.array([]))
def test_daterange_fails_with_timezone_start(library):
df = read_csv(StringIO("""2015-08-10 00:00:00,200005,1.0
2015-08-11 00:00:00,200016,3.0"""), parse_dates=[0],
names=['date', 'security_id', 'value']).set_index(['date', 'security_id'])
library.write('MYARR', df)
with pytest.raises(ValueError):
library.read('MYARR', date_range=DateRange(start=dt(2015, 1, 1, tzinfo=mktz())))
def test_raise_exception_if_date_range_does_not_contain_end_date():
store = TopLevelTickStore(Mock())
dr = DateRange(start=dt(2011, 1, 1), end=None)
with pytest.raises(Exception) as e:
store._get_library_metadata(dr)
assert "The date range {0} must contain a start and end date".format(dr) in str(e.value)
from datetime import datetime as dt
import operator
import pytest
import itertools
import six
from arctic.date import DateRange, string_to_daterange, CLOSED_CLOSED, CLOSED_OPEN, OPEN_CLOSED, OPEN_OPEN
test_ranges_for_bounding = {
"unbounded": (DateRange(),
None, None, True, None, None),
"unbounded_right": (DateRange('20110101'),
dt(2011, 1, 1), None, True, True, None),
"unbounded_left": (DateRange(None, '20111231'),
None, dt(2011, 12, 31), True, None, True),
"closed_by_default": (DateRange('20110101', '20111231'),
dt(2011, 1, 1), dt(2011, 12, 31), False, True, True),
"closed_explicitly": (DateRange('20110101', '20111231', CLOSED_CLOSED),
dt(2011, 1, 1), dt(2011, 12, 31), False, True, True),
"closed_open": (DateRange('20110101', '20111231', CLOSED_OPEN),
dt(2011, 1, 1), dt(2011, 12, 31), False, True, False),
"open_closed": (DateRange('20110101', '20111231', OPEN_CLOSED),
dt(2011, 1, 1), dt(2011, 12, 31), False, False, True),
"open_open": (DateRange('20110101', '20111231', OPEN_OPEN),
dt(2011, 1, 1), dt(2011, 12, 31), False, False, False),
}
test_ranges_for_bounding = sorted(six.iteritems(test_ranges_for_bounding), key=operator.itemgetter(1))
def eq_nan(*args):
def test_pandas_datetime_index_store_series(chunkstore_lib):
df = Series(data=[1, 2, 3],
index=Index(data=[dt(2016, 1, 1),
dt(2016, 1, 2),
dt(2016, 1, 3)],
name='date'),
name='data')
chunkstore_lib.write('chunkstore_test', df, chunk_size='D')
s = chunkstore_lib.read('chunkstore_test', chunk_range=DateRange(dt(2016, 1, 1), dt(2016, 1, 3)))
assert_series_equal(s, df)
def test_daterange_closedclosed():
date_range = DateRange(dt(2013, 1, 1, tzinfo=mktz('Europe/London')),
dt(2014, 2, 1, tzinfo=mktz('Europe/London')), OPEN_OPEN)
expected = DateRange(dt(2013, 1, 1, 0, 0, 0, 1000, tzinfo=mktz('Europe/London')),
dt(2014, 1, 31, 23, 59, 59, 999000, tzinfo=mktz('Europe/London')),
CLOSED_CLOSED)
act = to_pandas_closed_closed(date_range)
assert act == expected
)
# OPEN - CLOSED
assert_frame_equal(c.filter(df, DateRange(None, dt(2016, 1, 3))), df)
# CLOSED - OPEN
assert_frame_equal(c.filter(df, DateRange(dt(2016, 1, 1), None)), df)
# OPEN - OPEN
assert_frame_equal(c.filter(df, DateRange(None, None)), df)
# CLOSED - OPEN (far before data range)
assert_frame_equal(c.filter(df, DateRange(dt(2000, 1, 1), None)), df)
# CLOSED - OPEN (far after range)
assert(c.filter(df, DateRange(dt(2020, 1, 2), None)).empty)
# OPEN - CLOSED
assert_frame_equal(c.filter(df, DateRange(None, dt(2020, 1, 1))), df)
# CLOSED - CLOSED (after range)
assert(c.filter(df, DateRange(dt(2017, 1, 1), dt(2018, 1, 1))).empty)
(DateRange('20110101', '20110102', OPEN_CLOSED), {'$gt': dt(2011, 1, 1), '$lte': dt(2011, 1, 2)}),
(DateRange('20110101', '20110102', CLOSED_OPEN), {'$gte': dt(2011, 1, 1), '$lt': dt(2011, 1, 2)}),
(DateRange('20110101', '20110102'), {'$gte': dt(2011, 1, 1), '$lte': dt(2011, 1, 2)}),
(DateRange('20110101', None), {'$gte': dt(2011, 1, 1)}),
(DateRange(None, '20110102'), {'$lte': dt(2011, 1, 2)}),
(DateRange(), {})]
@pytest.mark.parametrize(['date_range', 'expected'], QUERY_TESTS)
def test_mongo_query(date_range, expected):
assert date_range.mongo_query() == expected
QUERY_TESTS_DB = [(DateRange('20110101', '20110102'), ('>=', dt(2011, 1, 1), '<=', dt(2011, 1, 2))),
(DateRange('20110101', '20110102', OPEN_OPEN), ('>', dt(2011, 1, 1), '<', dt(2011, 1, 2))),
(DateRange('20110101', '20110102', OPEN_CLOSED), ('>', dt(2011, 1, 1), '<=', dt(2011, 1, 2))),
(DateRange('20110101', '20110102', CLOSED_OPEN), ('>=', dt(2011, 1, 1), '<', dt(2011, 1, 2))),
(DateRange('20110101', '20110102'), ('>=', dt(2011, 1, 1), '<=', dt(2011, 1, 2))),
(DateRange('20110101', None), ('>=', dt(2011, 1, 1), '<=' , None)),
(DateRange(None, '20110102'), ('>=', None, '<=', dt(2011, 1, 2))),
(DateRange(), ('>=', None , '<=' , None))]
@pytest.mark.parametrize(['date_range', 'expected'], QUERY_TESTS_DB)
def test_get_date_bounds(date_range, expected):
assert date_range.get_date_bounds() == expected
@pytest.mark.parametrize(["dr"], [(DR1,), (DR2,), (DR3,), (DR4,), (DR5,), (DR6,), (DR7,)])
def test_intersection_with_self(dr):
assert dr == dr.intersection(dr)
def test_intersection_returns_inner_boundaries():
(DateRange('20110101', '20110102', CLOSED_OPEN), {'$gte': dt(2011, 1, 1), '$lt': dt(2011, 1, 2)}),
(DateRange('20110101', '20110102'), {'$gte': dt(2011, 1, 1), '$lte': dt(2011, 1, 2)}),
(DateRange('20110101', None), {'$gte': dt(2011, 1, 1)}),
(DateRange(None, '20110102'), {'$lte': dt(2011, 1, 2)}),
(DateRange(), {})]
@pytest.mark.parametrize(['date_range', 'expected'], QUERY_TESTS)
def test_mongo_query(date_range, expected):
assert date_range.mongo_query() == expected
QUERY_TESTS_DB = [(DateRange('20110101', '20110102'), ('>=', dt(2011, 1, 1), '<=', dt(2011, 1, 2))),
(DateRange('20110101', '20110102', OPEN_OPEN), ('>', dt(2011, 1, 1), '<', dt(2011, 1, 2))),
(DateRange('20110101', '20110102', OPEN_CLOSED), ('>', dt(2011, 1, 1), '<=', dt(2011, 1, 2))),
(DateRange('20110101', '20110102', CLOSED_OPEN), ('>=', dt(2011, 1, 1), '<', dt(2011, 1, 2))),
(DateRange('20110101', '20110102'), ('>=', dt(2011, 1, 1), '<=', dt(2011, 1, 2))),
(DateRange('20110101', None), ('>=', dt(2011, 1, 1), '<=' , None)),
(DateRange(None, '20110102'), ('>=', None, '<=', dt(2011, 1, 2))),
(DateRange(), ('>=', None , '<=' , None))]
@pytest.mark.parametrize(['date_range', 'expected'], QUERY_TESTS_DB)
def test_get_date_bounds(date_range, expected):
assert date_range.get_date_bounds() == expected
@pytest.mark.parametrize(["dr"], [(DR1,), (DR2,), (DR3,), (DR4,), (DR5,), (DR6,), (DR7,)])
def test_intersection_with_self(dr):
assert dr == dr.intersection(dr)
def test_intersection_returns_inner_boundaries():
# #start:
elif splice:
logger.warn("Symbol: %s already exists in destination, splicing in new data" % symbol)
else:
logger.warn("Symbol: {} already exists in {}@{}, use --force to overwrite or --splice to join "
"with existing data".format(symbol, _get_host(dest).get('l'),
_get_host(dest).get('mhost')))
continue
version = src.read(symbol)
new_data = version.data
if existing_data and splice:
original_data = dest.read(symbol).data
preserve_start = to_pandas_closed_closed(DateRange(None, new_data.index[0].to_pydatetime(),
interval=CLOSED_OPEN)).end
preserve_end = to_pandas_closed_closed(DateRange(new_data.index[-1].to_pydatetime(),
None,
interval=OPEN_CLOSED)).start
if not original_data.index.tz:
# No timezone on the original, should we even allow this?
preserve_start = preserve_start.replace(tzinfo=None)
preserve_end = preserve_end.replace(tzinfo=None)
before = original_data.loc[:preserve_start]
after = original_data[preserve_end:]
new_data = before.append(new_data).append(after)
mt.write(symbol, new_data, metadata=version.metadata)
return _copy_symbol