from pytest_postgresql import factories import pytest from psycopg2.extensions import connection from typing import Generic, TypeVar from database import database as Database from Model.Model import Model from Model.Session import Session from Model.User import User from config import DB_HOST, DB_NAME #Creates pytest fixture that injects connection to postgresql database factories.postgresql_proc('test_postgresql', DB_NAME) class TestDatabase(Database): def connect(self, postgresql: connection) -> None: self.conn = postgresql T = TypeVar('T') def test_model_factory(model: T) -> T: class TestModel(model): def __init__(self, db: Database) -> T: self.pg = db
ORDER BY id DESC LIMIT 2 ) SELECT * FROM employees_cte """ ALL = {"postgres", "sqlite"} DATABASE = os.environ["DB"] IS_POSTGRES = DATABASE == "postgres" IS_SQLITE = DATABASE == "sqlite" # Travis has only PostgreSQL 9.6 if IS_POSTGRES and "TRAVIS" in os.environ: from pytest_postgresql import factories postgresql_proc = factories.postgresql_proc( executable="/usr/lib/postgresql/9.6/bin/pg_ctl") postgresql = factories.postgresql("postgresql_proc") def is_search_path_fixed(connection): """Check if a security issue with `search_path` is fixed in the current PG version. Ref: CVE-2018-1058. """ version = connection.server_version return (version >= 100003 or 90608 <= version < 100000 or 90512 <= version < 90600 or 90417 <= version < 90500 or 90322 <= version < 90400) @pytest.fixture
import mock import pytest from pytest_postgresql import factories DE_DB_HOST = "127.0.0.1" DE_DB_USER = "******" DE_DB_PASS = None DE_DB_NAME = "decisionengine" DE_SCHEMA = [ os.path.dirname(os.path.abspath(__file__)) + "/../postgresql.sql", ] # DE_DB_PORT assigned at random PG_PROG = factories.postgresql_proc(user=DE_DB_USER, password=DE_DB_PASS, host=DE_DB_HOST, port=None) DE_DB = factories.postgresql("PG_PROG", dbname=DE_DB_NAME, load=DE_SCHEMA) @pytest.fixture def mock_data_block(): """ This fixture replaces the standard datablock implementation. The current DataBlock implementation does not own any data products but forwards them immediately to a backend datasource. The only implemented datasource requires Postgres, which is overkill when needing to test simple data-product communication between modules.
import pytest from pytest_postgresql import factories as psql_factories from pytest_redis import factories as redis_factories from sanauth.core import sanauth postgresql_my_proc = psql_factories.postgresql_proc() postgresql_my = psql_factories.postgresql('postgresql_my_proc') redis_my_proc = redis_factories.redis_proc() redis_my = redis_factories.redisdb('redis_my_proc') @pytest.fixture def db_settings(request): return dict( user='******', password='', host='127.0.0.1', port=5433, ), dict(address=('127.0.0.1', 6380), minsize=1, maxsize=10) @pytest.fixture def app(db_settings, postgresql_my_proc, postgresql_my, redis_my_proc, redis_my): pg_settings, redis_config = db_settings sanauth_app = sanauth(pg_cfg=pg_settings, r_cfg=redis_config) yield sanauth_app @pytest.fixture def app_fixture(loop, app, test_server):
"""Tests main conftest file.""" from pytest_postgresql import factories PG_CTL = '/usr/lib/postgresql/{ver}/bin/pg_ctl' # pylint:disable=invalid-name postgresql92 = factories.postgresql_proc(PG_CTL.format(ver='9.2'), port=None) postgresql93 = factories.postgresql_proc(PG_CTL.format(ver='9.3'), port=None) postgresql94 = factories.postgresql_proc(PG_CTL.format(ver='9.4'), port=None) postgresql95 = factories.postgresql_proc(PG_CTL.format(ver='9.5'), port=None) postgresql96 = factories.postgresql_proc(PG_CTL.format(ver='9.6'), port=None) postgresql10 = factories.postgresql_proc(PG_CTL.format(ver='10'), port=None) postgresql11 = factories.postgresql_proc(PG_CTL.format(ver='11'), port=None) postgresql_proc2 = factories.postgresql_proc(port=9876) postgresql2 = factories.postgresql('postgresql_proc2', db_name='test-db') postgresql_rand_proc = factories.postgresql_proc(port=None) postgresql_rand = factories.postgresql('postgresql_rand_proc') # pylint:enable=invalid-name
"""Tests main conftest file.""" import os from pytest_postgresql import factories pytest_plugins = ["pytester"] POSTGRESQL_VERSION = os.environ.get("POSTGRES", "13") TEST_SQL_DIR = os.path.dirname(os.path.abspath(__file__)) + "/test_sql/" postgresql_proc2 = factories.postgresql_proc(port=None) postgresql2 = factories.postgresql("postgresql_proc2", dbname="test-db") postgresql_load_1 = factories.postgresql( "postgresql_proc2", dbname="test-load-db", load=[ TEST_SQL_DIR + "test.sql", ], ) postgresql_load_2 = factories.postgresql( "postgresql_proc2", dbname="test-load-moredb", load=[TEST_SQL_DIR + "test.sql", TEST_SQL_DIR + "test2.sql"], )
__all__ = [ "DATABASES_TO_TEST", "PG_PROG", "PG_DE_DB_WITHOUT_SCHEMA", "SQLALCHEMY_PG_WITH_SCHEMA", "SQLALCHEMY_TEMPFILE_SQLITE", "datasource", "mock_data_block", ] # DE_DB_PORT assigned at random PG_PROG = factories.postgresql_proc( user="******", password=None, host="127.0.0.1", port=None, postgres_options="-N 1000", ) PG_DE_DB_WITHOUT_SCHEMA = factories.postgresql( "PG_PROG", dbname="decisionengine", ) if sys.version_info.major == 3 and sys.version_info.minor > 6 and platform.python_implementation( ) == "CPython": # sqlite on EL7 is too old for efficient testing # sqlite on pypy is unnecessary and really slow DATABASES_TO_TEST = ("SQLALCHEMY_PG_WITH_SCHEMA", "SQLALCHEMY_TEMPFILE_SQLITE") else:
import subprocess import pytest import pytest_postgresql from pytest_postgresql import factories from rdbms_subsetter.subsetter import Db try: subprocess.check_output('command -v pg_ctl', shell=True) PG_CTL_MISSING = False # sorry for the double-negative, but it's convenient later except subprocess.CalledProcessError: PG_CTL_MISSING = True postgresql_dest_proc = factories.postgresql_proc() postgresql_dest = factories.postgresql('postgresql_dest_proc') class DummyArgs(object): logarithmic = False fraction = 0.25 force_rows = {} children = 25 config = {} tables = [] schema = [] exclude_tables = [] full_tables = [] buffer = 1000
# License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import glob import os from pytest_postgresql import factories from swh.core.db import BaseDb from swh.core.db.pytest_plugin import gen_dump_files, postgresql_fact SQL_DIR = os.path.join(os.path.dirname(__file__), "data") test_postgresql_proc = factories.postgresql_proc( dbname="fun", load=sorted(glob.glob(f"{SQL_DIR}/*.sql")), # type: ignore[arg-type] # type ignored because load is typed as Optional[List[...]] instead of an # Optional[Sequence[...]] in pytest_postgresql<4 ) # db with special policy for tables dbversion and people postgres_fun = postgresql_fact( "test_postgresql_proc", no_db_drop=True, no_truncate_tables={"dbversion", "people"}, ) postgres_fun2 = postgresql_fact( "test_postgresql_proc", dbname="fun2", load=sorted(glob.glob(f"{SQL_DIR}/*.sql")), no_truncate_tables={"dbversion", "people"},
import pandas as pd from pytest_postgresql import factories from postgres_pandas import db_utils import pytest pginst = factories.postgresql_proc('/usr/local/bin/pg_ctl') dbinst = factories.postgresql('pginst', 'test_db') @pytest.fixture def dsn(pginst): return {'host': pginst.host, 'port': pginst.port, 'user': pginst.user} @pytest.fixture(autouse=True) def use_test_db(monkeypatch, dsn): def mock_config(): return {**dsn, 'dbname': 'test_db'} monkeypatch.setattr(db_utils, 'get_db_config', mock_config) @pytest.fixture def test_data(): df = pd.DataFrame({'plant_name': ['p1', 'p2', 'p3'], 'ph_6_code': ['0815', '0815', '0815'], 'date': ['2019-01-01', '2019-01-01', '2019-01-01'],
import tempfile from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from pytest_postgresql import factories from models import ParsedText import pytest socket_dir = tempfile.TemporaryDirectory() postgresql_my_proc = factories.postgresql_proc(port=None, unixsocketdir=socket_dir.name) postgresql_my = factories.postgresql("postgresql_my_proc") @pytest.fixture(scope="function") def setup_database(postgresql_my): def dbcreator(): return postgresql_my.cursor().connection engine = create_engine("postgresql+psycopg2://", creator=dbcreator) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() yield session session.close() def test_postgres_options(postgresql): """Ensure max connections exists""" cur = postgresql.cursor() cur.execute("SHOW max_connections") assert cur.fetchone() == ("100", )
pytest_v = pkg_resources.get_distribution("pytest").parsed_version if pytest_v < pkg_resources.extern.packaging.version.parse("3.9"): @pytest.fixture def tmp_path(): import pathlib import tempfile with tempfile.TemporaryDirectory() as tmpdir: yield pathlib.Path(tmpdir) storage_postgresql_proc = factories.postgresql_proc( dbname="storage", load=[ partial(initialize_database_for_module, "storage", StorageDb.current_version) ], ) vault_postgresql_proc = factories.postgresql_proc( dbname="vault", load=[ partial(initialize_database_for_module, "vault", VaultBackend.current_version) ], ) postgres_vault = postgresql_fact("vault_postgresql_proc") postgres_storage = postgresql_fact( "storage_postgresql_proc",
password=executor.password, host=executor.host, port=executor.port, ) with pytest.raises(psycopg.OperationalError): psycopg.connect( dbname=executor.user, user=executor.user, password="******", host=executor.host, port=executor.port, ) assert not executor.running() postgres_with_password = postgresql_proc(password="******") def test_proc_with_password( postgres_with_password: PostgreSQLExecutor, ) -> None: """Check that password option to postgresql_proc factory is honored.""" assert postgres_with_password.running() is True # no assertion necessary here; we just want to make sure it connects with # the password retry( lambda: psycopg.connect( dbname=postgres_with_password.user, user=postgres_with_password.user, password=postgres_with_password.password, host=postgres_with_password.host,
#! /usr/bin/env python3 import pytest from pytest_postgresql import factories import sqlalchemy pg_executable = "/usr/bin/pg_ctl" db_name = "wiki_scripts" # postgresql process fixture postgresql_proc = factories.postgresql_proc(logs_prefix="pytest-", executable=pg_executable) # fixture holding an instance of a psycopg2 connection postgresql = factories.postgresql("postgresql_proc", db=db_name) @pytest.fixture(scope="function") def pg_engine(postgresql): return sqlalchemy.create_engine("postgresql+psycopg2://", poolclass=sqlalchemy.pool.StaticPool, creator=lambda: postgresql) __all__ = ("postgresql_proc", "postgresql", "pg_engine")
#! /usr/bin/env python3 import pytest from pytest_postgresql import factories import sqlalchemy pg_executable = "/usr/bin/pg_ctl" db_name = "wiki_scripts" # postgresql process fixture postgresql_proc = factories.postgresql_proc(logs_prefix="pytest-", executable=pg_executable) # fixture holding an instance of a psycopg2 connection postgresql = factories.postgresql("postgresql_proc", db_name=db_name) @pytest.fixture(scope="function") def pg_engine(postgresql): return sqlalchemy.create_engine("postgresql+psycopg2://", poolclass=sqlalchemy.pool.StaticPool, creator=lambda: postgresql) __all__ = ("postgresql_proc", "postgresql", "pg_engine")
import pkg_resources import pytest from pytest_postgresql import factories from swh.core.db.pytest_plugin import initialize_database_for_module from swh.scheduler import get_scheduler from swh.scheduler.backend import SchedulerBackend # celery tasks for testing purpose; tasks themselves should be # in swh/scheduler/tests/tasks.py TASK_NAMES = ["ping", "multiping", "add", "error", "echo"] scheduler_postgresql_proc = factories.postgresql_proc(load=[ partial( initialize_database_for_module, modname="scheduler", version=SchedulerBackend.current_version, ) ], ) postgresql_scheduler = factories.postgresql("scheduler_postgresql_proc") @pytest.fixture def swh_scheduler_config(request, postgresql_scheduler): return { "db": postgresql_scheduler.dsn, } @pytest.fixture
Postgresql as Postgresql_datasource, ) __all__ = [ "DATABASES_TO_TEST", "PG_PROG", "PG_DE_DB_WITH_SCHEMA", "PG_DE_DB_WITHOUT_SCHEMA", "SQLALCHEMY_PG_WITH_SCHEMA", "SQLALCHEMY_IN_MEMORY_SQLITE", "datasource", "mock_data_block", ] # DE_DB_PORT assigned at random PG_PROG = factories.postgresql_proc(user="******", password=None, host="127.0.0.1", port=None) PG_DE_DB_WITH_SCHEMA = factories.postgresql( "PG_PROG", dbname="decisionengine", load=[ os.path.dirname(os.path.abspath(__file__)) + "/../postgresql.sql", ], ) PG_DE_DB_WITHOUT_SCHEMA = factories.postgresql( "PG_PROG", dbname="decisionengine", ) DATABASES_TO_TEST = ("PG_DE_DB_WITH_SCHEMA", )
"""All tests for pytest-postgresql.""" import psycopg2 import pytest from pytest_postgresql import factories query = "CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);" pg_ctl = '/usr/lib/postgresql/{ver}/bin/pg_ctl' postgresql92 = factories.postgresql_proc(pg_ctl.format(ver='9.2'), port=None) postgresql93 = factories.postgresql_proc(pg_ctl.format(ver='9.3'), port=None) postgresql94 = factories.postgresql_proc(pg_ctl.format(ver='9.4'), port=None) postgresql95 = factories.postgresql_proc(pg_ctl.format(ver='9.5'), port=None) postgresql96 = factories.postgresql_proc(pg_ctl.format(ver='9.6'), port=None) @pytest.mark.parametrize('postgres', ( 'postgresql92', 'postgresql93', 'postgresql94', 'postgresql95', 'postgresql96', )) def test_postgresql_proc(request, postgres): """Test different postgresql versions.""" postgresql_proc = request.getfixturevalue(postgres) assert postgresql_proc.running() is True
"""Create postgres version string for macos and linux.""" # macos if platform.system() == 'Darwin': if ver == '13': # brew installs 13 as postgresql right now return find_pg_ctl('/usr/local/Cellar/postgresql/') return find_pg_ctl(f'/usr/local/Cellar/postgresql@{ver}/') # linux return find_pg_ctl(f'/usr/lib/postgresql/{ver}/') TEST_SQL_DIR = os.path.dirname(os.path.abspath(__file__)) + '/test_sql/' # pylint:disable=invalid-name postgresql95 = factories.postgresql_proc(create_version(ver='9.5'), port=None) postgresql96 = factories.postgresql_proc(create_version(ver='9.6'), port=None) postgresql10 = factories.postgresql_proc(create_version(ver='10'), port=None) postgresql11 = factories.postgresql_proc(create_version(ver='11'), port=None) postgresql12 = factories.postgresql_proc(create_version(ver='12'), port=None) postgresql13 = factories.postgresql_proc(create_version(ver='13'), port=None) postgresql_proc2 = factories.postgresql_proc(port=9876) postgres10 = factories.postgresql('postgresql10') postgresql2 = factories.postgresql('postgresql_proc2', db_name='test-db') postgresql_load_1 = factories.postgresql('postgresql_proc2', db_name='test-db', load=[ TEST_SQL_DIR + 'test.sql', ]) postgresql_load_2 = factories.postgresql(
"""Template database tests.""" import pytest from pytest_postgresql.factories import postgresql, postgresql_proc from pytest_postgresql.compat import connection from tests.loader import load_database postgresql_proc_with_template = postgresql_proc( port=21987, dbname="stories_templated", load=[load_database], ) postgresql_template = postgresql( "postgresql_proc_with_template", dbname="stories_templated", ) @pytest.mark.parametrize("_", range(5)) def test_template_database(postgresql_template: connection, _: int) -> None: """Check that the database struture gets recreated out of a template.""" with postgresql_template.cursor() as cur: cur.execute("SELECT * FROM stories") res = cur.fetchall() assert len(res) == 4 cur.execute("TRUNCATE stories") cur.execute("SELECT * FROM stories") res = cur.fetchall() assert len(res) == 0
import os import pytest import shutil import mirakuru from elasticsearch import Elasticsearch from pytest_postgresql import factories pgsql_proc = factories.postgresql_proc(executable='/usr/bin/pg_ctl', host='localhost', port=45678, user='******') pgsql_db = factories.postgresql('pgsql_proc', db='jslcrud_tests') @pytest.fixture(scope='session') def es_proc(request): port = 9085 home_dir = '/tmp/elasticsearch_%s' % port os.environ['ES_HOME'] = home_dir command = [ os.environ['ELASTICSEARCH_EXECUTABLE'], '-p', '/tmp/elasticsearch.%s.pid' % port, '-E', 'http.port=%s' % port, '-E', 'default.path.logs=/tmp/elasticsearch_%s_logs' % port, '-E', 'cluster.name=elasticsearch_cluster_%s' % port, '-E', "network.publish_host='127.0.0.1'", '-E', 'index.store.type=mmapfs' ] es_proc = mirakuru.HTTPExecutor(command, shell=True, url='http://127.0.0.1:%s' % port) es_proc.start()
from types import SimpleNamespace import pytest from pytest_postgresql import factories from pgantomizer.anonymize import (InvalidAnonymizationSchemaError, MissingAnonymizationRuleError, load_anonymize_remove, load_db_to_new_instance) from pgantomizer.dump import dump_db from pgantomizer.dump import main as dump_main from pgantomizer.anonymize import main as anonymize_main from .asserts import assert_db_anonymized, assert_db_empty anonymized_proc = factories.postgresql_proc(port='8765', logsdir='/tmp') anonymized = factories.postgresql('anonymized_proc') DUMP_PATH = 'test_dump.sql' SCHEMA_PATH = 'example_schema.yaml' ORIGINAL_DB_ARGS = { 'password': '', 'dbname': 'tests', 'user': '******', 'host': '127.0.0.1', 'port': '9876' } ANONYMIZED_DB_ARGS = {**ORIGINAL_DB_ARGS, **{'port': '8765'}} DUMP_DB_ARGS = [ ORIGINAL_DB_ARGS[arg] for arg in ('dbname', 'user', 'host', 'port') ]
# Copyright (c) 2019 Mohd Izhar Firdaus Bin Ismail # # This software is released under the MIT License. # https://opensource.org/licenses/MIT from pytest_postgresql import factories pgsql_proc = factories.postgresql_proc( executable="/usr/bin/pg_ctl", host="localhost", port=45678, user="******", ) pgsql_db = factories.postgresql("pgsql_proc", db_name="morpcc_tests") pgsql_db_warehouse = factories.postgresql("pgsql_proc", db_name="morpcc_warehouse") pgsql_db_cache = factories.postgresql("pgsql_proc", db_name="morpcc_cache")
assert executor.running() psycopg2.connect(dbname=executor.user, user=executor.user, password=executor.password, host=executor.host, port=executor.port) with pytest.raises(psycopg2.OperationalError): psycopg2.connect(dbname=executor.user, user=executor.user, password='******', host=executor.host, port=executor.port) assert not executor.running() postgres_with_password = postgresql_proc(password='******') def test_proc_with_password(postgres_with_password): # pylint: disable=redefined-outer-name """Check that password option to postgresql_proc factory is honored.""" assert postgres_with_password.running() is True # no assertion necessary here; we just want to make sure it connects with # the password psycopg2.connect(dbname=postgres_with_password.user, user=postgres_with_password.user, password=postgres_with_password.password, host=postgres_with_password.host, port=postgres_with_password.port) with pytest.raises(psycopg2.OperationalError):
@pytest.fixture def kafka_server(kafka_port): _, port = kafka_port return "localhost", port @pytest.fixture def statsd_server(): port = port_for.select_random(None) server = StatsDStub(port=port) server.start() yield server server.stop() postgres_server = pg_factories.postgresql_proc(password="******") redis_server = redis_factories.redis_proc( executable=shutil.which("redis-server"), timeout=3600) KAFKA_BIN = download_kafka() zookeeper_server = make_zookeeper_process( str(KAFKA_BIN / "zookeeper-server-start.sh"), zk_config_template=""" dataDir={zk_data_dir} clientPort={zk_port} maxClientCnxns=0 admin.enableServer=false""", ) kafka_port = make_kafka_server( kafka_bin=str(KAFKA_BIN / "kafka-server-start.sh"), zookeeper_fixture_name="zookeeper_server",
@property def connection(self): """Simple connection instead of TMDB's pool.""" return self._connection def add_test_unit(self, source, target, slang=None, tlang=None): lang_config = tmdb.lang_to_config('en') po = pofile() u = po.addsourceunit(source) u.target = target self.add_store(po, slang or 'en', tlang or 'af') # avoid cached language lists: self._available_langs = {} pg_server = factories.postgresql_proc(port=None, logsdir='/tmp') pg_connection = factories.postgresql('pg_server', db_name='amagama_test') @pytest.fixture def amagama(pg_connection): """Returns an amagama app already connected to a database.""" app = amagama_server_factory() app.testing = True app.tmdb = TempTMDB(connection=pg_connection, app=app) app.tmdb.init_db(['en']) from flask_caching import Cache cache = Cache(app, config={ 'CACHE_TYPE': 'simple', 'CACHE_THRESHOLD': 100000,
from os import environ import pytest from pytest_postgresql import factories from swh.core.db.pytest_plugin import initialize_database_for_module from swh.storage import get_storage from swh.storage.postgresql.storage import Storage as StorageDatastore from swh.storage.tests.storage_data import StorageData environ["LC_ALL"] = "C.UTF-8" swh_storage_postgresql_proc = factories.postgresql_proc(load=[ partial( initialize_database_for_module, modname="storage", version=StorageDatastore.current_version, ) ], ) swh_storage_postgresql = factories.postgresql("swh_storage_postgresql_proc", ) @pytest.fixture def swh_storage_backend_config(swh_storage_postgresql): """Basic pg storage configuration with no journal collaborator (to avoid pulling optional dependency on clients of this fixture) """ yield { "cls": "postgresql",
import json import os import shutil from pathlib import Path import dodoo_init.cache as cache import pytest from psycopg2.extensions import make_dsn from pytest_postgresql import factories postgres = factories.postgresql_proc() pg_conn_main = factories.postgresql("postgres") pg_conn_test = factories.postgresql("postgres", db_name="test") @pytest.fixture(autouse=True, scope="package") def environ(tmp_path_factory) -> None: secrets = tmp_path_factory.mktemp("secrets") admin_passwd = secrets / "admin" admin_passwd.write_text("admin-pwd") admin_passwd.chmod(0o500) session_encryption_key = secrets / "sessionencryptionkey" session_encryption_key.write_text("secret-key") session_encryption_key.chmod(0o500) smtpuser = secrets / "smtpuser" smtpuser.write_text("smtp-user") smtpuser.chmod(0o500) smtppwd = secrets / "smtppwd" smtppwd.write_text("smtp-pwd") smtppwd.chmod(0o500) os.environ.update(ODOOADMINPASSWORD_FILE=str(admin_passwd))
"""This module contains tests for model.py module.""" import datetime import pytest from pytest_postgresql import factories import model postgresql_proc = factories.postgresql_proc(port='5432') postgresql_with_schema = factories.postgresql( 'postgresql_proc', load=['server/tf/scripts/create.sql']) # noinspection PyShadowingNames,DuplicatedCode def test_create(postgresql_with_schema): """Test create function.""" with pytest.raises(ValueError): model.create(postgresql_with_schema, '', 'こんにちは') with pytest.raises(ValueError): model.create(postgresql_with_schema, 'dummy_chat_id', '') with pytest.raises(ValueError): model.create(postgresql_with_schema, 'dummy_chat_id', 'こんにちは', provider='') assert model.create(postgresql_with_schema, 'dummy_chat_id', 'こんにちは') == 1 assert model.create(postgresql_with_schema, 'dummy_chat_id', '世界') == 2 assert model.create(postgresql_with_schema, 'dummy_chat_id', '世界') == 2 assert model.create(postgresql_with_schema, 'other_dummy_chat_id',