2018-05-21 18:56:45 +02:00
|
|
|
# These are tests for Zulip's database migrations. System documented at:
|
|
|
|
# https://zulip.readthedocs.io/en/latest/subsystems/schema-migrations.html
|
|
|
|
#
|
|
|
|
# You can also read
|
|
|
|
# https://www.caktusgroup.com/blog/2016/02/02/writing-unit-tests-django-migrations/
|
|
|
|
# to get a tutorial on the framework that inspired this feature.
|
2024-02-28 22:05:24 +01:00
|
|
|
from unittest import skip
|
2023-11-03 12:07:53 +01:00
|
|
|
from unittest.mock import patch
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.db.migrations.state import StateApps
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2018-05-21 18:56:45 +02:00
|
|
|
|
2018-04-09 18:37:07 +02:00
|
|
|
from zerver.lib.test_classes import MigrationsTestCase
|
|
|
|
|
2019-02-13 10:22:16 +01:00
|
|
|
# Important note: These tests are very expensive, and details of
|
|
|
|
# Django's database transaction model mean it does not super work to
|
|
|
|
# have a lot of migrations tested in this file at once; so we usually
|
|
|
|
# delete the old migration tests when adding a new one, so this file
|
|
|
|
# always has a single migration test in it as an example.
|
|
|
|
#
|
|
|
|
# The error you get with multiple similar tests doing migrations on
|
|
|
|
# the same table is this (table name may vary):
|
|
|
|
#
|
|
|
|
# django.db.utils.OperationalError: cannot ALTER TABLE
|
|
|
|
# "zerver_subscription" because it has pending trigger events
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-10-19 10:17:09 +02:00
|
|
|
|
2024-02-28 22:05:24 +01:00
|
|
|
@skip("Fails because newer migrations have since been merged.") # nocoverage
|
2023-12-01 08:20:48 +01:00
|
|
|
class RenameUserHotspot(MigrationsTestCase):
|
|
|
|
migrate_from = "0492_realm_push_notifications_enabled_and_more"
|
|
|
|
migrate_to = "0493_rename_userhotspot_to_onboardingstep"
|
2023-10-19 10:17:09 +02:00
|
|
|
|
2023-11-03 12:07:53 +01:00
|
|
|
@override
|
|
|
|
def setUp(self) -> None:
|
|
|
|
with patch("builtins.print") as _:
|
|
|
|
super().setUp()
|
2022-03-02 02:19:34 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2022-03-02 02:19:34 +01:00
|
|
|
def setUpBeforeMigration(self, apps: StateApps) -> None:
|
2023-12-01 08:20:48 +01:00
|
|
|
self.assertRaises(LookupError, lambda: apps.get_model("zerver", "onboardingstep"))
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-12-01 08:20:48 +01:00
|
|
|
UserHotspot = apps.get_model("zerver", "userhotspot")
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-12-01 08:20:48 +01:00
|
|
|
expected_field_names = {"id", "hotspot", "timestamp", "user"}
|
|
|
|
fields_name = {field.name for field in UserHotspot._meta.get_fields()}
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-12-01 08:20:48 +01:00
|
|
|
self.assertEqual(fields_name, expected_field_names)
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-12-01 08:20:48 +01:00
|
|
|
def test_renamed_model_and_field(self) -> None:
|
|
|
|
self.assertRaises(LookupError, lambda: self.apps.get_model("zerver", "userhotspot"))
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-12-01 08:20:48 +01:00
|
|
|
OnboardingStep = self.apps.get_model("zerver", "onboardingstep")
|
2023-11-03 12:07:53 +01:00
|
|
|
|
2023-12-01 08:20:48 +01:00
|
|
|
expected_field_names = {"id", "onboarding_step", "timestamp", "user"}
|
|
|
|
fields_name = {field.name for field in OnboardingStep._meta.get_fields()}
|
2023-07-18 12:18:56 +02:00
|
|
|
|
2023-12-01 08:20:48 +01:00
|
|
|
self.assertEqual(fields_name, expected_field_names)
|