<s> import os , shutil <EOL> from unittest import TestCase <EOL> from tests import settings <EOL> from vilya import gitmodels <EOL> import yoyo <EOL> from yoyo . connections import connect <EOL> class VilyaTestCase ( TestCase ) : <EOL> def _init_git_env ( self ) : <EOL> if not os . path . isdir ( settings . GIT_REPO_ROOT ) : <EOL> os . mkdir ( settings . GIT_REPO_ROOT ) <EOL> if not os . path . isdir ( settings . GIT_TEMP_REPO_ROOT ) : <EOL> os . mkdir ( settings . GIT_TEMP_REPO_ROOT ) <EOL> def _clear_git_env ( self ) : <EOL> if os . path . isdir ( settings . GIT_REPO_ROOT ) : <EOL> shutil . rmtree ( settings . GIT_REPO_ROOT ) <EOL> if os . path . isdir ( settings . GIT_TEMP_REPO_ROOT ) : <EOL> shutil . rmtree ( settings . GIT_TEMP_REPO_ROOT ) <EOL> def setUp ( self ) : <EOL> super ( VilyaTestCase , self ) . setUp ( ) <EOL> self . _settings = settings <EOL> self . _init_git_env ( ) <EOL> def tearDown ( self ) : <EOL> self . _clear_git_env ( ) <EOL> super ( VilyaTestCase , self ) . tearDown ( ) <EOL> class VilyaAppTestCase ( VilyaTestCase ) : <EOL> def _create_app ( self , settings ) : <EOL> raise NotImplementedError <EOL> def _migrate_database ( self ) : <EOL> db_config = { <EOL> '<STR_LIT:host>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT:user>' : '<STR_LIT:root>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' } <EOL> db_config . update ( self . app . config [ '<STR_LIT>' ] ) <EOL> migrations_dir = self . app . config [ '<STR_LIT>' ] <EOL> if db_config [ '<STR_LIT>' ] : <EOL> db_config [ '<STR_LIT:user>' ] += '<STR_LIT::>' + db_config [ '<STR_LIT>' ] <EOL> if db_config . get ( '<STR_LIT:port>' ) : <EOL> db_config [ '<STR_LIT:host>' ] += '<STR_LIT::>' + db_config [ '<STR_LIT:port>' ] <EOL> db_connection_url = "<STR_LIT>" . format ( ** db_config ) <EOL> connection , paramstyle = connect ( db_connection_url ) <EOL> migrations = yoyo . read_migrations ( connection , paramstyle , migrations_dir ) <EOL> migrations . to_apply ( ) . apply ( ) <EOL> connection . commit ( ) <EOL> self . migrate_info = ( connection , paramstyle , migrations_dir ) <EOL> def _rollback_database ( self ) : <EOL> connection , paramstyle , migrations_dir = self . migrate_info <EOL> migrations = yoyo . read_migrations ( connection , paramstyle , migrations_dir ) <EOL> migrations . to_rollback ( ) . rollback ( ) <EOL> connection . commit ( ) <EOL> def _create_fixtures ( self ) : <EOL> pass <EOL> def setUp ( self ) : <EOL> super ( VilyaAppTestCase , self ) . setUp ( ) <EOL> self . app = self . _create_app ( settings ) <EOL> self . client = self . app . test_client ( ) <EOL> self . app_context = self . app . app_context ( ) <EOL> self . app_context . push ( ) <EOL> self . _migrate_database ( ) <EOL> self . _create_fixtures ( ) <EOL> def tearDown ( self ) : <EOL> self . _rollback_database ( ) <EOL> self . app_context . pop ( ) <EOL> super ( VilyaAppTestCase , self ) . tearDown ( ) </s>
