Пример #1
0
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_, db

    # 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
    with Postgresql() as postgresql:
        app_.config['SQLALCHEMY_DATABASE_URI'] = postgresql.url()

        temp_download = tmpdir_factory.mktemp('downloads')
        temp_uploads = tmpdir_factory.mktemp('uploads')
        app_.config['DPS_UPLOADS'] = str(temp_uploads)
        app_.config['DPS_DOWNLOADS'] = str(temp_download)

        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_download))
    shutil.rmtree(str(temp_uploads))
    postgresql.stop()
Пример #2
0
def sql_sc(sc):
    with Postgresql() as postgresql:
        conn = psycopg2.connect(**postgresql.dsn())
        cur = conn.cursor()

        cur.execute(
            'CREATE TABLE test (id serial PRIMARY KEY, a integer, b integer, c text, d varchar(255), e boolean, f float, grp integer)'
        )
        cur.execute(
            "INSERT INTO test (a, b, c, d, e, f, grp) VALUES (10, 0, 'hi', 'hello', true, 2.0, 0)"
        )
        cur.execute(
            "INSERT INTO test (a, b, c, d, e, f, grp) VALUES (20, 0, 'hi', 'hello', true, 2.0, 0)"
        )
        cur.execute(
            "INSERT INTO test (a, b, c, d, e, f, grp) VALUES (30, 0, 'hi', 'hello', true, 2.0, 1)"
        )
        cur.execute('CREATE TABLE jobs (id serial PRIMARY KEY, name text)')
        cur.execute(
            'CREATE TABLE test2 (id serial PRIMARY KEY, b integer, s text)')
        conn.commit()

        sql_params = postgresql.dsn()
        sql_config = protobufs.SQLConfig(hostaddr=sql_params['host'],
                                         port=sql_params['port'],
                                         dbname=sql_params['database'],
                                         user=sql_params['user'],
                                         adapter='postgres')

        yield sc, SQLStorage(config=sql_config, job_table='jobs'), cur

        cur.close()
        conn.close()
Пример #3
0
    def test_fetch_multiple_pages_yesdb(self, http_mocker):
        http_mocker.get(self.test_re, text=self.mock_response)
        initdb_args = Postgresql.DEFAULT_SETTINGS['initdb_args']
        initdb_args = ' '.join([initdb_args, '-E UTF-8'])
        db = Postgresql(initdb_args=initdb_args)

        fl = self.get_fl(override_config={
            'lister': {
                'cls': 'local',
                'args': {
                    'db': db.url()
                }
            }
        })
        self.init_db(db, fl.MODEL)

        self.disable_storage_and_scheduler(fl)

        fl.run(min_bound=self.first_index)

        self.assertEqual(fl.db_last_index(), self.last_index)
        partitions = fl.db_partition_indices(5)
        self.assertGreater(len(partitions), 0)
        for k in partitions:
            self.assertLessEqual(len(k), 5)
            self.assertGreater(len(k), 0)
Пример #4
0
class TestConfig(object):
    DEBUG = True
    TESTING = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_DATABASE_URI = Postgresql().url()
    ENV = 'test'
    TESTING = True
    ADMIN = os.getenv('ADMIN')
Пример #5
0
def postgres():
    """
    Postgres fixture - starts a postgres instance inside a temp directory
    and closes it after tests are done
    """
    with Postgresql() as postgresql:
        yield postgresql
        postgresql.stop()
Пример #6
0
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()
Пример #7
0
def app():
    _app = create_app()
    with Postgresql() as postgresql:
        _app.config['SQLALCHEMY_DATABASE_URI'] = postgresql.url()
        ctx = _app.app_context()
        ctx.push()

        yield _app

        ctx.pop()
 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.")
Пример #9
0
def app():
    with Postgresql() as postgresql:
        _app = create_app(
            config_object=TestingConfig,
            settings_override={'SQLALCHEMY_DATABASE_URI': postgresql.url()})
        ctx = _app.app_context()
        ctx.push()

        yield _app

        ctx.pop()
Пример #10
0
def app():
    _app = flask_app
    _app.config.from_object(__name__ + '.TestConfig')
    with Postgresql() as postgresql:
        _app.config['SQLALCHEMY_DATABASE_URI'] = postgresql.url()
        ctx = _app.app_context()
        ctx.push()

        yield _app

        ctx.pop()
