diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/test_cli.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py index 5699bb3..1507544 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -900,3 +900,95 @@ def test_users_cli_accepts_minimum_whitespace_and_unbounded_passwords( assert result.exit_code == 0, result.output user = _get_user(app, "alice") assert user is not None and user.check_password(passphrase) + + +def test_all_migrations_can_downgrade_to_empty_schema(database_app: Flask) -> None: + """Exercise every downgrade, including the two merged migration branches.""" + with database_app.app_context(): + migrate_upgrade() + assert _database_revision(database_app) == _head_revision(database_app) + assert "received_webmentions" in inspect(db.engine).get_table_names() + + migrate_downgrade(revision="base") + with db.engine.connect() as connection: + tables = set(inspect(connection).get_table_names()) + # Alembic may keep its empty version table after downgrading to base. + assert tables <= {"alembic_version"} + if "alembic_version" in tables: + assert ( + connection.execute(sa.text("SELECT count(*) FROM alembic_version")) + .scalar_one() + == 0 + ) + + +def test_fingerprint_migration_backfills_existing_sent_target( + database_app: Flask, +) -> None: + from webmentions_ssg.models.utils import target_digest + + source_uuid = "0" * 31 + "1" + sent_uuid = "0" * 31 + "2" + target = "https://target.example/legacy" + with database_app.app_context(): + migrate_upgrade(revision="c0272f25ce17") + with db.engine.begin() as connection: + connection.execute( + sa.text( + "INSERT INTO sources (uuid, path, url, content_hash, revision, " + "last_seen_at, revised_at) VALUES (:uuid, 'post/index.html', " + "'https://source.example/post', :hash, 1, " + "CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" + ), + {"uuid": source_uuid, "hash": "0" * 32}, + ) + connection.execute( + sa.text( + "INSERT INTO sent_webmentions " + "(uuid, source_id, target, active, desired_revision, attempt_count) " + "VALUES (:uuid, :source_id, :target, 1, 1, 0)" + ), + {"uuid": sent_uuid, "source_id": source_uuid, "target": target}, + ) + + migrate_upgrade(revision="d845aa7fbc19") + with db.engine.connect() as connection: + row = connection.execute( + sa.text("SELECT target, target_digest FROM sent_webmentions") + ).one() + assert row.target == target + assert row.target_digest == target_digest(target) + assert any( + constraint["column_names"] == ["source_id", "target_digest"] + for constraint in inspect(connection).get_unique_constraints( + "sent_webmentions" + ) + ) + + +def test_autogenerate_skips_empty_migration_revisions( + database_app: Flask, monkeypatch: pytest.MonkeyPatch +) -> None: + from types import SimpleNamespace + from unittest.mock import Mock + + with database_app.app_context(): + migrate_upgrade(revision="f63641044cc1") + callback = database_app.extensions["migrate"].configure_args[ + "process_revision_directives" + ] + config = callback.__globals__["config"] + monkeypatch.setattr( + config, "cmd_opts", SimpleNamespace(autogenerate=True), raising=False + ) + empty = Mock() + empty.upgrade_ops.is_empty.return_value = True + directives = [empty] + callback(None, None, directives) + assert directives == [] + + nonempty = Mock() + nonempty.upgrade_ops.is_empty.return_value = False + directives = [nonempty] + callback(None, None, directives) + assert directives == [nonempty] |
