summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--alembic/environment.py5
-rw-r--r--docs/build/changelog.rst8
-rw-r--r--tests/test_environment.py64
3 files changed, 76 insertions, 1 deletions
diff --git a/alembic/environment.py b/alembic/environment.py
index 0c9095d..cba3beb 100644
--- a/alembic/environment.py
+++ b/alembic/environment.py
@@ -246,7 +246,10 @@ class EnvironmentContext(object):
:attr:`.Config.cmd_opts`
"""
- value = self.config.cmd_opts.x or []
+ if self.config.cmd_opts is not None:
+ value = self.config.cmd_opts.x or []
+ else:
+ value = []
if as_dictionary:
value = dict(
arg.split('=', 1) for arg in value
diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst
index b763c74..a6cefc6 100644
--- a/docs/build/changelog.rst
+++ b/docs/build/changelog.rst
@@ -6,6 +6,14 @@ Changelog
:version: 0.6.5
.. change::
+ :tags: bug, environment
+ :tickets: 195
+
+ Fixed bug where :meth:`.EnvironmentContext.get_x_argument`
+ would fail if the :class:`.Config` in use didn't actually
+ originate from a command line call.
+
+ .. change::
:tags: bug, autogenerate
:tickets: 194
diff --git a/tests/test_environment.py b/tests/test_environment.py
new file mode 100644
index 0000000..d7a4554
--- /dev/null
+++ b/tests/test_environment.py
@@ -0,0 +1,64 @@
+#!coding: utf-8
+
+from alembic.config import Config
+from alembic.script import ScriptDirectory
+from alembic.environment import EnvironmentContext
+import unittest
+from . import Mock, call, _no_sql_testing_config, staging_env, clear_staging_env
+
+from . import eq_
+
+class EnvironmentTest(unittest.TestCase):
+ def setUp(self):
+ staging_env()
+ self.cfg = _no_sql_testing_config()
+
+ def tearDown(self):
+ clear_staging_env()
+
+ def _fixture(self, **kw):
+ script = ScriptDirectory.from_config(self.cfg)
+ env = EnvironmentContext(
+ self.cfg,
+ script,
+ **kw
+ )
+ return env
+
+ def test_x_arg(self):
+ env = self._fixture()
+ self.cfg.cmd_opts = Mock(x="y=5")
+ eq_(
+ env.get_x_argument(),
+ "y=5"
+ )
+
+ def test_x_arg_asdict(self):
+ env = self._fixture()
+ self.cfg.cmd_opts = Mock(x=["y=5"])
+ eq_(
+ env.get_x_argument(as_dictionary=True),
+ {"y": "5"}
+ )
+
+ def test_x_arg_no_opts(self):
+ env = self._fixture()
+ eq_(
+ env.get_x_argument(),
+ []
+ )
+
+ def test_x_arg_no_opts_asdict(self):
+ env = self._fixture()
+ eq_(
+ env.get_x_argument(as_dictionary=True),
+ {}
+ )
+
+ def test_tag_arg(self):
+ env = self._fixture(tag="x")
+ eq_(
+ env.get_tag_argument(),
+ "x"
+ )
+