def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(PostgresTableCreationSQLTest)) suite.addTest(unittest.makeSuite(PostgresTableAlterationSQLTest)) if get_dburi().startswith('postgres:'): suite.addTest(unittest.makeSuite(PostgresConnectionTestCase)) return suite
def test_migrate_with_plugin_egg_to_sqlite_env(self): dburi = get_dburi() if dburi == 'sqlite::memory:': dburi = 'sqlite:db/trac.db' self._create_env(self.src_path, dburi) self._extract_zipfile(self._build_egg_file(), os.path.join(self.src_path, 'plugins', 'tracmigratetest.egg')) self._test_migrate_with_plugin_to_sqlite_env()
def test_convert_with_plugin_egg_to_sqlite_env(self): dburi = get_dburi() if dburi == 'sqlite::memory:': dburi = 'sqlite:db/trac.db' self._create_env(self.src_path, dburi) extract_zipfile( self._build_egg_file(), os.path.join(self.src_path, 'plugins', 'trac_convert_db_test.egg')) self._test_convert_with_plugin_to_sqlite_env()
def test_migrate_with_plugin_py_to_sqlite_env(self): dburi = get_dburi() if dburi == 'sqlite::memory:': dburi = 'sqlite:db/trac.db' self._create_env(self.src_path, dburi) plugin_name = self._generate_module_name() + '.py' create_file(os.path.join(self.src_path, 'plugins', plugin_name), _plugin_py) self._test_migrate_with_plugin_to_sqlite_env()
def test_convert_with_plugin_py_to_sqlite_env(self): dburi = get_dburi() if dburi == 'sqlite::memory:': dburi = 'sqlite:db/trac.db' self._create_env(self.src_path, dburi) plugin_name = self._generate_module_name() + '.py' create_file(os.path.join(self.src_path, 'plugins', plugin_name), _plugin_py) self._test_convert_with_plugin_to_sqlite_env()
def test_destroy_db(self): """Database doesn't exist after calling destroy_db.""" self.env.db_query("SELECT name FROM system") self.assertIsNotNone(self.dbm._cnx_pool) self.dbm.destroy_db() self.assertIsNone(self.dbm._cnx_pool) # No connection pool scheme, params = parse_connection_uri(get_dburi()) if scheme != 'postgres' or params.get('schema', 'public') != 'public': self.assertFalse(self.dbm.db_exists()) else: self.assertEqual([], self.dbm.get_table_names())
def test_migrate_to_sqlite_inplace(self): dburi = get_dburi() if dburi in ('sqlite::memory:', 'sqlite:db/trac.db'): dburi = 'sqlite:db/trac-migrate.db' self._create_env(self.src_path, dburi) self.src_env = Environment(self.src_path) src_options = self._get_options(self.src_env) src_records = self._get_all_records(self.src_env) self._migrate_inplace(self.src_env, 'sqlite:db/trac.db') self.src_env.shutdown() self.src_env = Environment(self.src_path) dst_options = self._get_options(self.src_env) dst_records = self._get_all_records(self.src_env) self.assertEqual({'name': 'initial_database_version', 'value': '21'}, dst_records['system']['initial_database_version']) self._compare_records(src_records, dst_records) self.assertEqual(src_options, dst_options)
def test_convert_to_sqlite_inplace(self): dburi = get_dburi() if dburi in ('sqlite::memory:', 'sqlite:db/trac.db'): dburi = 'sqlite:db/trac-convert.db' self._create_env(self.src_path, dburi) self.src_env = Environment(self.src_path) src_options = self._get_options(self.src_env) src_records = self._get_all_records(self.src_env) self._convert_db_inplace(self.src_env, 'sqlite:db/trac.db') self.src_env.shutdown() self.src_env = Environment(self.src_path) dst_options = self._get_options(self.src_env) dst_records = self._get_all_records(self.src_env) self.assertEqual({'name': 'initial_database_version', 'value': '21'}, dst_records['system']['initial_database_version']) self._compare_records(src_records, dst_records) self.assertEqual(src_options, dst_options)
def test_migrate_to_sqlite_env(self): dburi = get_dburi() if dburi == 'sqlite::memory:': dburi = 'sqlite:db/trac.db' self._create_env(self.src_path, dburi) self.src_env = Environment(self.src_path) src_options = self._get_options(self.src_env) src_records = self._get_all_records(self.src_env) self._migrate(self.src_env, self.dst_path, 'sqlite:db/trac.db') self.dst_env = Environment(self.dst_path) dst_options = self._get_options(self.dst_env) dst_records = self._get_all_records(self.dst_env) self.assertEqual({'name': 'initial_database_version', 'value': '21'}, dst_records['system']['initial_database_version']) self._compare_records(src_records, dst_records) self.assertEqual(src_options, dst_options) att = Attachment(self.dst_env, 'wiki', 'WikiStart', 'filename.txt') self.assertEqual('test', read_file(att.path))
def test_convert_to_sqlite_env(self): dburi = get_dburi() if dburi == 'sqlite::memory:': dburi = 'sqlite:db/trac.db' self._create_env(self.src_path, dburi) self.src_env = Environment(self.src_path) src_options = self._get_options(self.src_env) src_records = self._get_all_records(self.src_env) self._convert_db(self.src_env, 'sqlite:db/trac.db', self.dst_path) self.dst_env = Environment(self.dst_path) dst_options = self._get_options(self.dst_env) dst_records = self._get_all_records(self.dst_env) self.assertEqual({'name': 'initial_database_version', 'value': '21'}, dst_records['system']['initial_database_version']) self._compare_records(src_records, dst_records) self.assertEqual(src_options, dst_options) att = Attachment(self.dst_env, 'wiki', 'WikiStart', 'filename.txt') self.assertEqual('test', read_file(att.path))
def dburi(self): dburi = get_dburi() if dburi == 'sqlite::memory:': # functional tests obviously can't work with the in-memory database dburi = 'sqlite:db/trac.db' return dburi
def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(DatabaseFileTestCase)) if get_dburi().startswith('sqlite:'): suite.addTest(unittest.makeSuite(SQLiteConnectionTestCase)) return suite
def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(MySQLTableAlterationSQLTest)) if get_dburi().startswith('mysql:'): suite.addTest(unittest.makeSuite(MySQLConnectionTestCase)) return suite
class FunctionalTestEnvironment(object): """Common location for convenience functions that work with the test environment on Trac. Subclass this and override some methods if you are using a different :term:`VCS`. :class:`FunctionalTestEnvironment` requires a `dirname` in which the test repository and Trac environment will be created, `port` for the :command:`tracd` webserver to run on, and the `url` which can access this (usually ``localhost``).""" def __init__(self, dirname, port, url): """Create a :class:`FunctionalTestEnvironment`, see the class itself for parameter information.""" self.url = url self.command_cwd = os.path.normpath(os.path.join(dirname, '..')) self.dirname = os.path.abspath(dirname) self.tracdir = os.path.join(self.dirname, "trac") self.htpasswd = os.path.join(self.dirname, "htpasswd") self.port = port self.pid = None self.init() self.destroy() time.sleep(0.1) # Avoid race condition on Windows self.create() locale.setlocale(locale.LC_ALL, '') trac_src = '.' dburi = property(lambda x: get_dburi()) def destroy(self): """Remove all of the test environment data.""" env = EnvironmentStub() env.destroy_db() env.shutdown() self.destroy_repo() if os.path.exists(self.dirname): rmtree(self.dirname) repotype = 'svn' def init(self): """ Hook for modifying settings or class attributes before any methods are called. """ pass def create_repo(self): """Hook for creating the repository.""" # The default test environment does not include a source repo def destroy_repo(self): """Hook for removing the repository.""" # The default test environment does not include a source repo def post_create(self, env): """Hook for modifying the environment after creation. For example, to set configuration like:: def post_create(self, env): env.config.set('git', 'path', '/usr/bin/git') env.config.save() """ pass def get_enabled_components(self): """Return a list of components that should be enabled after environment creation. For anything more complicated, use the :meth:`post_create` method. """ return [] def create(self): """Create a new test environment. This sets up Trac, calls :meth:`create_repo` and sets up authentication. """ os.mkdir(self.dirname) self.create_repo() self._tracadmin('initenv', 'testenv%s' % self.port, self.dburi, self.repotype, self.repo_path_for_initenv()) if call([ sys.executable, os.path.join(self.trac_src, 'contrib', 'htpasswd.py'), "-c", "-b", self.htpasswd, "admin", "admin" ], close_fds=close_fds, cwd=self.command_cwd): raise Exception('Unable to setup admin password') self.adduser('user') self._tracadmin('permission', 'add', 'admin', 'TRAC_ADMIN') # Setup Trac logging env = self.get_trac_environment() env.config.set('logging', 'log_type', 'file') for component in self.get_enabled_components(): env.config.set('components', component, 'enabled') env.config.save() self.post_create(env) def adduser(self, user): """Add a user to the environment. The password will be set to the same as username.""" if call([ sys.executable, os.path.join(self.trac_src, 'contrib', 'htpasswd.py'), '-b', self.htpasswd, user, user ], close_fds=close_fds, cwd=self.command_cwd): raise Exception('Unable to setup password for user "%s"' % user) def _tracadmin(self, *args): """Internal utility method for calling trac-admin""" proc = Popen([ sys.executable, os.path.join(self.trac_src, 'trac', 'admin', 'console.py'), self.tracdir ] + list(args), stdout=PIPE, stderr=STDOUT, close_fds=close_fds, cwd=self.command_cwd) out = proc.communicate()[0] if proc.returncode: print(out) logfile.write(out) raise Exception('Failed with exitcode %s running trac-admin ' \ 'with %r' % (proc.returncode, args)) def start(self): """Starts the webserver, and waits for it to come up.""" if 'FIGLEAF' in os.environ: exe = os.environ['FIGLEAF'] if ' ' in exe: # e.g. 'coverage run' args = exe.split() else: args = [exe] else: args = [sys.executable] options = [ "--port=%s" % self.port, "-s", "--hostname=127.0.0.1", "--basic-auth=trac,%s," % self.htpasswd ] if 'TRAC_TEST_TRACD_OPTIONS' in os.environ: options += os.environ['TRAC_TEST_TRACD_OPTIONS'].split() args.append(os.path.join(self.trac_src, 'trac', 'web', 'standalone.py')) server = Popen( args + options + [self.tracdir], stdout=logfile, stderr=logfile, close_fds=close_fds, cwd=self.command_cwd, ) self.pid = server.pid # Verify that the url is ok timeout = 30 while timeout: try: tc.go(self.url) break except ConnectError: time.sleep(1) timeout -= 1 else: raise Exception('Timed out waiting for server to start.') tc.url(self.url) def stop(self): """Stops the webserver, if running FIXME: probably needs a nicer way to exit for coverage to work """ if self.pid: if os.name == 'nt': # Untested res = call(["taskkill", "/f", "/pid", str(self.pid)], stdin=PIPE, stdout=PIPE, stderr=PIPE) else: os.kill(self.pid, signal.SIGTERM) try: os.waitpid(self.pid, 0) except OSError, e: if e.errno != errno.ESRCH: raise