Пример #11
0
def postgres():
    try:
        import psycopg2
    except ImportError:
        yield None  # PostgreSQL database adapter is unavailable on this system
    else:
        try:
            with Postgresql() as pg:
                yield pg
        except FileNotFoundError:
            yield None  # PostgreSQL is unavailable on this system
Пример #12
0
def test_db(app):
    """Context manager to provide a URL for the test database. If one is
    configured, then that is used. Otherwise one is created using
    testing.postgresql."""
    test_db_url = app.config.get('SQLALCHEMY_DATABASE_URI', None)
    if test_db_url:
        # Test database configured manually, use that
        yield test_db_url
    else:
        # No test database configured, create one using testing.postgresql
        with Postgresql() as postgresql:
            yield postgresql.url()
Пример #13
0
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()
Пример #14
0
def _app(request):
    app = create_app('test')
    app.config['DEBUG'] = True
    app.config['TESTING'] = True

    with Postgresql() as postgresql:
        app.config['SQLALCHEMY_DATABASE_URI'] = postgresql.url()

        with app.app_context():
            db.init_app(app)
            db.create_all()
            populate(db)
            yield app
Пример #15
0
def app():
    _app = create_app()
    with Postgresql() as postgresql:
        _app.config.update({
            "TESTING": True,
            "SQLALCHEMY_DATABASE_URI": postgresql.url(),
            "SECURITY_PASSWORD_HASH": "plaintext",
            "SECURITY_PASSWORD_SCHEMES": ['plaintext'],
            "SECURITY_HASHING_SCHEMES": ["plaintext"],
            "SECURITY_DEPRECATED_HASHING_SCHEMES": [],
        })
        ctx = _app.app_context()
        ctx.push()

        yield _app

        ctx.pop()
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()
import os

import pytest
from dj_database_url import parse
from django.conf import settings
from testing.postgresql import Postgresql

postgres = os.environ.get("POSTGRESQL_PATH")
initdb = os.environ.get("INITDB_PATH")
_POSTGRESQL = Postgresql(postgres=postgres, initdb=initdb)


@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config, parser, args):
    os.environ["DJANGO_SETTINGS_MODULE"] = early_config.getini(
        "DJANGO_SETTINGS_MODULE")
    settings.DATABASES["default"] = parse(_POSTGRESQL.url())
    settings.DATABASES["dashboard"] = parse(_POSTGRESQL.url())


def pytest_unconfigure(config):
    _POSTGRESQL.stop()
Пример #19
0
import os

import pytest
from dj_database_url import parse
from django.conf import settings
from testing.postgresql import Postgresql

_POSTGRESQL = Postgresql()


@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config, parser, args):
    os.environ["DJANGO_SETTINGS_MODULE"] = early_config.getini("DJANGO_SETTINGS_MODULE")
    settings.DATABASES["default"] = parse(_POSTGRESQL.url())
    settings.DATABASES["dashboard"] = parse(_POSTGRESQL.url())


def pytest_unconfigure(config):
    _POSTGRESQL.stop()
Пример #20
0
from app.db.tables import Base
from sqlalchemy import create_engine
from contextlib2 import contextmanager
from sqlalchemy.orm import sessionmaker
from testing.postgresql import Postgresql
from app.db.crud import (get_near_users, add_user, get_all_users, delete_user,
                         get_user_by_id, is_authenticated_user,
                         get_user_by_email, update_categories_to_user)

# Tests logger.
logger = logging.getLogger()
logging.basicConfig(level=logging.DEBUG)

# Startup test db.
fake = Faker()
temp_test_db = Postgresql()
engine = create_engine(temp_test_db.url(), echo=True)
MockedSession = sessionmaker(bind=engine)
logger.info("Tests DB was created: %s", temp_test_db.url())


@contextmanager
def mocked_transaction():
    s = MockedSession()

    try:
        yield s

    except Exception:
        s.rollback()
        raise
Пример #21
0
 def start_postgres(self):
     self.postgres = Postgresql()
     return self.postgres.url().replace(
         'postgresql://', 'postgresql+psycopg2://')
Пример #22
0
def postgres_db():
    with Postgresql() as postgresql:
        yield create_engine(postgresql.url())
Пример #23
0
 def setUp(self):
     self.postgresql = Postgresql()
     self.engine = create_engine(self.postgresql.url())
Пример #24
0
def postgres():
    with Postgresql() as pg:
        yield pg
Пример #25
0
def postgresql(request):
    psql = Postgresql()

    yield psql
    psql.stop()