From d8055d52e5c6f3e0dfd49b857f715edae6520e03 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Fri, 2 Dec 2016 18:14:57 +0000 Subject: [PATCH] Support alembic 0.8.9 in test_autogen_process_directives The test case validates that autogenerated alembic commands meet our expectations. The new alembic version adds a leading '# ' to each autogenerated comment to make flake8 happy. This patch adopts the test case to handle both new and older versions. This is achieved by switching from exact match to using a regexp. Change-Id: I9ca411e5b3d20412fffa05f6eb79659f6c56f3fd Closes-Bug: #1647027 --- neutron/tests/unit/db/test_migration.py | 29 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/neutron/tests/unit/db/test_migration.py b/neutron/tests/unit/db/test_migration.py index fa403b78b0c..09d7121be61 100644 --- a/neutron/tests/unit/db/test_migration.py +++ b/neutron/tests/unit/db/test_migration.py @@ -15,6 +15,7 @@ import copy import os +import re import sys import textwrap @@ -27,6 +28,7 @@ import mock from oslo_utils import fileutils import pkg_resources import sqlalchemy as sa +from testtools import matchers from neutron.db import migration from neutron.db.migration import autogen @@ -674,8 +676,13 @@ class TestCli(base.BaseTestCase): self.assertTrue(expand.downgrade_ops.is_empty()) self.assertTrue(contract.downgrade_ops.is_empty()) - self.assertEqual( - textwrap.dedent("""\ + def _get_regex(s): + s = textwrap.dedent(s) + s = re.escape(s) + # alembic 0.8.9 added additional leading '# ' before comments + return s.replace('\\#\\#\\#\\ ', '(# )?### ') + + expected_regex = ("""\ ### commands auto generated by Alembic - please adjust! ### op.create_table('organization', sa.Column('id', sa.Integer(), nullable=False), @@ -686,17 +693,19 @@ class TestCli(base.BaseTestCase): """sa.Column('organization_id', sa.Integer(), nullable=True)) op.create_foreign_key('org_fk', 'user', """ """'organization', ['organization_id'], ['id']) - ### end Alembic commands ###"""), - alembic_ag_api.render_python_code(expand.upgrade_ops) - ) - self.assertEqual( - textwrap.dedent("""\ + ### end Alembic commands ###""") + self.assertThat( + alembic_ag_api.render_python_code(expand.upgrade_ops), + matchers.MatchesRegex(_get_regex(expected_regex))) + + expected_regex = ("""\ ### commands auto generated by Alembic - please adjust! ### op.drop_constraint('user', 'uq_user_org', type_=None) op.drop_column('user', 'organization_name') - ### end Alembic commands ###"""), - alembic_ag_api.render_python_code(contract.upgrade_ops) - ) + ### end Alembic commands ###""") + self.assertThat( + alembic_ag_api.render_python_code(contract.upgrade_ops), + matchers.MatchesRegex(_get_regex(expected_regex))) @mock.patch('alembic.script.ScriptDirectory.walk_revisions') def test__find_milestone_revisions_one_branch(self, walk_mock):