]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Delete DB records instead of tables to speedup UT
authorKevin Benton <blak111@gmail.com>
Wed, 17 Sep 2014 00:29:51 +0000 (17:29 -0700)
committerKevin Benton <blak111@gmail.com>
Wed, 17 Sep 2014 16:05:49 +0000 (09:05 -0700)
Now that the schema is fixed for all of the plugins,
there isn't a need to delete and recreate the entire
schema for every unit test.

This patch clears the tables at the end of each test
instead of deleting them. This eliminated overhead seems
to save 10%+ execution time of the entire set of unit
tests.

Example of performance gain from tox -epy27 tests.unit.ml2:
Before: Ran 2495 tests in 284.186s
After: Ran 2495 tests in 223.299s

Change-Id: Ic5bcbb0cf941e0745890abc776d719e58bb42e35

neutron/tests/unit/testlib_api.py

index a8c7a7e6edbf765cd553c356feb07ac7df935a82..564e0b501f339151b31bf5915e845143bd11465d 100644 (file)
@@ -55,17 +55,24 @@ def create_request(path, body, content_type, method='GET',
 
 class SqlTestCase(base.BaseTestCase):
 
+    # flag to indicate that the models have been loaded
+    _TABLES_ESTABLISHED = False
+
     def setUp(self):
         super(SqlTestCase, self).setUp()
         # Register all data models
         engine = db_api.get_engine()
-        model_base.BASEV2.metadata.create_all(engine)
+        if not SqlTestCase._TABLES_ESTABLISHED:
+            model_base.BASEV2.metadata.create_all(engine)
+            SqlTestCase._TABLES_ESTABLISHED = True
 
-        def unregister_models():
-            """Unregister all data models."""
-            model_base.BASEV2.metadata.drop_all(engine)
+        def clear_tables():
+            with engine.begin() as conn:
+                for table in reversed(
+                        model_base.BASEV2.metadata.sorted_tables):
+                    conn.execute(table.delete())
 
-        self.addCleanup(unregister_models)
+        self.addCleanup(clear_tables)
 
 
 class WebTestCase(SqlTestCase):