1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
import os
from pathlib import Path
import pytest
from sqlalchemy import MetaData
from sqlalchemy import create_engine
from sqlalchemy import text
from sqlalchemy.dialects.mysql.base import MySQLDialect
from sqlalchemy.dialects.sqlite.base import SQLiteDialect
from sqlalchemy.exc import InvalidRequestError
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool
from geoalchemy2.alembic_helpers import _monkey_patch_get_indexes_for_mysql
from geoalchemy2.alembic_helpers import _monkey_patch_get_indexes_for_sqlite
from . import copy_and_connect_sqlite_db
from . import get_postgis_major_version
from . import get_postgres_major_version
from . import get_versions
from . import print_versions
from .schema_fixtures import * # noqa
def pytest_addoption(parser):
parser.addoption(
"--postgresql_dburl",
action="store",
help="PostgreSQL DB URL used for tests (`postgresql://user:password@host:port/dbname`).",
)
parser.addoption(
"--sqlite_spatialite3_dburl",
action="store",
help="SQLite DB URL used for tests with SpatiaLite3 (`sqlite:///path_to_db_file`).",
)
parser.addoption(
"--sqlite_spatialite4_dburl",
action="store",
help="SQLite DB URL used for tests with SpatiaLite4 (`sqlite:///path_to_db_file`).",
)
parser.addoption(
"--sqlite_geopackage_dburl",
action="store",
help="SQLite DB URL used for tests with GeoPackage (`gpkg:///path_to_db_file.gpkg`).",
)
parser.addoption(
"--mysql_dburl",
action="store",
help="MySQL DB URL used for tests (`mysql://user:password@host:port/dbname`).",
)
parser.addoption(
"--mariadb_dburl",
action="store",
help="MariaDB DB URL used for tests (`mariadb://user:password@host:port/dbname`).",
)
parser.addoption(
"--engine-echo",
action="store_true",
default=False,
help="If set to True, all statements of the engine are logged.",
)
parser.addoption(
"--require-all-dialects",
action="store_true",
default=False,
help="If set to True, all dialects muts be properly executed.",
)
parser.addoption(
"--long-benchmarks",
action="store_true",
default=False,
help="If set to True, tests marked as long benchmarks will be run.",
)
def pytest_configure(config):
"""Register additional marker."""
config.addinivalue_line("markers", "long_benchmark: mark a test as a long benchmark.")
def pytest_collection_modifyitems(config, items):
"""Skip tests marked with 'long_benchmark' if the '--long-benchmarks' option is not provided."""
# Check if the '--long-benchmarks' option is provided
if not config.getoption("--long-benchmarks"):
# Create custom marker to skip long benchmarks
skip_long_benchmarks = pytest.mark.skip(reason="Skip long benchmarks")
for item in items:
# If the test is marked with 'long_benchmark', add the skip marker
if "long_benchmark" in item.keywords:
item.add_marker(skip_long_benchmarks)
def pytest_generate_tests(metafunc):
"""Parametrize the tests with different database URLs.
This function is called for each test function and applies the appropriate database URL fixture
based on the test's module name.
"""
if "db_url" in metafunc.fixturenames:
sqlite_dialects = ["sqlite-spatialite3", "sqlite-spatialite4", "geopackage"]
dialects = None
if metafunc.module.__name__ == "tests.test_functional_postgresql":
dialects = ["postgresql"]
elif metafunc.module.__name__ == "tests.test_functional_sqlite":
dialects = sqlite_dialects
elif metafunc.module.__name__ == "tests.test_functional_mysql":
dialects = ["mysql", "mariadb"]
elif metafunc.module.__name__ == "tests.test_functional_geopackage":
dialects = ["geopackage"]
if getattr(metafunc.function, "tested_dialects", False):
dialects = metafunc.function.tested_dialects
elif getattr(metafunc.cls, "tested_dialects", False):
dialects = metafunc.cls.tested_dialects
if dialects is None:
dialects = ["mysql", "mariadb", "postgresql"] + sqlite_dialects
if "sqlite" in dialects:
# Order dialects
dialects = [i for i in dialects if i != "sqlite"] + sqlite_dialects
metafunc.parametrize("db_url", dialects, indirect=True)
@pytest.fixture(scope="session")
def db_url_postgresql(request):
return (
request.config.getoption("--postgresql_dburl")
or os.getenv("PYTEST_POSTGRESQL_DB_URL")
or "postgresql://gis:gis@localhost/gis"
)
@pytest.fixture(scope="session")
def db_url_mysql(request, tmpdir_factory):
return (
request.config.getoption("--mysql_dburl")
or os.getenv("PYTEST_MYSQL_DB_URL")
or "mysql://gis:gis@localhost/gis"
)
@pytest.fixture(scope="session")
def db_url_mariadb(request, tmpdir_factory):
return (
request.config.getoption("--mariadb_dburl")
or os.getenv("PYTEST_MARIADB_DB_URL")
or "mariadb://gis:gis@localhost/gis"
)
@pytest.fixture(scope="session")
def db_url_sqlite_spatialite3(request, tmpdir_factory):
return (
request.config.getoption("--sqlite_spatialite3_dburl")
or os.getenv("PYTEST_SPATIALITE3_DB_URL")
or f"sqlite:///{Path(__file__).parent / 'data' / 'spatialite_lt_4.sqlite'}"
)
@pytest.fixture(scope="session")
def db_url_sqlite_spatialite4(request, tmpdir_factory):
return (
request.config.getoption("--sqlite_spatialite4_dburl")
or os.getenv("PYTEST_SPATIALITE4_DB_URL")
or f"sqlite:///{Path(__file__).parent / 'data' / 'spatialite_ge_4.sqlite'}"
)
@pytest.fixture(scope="session")
def db_url_geopackage(request, tmpdir_factory):
return (
request.config.getoption("--sqlite_geopackage_dburl")
or os.getenv("PYTEST_GEOPACKAGE_DB_URL")
or f"gpkg:///{Path(__file__).parent / 'data' / 'spatialite_geopackage.gpkg'}"
)
@pytest.fixture(scope="session")
def db_url(
request,
db_url_postgresql,
db_url_sqlite_spatialite3,
db_url_sqlite_spatialite4,
db_url_geopackage,
db_url_mysql,
db_url_mariadb,
):
if request.param == "postgresql":
return db_url_postgresql
if request.param == "mysql":
return db_url_mysql
if request.param == "mariadb":
return db_url_mariadb
if request.param == "sqlite-spatialite3":
return db_url_sqlite_spatialite3
if request.param == "sqlite-spatialite4":
return db_url_sqlite_spatialite4
if request.param == "geopackage":
return db_url_geopackage
return None
@pytest.fixture(scope="session")
def _engine_echo(request):
_engine_echo = request.config.getoption("--engine-echo")
return _engine_echo
@pytest.fixture(scope="session")
def _require_all_dialects(request):
_require_all_dialects = request.config.getoption("--require-all-dialects")
return _require_all_dialects
@pytest.fixture
def engine(tmpdir, db_url, _engine_echo, _require_all_dialects):
"""Provide an engine to test database."""
try:
if db_url.startswith("mysql://") or db_url.startswith("mariadb://"):
pool_kwargs = {"poolclass": NullPool}
else:
pool_kwargs = {}
if db_url.startswith("sqlite:///"):
# Copy the input SQLite DB to a temporary file and return an engine to it
input_url = str(db_url)[10:]
output_file = "test_spatial_db.sqlite"
current_engine = copy_and_connect_sqlite_db(
input_url, tmpdir / output_file, _engine_echo, "sqlite"
)
elif db_url.startswith("gpkg:///"):
# Copy the input GeoPackage to a temporary file and return an engine to it
input_url = str(db_url)[8:]
output_file = "test_spatial_db.gpkg"
current_engine = copy_and_connect_sqlite_db(
input_url, tmpdir / output_file, _engine_echo, "gpkg"
)
else:
# For other dialects the engine is directly returned
current_engine = create_engine(
db_url,
echo=_engine_echo,
plugins=["geoalchemy2"],
**pool_kwargs,
)
current_engine.update_execution_options(search_path=["gis", "public"])
except Exception as exc:
msg = f"Could not create engine for this URL: {db_url}\nThe exception was: {exc}"
if _require_all_dialects:
pytest.fail("All dialects are required. " + msg)
else:
pytest.skip(reason=msg)
# Disambiguate MySQL and MariaDB
if current_engine.dialect.name in ["mysql", "mariadb"]:
try:
with current_engine.connect() as connection:
mysql_type = (
"MariaDB"
if "mariadb" in connection.execute(text("SELECT VERSION();")).scalar().lower()
else "MySQL"
)
if current_engine.dialect.name.lower() != mysql_type.lower():
msg = f"Can not execute {mysql_type} queries on {db_url}"
if _require_all_dialects:
pytest.fail("All dialects are required. " + msg)
else:
pytest.skip(reason=msg)
except InvalidRequestError:
msg = f"Can not execute MariaDB queries on {db_url}"
if _require_all_dialects:
pytest.fail("All dialects are required. " + msg)
else:
pytest.skip(reason=msg)
with current_engine.connect() as connection:
versions = get_versions(connection)
print_versions(versions)
try:
yield current_engine
finally:
current_engine.dispose()
@pytest.fixture
def check_spatialite():
if "SPATIALITE_LIBRARY_PATH" not in os.environ:
pytest.skip("SPATIALITE_LIBRARY_PATH is not defined, skip SpatiaLite tests")
@pytest.fixture
def dialect_name(engine):
return engine.dialect.name
@pytest.fixture
def conn(engine):
"""Provide a connection to test database."""
with engine.connect() as connection:
trans = connection.begin()
try:
yield connection
finally:
if trans.is_active:
trans.rollback()
@pytest.fixture
def session(engine, conn):
Session = sessionmaker(bind=conn)
with Session(bind=conn) as session:
yield session
@pytest.fixture
def schema(engine):
if engine.dialect.name == "postgresql":
return "gis"
else:
return None
@pytest.fixture
def metadata():
return MetaData()
@pytest.fixture()
def base(metadata):
return declarative_base(metadata=metadata)
@pytest.fixture
def postgis_version(conn):
return get_postgis_major_version(conn)
@pytest.fixture
def postgres_major_version(conn):
return get_postgres_major_version(conn)
@pytest.fixture(autouse=True)
def reset_alembic_monkeypatch():
"""Disable Alembic monkeypatching by default."""
try:
normal_behavior_sqlite = SQLiteDialect._get_indexes_normal_behavior
SQLiteDialect.get_indexes = normal_behavior_sqlite
SQLiteDialect._get_indexes_normal_behavior = normal_behavior_sqlite
normal_behavior_mysql = MySQLDialect._get_indexes_normal_behavior
MySQLDialect.get_indexes = normal_behavior_mysql
MySQLDialect._get_indexes_normal_behavior = normal_behavior_mysql
except AttributeError:
pass
@pytest.fixture()
def use_alembic_monkeypatch():
"""Enable Alembic monkeypatching ."""
_monkey_patch_get_indexes_for_sqlite()
_monkey_patch_get_indexes_for_mysql()
@pytest.fixture
def setup_tables(conn, metadata):
metadata.drop_all(conn, checkfirst=True)
metadata.create_all(conn)
yield
|