From 00879245a4a7b144938d6be6f05529b7acdde9b4 Mon Sep 17 00:00:00 2001 From: jfkirk Date: Wed, 30 Dec 2015 11:48:38 -0500 Subject: [PATCH] BUG: Adds version checking to AssetFinder init --- tests/test_assets.py | 21 +++++++++++++++++++++ zipline/assets/assets.py | 9 ++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/test_assets.py b/tests/test_assets.py index ba939084..11b92d31 100644 --- a/tests/test_assets.py +++ b/tests/test_assets.py @@ -1333,3 +1333,24 @@ class TestAssetDBVersioning(TestCase): # Assert that trying to overwrite the version fails with self.assertRaises(sa.exc.IntegrityError): write_version_info(version_table, -3) + + def test_finder_checks_version(self): + # Create an env and give it a bogus version number + env = TradingEnvironment(load=noop_load) + metadata = sa.MetaData(bind=env.engine) + version_table = _version_table_schema(metadata) + version_table.delete().execute() + write_version_info(version_table, -2) + check_version_info(version_table, -2) + + # Assert that trying to build a finder with a bad db raises an error + with self.assertRaises(AssetDBVersionError): + AssetFinder(engine=env.engine) + + # Change the version number of the db to the correct version + version_table.delete().execute() + write_version_info(version_table, ASSET_DB_VERSION) + check_version_info(version_table, ASSET_DB_VERSION) + + # Now that the versions match, this Finder should succeed + AssetFinder(engine=env.engine) diff --git a/zipline/assets/assets.py b/zipline/assets/assets.py index 01f350cb..13b457b4 100644 --- a/zipline/assets/assets.py +++ b/zipline/assets/assets.py @@ -37,9 +37,13 @@ from zipline.assets import ( Asset, Equity, Future, ) from zipline.assets.asset_writer import ( + check_version_info, split_delimited_symbol, asset_db_table_names, - SQLITE_MAX_VARIABLE_NUMBER + SQLITE_MAX_VARIABLE_NUMBER, +) +from zipline.assets.asset_db_schema import ( + ASSET_DB_VERSION ) from zipline.utils.control_flow import invert @@ -104,6 +108,9 @@ class AssetFinder(object): for table_name in asset_db_table_names: setattr(self, table_name, metadata.tables[table_name]) + # Check the version info of the db for compatibility + check_version_info(self.version_info, ASSET_DB_VERSION) + # Cache for lookup of assets by sid, the objects in the asset lookup # may be shared with the results from equity and future lookup caches. #