diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-11-29 15:50:35 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-11-29 15:50:35 -0500 |
commit | 43f3a667fa1b75e75f23c11a50e35d7433ceab4f (patch) | |
tree | 5c29b5a2bcabfb226a6a5ab18fd2198317314932 /tests | |
parent | ebf9ab42f4eddd6cb06ce910737355d97511812d (diff) | |
download | alembic-43f3a667fa1b75e75f23c11a50e35d7433ceab4f.tar.gz |
- add begin_transaction() env.py helper. Emits the appropriate
begin/commit pair regardless of context.
- add dialect support for BEGIN/COMMIT working corresponding
to backend. Add implementation for SQL server.
- add tests for BEGIN/COMMIT , #11
- rework SQL server test suite for more classes of test
- fix test suite to clean up after a prior failed suite
Diffstat (limited to 'tests')
-rw-r--r-- | tests/__init__.py | 18 | ||||
-rw-r--r-- | tests/test_mssql.py | 129 | ||||
-rw-r--r-- | tests/test_sql_script.py | 11 |
3 files changed, 100 insertions, 58 deletions
diff --git a/tests/__init__.py b/tests/__init__.py index e44e23b..0c35747 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -14,6 +14,7 @@ import ConfigParser from nose import SkipTest from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.util import decorator +import shutil staging_directory = os.path.join(os.path.dirname(__file__), 'scratch') files_directory = os.path.join(os.path.dirname(__file__), 'files') @@ -74,9 +75,13 @@ def assert_compiled(element, assert_string, dialect=None): assert_string.replace("\n", "").replace("\t", "") ) -def capture_context_buffer(): +def capture_context_buffer(transactional_ddl=None): buf = StringIO.StringIO() + if transactional_ddl is not None: + context._context_opts['transactional_ddl'] = \ + transactional_ddl + class capture(object): def __enter__(self): context._context_opts['output_buffer'] = buf @@ -210,13 +215,13 @@ datefmt = %%H:%%M:%%S """ % (dir_, dir_)) -def no_sql_testing_config(): +def no_sql_testing_config(dialect="postgresql"): """use a postgresql url with no host so that connections guaranteed to fail""" dir_ = os.path.join(staging_directory, 'scripts') return _write_config_file(""" [alembic] script_location = %s -sqlalchemy.url = postgresql:// +sqlalchemy.url = %s:// [loggers] keys = root @@ -242,7 +247,7 @@ keys = generic format = %%(levelname)-5.5s [%%(name)s] %%(message)s datefmt = %%H:%%M:%%S -""" % (dir_)) +""" % (dir_, dialect)) def _write_config_file(text): cfg = _testing_config() @@ -254,7 +259,10 @@ def staging_env(create=True, template="generic"): from alembic import command, script cfg = _testing_config() if create: - command.init(cfg, os.path.join(staging_directory, 'scripts')) + path = os.path.join(staging_directory, 'scripts') + if os.path.exists(path): + shutil.rmtree(path) + command.init(cfg, path) sc = script.ScriptDirectory.from_config(cfg) context._opts(cfg,sc, fn=lambda:None) return sc diff --git a/tests/test_mssql.py b/tests/test_mssql.py index ab546e6..9392c6c 100644 --- a/tests/test_mssql.py +++ b/tests/test_mssql.py @@ -1,60 +1,83 @@ """Test op functions against MSSQL.""" -from tests import op_fixture -from alembic import op +from tests import op_fixture, capture_context_buffer, no_sql_testing_config, staging_env, three_rev_fixture, clear_staging_env +from alembic import op, command from sqlalchemy import Integer, Column, ForeignKey, \ UniqueConstraint, Table, MetaData, String from sqlalchemy.sql import table +from unittest import TestCase -def test_add_column(): - context = op_fixture('mssql') - op.add_column('t1', Column('c1', Integer, nullable=False)) - context.assert_("ALTER TABLE t1 ADD c1 INTEGER NOT NULL") - -def test_add_column_with_default(): - context = op_fixture("mssql") - op.add_column('t1', Column('c1', Integer, nullable=False, server_default="12")) - context.assert_("ALTER TABLE t1 ADD c1 INTEGER NOT NULL DEFAULT '12'") - -def test_alter_column_rename_mssql(): - context = op_fixture('mssql') - op.alter_column("t", "c", name="x") - context.assert_( - "EXEC sp_rename 't.c', 'x', 'COLUMN'" - ) - -def test_drop_column_w_default(): - context = op_fixture('mssql') - op.drop_column('t1', 'c1', mssql_drop_default=True) - context.assert_contains("exec('alter table t1 drop constraint ' + @const_name)") - context.assert_contains("ALTER TABLE t1 DROP COLUMN c1") - - -def test_drop_column_w_check(): - context = op_fixture('mssql') - op.drop_column('t1', 'c1', mssql_drop_check=True) - context.assert_contains("exec('alter table t1 drop constraint ' + @const_name)") - context.assert_contains("ALTER TABLE t1 DROP COLUMN c1") - -def test_alter_column_nullable(): - context = op_fixture('mssql') - op.alter_column("t", "c", nullable=True) - context.assert_( - "ALTER TABLE t ALTER COLUMN c NULL" - ) - -def test_alter_column_not_nullable(): - context = op_fixture('mssql') - op.alter_column("t", "c", nullable=False) - context.assert_( - "ALTER TABLE t ALTER COLUMN c SET NOT NULL" - ) - -# TODO: when we add schema support -#def test_alter_column_rename_mssql_schema(): -# context = op_fixture('mssql') -# op.alter_column("t", "c", name="x", schema="y") -# context.assert_( -# "EXEC sp_rename 'y.t.c', 'x', 'COLUMN'" -# ) + +class FullEnvironmentTests(TestCase): + @classmethod + def setup_class(cls): + env = staging_env() + cls.cfg = cfg = no_sql_testing_config("mssql") + + cls.a, cls.b, cls.c = \ + three_rev_fixture(cfg) + + @classmethod + def teardown_class(cls): + clear_staging_env() + + def test_begin_comit(self): + with capture_context_buffer(transactional_ddl=True) as buf: + command.upgrade(self.cfg, self.a, sql=True) + assert "BEGIN TRANSACTION" in buf.getvalue() + assert "COMMIT" in buf.getvalue() + +class OpTest(TestCase): + def test_add_column(self): + context = op_fixture('mssql') + op.add_column('t1', Column('c1', Integer, nullable=False)) + context.assert_("ALTER TABLE t1 ADD c1 INTEGER NOT NULL") + + + def test_add_column_with_default(self): + context = op_fixture("mssql") + op.add_column('t1', Column('c1', Integer, nullable=False, server_default="12")) + context.assert_("ALTER TABLE t1 ADD c1 INTEGER NOT NULL DEFAULT '12'") + + def test_alter_column_rename_mssql(self): + context = op_fixture('mssql') + op.alter_column("t", "c", name="x") + context.assert_( + "EXEC sp_rename 't.c', 'x', 'COLUMN'" + ) + + def test_drop_column_w_default(self): + context = op_fixture('mssql') + op.drop_column('t1', 'c1', mssql_drop_default=True) + context.assert_contains("exec('alter table t1 drop constraint ' + @const_name)") + context.assert_contains("ALTER TABLE t1 DROP COLUMN c1") + + + def test_drop_column_w_check(self): + context = op_fixture('mssql') + op.drop_column('t1', 'c1', mssql_drop_check=True) + context.assert_contains("exec('alter table t1 drop constraint ' + @const_name)") + context.assert_contains("ALTER TABLE t1 DROP COLUMN c1") + + def test_alter_column_nullable(self): + context = op_fixture('mssql') + op.alter_column("t", "c", nullable=True) + context.assert_( + "ALTER TABLE t ALTER COLUMN c NULL" + ) + + def test_alter_column_not_nullable(self): + context = op_fixture('mssql') + op.alter_column("t", "c", nullable=False) + context.assert_( + "ALTER TABLE t ALTER COLUMN c SET NOT NULL" + ) + + # TODO: when we add schema support + #def test_alter_column_rename_mssql_schema(self): + # context = op_fixture('mssql') + # op.alter_column("t", "c", name="x", schema="y") + # context.assert_( + # "EXEC sp_rename 'y.t.c', 'x', 'COLUMN'" + # ) diff --git a/tests/test_sql_script.py b/tests/test_sql_script.py index 1df94cd..a615bdf 100644 --- a/tests/test_sql_script.py +++ b/tests/test_sql_script.py @@ -14,6 +14,17 @@ def setup(): def teardown(): clear_staging_env() +def test_begin_comit(): + with capture_context_buffer(transactional_ddl=True) as buf: + command.upgrade(cfg, a, sql=True) + assert "BEGIN" in buf.getvalue() + assert "COMMIT" in buf.getvalue() + + with capture_context_buffer(transactional_ddl=False) as buf: + command.upgrade(cfg, a, sql=True) + assert "BEGIN" not in buf.getvalue() + assert "COMMIT" not in buf.getvalue() + def test_version_from_none_insert(): with capture_context_buffer() as buf: command.upgrade(cfg, a, sql=True) |