def setUpClass(cls) -> None: """Create a fresh template data from mediawords.sql. The template database will be used to execute the 'create database mediacloud_test template mediacloud_test_template' functionality to create a fresh database for each individual unit test. Recreating from a template is much faster than creating a database from scratch from our large schema. """ super().setUpClass() config = py_get_config() db_config = list( filter(lambda x: x['label'] == cls.TEST_DB_LABEL, config['database'])) if len(db_config) < 1: raise McTestDatabaseTestCaseException( "Unable to find %s database in mediawords.yml" % cls.TEST_DB_LABEL) cls.db_name = (db_config[0])['db'] cls.template_db_name = config['mediawords'].get( 'test_template_db_name', None) if cls.template_db_name is not None: log.warning("use existing test db template: %s" % cls.template_db_name) return log.info("create test db template") cls.template_db_name = cls.db_name + '_template' # we insert this db name directly into sql, so be paranoid about what is in it if re.search('[^a-z0-9_]', cls.db_name, flags=re.I) is not None: raise McTestDatabaseTestCaseException("Illegal table name: " + cls.db_name) # mediacloud_test should already exist, so we have to connect to it to create the template database db = connect_to_db(label=cls.TEST_DB_LABEL, do_not_check_schema_version=True) cls.__kill_connections_to_database(db=db, database_name=cls.template_db_name) db.query("DROP DATABASE IF EXISTS {}".format(cls.template_db_name)) db.query("CREATE DATABASE {}".format(cls.template_db_name)) db.disconnect() recreate_db(label=cls.TEST_DB_LABEL, is_template=True)
def setUp(self) -> None: """Create a fresh testing database for each unit test. The first time this function is called within a given process, it will create a template database from mediawords.sql. For each test, it will create a new test database using the postgres 'create database mediacloud_test template mediacloud_test_template' functionality. Recreating each unit test database from the template is much faster than recreating from mediawords.sql. """ test_db_label = 'test' config = py_get_config() db_config = list(filter(lambda x: x['label'] == test_db_label, config['database'])) if len(db_config) < 1: raise McTestDatabaseTestCaseException("Unable to find %s database in mediawords.yml" % [test_db_label]) db_name = (db_config[0])['db'] template_db_name = db_name + '_template' if re.search('[^a-z0-9_]', db_name, flags=re.I) is not None: raise McTestDatabaseTestCaseException("Illegal table name: " + db_name) if not TestDatabaseWithSchemaTestCase._template_db_created: log.info("create test db template") # mediacloud_test should already exist, so we have to connect to it to create the template database db = connect_to_db(label=test_db_label, do_not_check_schema_version=True) db.query("drop database if exists %s" % (template_db_name,)) db.query("create database %s" % (template_db_name,)) db.disconnect() recreate_db(label=test_db_label, is_template=True) TestDatabaseWithSchemaTestCase._template_db_created = True # now connect to the template database to execure the create command for the test database log.info("recreate test db template") db = connect_to_db(label=test_db_label, is_template=True) db.query("drop database if exists %s" % (db_name,)) db.query("create database %s template %s" % (db_name, template_db_name)) db.disconnect() db = connect_to_db(label=test_db_label) force_using_test_database() self.__db = db
def setUpClass(cls) -> None: """Create a fresh template data from mediawords.sql. The template database will be used to execute the 'create database mediacloud_test template mediacloud_test_template' functionality to create a fresh database for each individual unit test. Recreating from a template is much faster than creating a database from scratch from our large schema. """ log.info("create test db template") config = py_get_config() db_config = list( filter(lambda x: x['label'] == cls.TEST_DB_LABEL, config['database'])) if len(db_config) < 1: raise McTestDatabaseTestCaseException( "Unable to find %s database in mediawords.yml" % cls.TEST_DB_LABEL) cls.db_name = (db_config[0])['db'] cls.template_db_name = cls.db_name + '_template' # we only want to run this once per test suite for all database test cases, so this needs to be a global global _template_db_created if _template_db_created: return # we insert this db name directly into sql, so be paranoid about what is in it if re.search('[^a-z0-9_]', cls.db_name, flags=re.I) is not None: raise McTestDatabaseTestCaseException("Illegal table name: " + cls.db_name) # mediacloud_test should already exist, so we have to connect to it to create the template database db = connect_to_db(label=cls.TEST_DB_LABEL, do_not_check_schema_version=True) db.query("drop database if exists %s" % (cls.template_db_name, )) db.query("create database %s" % (cls.template_db_name, )) db.disconnect() recreate_db(label=cls.TEST_DB_LABEL, is_template=True) _template_db_created = True
def setUpClass(cls) -> None: """Create a fresh template data from mediawords.sql. The template database will be used to execute the 'create database mediacloud_test template mediacloud_test_template' functionality to create a fresh database for each individual unit test. Recreating from a template is much faster than creating a database from scratch from our large schema. """ super().setUpClass() config = py_get_config() db_config = list(filter(lambda x: x['label'] == cls.TEST_DB_LABEL, config['database'])) if len(db_config) < 1: raise McTestDatabaseTestCaseException("Unable to find %s database in mediawords.yml" % cls.TEST_DB_LABEL) cls.db_name = (db_config[0])['db'] cls.template_db_name = config['mediawords'].get('test_template_db_name', None) if cls.template_db_name is not None: log.warning("use existing test db template: %s" % cls.template_db_name) return log.info("create test db template") cls.template_db_name = cls.db_name + '_template' # we insert this db name directly into sql, so be paranoid about what is in it if re.search('[^a-z0-9_]', cls.db_name, flags=re.I) is not None: raise McTestDatabaseTestCaseException("Illegal table name: " + cls.db_name) # mediacloud_test should already exist, so we have to connect to it to create the template database db = connect_to_db(label=cls.TEST_DB_LABEL, do_not_check_schema_version=True) cls.__kill_connections_to_database(db=db, database_name=cls.template_db_name) db.query("DROP DATABASE IF EXISTS {}".format(cls.template_db_name)) db.query("CREATE DATABASE {}".format(cls.template_db_name)) db.disconnect() recreate_db(label=cls.TEST_DB_LABEL, is_template=True)
def setUp(self): test_db_label = 'test' recreate_db(label=test_db_label) db = connect_to_db(label=test_db_label) force_using_test_database() self.__db = db