Files
upstream-zulip/zerver/tests/test_populate_db.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

75 lines
3.2 KiB
Python

from collections import defaultdict
from datetime import timedelta
from math import floor
from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_ids
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import Stream
from zerver.models.realms import get_realm
from zilencer.management.commands.populate_db import choose_date_sent
class TestChoosePubDate(ZulipTestCase):
def test_choose_date_sent_large_tot_messages(self) -> None:
"""
Test for a bug that was present, where specifying a large amount of messages to generate
would cause each message to have date_sent set to timezone_now(), instead of the date_sents
being distributed across the span of several days.
"""
tot_messages = 1000000
oldest_message_days = 10
datetimes_list = [
choose_date_sent(i, tot_messages, oldest_message_days, 1)
for i in range(1, tot_messages, tot_messages // 100)
]
# Verify there is a meaningful difference between elements.
for i in range(1, len(datetimes_list)):
self.assertTrue(datetimes_list[i] - datetimes_list[i - 1] > timedelta(minutes=5))
# Verify messages are split across the oldest_message_days
idx_for_last_message_in_old_message_days = floor(0.8 * len(datetimes_list)) - 1
# The first chunk should span roughly (oldest_message_days - 2) days.
expected_span_seconds = (oldest_message_days - 2) * 24 * 60 * 60
actual_span = datetimes_list[idx_for_last_message_in_old_message_days] - datetimes_list[0]
# Assert the span is at least, say, 90% of the intended window to account for randomness
self.assertGreaterEqual(actual_span.total_seconds(), expected_span_seconds * 0.9)
class TestUserTimeZones(ZulipTestCase):
def test_timezones_assigned_to_users(self) -> None:
othello = self.example_user("othello")
self.assertEqual(othello.timezone, "US/Pacific")
shiva = self.example_user("shiva")
self.assertEqual(shiva.timezone, "Asia/Kolkata")
cordelia = self.example_user("cordelia")
self.assertEqual(cordelia.timezone, "UTC")
class TestSubscribeUsers(ZulipTestCase):
def test_bulk_create_stream_subscriptions(self) -> None:
"""
This insures bulk_create_stream_subscriptions() ran successfully when test data is loaded via populate_db.py
"""
realm = get_realm("zulip")
streams = Stream.objects.filter(realm=realm)
active_subscriptions = get_active_subscriptions_for_stream_ids(
{stream.id for stream in streams}
).select_related("recipient")
# Map stream_id to its No. active subscriptions.
expected_subscriber_count: dict[int, int] = defaultdict(int)
for sub in active_subscriptions:
expected_subscriber_count[sub.recipient.type_id] += 1
for stream in streams:
self.assertEqual(
stream.subscriber_count,
expected_subscriber_count[stream.id],
msg=f"""
stream of ID ({stream.id}) should have a subscriber_count of {expected_subscriber_count[stream.id]}.
""",
)