def test_with_range(self):
        out = six.StringIO()
        cmd = Command(stdout=out)

        cmd.run_from_argv(["./manage.py sorcery", "upgrade", "tests.testapp", "-r", ":000000000000", "--no-color"])
        revs = db.execute("select * from public.alembic_version_tests_testapp").fetchall()
        self.assertEqual(revs, [("000000000000",)])
    def test(self):
        out = six.StringIO()
        cmd = Command(stdout=out)
        cmd.run_from_argv(["./manage.py sorcery", "upgrade", "--no-color"])

        revs = db.execute("select * from alembic_version_tests_testapp").fetchall()
        self.assertEqual({r.version_num for r in revs}, {"000000000001", "000000000000"})
    def test_with_range_no_app(self):
        out = six.StringIO()
        cmd = Command(stdout=out)

        with self.assertRaises(SystemExit):
            cmd.run_from_argv([
                "./manage.py sorcery", "upgrade", "-r", "000000000001",
                "--no-color"
            ])
    def test_catching_alembic_error(self):
        out = six.StringIO()
        err = six.StringIO()

        cmd = Command(stdout=out, stderr=err)

        with self.assertRaises(SystemExit):
            cmd.run_from_argv(["./manage.py sorcery", "upgrade", "tests.testapp", "-r", "revision", "--no-color"])

        err.seek(0)
        self.assertEqual(err.readlines(), ["CommandError: Can't locate revision identified by 'revision'\n"])
    def test_sql(self):
        out = six.StringIO()
        cmd = Command(stdout=out)
        cmd.run_from_argv(["./manage.py sorcery", "upgrade", "-s", "--no-color"])

        out.seek(0)
        lines = out.readlines()

        self.assertEqual(len(lines), 23)
        statements = [
            "-- Running upgrade  -> 000000000001\n",
            "INSERT INTO public.alembic_version_tests_testapp (version_num) VALUES ('000000000001');\n",
            "-- Running upgrade  -> 000000000000\n",
            "INSERT INTO public.alembic_version_tests_testapp (version_num) VALUES ('000000000000');\n",
        ]
        for statement in statements:
            self.assertIn(statement, lines)