<commit_before>import datetime
import re
from typing import Any, Dict, List, Mapping
from unittest import mock

import ujson
from django.conf import settings

from confirmation.models import Confirmation, create_confirmation_link
from zerver.lib.actions import (
    do_change_plan_type,
    do_change_realm_subdomain,
    do_create_realm,
    do_deactivate_realm,
    do_deactivate_stream,
    do_scrub_realm,
    do_send_realm_reactivation_email,
    do_set_realm_property,
)
from zerver.lib.realm_description import get_realm_rendered_description, get_realm_text_description
from zerver.lib.send_email import send_future_email
from zerver.lib.streams import create_stream_if_needed
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import reset_emails_in_zulip_realm, tornado_redirected_to_list
from zerver.models import (
    Attachment,
    CustomProfileField,
    Message,
    Realm,
    ScheduledEmail,
    UserMessage,
    UserProfile,
    get_realm,
    get_stream,
    get_user_profile_by_email,
    get_user_profile_by_id,
)


class RealmTest(ZulipTestCase):
    def assert_user_profile_cache_gets_new_name(self, user_profile: UserProfile,
                                                new_realm_name: str) -> None:
        self.assertEqual(user_profile.realm.name, new_realm_name)

    def test_realm_creation_ensures_internal_realms(self) -> None:
        with mock.patch("zerver.lib.actions.server_initialized", return_value=False):
            with mock.patch("zerver.lib.actions.create_internal_realm") as mock_create_internal:
                do_create_realm("testrealm", "Test Realm")
                mock_create_internal.assert_called_once()

    def test_do_set_realm_name_caching(self) -> None:<commit_msg>test_realm: Use assertLogs to avoid spam in test-backend output.<commit_after>import datetime
import re
from typing import Any, Dict, List, Mapping
from unittest import mock

import ujson
from django.conf import settings

from confirmation.models import Confirmation, create_confirmation_link
from zerver.lib.actions import (
    do_change_plan_type,
    do_change_realm_subdomain,
    do_create_realm,
    do_deactivate_realm,
    do_deactivate_stream,
    do_scrub_realm,
    do_send_realm_reactivation_email,
    do_set_realm_property,
)
from zerver.lib.realm_description import get_realm_rendered_description, get_realm_text_description
from zerver.lib.send_email import send_future_email
from zerver.lib.streams import create_stream_if_needed
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import reset_emails_in_zulip_realm, tornado_redirected_to_list
from zerver.models import (
    Attachment,
    CustomProfileField,
    Message,
    Realm,
    ScheduledEmail,
    UserMessage,
    UserProfile,
    get_realm,
    get_stream,
    get_user_profile_by_email,
    get_user_profile_by_id,
)


class RealmTest(ZulipTestCase):
    def assert_user_profile_cache_gets_new_name(self, user_profile: UserProfile,
                                                new_realm_name: str) -> None:
        self.assertEqual(user_profile.realm.name, new_realm_name)

    def test_realm_creation_ensures_internal_realms(self) -> None:
        with mock.patch("zerver.lib.actions.server_initialized", return_value=False):
            with mock.patch("zerver.lib.actions.create_internal_realm") as mock_create_internal, \
                    self.assertLogs(level='INFO') as info_logs:
                do_create_realm("testrealm", "Test Realm")
                mock_create_internal.assert_called_once()
            self.assertEqual(info_logs.output, [
                'INFO:root:Server not yet initialized. Creating the internal realm first.'
            ])

    def test_do_set_realm_name_caching(self) -> None:
{'fraction_non_alphanumeric': 0.0617740232312566, 'fraction_numerical': 0.0, 'mean_word_length': 3.914396887159533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4596053', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1178, 'n_words': 222}