Skip to content

Commit

Permalink
Merge pull request #43 from schireson/dc/extended-alembic-config
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Dec 2, 2021
2 parents 1bd4173 + c3cab87 commit cd9ca37
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest-alembic"
version = "0.6.0"
version = "0.6.1"
description = "A pytest plugin for verifying alembic migrations."
authors = [
"Dan Cardin <ddcardin@gmail.com>",
Expand Down
32 changes: 27 additions & 5 deletions src/pytest_alembic/config.py
Expand Up @@ -8,6 +8,17 @@
class Config:
"""Pytest-alembic configuration options.
Arguments:
- `config_options`: Meant to simplify the creation of ``alembic.config.Config``
objects. Supply keys common to customization in alembic configuration. For
example:
- file/config_file_name (commonly alembic.ini)
- script_location
- sqlalchemy.url
- target_metadata
- process_revision_directives
- include_schemas
- Both `before_revision_data` and `at_revision_data` are described in detail
in :ref:`Custom data`.
Expand Down Expand Up @@ -80,7 +91,12 @@ def from_raw_config(
)

def make_alembic_config(self, stdout):
file = self.config_options.get("file", "alembic.ini")
file = (
self.config_options.get("file")
or self.config_options.get("config_file_name")
or "alembic.ini"
)

alembic_config = self.config_options.get("alembic_config")

if not alembic_config and self.alembic_config:
Expand All @@ -89,21 +105,27 @@ def make_alembic_config(self, stdout):
else:
alembic_config = alembic.config.Config(file, stdout=stdout)

script_location = self.config_options.get("script_location")
target_metadata = self.config_options.get("target_metadata")
process_revision_directives = self.config_options.get("process_revision_directives")
include_schemas = self.config_options.get("include_schemas", True)
sqlalchemy_url = self.config_options.get("sqlalchemy.url")
if sqlalchemy_url:
alembic_config.set_main_option("sqlalchemy.url", sqlalchemy_url)

# Only set script_location if set.
script_location = self.config_options.get("script_location")
if script_location:
alembic_config.set_section_option("alembic", "script_location", script_location)
elif not alembic_config.get_section_option("alembic", "script_location"):
# Or in the event that it's not set after already having loaded the config.
alembic_config.set_main_option("script_location", "migrations")

target_metadata = self.config_options.get("target_metadata")
alembic_config.attributes["target_metadata"] = target_metadata

process_revision_directives = self.config_options.get("process_revision_directives")
alembic_config.attributes["process_revision_directives"] = process_revision_directives

include_schemas = self.config_options.get("include_schemas", True)
alembic_config.attributes["include_schemas"] = include_schemas

return alembic_config


Expand Down
2 changes: 2 additions & 0 deletions src/pytest_alembic/plugin/fixtures.py
Expand Up @@ -81,7 +81,9 @@ def alembic_config() -> Union[Dict[str, Any], alembic.config.Config, Config]:
The following common alembic config options are accepted as keys.
- file/config_file_name (commonly alembic.ini)
- script_location
- sqlalchemy.url
- target_metadata
- process_revision_directives
- include_schemas
Expand Down
45 changes: 45 additions & 0 deletions tests/test_config.py
@@ -0,0 +1,45 @@
from unittest import mock

from pytest_alembic.config import Config


def mock_open(*args, **kargs):
f_open = mock.mock_open(*args, **kargs)
f_open.return_value.__iter__ = lambda self: iter(self.readline, "")
return f_open


def test_default_file():
config = Config()

with mock.patch("builtins.open", mock_open(read_data="[alembic]")):
alembic_config = config.make_alembic_config(None)
assert alembic_config.config_file_name == "alembic.ini"


def test_set_file():
config = Config(config_options={"file": "foo.ini"})

with mock.patch("builtins.open", mock_open(read_data="[alembic]")):
alembic_config = config.make_alembic_config(None)
assert alembic_config.config_file_name == "foo.ini"


def test_set_config_file_name():
config = Config(config_options={"config_file_name": "foo.ini"})

with mock.patch("builtins.open", mock_open(read_data="[alembic]")):
alembic_config = config.make_alembic_config(None)
assert alembic_config.config_file_name == "foo.ini"


def test_set_sqlalchemy_url():
config = Config(config_options={"sqlalchemy.url": "sqlite:///"})
alembic_config = config.make_alembic_config(None)
assert alembic_config.get_main_option("sqlalchemy.url") == "sqlite:///"


def test_set_script_location():
config = Config(config_options={"script_location": "alembic"})
alembic_config = config.make_alembic_config(None)
assert alembic_config.get_main_option("script_location") == "alembic"

0 comments on commit cd9ca37

Please sign in to comment.