def test_config_file_default(self): cl = config.CommandLine() with mock.patch.object(cl, "run_cmd") as run_cmd: cl.main(argv=["list_templates"]) cfg = run_cmd.mock_calls[0][1][0] eq_(cfg.config_file_name, "alembic.ini")
def test_run_cmd_args_missing(self): canary = mock.Mock() orig_revision = command.revision # the command function has "process_revision_directives" # however the ArgumentParser does not. ensure things work def revision( config, message=None, autogenerate=False, sql=False, head="head", splice=False, branch_label=None, version_path=None, rev_id=None, depends_on=None, process_revision_directives=None, ): canary(config, message=message) revision.__module__ = "alembic.command" # CommandLine() pulls the function into the ArgumentParser # and needs the full signature, so we can't patch the "revision" # command normally as ArgumentParser gives us no way to get to it. config.command.revision = revision try: commandline = config.CommandLine() options = commandline.parser.parse_args(["revision", "-m", "foo"]) commandline.run_cmd(self.cfg, options) finally: config.command.revision = orig_revision eq_(canary.mock_calls, [mock.call(self.cfg, message="foo")])
def test_stamp_argparser_single_rev(self): cmd = config.CommandLine() options = cmd.parser.parse_args(["stamp", self.c, "--sql"]) with capture_context_buffer() as buf: cmd.run_cmd(self.cfg, options) assert ("INSERT INTO alembic_version (version_num) VALUES ('%s')" % self.c in buf.getvalue())
def test_config_file_c_override(self): cl = config.CommandLine() with mock.patch.object(cl, "run_cmd") as run_cmd: cl.main(argv=["-c", "myconf.ini", "list_templates"]) cfg = run_cmd.mock_calls[0][1][0] eq_(cfg.config_file_name, "myconf.ini")
def test_config_file_env_variable_c_override(self): os.environ["ALEMBIC_CONFIG"] = "/foo/bar/bat.conf" cl = config.CommandLine() with mock.patch.object(cl, "run_cmd") as run_cmd: cl.main(argv=["-c", "myconf.conf", "list_templates"]) cfg = run_cmd.mock_calls[0][1][0] eq_(cfg.config_file_name, "myconf.conf")
def test_stamp_argparser_multiple_rev(self): cmd = config.CommandLine() options = cmd.parser.parse_args(["stamp", self.b, self.c, "--sql"]) with capture_context_buffer() as buf: cmd.run_cmd(self.cfg, options) # TODO: this is still wrong, as this stamp command is putting # conflicting heads into the table. The test here is only to test # that the revisions are passed as a list. assert ("INSERT INTO alembic_version (version_num) VALUES ('%s')" % self.b in buf.getvalue()) assert ("INSERT INTO alembic_version (version_num) VALUES ('%s')" % self.c in buf.getvalue())
def test_version_text(self): buf = compat.StringIO() to_mock = "sys.stdout" if util.compat.py3k else "sys.stderr" with mock.patch(to_mock, buf): try: config.CommandLine(prog="test_prog").main(argv=["--version"]) assert False except SystemExit: pass is_true("test_prog" in str(buf.getvalue())) is_true(__version__ in str(buf.getvalue()))
def test_help_text(self): commands = { fn.__name__ for fn in [getattr(command, n) for n in dir(command)] if inspect.isfunction(fn) and fn.__name__[0] != "_" and fn.__module__ == "alembic.command" } # make sure we found them assert commands.intersection( {"upgrade", "downgrade", "merge", "revision"} ) # catch help text coming intersection with mock.patch("alembic.config.ArgumentParser") as argparse: config.CommandLine() for kall in argparse().add_subparsers().mock_calls: for sub_kall in kall.call_list(): if sub_kall[0] == "add_parser": cmdname = sub_kall[1][0] help_text = sub_kall[2]["help"] if help_text: commands.remove(cmdname) # more than two spaces assert not re.search(r" ", help_text) # no markup stuff assert ":" not in help_text # no newlines assert "\n" not in help_text # ends with a period assert help_text.endswith(".") # not too long assert len(help_text) < 80 assert not commands, "Commands without help text: %s" % commands