def app(tmpdir_factory): """Method to create an app for testing.""" # need to import this late as it might have side effects from app import app as app_ from app import celery # need to save old configurations of the app # to restore them later upon tear down old_url_map = copy.copy(app_.url_map) old_view_functions = copy.copy(app_.view_functions) app_.testing = True app_.debug = False old_config = copy.copy(app_.config) # set task always eager to true in celery configurations in order to run celery tasks synchronously app_.config['task_always_eager'] = True celery.conf.update(app_.config) # update configuration file path global_config = yaml.load( open( path.abspath(path.dirname(__file__) + "/unittest_data/config.yml"))) app_.config['system_config'] = global_config # update conditions file path conditions = yaml.load( open( path.abspath( path.dirname(__file__) + "/unittest_data/conditions.yml"))) app_.config['conditions'] = conditions config = configparser.ConfigParser() config.read( path.abspath(path.dirname(__file__) + "/unittest_data/config.ini")) app_.config['dev_config'] = config temp_tasks = tmpdir_factory.mktemp('tasks') temp_reports = tmpdir_factory.mktemp('reports') # update upload directories path app_.config['dev_config']['UPLOADS']['task_dir'] = str( temp_tasks) # path to task_ids file upload app_.config['dev_config']['UPLOADS']['report_dir'] = str( temp_reports) # path to non compliant report upload # initialize temp database and yield app postgresql = Postgresql() app_.config['SQLALCHEMY_DATABASE_URI'] = postgresql.url() yield app_ # restore old configs after successful session app_.url_map = old_url_map app_.view_functions = old_view_functions app_.config = old_config shutil.rmtree(path=str(temp_tasks)) shutil.rmtree(path=str(temp_reports)) postgresql.stop()
class TestDataStoreInitialisePostgres(TestCase): def setUp(self): self.postgres = None try: self.postgres = Postgresql(database="test", host="localhost", user="******", port=55527) except RuntimeError: print("PostgreSQL database couldn't be created! " "Test is skipping.") def tearDown(self): try: self.postgres.stop() except AttributeError: return def test_postgres_initialise(self): """Test whether schemas created successfully on PostgresSQL""" if self.postgres is None: self.skipTest("Postgres is not available. Test is skipping") data_store_postgres = DataStore( db_username="******", db_password="", db_host="localhost", db_port=55527, db_name="test", ) # inspector makes it possible to load lists of schema, table, column # information for the sqlalchemy engine inspector = inspect(data_store_postgres.engine) table_names = inspector.get_table_names() schema_names = inspector.get_schema_names() # there must be no table, and no schema for datastore at the beginning self.assertEqual(len(table_names), 0) self.assertNotIn("datastore_schema", schema_names) # creating database from schema data_store_postgres.initialise() inspector = inspect(data_store_postgres.engine) table_names = inspector.get_table_names() schema_names = inspector.get_schema_names() # 11 tables must be created. A few of them tested self.assertEqual(len(table_names), 11) self.assertIn("Entry", table_names) self.assertIn("Platforms", table_names) self.assertIn("State", table_names) self.assertIn("Datafiles", table_names) self.assertIn("Nationalities", table_names) # datastore_schema must be created self.assertIn("datastore_schema", schema_names)
class TestDatabaseReflection(TestCase): def setUp(self): self.postgresql = Postgresql() self.engine = create_engine(self.postgresql.url()) def tearDown(self): self.postgresql.stop() def test_split_table(self): assert dbreflect.split_table("staging.incidents") == ("staging", "incidents") assert dbreflect.split_table("incidents") == (None, "incidents") with self.assertRaises(ValueError): dbreflect.split_table("blah.staging.incidents") def test_table_object(self): assert isinstance(dbreflect.table_object("incidents", self.engine), Table) def test_reflected_table(self): self.engine.execute("create table incidents (col1 varchar)") assert dbreflect.reflected_table("incidents", self.engine).exists() def test_table_exists(self): self.engine.execute("create table incidents (col1 varchar)") assert dbreflect.table_exists("incidents", self.engine) assert not dbreflect.table_exists("compliments", self.engine) def test_table_has_data(self): self.engine.execute("create table incidents (col1 varchar)") self.engine.execute("create table compliments (col1 varchar)") self.engine.execute("insert into compliments values ('good job')") assert dbreflect.table_has_data("compliments", self.engine) assert not dbreflect.table_has_data("incidents", self.engine) def test_table_has_duplicates(self): self.engine.execute("create table events (col1 int, col2 int)") assert not dbreflect.table_has_duplicates("events", ['col1', 'col2'], self.engine) self.engine.execute("insert into events values (1,2)") self.engine.execute("insert into events values (1,3)") assert dbreflect.table_has_duplicates("events", ['col1'], self.engine) assert not dbreflect.table_has_duplicates("events", ['col1', 'col2'], self.engine) self.engine.execute("insert into events values (1,2)") assert dbreflect.table_has_duplicates("events", ['col1', 'col2'], self.engine) def test_table_has_column(self): self.engine.execute("create table incidents (col1 varchar)") assert dbreflect.table_has_column("incidents", "col1", self.engine) assert not dbreflect.table_has_column("incidents", "col2", self.engine) def test_column_type(self): self.engine.execute("create table incidents (col1 varchar)") assert dbreflect.column_type("incidents", "col1", self.engine) == VARCHAR
def app(): """api fixture""" from flask_bp.api import app as app_ app_.testing = True app_.debug = False # initialize a temp db instance postgresql = Postgresql() app_.config['SQLALCHEMY_DATABASE_URI'] = postgresql.url() yield app_ postgresql.stop()
def dsn(): postgres = Postgresql() scheduler = new_test_scheduler(postgres.url()) scheduler.setup_database() scheduler.commit() yield postgres.url() scheduler.close() postgres.stop()
class SqlLayer(PloneSandboxLayer): default_bases = (PLONE_FIXTURE,) class Session(dict): def set(self, key, value): self[key] = value def start_postgres(self): self.postgres = Postgresql() return self.postgres.url().replace( 'postgresql://', 'postgresql+psycopg2://') def stop_postgres(self): self.postgres.stop() def init_config(self, dsn): config = getConfiguration() if not hasattr(config, 'product_config'): config.product_config = {} config.product_config['seantis.reservation'] = dict(dsn=dsn) setConfiguration(config) def setUpZope(self, app, configurationContext): # Set up sessioning objects app.REQUEST['SESSION'] = self.Session() ZopeTestCase.utils.setupCoreSessions(app) self.init_config(dsn=self.start_postgres()) import seantis.reservation xmlconfig.file( 'configure.zcml', seantis.reservation, context=configurationContext ) self.loadZCML(package=seantis.reservation) def setUpPloneSite(self, portal): quickInstallProduct(portal, 'plone.app.dexterity') quickInstallProduct(portal, 'seantis.reservation') applyProfile(portal, 'seantis.reservation:default') def tearDownZope(self, app): z2.uninstallProduct(app, 'seantis.reservation') self.stop_postgres()
class TestDatabaseReflection(TestCase): def setUp(self): self.postgresql = Postgresql() self.engine = create_engine(self.postgresql.url()) def tearDown(self): self.postgresql.stop() def test_split_table(self): assert dbreflect.split_table('staging.incidents') == ('staging', 'incidents') assert dbreflect.split_table('incidents') == (None, 'incidents') with self.assertRaises(ValueError): dbreflect.split_table('blah.staging.incidents') def test_table_object(self): assert isinstance(dbreflect.table_object('incidents', self.engine), Table) def test_reflected_table(self): self.engine.execute('create table incidents (col1 varchar)') assert dbreflect.reflected_table('incidents', self.engine).exists() def test_table_exists(self): self.engine.execute('create table incidents (col1 varchar)') assert dbreflect.table_exists('incidents', self.engine) assert not dbreflect.table_exists('compliments', self.engine) def test_table_has_data(self): self.engine.execute('create table incidents (col1 varchar)') self.engine.execute('create table compliments (col1 varchar)') self.engine.execute('insert into compliments values (\'good job\')') assert dbreflect.table_has_data('compliments', self.engine) assert not dbreflect.table_has_data('incidents', self.engine) def test_table_has_column(self): self.engine.execute('create table incidents (col1 varchar)') assert dbreflect.table_has_column('incidents', 'col1', self.engine) assert not dbreflect.table_has_column('incidents', 'col2', self.engine) def test_column_type(self): self.engine.execute('create table incidents (col1 varchar)') assert dbreflect.column_type('incidents', 'col1', self.engine) == VARCHAR
def app(mocked_config, tmpdir_factory): """Method to create an app for testing.""" # need to import this late as it might have side effects from app import app as app_ # need to save old configurations of the app # to restore them later upon tear down old_url_map = copy.copy(app_.url_map) old_view_functions = copy.copy(app_.view_functions) app_.testing = True app_.debug = False old_config = copy.copy(app_.config) # initialize temp database and yield app postgresql = Postgresql() dsn = postgresql.dsn() # monkey patch database configs for setting in ['user', 'password', 'port', 'host', 'database']: mocked_config['database'][setting] = dsn.get(setting, '') # monkey patch temp dirs temp_lists = tmpdir_factory.mktemp('lists') mocked_config['lists']['path'] = str(temp_lists) temp_uploads = tmpdir_factory.mktemp('uploads') mocked_config['global']['upload_directory'] = str(temp_uploads) app_.config['drs_config'] = mocked_config app_.config['SQLALCHEMY_DATABASE_URI'] = postgresql.url() app_.config['DRS_UPLOADS'] = str(temp_uploads) app_.config['DRS_LISTS'] = str(temp_lists) app_.config['CORE_BASE_URL'] = mocked_config['global']['core_api_v2'] app_.config['DVS_BASE_URL'] = mocked_config['global']['dvs_api_v1'] yield app_ # restore old configs after successful session app_.url_map = old_url_map app_.view_functions = old_view_functions app_.config = old_config shutil.rmtree(str(temp_lists)) shutil.rmtree(str(temp_uploads)) postgresql.stop()
def app(tmpdir_factory): """Method to create an app for testing.""" # need to import this late as it might have side effects from app import app as app_ # need to save old configurations of the app # to restore them later upon tear down old_url_map = copy.copy(app_.url_map) old_view_functions = copy.copy(app_.view_functions) app_.testing = True app_.debug = False old_config = copy.copy(app_.config) # update configuration file path global_config = yaml.safe_load( open(path.abspath(path.dirname(__file__) + "/testdata/config.yml"))) app_.config['system_config'] = global_config # update configuration file path config = configparser.ConfigParser() config.read( path.abspath(path.dirname(__file__) + "/testdata/config_test.ini")) temp_lists = tmpdir_factory.mktemp('uploads') # update upload directories path app_.config['system_config'] = config app_.config['system_config']['UPLOADS']['list_dir'] = str(temp_lists) # initialize temp database and yield app postgresql = Postgresql() app_.config['SQLALCHEMY_DATABASE_URI'] = postgresql.url() yield app_ # restore old configs after successful session app_.url_map = old_url_map app_.view_functions = old_view_functions app_.config = old_config shutil.rmtree(path=str(temp_lists)) postgresql.stop()
def postgresql(request): psql = Postgresql() yield psql psql.stop()
scheduler.rollback() raise return json.dumps( {'status': 'success', 'message': 'A reservation has been made'} ) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': postgresql = Postgresql() try: context = libres.registry.register_context('flask-exmaple') context.set_setting('dsn', postgresql.url()) scheduler = libres.new_scheduler( context, 'Test Scheduler', timezone='Europe/Zurich' ) scheduler.setup_database() scheduler.commit() app.run(debug=True) finally: postgresql.stop()