#NEWLINE# @ task #NEWLINE# @ cmdopts ( [ ( 'url=' , 'u' , 'pycsw endpoint' ) , ( 'suites=' , 's' , 'comma-separated list of testsuites' ) , ( 'database=' , 'd' , 'database (SQLite3 [default], PostgreSQL, MySQL)' ) , ( 'user=' , 'U' , 'database username' ) , ( 'pass=' , 'p' , 'database password' ) , ( 'remote' , 'r' , 'remote testing (harvesting)' ) ] ) #NEWLINE# def test( options ) : #NEWLINE# #INDENT# 'Run unit tests' #NEWLINE# db_setup = False #NEWLINE# db_conn = None #NEWLINE# cfg_files = [ ] #NEWLINE# status = 0 #NEWLINE# url = options . get ( 'url' , None ) #NEWLINE# suites = options . get ( 'suites' , None ) #NEWLINE# database = options . get ( 'database' , 'SQLite3' ) #NEWLINE# remote = options . get ( 'remote' ) #NEWLINE# if ( url is None ) : #NEWLINE# #INDENT# call_task ( 'stop' ) #NEWLINE# call_task ( 'reset' ) #NEWLINE# if ( database == 'SQLite3' ) : #NEWLINE# #INDENT# call_task ( 'setup_testdata' ) #NEWLINE# #UNINDENT# call_task ( 'start' ) #NEWLINE# url = 'http://localhost:8000' #NEWLINE# #UNINDENT# if ( suites is not None ) : #NEWLINE# #INDENT# cmd = ( 'python run_tests.py -u %s -s %s' % ( url , suites ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# cmd = ( 'python run_tests.py -u %s' % url ) #NEWLINE# #UNINDENT# if remote : #NEWLINE# #INDENT# cmd = ( '%s -r' % cmd ) #NEWLINE# #UNINDENT# if ( database != 'SQLite3' ) : #NEWLINE# #INDENT# db_setup = True #NEWLINE# temp_db = ( 'pycsw_ci_test_pid_%d' % os . getpid ( ) ) #NEWLINE# if ( database == 'PostgreSQL' ) : #NEWLINE# #INDENT# from pycsw.admin import setup_db , load_records #NEWLINE# from pycsw.config import StaticContext #NEWLINE# cmd = ( '%s -d %s' % ( cmd , database ) ) #NEWLINE# init_sfsql = True #NEWLINE# home = os . path . abspath ( os . path . dirname ( __file__ ) ) #NEWLINE# user = options . get ( 'user' , 'postgres' ) #NEWLINE# password = options . get ( 'pass' , '' ) #NEWLINE# context = StaticContext ( ) #NEWLINE# db_conn = ( 'postgresql://%s:%s@localhost/%s' % ( user , password , temp_db ) ) #NEWLINE# if password : #NEWLINE# #INDENT# sh ( ( 'set PGPASSWORD=%s' % password ) ) #NEWLINE# #UNINDENT# sh ( ( 'createdb %s -U %s' % ( temp_db , user ) ) ) #NEWLINE# sh ( ( 'createlang --dbname=%s plpythonu -U %s' % ( temp_db , user ) ) ) #NEWLINE# cfg_files = glob . glob ( ( 'tests%ssuites%s*%s*.cfg' % ( 3 * ( os . sep , ) ) ) ) #NEWLINE# for cfg in cfg_files : #NEWLINE# #INDENT# suite = cfg . split ( os . sep ) [ 2 ] #NEWLINE# tablename = 'records_cite' #NEWLINE# if ( suite == 'manager' ) : #NEWLINE# #INDENT# tablename = 'records_manager' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( suite == 'apiso' ) : #NEWLINE# #INDENT# tablename = 'records_apiso' #NEWLINE# #UNINDENT# #UNINDENT# config = configparser . SafeConfigParser ( ) #NEWLINE# with open ( cfg ) as read_data : #NEWLINE# #INDENT# config . readfp ( read_data ) #NEWLINE# #UNINDENT# config . set ( 'repository' , 'database' , db_conn ) #NEWLINE# config . set ( 'repository' , 'table' , tablename ) #NEWLINE# with open ( cfg , 'wb' ) as config2 : #NEWLINE# #INDENT# config . write ( config2 ) #NEWLINE# #UNINDENT# if ( suite in [ 'cite' , 'manager' , 'apiso' ] ) : #NEWLINE# #INDENT# setup_db ( db_conn , tablename , home , init_sfsql , init_sfsql ) #NEWLINE# init_sfsql = False #NEWLINE# #UNINDENT# if ( suite in [ 'cite' , 'apiso' ] ) : #NEWLINE# #INDENT# dirname = ( '%s%sdata' % ( os . path . dirname ( cfg ) , os . sep ) ) #NEWLINE# load_records ( context , db_conn , tablename , dirname ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( 'Invalid database specified' ) #NEWLINE# #UNINDENT# #UNINDENT# with pushd ( 'tests' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# sh ( cmd ) #NEWLINE# #UNINDENT# except BuildFailure as err : #NEWLINE# #INDENT# status = 1 #NEWLINE# #UNINDENT# call_task ( 'stop' ) #NEWLINE# #UNINDENT# if db_setup : #NEWLINE# #INDENT# for cfg in cfg_files : #NEWLINE# #INDENT# sh ( ( 'git checkout %s' % cfg ) ) #NEWLINE# #UNINDENT# if ( database == 'PostgreSQL' ) : #NEWLINE# #INDENT# sh ( ( 'psql -c "select pg_terminate_backend(procpid) from pg_stat_activity where datname=\'%s\';" -U %s' % ( temp_db , user ) ) ) #NEWLINE# sh ( ( 'dropdb %s -U %s' % ( temp_db , user ) ) ) #NEWLINE# sh ( 'unset PGPASSWORD' ) #NEWLINE# #UNINDENT# #UNINDENT# sys . exit ( status )