diff --git a/requirements-swh-server.txt b/requirements-swh-server.txt --- a/requirements-swh-server.txt +++ b/requirements-swh-server.txt @@ -1,4 +1,4 @@ -swh.core[http] >= 0.4 +swh.core[http,db] >= 0.4 swh.loader.core >= 0.0.71 swh.scheduler >= 0.7.0 swh.model >= 0.3.8 diff --git a/requirements-test.txt b/requirements-test.txt --- a/requirements-test.txt +++ b/requirements-test.txt @@ -7,3 +7,4 @@ requests_mock django-stubs djangorestframework-stubs >= 1.4 +django-test-migrations diff --git a/swh/deposit/tests_migration/__init__.py b/swh/deposit/tests_migration/__init__.py new file mode 100644 diff --git a/swh/deposit/tests_migration/test_migrations.py b/swh/deposit/tests_migration/test_migrations.py new file mode 100644 --- /dev/null +++ b/swh/deposit/tests_migration/test_migrations.py @@ -0,0 +1,49 @@ +# Copyright (C) 2021 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +# Quick note: Django migrations already depend on one another. So to migrate a schema up +# to a point, it's enough to migrate the model to the last but one migration + + +def test_migrations_20_rename_swhid_column_in_deposit_model(migrator): + """Ensures that renaming the model deposit's fields swh_id* is ok.""" + + old_state = migrator.apply_initial_migration(("deposit", "0019_auto_20200519_1035")) + old_deposit = old_state.apps.get_model("deposit", "Deposit") + + assert hasattr(old_deposit, "swh_id") is True + assert hasattr(old_deposit, "swhid") is False + assert hasattr(old_deposit, "swh_id_context") is True + assert hasattr(old_deposit, "swhid_context") is False + + # Check that the last migration adds the origin_url field to the model Deposit + + new_state = migrator.apply_tested_migration( + ("deposit", "0021_deposit_origin_url_20201124_1438") + ) + new_deposit = new_state.apps.get_model("deposit", "Deposit") + + assert hasattr(new_deposit, "swh_id") is False + assert hasattr(new_deposit, "swhid") is True + assert hasattr(new_deposit, "swh_id_context") is False + assert hasattr(new_deposit, "swhid_context") is True + + +def test_migrations_21_add_origin_url_column_to_deposit_model(migrator): + """Ensures that adding the origin_url field to the model deposit is ok.""" + + old_state = migrator.apply_initial_migration(("deposit", "0020_auto_20200929_0855")) + old_deposit = old_state.apps.get_model("deposit", "Deposit") + + assert hasattr(old_deposit, "origin_url") is False + + # Check that the last migration adds the origin_url field to the model Deposit + + new_state = migrator.apply_tested_migration( + ("deposit", "0021_deposit_origin_url_20201124_1438") + ) + new_deposit = new_state.apps.get_model("deposit", "Deposit") + + assert hasattr(new_deposit, "origin_url") is True