def test_backwards_no_migrations(self, print_: MagicMock):
     """
     Test running migrations backwards if none have been run previously.
     """
     run_sync(
         backwards(
             app_name="example_app",
             migration_id="2020-12-17T18:44:30",
             auto_agree=True,
         ))
     self.assertTrue(call("No migrations to reverse!") in print_.mock_calls)
    def test_backwards_no_migrations(self, print_):
        """
        Test running migrations backwards if none have been run previously.
        """
        with self.assertRaises(SystemExit) as manager:
            run_sync(
                backwards(
                    app_name="example_app",
                    migration_id="2020-12-17T18:44:30",
                    auto_agree=True,
                ))

        self.assertEqual(manager.exception.code, 0)
        self.assertTrue(
            print_.mock_calls[-1] == call("No migrations to reverse!"))
    def test_backwards_unknown_migration(self):
        """
        Test running an unknown migrations backwards.
        """
        run_sync(forwards(app_name="example_app", migration_id="all"))

        with self.assertRaises(SystemExit) as manager:
            run_sync(
                backwards(
                    app_name="example_app",
                    migration_id="migration-12345",
                    auto_agree=True,
                ))

        self.assertTrue(manager.exception.__str__().startswith(
            "Unrecognized migration name - must be one of "))
    def test_backwards_unknown_migration(self, print_: MagicMock):
        """
        Test running an unknown migrations backwards.
        """
        run_sync(forwards(app_name="music", migration_id="all"))

        with self.assertRaises(SystemExit):
            run_sync(
                backwards(
                    app_name="music",
                    migration_id="migration-12345",
                    auto_agree=True,
                ))

        self.assertTrue(
            tuple(print_.mock_calls[0])[1][0].startswith(
                "Unrecognized migration name - must be one of "))
    def test_forwards_backwards_all_migrations(self):
        """
        Test running all of the migrations forwards, then backwards.
        """
        for app_name in ("music", "all"):
            run_sync(forwards(app_name=app_name, migration_id="all"))

            # Check the tables exist
            for table_class in TABLE_CLASSES:
                self.assertTrue(table_class.table_exists().run_sync())

            run_sync(
                backwards(app_name=app_name,
                          migration_id="all",
                          auto_agree=True))

            # Check the tables don't exist
            for table_class in TABLE_CLASSES:
                self.assertTrue(not table_class.table_exists().run_sync())
    def test_forwards_backwards_single_migration(self):
        """
        Test running a single migrations forwards, then backwards.
        """
        for migration_id in ["1", "2020-12-17T18:44:30"]:
            run_sync(forwards(app_name="music", migration_id=migration_id))

            table_classes = [Band, Manager]

            # Check the tables exist
            for table_class in table_classes:
                self.assertTrue(table_class.table_exists().run_sync())

            run_sync(
                backwards(
                    app_name="music",
                    migration_id=migration_id,
                    auto_agree=True,
                ))

            # Check the tables don't exist
            for table_class in table_classes:
                self.assertTrue(not table_class.table_exists().run_sync())