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.
|
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
|
|
|
|
2023-11-03 12:07:53 +01:00
|
|
|
class UserMessageIndex(MigrationsTestCase):
|
|
|
|
migrate_from = "0485_alter_usermessage_flags_and_add_index"
|
|
|
|
migrate_to = "0486_clear_old_data_for_unused_usermessage_flags"
|
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-11-03 12:07:53 +01:00
|
|
|
UserMessage = apps.get_model("zerver", "usermessage")
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-11-03 12:07:53 +01:00
|
|
|
um_1 = UserMessage.objects.get(id=1)
|
|
|
|
um_1.flags.topic_wildcard_mentioned = True
|
2023-11-03 15:20:44 +01:00
|
|
|
um_1.flags.stream_wildcard_mentioned = True
|
2023-11-03 12:07:53 +01:00
|
|
|
um_1.flags.force_expand = True
|
|
|
|
um_1.save()
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-11-03 12:07:53 +01:00
|
|
|
um_2 = UserMessage.objects.get(id=2)
|
|
|
|
um_2.flags.group_mentioned = True
|
|
|
|
um_2.flags.topic_wildcard_mentioned = True
|
|
|
|
um_2.flags.mentioned = True
|
|
|
|
um_2.flags.force_collapse = True
|
|
|
|
um_2.save()
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-11-03 12:07:53 +01:00
|
|
|
um_1 = UserMessage.objects.get(id=1)
|
|
|
|
um_2 = UserMessage.objects.get(id=2)
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-11-03 12:07:53 +01:00
|
|
|
self.assertTrue(um_1.flags.topic_wildcard_mentioned)
|
2023-11-03 15:20:44 +01:00
|
|
|
self.assertTrue(um_1.flags.stream_wildcard_mentioned)
|
2023-11-03 12:07:53 +01:00
|
|
|
self.assertTrue(um_1.flags.force_expand)
|
|
|
|
self.assertTrue(um_2.flags.group_mentioned)
|
|
|
|
self.assertTrue(um_2.flags.topic_wildcard_mentioned)
|
|
|
|
self.assertTrue(um_2.flags.mentioned)
|
|
|
|
self.assertTrue(um_2.flags.force_collapse)
|
2023-11-06 22:49:30 +01:00
|
|
|
|
2023-11-03 12:07:53 +01:00
|
|
|
def test_clear_topic_wildcard_and_group_mentioned_flags(self) -> None:
|
|
|
|
UserMessage = self.apps.get_model("zerver", "usermessage")
|
|
|
|
|
|
|
|
um_1 = UserMessage.objects.get(id=1)
|
|
|
|
um_2 = UserMessage.objects.get(id=2)
|
2023-07-18 12:18:56 +02:00
|
|
|
|
2023-11-03 12:07:53 +01:00
|
|
|
self.assertFalse(um_1.flags.topic_wildcard_mentioned)
|
2023-11-03 15:20:44 +01:00
|
|
|
self.assertTrue(um_1.flags.stream_wildcard_mentioned)
|
2023-11-03 12:07:53 +01:00
|
|
|
self.assertFalse(um_1.flags.force_expand)
|
|
|
|
self.assertFalse(um_2.flags.group_mentioned)
|
|
|
|
self.assertFalse(um_2.flags.topic_wildcard_mentioned)
|
|
|
|
self.assertTrue(um_2.flags.mentioned)
|
|
|
|
self.assertFalse(um_2.flags.force_collapse)
|