def _test_validate_head_file_helper(self, heads, file_heads=None,
                                        branchless=False):
        if file_heads is None:
            file_heads = []
        fake_config = self.configs[0]
        mock_open = self.useFixture(
            tools.OpenFixture(cli._get_head_file_path(fake_config),
                              '\n'.join(file_heads))
        ).mock_open
        with mock.patch('alembic.script.ScriptDirectory.from_config') as fc,\
                mock.patch.object(cli, '_use_separate_migration_branches',
                                  return_value=not branchless):
            fc.return_value.get_heads.return_value = heads

            if not branchless or all(head in file_heads for head in heads):
                cli.validate_head_file(fake_config)
            else:
                self.assertRaises(
                    SystemExit,
                    cli.validate_head_file,
                    fake_config
                )
                self.assertTrue(self.mock_alembic_err.called)

            if branchless:
                mock_open.assert_called_with(
                    cli._get_head_file_path(fake_config))
                fc.assert_called_once_with(fake_config)
            else:
                self.assertFalse(mock_open.called)
                self.assertFalse(fc.called)
Beispiel #2
0
    def _test_validate_head_file_helper(self,
                                        heads,
                                        file_heads=None,
                                        branchless=False):
        if file_heads is None:
            file_heads = []
        fake_config = self.configs[0]
        mock_open = self.useFixture(
            tools.OpenFixture(cli._get_head_file_path(fake_config),
                              '\n'.join(file_heads))).mock_open
        with mock.patch('alembic.script.ScriptDirectory.from_config') as fc,\
                mock.patch.object(cli, '_use_separate_migration_branches',
                                  return_value=not branchless):
            fc.return_value.get_heads.return_value = heads

            if not branchless or all(head in file_heads for head in heads):
                cli.validate_head_file(fake_config)
            else:
                self.assertRaises(SystemExit, cli.validate_head_file,
                                  fake_config)
                self.assertTrue(self.mock_alembic_err.called)

            if branchless:
                mock_open.assert_called_with(
                    cli._get_head_file_path(fake_config))
                fc.assert_called_once_with(fake_config)
            else:
                self.assertFalse(mock_open.called)
                self.assertFalse(fc.called)
    def _test_validate_head_file_helper(self, heads, file_heads=None,
                                        branchless=False):
        if file_heads is None:
            file_heads = []
        fake_config = self.configs[0]
        with mock.patch('alembic.script.ScriptDirectory.from_config') as fc,\
                mock.patch.object(cli, '_use_separate_migration_branches',
                                  return_value=not branchless):
            fc.return_value.get_heads.return_value = heads
            with mock.patch('six.moves.builtins.open') as mock_open:
                mock_open.return_value.__enter__ = lambda s: s
                mock_open.return_value.__exit__ = mock.Mock()
                mock_open.return_value.read.return_value = (
                    '\n'.join(file_heads))

                if not branchless or all(head in file_heads for head in heads):
                    cli.validate_head_file(fake_config)
                else:
                    self.assertRaises(
                        SystemExit,
                        cli.validate_head_file,
                        fake_config
                    )
                    self.assertTrue(self.mock_alembic_err.called)

                if branchless:
                    mock_open.assert_called_with(
                        cli._get_head_file_path(fake_config))
                else:
                    self.assertFalse(mock_open.called)

            if branchless:
                fc.assert_called_once_with(fake_config)
            else:
                self.assertFalse(fc.called)
Beispiel #4
0
    def _test_validate_head_files_helper(self,
                                         heads,
                                         contract_head='',
                                         expand_head=''):
        fake_config = self.configs[0]
        head_files_not_exist = (contract_head == expand_head == '')
        with mock.patch('alembic.script.ScriptDirectory.from_config') as fc,\
                mock.patch('os.path.exists') as os_mock,\
                mock.patch.object(cli, '_use_separate_migration_branches',
                                  return_value=True):
            if head_files_not_exist:
                os_mock.return_value = False
            else:
                os_mock.return_value = True

            fc.return_value.get_heads.return_value = heads

            revs = {
                heads[0]: FakeRevision(labels=cli.CONTRACT_BRANCH),
                heads[1]: FakeRevision(labels=cli.EXPAND_BRANCH)
            }
            fc.return_value.get_revision.side_effect = revs.__getitem__
            mock_open_con = self.useFixture(
                tools.OpenFixture(
                    cli._get_contract_head_file_path(fake_config),
                    contract_head + '\n')).mock_open
            mock_open_ex = self.useFixture(
                tools.OpenFixture(cli._get_expand_head_file_path(fake_config),
                                  expand_head + '\n')).mock_open

            if contract_head in heads and expand_head in heads:
                cli.validate_head_file(fake_config)
            elif head_files_not_exist:
                cli.validate_head_file(fake_config)
                self.assertTrue(self.mock_alembic_warn.called)
            else:
                self.assertRaises(SystemExit, cli.validate_head_file,
                                  fake_config)
                self.assertTrue(self.mock_alembic_err.called)

            if contract_head in heads and expand_head in heads:
                mock_open_ex.assert_called_with(
                    cli._get_expand_head_file_path(fake_config))
                mock_open_con.assert_called_with(
                    cli._get_contract_head_file_path(fake_config))

            if not head_files_not_exist:
                fc.assert_called_once_with(fake_config)
