Improved test runtime from 352 seconds to 59 seconds by implementing an in-memory...
authorEmily O'Leary <Emma.C.Echo@gmail.com>
Wed, 26 Jun 2013 00:57:50 +0000 (20:57 -0400)
committerEmily O'Leary <Emma.C.Echo@gmail.com>
Wed, 26 Jun 2013 00:57:50 +0000 (20:57 -0400)
mediagoblin/app.py
mediagoblin/config_spec.ini
mediagoblin/gmg_commands/dbupdate.py
mediagoblin/init/__init__.py
mediagoblin/tests/appconfig_context_modified.ini
mediagoblin/tests/appconfig_static_plugin.ini
mediagoblin/tests/test_mgoblin_app.ini

index 580583607f32f2ca3bd7558993ec5900f469ebf2..3da166ab10b8e3b3616a443cbe9e41afa9fb9669 100644 (file)
@@ -86,7 +86,7 @@ class MediaGoblinApp(object):
         setup_plugins()
 
         # Set up the database
-        self.db = setup_database()
+        self.db = setup_database(app_config['run_migrations'])
 
         # Register themes
         self.theme_registry, self.current_theme = register_themes(app_config)
index 4547ea545c65d76432372103bba98e821ddf6075..6b94901bcaa2c50cdbe5f4d06956ea8bc702277d 100644 (file)
@@ -10,7 +10,8 @@ media_types = string_list(default=list("mediagoblin.media_types.image"))
 
 # database stuff
 sql_engine = string(default="sqlite:///%(here)s/mediagoblin.db")
-
+# Flag used during testing to determine if migrations should be run before the ORM is loaded
+run_migrations = boolean(default=False)
 # Where temporary files used in processing and etc are kept
 workbench_path = string(default="%(here)s/user_dev/media/workbench")
 
index fa25ecb2f605c457c6988549afb3ba98c5d0af5b..1d9bbf1e90f32f9e013de10f72d12ec8f3ece975 100644 (file)
@@ -110,14 +110,22 @@ def run_dbupdate(app_config, global_config):
     in the future, plugins)
     """
 
+    # Set up the database
+    db = setup_connection_and_db_from_config(app_config, migrations=True)
+    #Run the migrations
+    run_all_migrations(db, app_config, global_config)
+
+
+def run_all_migrations(db, app_config, global_config):
+    """
+    Moved the migration part of run_dbupdate to a separate function so
+    it can be used to initialize the database during tests.
+    """
     # Gather information from all media managers / projects
     dbdatas = gather_database_data(
             app_config['media_types'],
             global_config.get('plugins', {}).keys())
 
-    # Set up the database
-    db = setup_connection_and_db_from_config(app_config, migrations=True)
-
     Session = sessionmaker(bind=db.engine)
 
     # Setup media managers for all dbdata, run init/migrate and print info
index 444c624f934b4c2efc5e842184ea7dfd49b65ae5..e0711416378cbf4c1e5cf7ceb874297a544bffb3 100644 (file)
@@ -58,16 +58,20 @@ def setup_global_and_app_config(config_path):
     return global_config, app_config
 
 
-def setup_database():
+def setup_database(run_migrations=False):
     app_config = mg_globals.app_config
+    global_config = mg_globals.global_config
 
     # Load all models for media types (plugins, ...)
     load_models(app_config)
-
     # Set up the database
-    db = setup_connection_and_db_from_config(app_config)
-
-    check_db_migrations_current(db)
+    db = setup_connection_and_db_from_config(app_config, run_migrations)
+    if run_migrations:
+        #Run the migrations to initialize/update the database.
+        from mediagoblin.gmg_commands.dbupdate import run_all_migrations
+        run_all_migrations(db, app_config, global_config)
+    else:
+        check_db_migrations_current(db)
 
     setup_globals(database=db)
 
index 80ca69b11c752bd8bbaa2ecd3aa72203f20b589c..cc6721f5fb0d325e030b8c2f79742add08951df5 100644 (file)
@@ -3,8 +3,9 @@ direct_remote_path = /test_static/
 email_sender_address = "notice@mediagoblin.example.org"
 email_debug_mode = true
 
-# TODO: Switch to using an in-memory database
-sql_engine = "sqlite:///%(here)s/user_dev/mediagoblin.db"
+#Runs with an in-memory sqlite db for speed.
+sql_engine = "sqlite://"
+run_migrations = true
 
 # Celery shouldn't be set up by the application as it's setup via
 # mediagoblin.init.celery.from_celery
index dc251171499fb074a7330370b32c3d4e4ff4f698..5ce5c5bd129588ee9c298f44d9db3fbe635c89ec 100644 (file)
@@ -3,8 +3,9 @@ direct_remote_path = /test_static/
 email_sender_address = "notice@mediagoblin.example.org"
 email_debug_mode = true
 
-# TODO: Switch to using an in-memory database
-sql_engine = "sqlite:///%(here)s/user_dev/mediagoblin.db"
+#Runs with an in-memory sqlite db for speed.
+sql_engine = "sqlite://"
+run_migrations = true
 
 # Celery shouldn't be set up by the application as it's setup via
 # mediagoblin.init.celery.from_celery
index 0466b53b901d60c77e0a0ebc2c5a0f17a19a8749..78905e33bfbb557013ca045d10109c9e752d730e 100644 (file)
@@ -3,8 +3,9 @@ direct_remote_path = /test_static/
 email_sender_address = "notice@mediagoblin.example.org"
 email_debug_mode = true
 
-# TODO: Switch to using an in-memory database
-sql_engine = "sqlite:///%(here)s/user_dev/mediagoblin.db"
+#Runs with an in-memory sqlite db for speed.
+sql_engine = "sqlite://"
+run_migrations = true
 
 # tag parsing
 tags_max_length = 50