示例#1
0
    def test_upgrade_empty(self, db_conn, setup_teardown):
        """ Test with 0 upgrade tests. """
        conn = db_conn

        try:
            current_version = int(_fetch_version(conn))
        except psycopg2.ProgrammingError:
            pytest.fail("Version cannot be fetched.")

        # Reparse the connection string from the fixture, because
        # upgrade script does upgrading with own connection & cursor
        db_host, db_user, db_name, db_port = _parse_dsn(conn.dsn)

        os.environ["POSTGRESQL_DATABASE"] = db_name
        os.environ["POSTGRESQL_USER"] = db_user
        os.environ["POSTGRESQL_HOST"] = db_host
        os.environ["POSTGRESQL_PORT"] = db_port

        from database import upgrade

        os.environ["DB_UPGRADE_SCRIPTS_DIR"] = DB_UPDATES_FIXTURE_PATH
        upgrade.main()

        new_version = _fetch_version(conn)
        assert new_version == current_version

        logs = _fetch_version_logs(conn)
        assert not logs
示例#2
0
    def test_upgrade_multiple(self, db_conn, setup_teardown):
        """ Test with multiple test upgrade scripts. """
        conn = db_conn

        try:
            current_version = int(_fetch_version(conn))
        except psycopg2.ProgrammingError:
            pytest.fail("Version cannot be fetched.")

        script_locations = []
        max_script = current_version + SCRIPT_COUNT

        for dummy_version in range(current_version, max_script + 1):
            script_locations.append(_create_script(dummy_version, TEST_SCRIPT_1))

        # Reparse the connection string from the fixture, because
        # upgrade script does upgrading with own connection & cursor
        db_host, db_user, db_name, db_port = _parse_dsn(conn.dsn)

        os.environ["POSTGRESQL_DATABASE"] = db_name
        os.environ["POSTGRESQL_USER"] = db_user
        os.environ["POSTGRESQL_HOST"] = db_host
        os.environ["POSTGRESQL_PORT"] = db_port

        from database import upgrade

        os.environ["DB_UPGRADE_SCRIPTS_DIR"] = DB_UPDATES_FIXTURE_PATH

        upgrade.main()

        new_version = int(_fetch_version(conn))
        assert new_version == max_script

        logs = _fetch_version_logs(conn)
        script_num = 0
        while script_num < SCRIPT_COUNT:
            correct_result = (current_version + script_num,
                              "complete",
                              script_locations[script_num].split("/")[-1],
                              0)
            assert correct_result in logs
            script_num += 1
示例#3
0
    def test_upgrade_single(self, db_conn, setup_teardown):
        """ Test with only single upgrade script. """
        conn = db_conn

        try:
            current_version = int(_fetch_version(conn))
        except psycopg2.ProgrammingError:
            pytest.fail("Version cannot be fetched.")

        test_version = current_version + 1
        script_location = _create_script(test_version, TEST_SCRIPT_1)

        # Reparse the connection string from the fixture, because
        # upgrade script does upgrading with own connection & cursor
        db_host, db_user, db_name, db_port = _parse_dsn(conn.dsn)

        os.environ["POSTGRESQL_DATABASE"] = db_name
        os.environ["POSTGRESQL_USER"] = db_user
        os.environ["POSTGRESQL_HOST"] = db_host
        os.environ["POSTGRESQL_PORT"] = db_port

        from database import upgrade

        os.environ["DB_UPGRADE_SCRIPTS_DIR"] = DB_UPDATES_FIXTURE_PATH

        upgrade.main()

        new_version = _fetch_version(conn)
        assert new_version == test_version

        logs = _fetch_version_logs(conn)
        log = logs.pop()

        assert log[VERSION] == test_version
        assert log[STATUS] == "complete"
        assert log[SCRIPT] == script_location.split("/")[-1]
        assert log[RETURNCODE] == 0
示例#4
0
    def test_upgrade(self, pg_old, pg_new):
        """ Test the upgrades in folders. """
        dsn_old = pg_old.dsn()
        dsn_new = pg_new.dsn()

        os.environ["POSTGRESQL_DATABASE"] = str(dsn_old["database"])
        os.environ["POSTGRESQL_USER"] = "******"
        os.environ["POSTGRESQL_HOST"] = str(dsn_old["host"])
        os.environ["POSTGRESQL_PORT"] = str(dsn_old["port"])

        # pylint: disable=import-outside-toplevel
        from database import upgrade
        upgrade.main()

        schema_old_args = [
            "pg_dump", "-s", "-h",
            str(dsn_old["host"]), "-p",
            str(dsn_old["port"]), "-U",
            str(dsn_old["user"]), "-d",
            str(dsn_old["database"])
        ]

        schema_new_args = [
            "pg_dump", "-s", "-h",
            str(dsn_new["host"]), "-p",
            str(dsn_new["port"]), "-U",
            str(dsn_new["user"]), "-d",
            str(dsn_new["database"])
        ]

        dump_old = _dump_database(schema_old_args)
        dump_new = _dump_database(schema_new_args)

        try:
            assert dump_old == dump_new
        except AssertionError:
            diff = difflib.unified_diff(dump_old, dump_new)
            diffs = "\n".join([x for x in diff])
            assert False, f"Diffs:\n{diffs}"

        data_args = ["pg_dump", "-a"]
        for table in STATIC_TABLES:
            data_args.append("-t")
            data_args.append(table)

        data_old_args = data_args + [
            "-h",
            str(dsn_old["host"]), "-p",
            str(dsn_old["port"]), "-U",
            str(dsn_old["user"]), "-d",
            str(dsn_old["database"])
        ]

        data_new_args = data_args + [
            "-h",
            str(dsn_new["host"]), "-p",
            str(dsn_new["port"]), "-U",
            str(dsn_new["user"]), "-d",
            str(dsn_new["database"])
        ]

        old_data_dump = _dump_database(data_old_args)
        new_data_dump = _dump_database(data_new_args)

        try:
            assert old_data_dump == new_data_dump
        except AssertionError:
            diff = difflib.unified_diff(old_data_dump, new_data_dump)
            diffs = "\n".join([x for x in diff])
            assert False, f"Diff:\n{diffs}"