Beispiel #5
0
    def _test_validate_head_files_helper(self, heads, contract_head='',
                                         expand_head=''):
        fake_config = self.configs[0]
        head_files_not_exist = (contract_head == expand_head == '')
        with mock.patch('alembic.script.ScriptDirectory.from_config') as fc,\
                mock.patch('os.path.exists') as os_mock,\
                mock.patch.object(cli, '_use_separate_migration_branches',
                                  return_value=True):
            if head_files_not_exist:
                os_mock.return_value = False
            else:
                os_mock.return_value = True

            fc.return_value.get_heads.return_value = heads

            revs = {heads[0]: FakeRevision(labels=cli.CONTRACT_BRANCH),
                    heads[1]: FakeRevision(labels=cli.EXPAND_BRANCH)}
            fc.return_value.get_revision.side_effect = revs.__getitem__
            mock_open_con = self.useFixture(
                tools.OpenFixture(cli._get_contract_head_file_path(
                    fake_config), contract_head + '\n')).mock_open
            mock_open_ex = self.useFixture(
                tools.OpenFixture(cli._get_expand_head_file_path(
                    fake_config), expand_head + '\n')).mock_open

            if contract_head in heads and expand_head in heads:
                cli.validate_head_file(fake_config)
            elif head_files_not_exist:
                cli.validate_head_file(fake_config)
                self.assertTrue(self.mock_alembic_warn.called)
            else:
                self.assertRaises(
                    SystemExit,
                    cli.validate_head_file,
                    fake_config
                )
                self.assertTrue(self.mock_alembic_err.called)

            if contract_head in heads and expand_head in heads:
                mock_open_ex.assert_called_with(
                    cli._get_expand_head_file_path(fake_config))
                mock_open_con.assert_called_with(
                    cli._get_contract_head_file_path(fake_config))

            if not head_files_not_exist:
                fc.assert_called_once_with(fake_config)
Beispiel #6
0
    def _test_validate_head_file_helper(self, heads, file_content=None):
        with mock.patch("alembic.script.ScriptDirectory.from_config") as fc:
            fc.return_value.get_heads.return_value = heads
            fc.return_value.get_current_head.return_value = heads[0]
            with mock.patch("six.moves.builtins.open") as mock_open:
                mock_open.return_value.__enter__ = lambda s: s
                mock_open.return_value.__exit__ = mock.Mock()
                mock_open.return_value.read.return_value = file_content

                with mock.patch("os.path.isfile") as is_file:
                    is_file.return_value = file_content is not None

                    if file_content in heads:
                        cli.validate_head_file(mock.sentinel.config)
                    else:
                        self.assertRaises(SystemExit, cli.validate_head_file, mock.sentinel.config)
                        self.mock_alembic_err.assert_called_once_with(mock.ANY)
            fc.assert_called_once_with(mock.sentinel.config)
Beispiel #7
0
    def _test_validate_head_file_helper(self, heads, file_content=None):
        with mock.patch('alembic.script.ScriptDirectory.from_config') as fc:
            fc.return_value.get_heads.return_value = heads
            fc.return_value.get_current_head.return_value = heads[0]
            with mock.patch('__builtin__.open') as mock_open:
                mock_open.return_value.__enter__ = lambda s: s
                mock_open.return_value.__exit__ = mock.Mock()
                mock_open.return_value.read.return_value = file_content

                with mock.patch('os.path.isfile') as is_file:
                    is_file.return_value = file_content is not None

                    if file_content in heads:
                        cli.validate_head_file(mock.sentinel.config)
                    else:
                        self.assertRaises(SystemExit, cli.validate_head_file,
                                          mock.sentinel.config)
                        self.mock_alembic_err.assert_called_once_with(mock.ANY)
            fc.assert_called_once_with(mock.sentinel.config)