From e507ee097daf8eec6d98a93480822eb13a68a722 Mon Sep 17 00:00:00 2001 From: Stewart Douglas Date: Fri, 4 Sep 2015 14:55:11 -0400 Subject: [PATCH] TST: Add coverage for set_symbol_lookup_date method --- tests/test_algorithm.py | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 4e9735d0..fcc62335 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -40,6 +40,7 @@ from zipline.errors import ( AccountControlViolation, SymbolNotFound, RootSymbolNotFound, + UnsupportedDatetimeFormat, ) from zipline.test_algorithms import ( access_account_in_init, @@ -428,6 +429,55 @@ class TestMiscellaneousAPI(TestCase): with self.assertRaises(RootSymbolNotFound): algo.future_chain('') + # Check that invalid dates raise UnsupportedDatetimeFormat + with self.assertRaises(UnsupportedDatetimeFormat): + algo.future_chain('CL', 'my_finger_slipped') + + with self.assertRaises(UnsupportedDatetimeFormat): + algo.future_chain('CL', '2015-09-') + + def test_set_symbol_lookup_date(self): + """ + Test the set_symbol_lookup_date API method. + """ + # Note we start sid enumeration at i+3 so as not to + # collide with sids [1, 2] added in the setUp() method. + dates = pd.date_range('2013-01-01', freq='2D', periods=2, tz='UTC') + # Create two assets with the same symbol but different + # non-overlapping date ranges. + metadata = pd.DataFrame.from_records( + [ + { + 'sid': i + 3, + 'file_name': 'DUP', + 'start_date': date.value, + 'end_date': (date + timedelta(days=1)).value, + } + for i, date in enumerate(dates) + ] + ) + algo = TradingAlgorithm(asset_metadata=metadata) + # Set the period end to a date after the period end + # dates for our assets. + algo.sim_params.period_end = pd.Timestamp('2015-01-01', tz='UTC') + + # With no symbol lookup date set, we will use the period end date + # for the as_of_date, resulting here in the asset with the earlier + # start date being returned. + result = algo.symbol('DUP') + self.assertEqual(result.symbol, 'DUP') + + # By first calling set_symbol_lookup_date, the relevant asset + # should be returned by lookup_symbol_resolve_multiple + for i, date in enumerate(dates): + algo.set_symbol_lookup_date(date) + result = algo.symbol('DUP') + self.assertEqual(result.symbol, 'DUP') + self.assertEqual(result.sid, i + 3) + + with self.assertRaises(UnsupportedDatetimeFormat): + algo.set_symbol_lookup_date('foobar') + class TestTransformAlgorithm(TestCase): def setUp(self):