mirror of https://github.com/zulip/zulip.git
slack importer: Remove id allocation function and its implementation.
Remove allocation ID function from slack import script. All the IDs count will start from 0. Hence the ID List returned by the allocation function is of no use, and we remove its implementation. (example: get_total_messages_and_attachments function is of no use anymore, hence we remove it)
This commit is contained in:
parent
d147bd25d0
commit
8a028142d8
|
@ -15,8 +15,6 @@ from django.conf import settings
|
|||
from django.db import connection
|
||||
from django.utils.timezone import now as timezone_now
|
||||
from typing import Any, Dict, List, Tuple
|
||||
from zerver.models import UserProfile, Realm, Stream, UserMessage, \
|
||||
Subscription, Message, Recipient, DefaultStream, Attachment
|
||||
from zerver.forms import check_subdomain_available
|
||||
from zerver.lib.slack_message_conversion import convert_to_zulip_markdown, \
|
||||
get_user_full_name
|
||||
|
@ -34,24 +32,6 @@ def rm_tree(path: str) -> None:
|
|||
if os.path.exists(path):
|
||||
shutil.rmtree(path)
|
||||
|
||||
def idseq(model_class: Any) -> str:
|
||||
return '{}_id_seq'.format(model_class._meta.db_table)
|
||||
|
||||
def allocate_ids(model_class: Any, count: int) -> List[int]:
|
||||
"""
|
||||
Increases the sequence number for a given table by the amount of objects being
|
||||
imported into that table. Hence, this gives a reserved range of ids to import the converted
|
||||
slack objects into the tables.
|
||||
"""
|
||||
conn = connection.cursor()
|
||||
sequence = idseq(model_class)
|
||||
conn.execute("select nextval('%s') from generate_series(1,%s)" %
|
||||
(sequence, str(count)))
|
||||
query = conn.fetchall() # Each element in the result is a tuple like (5,)
|
||||
conn.close()
|
||||
# convert List[Tuple[int]] to List[int]
|
||||
return [item[0] for item in query]
|
||||
|
||||
def slack_workspace_to_realm(domain_name: str, realm_id: int, user_list: List[ZerverFieldsT],
|
||||
realm_subdomain: str, fixtures_path: str,
|
||||
slack_data_dir: str) -> Tuple[ZerverFieldsT, AddedUsersT,
|
||||
|
@ -129,13 +109,10 @@ def users_to_zerver_userprofile(slack_data_dir: str, users: List[ZerverFieldsT],
|
|||
user id
|
||||
"""
|
||||
logging.info('######### IMPORTING USERS STARTED #########\n')
|
||||
total_users = len(users)
|
||||
zerver_userprofile = []
|
||||
avatar_list = [] # type: List[ZerverFieldsT]
|
||||
added_users = {}
|
||||
|
||||
user_id_list = allocate_ids(UserProfile, total_users)
|
||||
|
||||
# We have only one primary owner in slack, see link
|
||||
# https://get.slack.help/hc/en-us/articles/201912948-Owners-and-Administrators
|
||||
# This is to import the primary owner first from all the users
|
||||
|
@ -148,9 +125,9 @@ def users_to_zerver_userprofile(slack_data_dir: str, users: List[ZerverFieldsT],
|
|||
DESKTOP_NOTIFICATION = True
|
||||
|
||||
if user.get('is_primary_owner', False):
|
||||
user_id = user_id_list[primary_owner_id]
|
||||
user_id = primary_owner_id
|
||||
else:
|
||||
user_id = user_id_list[user_id_count]
|
||||
user_id = user_id_count
|
||||
|
||||
# email
|
||||
email = get_user_email(user, domain_name)
|
||||
|
@ -309,21 +286,6 @@ def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: A
|
|||
zerver_recipient = []
|
||||
zerver_defaultstream = []
|
||||
|
||||
# Pre-compute all the total number of ids to fastword the ids in active db
|
||||
total_channels = len(channels)
|
||||
total_users = len(zerver_userprofile)
|
||||
total_recipients = total_channels + total_users
|
||||
total_subscription = total_users
|
||||
for channel in channels:
|
||||
for member in channel['members']:
|
||||
total_subscription += 1
|
||||
|
||||
stream_id_list = allocate_ids(Stream, total_channels)
|
||||
subscription_id_list = allocate_ids(Subscription, total_subscription)
|
||||
recipient_id_list = allocate_ids(Recipient, total_recipients)
|
||||
# corresponding to channels 'general' and 'random' which are slack specific
|
||||
defaultstream_id_list = allocate_ids(DefaultStream, 2)
|
||||
|
||||
stream_id_count = subscription_id_count = recipient_id_count = defaultstream_id = 0
|
||||
|
||||
for channel in channels:
|
||||
|
@ -333,8 +295,8 @@ def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: A
|
|||
# WARN This mapping is lossy since the topic.creator, topic.last_set,
|
||||
# purpose.creator, purpose.last_set fields are not preserved.
|
||||
description = channel["purpose"]["value"]
|
||||
stream_id = stream_id_list[stream_id_count]
|
||||
recipient_id = recipient_id_list[recipient_id_count]
|
||||
stream_id = stream_id_count
|
||||
recipient_id = recipient_id_count
|
||||
|
||||
# construct the stream object and append it to zerver_stream
|
||||
stream = dict(
|
||||
|
@ -354,7 +316,7 @@ def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: A
|
|||
default_channels = ['general', 'random'] # Slack specific
|
||||
if channel['name'] in default_channels:
|
||||
defaultstream = build_defaultstream(channel['name'], realm_id, stream_id,
|
||||
defaultstream_id_list[defaultstream_id])
|
||||
defaultstream_id)
|
||||
zerver_defaultstream.append(defaultstream)
|
||||
defaultstream_id += 1
|
||||
|
||||
|
@ -376,7 +338,7 @@ def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: A
|
|||
# construct the subscription object and append it to zerver_subscription
|
||||
subscription_id_count = build_subscription(channel['members'], zerver_subscription,
|
||||
recipient_id, added_users,
|
||||
subscription_id_list, subscription_id_count)
|
||||
subscription_id_count)
|
||||
# TOODO add zerver_subscription which correspond to
|
||||
# huddles type recipient
|
||||
# For huddles:
|
||||
|
@ -406,8 +368,8 @@ def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: A
|
|||
zulip_user_id = user['id']
|
||||
# this maps the recipients and subscriptions
|
||||
# related to private messages
|
||||
recipient_id = recipient_id_list[recipient_id_count]
|
||||
subscription_id = subscription_id_list[subscription_id_count]
|
||||
recipient_id = recipient_id_count
|
||||
subscription_id = subscription_id_count
|
||||
|
||||
recipient, sub = build_pm_recipient_sub_from_user(zulip_user_id, recipient_id,
|
||||
subscription_id)
|
||||
|
@ -451,9 +413,8 @@ def build_pm_recipient_sub_from_user(zulip_user_id: int, recipient_id: int,
|
|||
|
||||
def build_subscription(channel_members: List[str], zerver_subscription: List[ZerverFieldsT],
|
||||
recipient_id: int, added_users: AddedUsersT,
|
||||
subscription_id_list: List[int], subscription_id_count: int) -> int:
|
||||
subscription_id: int) -> int:
|
||||
for member in channel_members:
|
||||
subscription_id = subscription_id_list[subscription_id_count]
|
||||
sub = dict(
|
||||
recipient=recipient_id,
|
||||
notifications=False,
|
||||
|
@ -468,8 +429,8 @@ def build_subscription(channel_members: List[str], zerver_subscription: List[Zer
|
|||
# proof : https://github.com/zulip/zulip/blob/master/zerver/views/messages.py#L240 &
|
||||
# https://github.com/zulip/zulip/blob/master/zerver/views/messages.py#L324
|
||||
zerver_subscription.append(sub)
|
||||
subscription_id_count += 1
|
||||
return subscription_id_count
|
||||
subscription_id += 1
|
||||
return subscription_id
|
||||
|
||||
def convert_slack_workspace_messages(slack_data_dir: str, users: List[ZerverFieldsT], realm_id: int,
|
||||
added_users: AddedUsersT, added_recipient: AddedRecipientsT,
|
||||
|
@ -495,18 +456,9 @@ def convert_slack_workspace_messages(slack_data_dir: str, users: List[ZerverFiel
|
|||
|
||||
logging.info('######### IMPORTING MESSAGES STARTED #########\n')
|
||||
|
||||
# To pre-compute the total number of messages and usermessages
|
||||
total_messages, total_usermessages, total_attachments = get_total_messages_and_attachments(
|
||||
realm['zerver_subscription'], added_recipient, all_messages)
|
||||
|
||||
message_id_list = allocate_ids(Message, total_messages)
|
||||
usermessage_id_list = allocate_ids(UserMessage, total_usermessages)
|
||||
attachment_id_list = allocate_ids(UserMessage, total_attachments)
|
||||
|
||||
id_list = [message_id_list, usermessage_id_list, attachment_id_list]
|
||||
zerver_message, zerver_usermessage, attachment, uploads = channel_message_to_zerver_message(
|
||||
realm_id, users, added_users, added_recipient, all_messages,
|
||||
realm['zerver_subscription'], domain_name, id_list)
|
||||
realm['zerver_subscription'], domain_name)
|
||||
|
||||
logging.info('######### IMPORTING MESSAGES FINISHED #########\n')
|
||||
|
||||
|
@ -529,43 +481,15 @@ def get_all_messages(slack_data_dir: str, added_channels: AddedChannelsT) -> Lis
|
|||
all_messages += messages
|
||||
return all_messages
|
||||
|
||||
def get_total_messages_and_attachments(zerver_subscription: List[ZerverFieldsT],
|
||||
added_recipient: AddedRecipientsT,
|
||||
all_messages: List[ZerverFieldsT]) -> Tuple[int, int,
|
||||
int]:
|
||||
"""
|
||||
Returns:
|
||||
1. message_id, which is total number of messages
|
||||
2. usermessage_id, which is total number of usermessages
|
||||
3. attachment_id, which is total number of attachments
|
||||
"""
|
||||
total_messages = total_usermessages = total_attachments = 0
|
||||
|
||||
for message in all_messages:
|
||||
if 'subtype' in message.keys():
|
||||
subtype = message['subtype']
|
||||
if subtype in ["channel_join", "channel_leave", "channel_name"]:
|
||||
continue
|
||||
elif subtype == "file_share":
|
||||
total_attachments += 1
|
||||
|
||||
for subscription in zerver_subscription:
|
||||
if subscription['recipient'] == added_recipient[message['channel_name']]:
|
||||
total_usermessages += 1
|
||||
total_messages += 1
|
||||
|
||||
return total_messages, total_usermessages, total_attachments
|
||||
|
||||
def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT],
|
||||
added_users: AddedUsersT,
|
||||
added_recipient: AddedRecipientsT,
|
||||
all_messages: List[ZerverFieldsT],
|
||||
zerver_subscription: List[ZerverFieldsT],
|
||||
domain_name: str,
|
||||
ids: List[Any]) -> Tuple[List[ZerverFieldsT],
|
||||
List[ZerverFieldsT],
|
||||
List[ZerverFieldsT],
|
||||
List[ZerverFieldsT]]:
|
||||
domain_name: str) -> Tuple[List[ZerverFieldsT],
|
||||
List[ZerverFieldsT],
|
||||
List[ZerverFieldsT],
|
||||
List[ZerverFieldsT]]:
|
||||
"""
|
||||
Returns:
|
||||
1. zerver_message, which is a list of the messages
|
||||
|
@ -574,7 +498,6 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT],
|
|||
4. uploads_list, which is a list of uploads to be mapped in uploads records.json
|
||||
"""
|
||||
message_id_count = usermessage_id_count = attachment_id_count = 0
|
||||
message_id_list, usermessage_id_list, attachment_id_list = ids
|
||||
zerver_message = []
|
||||
zerver_usermessage = [] # type: List[ZerverFieldsT]
|
||||
uploads_list = [] # type: List[ZerverFieldsT]
|
||||
|
@ -594,7 +517,7 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT],
|
|||
rendered_content = None
|
||||
|
||||
recipient_id = added_recipient[message['channel_name']]
|
||||
message_id = message_id_list[message_id_count]
|
||||
message_id = message_id_count
|
||||
|
||||
# Process different subtypes of slack messages
|
||||
if 'subtype' in message.keys():
|
||||
|
@ -625,7 +548,7 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT],
|
|||
build_uploads(added_users[user], realm_id, file_user_email, fileinfo, s3_path,
|
||||
uploads_list)
|
||||
|
||||
attachment_id = attachment_id_list[attachment_id_count]
|
||||
attachment_id = attachment_id_count
|
||||
build_zerver_attachment(realm_id, message_id, attachment_id, added_users[user],
|
||||
fileinfo, s3_path, zerver_attachment)
|
||||
attachment_id_count += 1
|
||||
|
@ -662,8 +585,8 @@ def channel_message_to_zerver_message(realm_id: int, users: List[ZerverFieldsT],
|
|||
|
||||
# construct usermessages
|
||||
usermessage_id_count = build_zerver_usermessage(
|
||||
zerver_usermessage, usermessage_id_count, usermessage_id_list,
|
||||
zerver_subscription, recipient_id, mentioned_users_id, message_id)
|
||||
zerver_usermessage, usermessage_id_count, zerver_subscription,
|
||||
recipient_id, mentioned_users_id, message_id)
|
||||
|
||||
message_id_count += 1
|
||||
return zerver_message, zerver_usermessage, zerver_attachment, uploads_list
|
||||
|
@ -718,8 +641,7 @@ def get_message_sending_user(message: ZerverFieldsT) -> str:
|
|||
user = message.get('user')
|
||||
return user
|
||||
|
||||
def build_zerver_usermessage(zerver_usermessage: List[ZerverFieldsT], usermessage_id_count: int,
|
||||
usermessage_id_list: List[int],
|
||||
def build_zerver_usermessage(zerver_usermessage: List[ZerverFieldsT], usermessage_id: int,
|
||||
zerver_subscription: List[ZerverFieldsT], recipient_id: int,
|
||||
mentioned_users_id: List[int], message_id: int) -> int:
|
||||
for subscription in zerver_subscription:
|
||||
|
@ -730,12 +652,12 @@ def build_zerver_usermessage(zerver_usermessage: List[ZerverFieldsT], usermessag
|
|||
|
||||
usermessage = dict(
|
||||
user_profile=subscription['user_profile'],
|
||||
id=usermessage_id_list[usermessage_id_count],
|
||||
id=usermessage_id,
|
||||
flags_mask=flags_mask,
|
||||
message=message_id)
|
||||
usermessage_id_count += 1
|
||||
usermessage_id += 1
|
||||
zerver_usermessage.append(usermessage)
|
||||
return usermessage_id_count
|
||||
return usermessage_id
|
||||
|
||||
def do_convert_data(slack_zip_file: str, realm_subdomain: str, output_dir: str, token: str) -> None:
|
||||
check_subdomain_available(realm_subdomain)
|
||||
|
@ -758,7 +680,7 @@ def do_convert_data(slack_zip_file: str, realm_subdomain: str, output_dir: str,
|
|||
script_path = os.path.dirname(os.path.abspath(__file__)) + '/'
|
||||
fixtures_path = script_path + '../fixtures/'
|
||||
|
||||
realm_id = allocate_ids(Realm, 1)[0]
|
||||
realm_id = 0
|
||||
|
||||
user_list = get_user_data(token)
|
||||
realm, added_users, added_recipient, added_channels, avatar_list = slack_workspace_to_realm(
|
||||
|
|
|
@ -4,7 +4,6 @@ from django.utils.timezone import now as timezone_now
|
|||
|
||||
from zerver.lib.slack_data_to_zulip_data import (
|
||||
rm_tree,
|
||||
allocate_ids,
|
||||
get_user_data,
|
||||
build_zerver_realm,
|
||||
get_user_email,
|
||||
|
@ -18,7 +17,6 @@ from zerver.lib.slack_data_to_zulip_data import (
|
|||
build_subscription,
|
||||
channels_to_zerver_stream,
|
||||
slack_workspace_to_realm,
|
||||
get_total_messages_and_attachments,
|
||||
get_message_sending_user,
|
||||
build_zerver_usermessage,
|
||||
channel_message_to_zerver_message,
|
||||
|
@ -74,10 +72,6 @@ class SlackImporter(ZulipTestCase):
|
|||
# set logger to a higher level to suppress 'logger.INFO' outputs
|
||||
logger.setLevel(logging.WARNING)
|
||||
|
||||
def test_allocate_ids(self) -> None:
|
||||
start_id_sequence = allocate_ids(Realm, 3)
|
||||
self.assertEqual(len(start_id_sequence), 3)
|
||||
|
||||
@mock.patch('requests.get', side_effect=mocked_requests_get)
|
||||
def test_get_user_data(self, mock_get: mock.Mock) -> None:
|
||||
token = 'valid-token'
|
||||
|
@ -141,9 +135,7 @@ class SlackImporter(ZulipTestCase):
|
|||
self.assertEqual(get_user_timezone(user_timezone_none), "America/New_York")
|
||||
self.assertEqual(get_user_timezone(user_no_timezone), "America/New_York")
|
||||
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.allocate_ids",
|
||||
return_value=[1, 2, 3])
|
||||
def test_users_to_zerver_userprofile(self, mock_allocate_ids: mock.Mock) -> None:
|
||||
def test_users_to_zerver_userprofile(self) -> None:
|
||||
user_data = [{"id": "U08RGD1RD",
|
||||
"team_id": "T5YFFM2QY",
|
||||
"name": "john",
|
||||
|
@ -172,9 +164,9 @@ class SlackImporter(ZulipTestCase):
|
|||
|
||||
# As user with slack_id 'U0CBK5KAT' is the primary owner, that user should be imported first
|
||||
# and hence has zulip_id = 1
|
||||
test_added_users = {'U08RGD1RD': 2,
|
||||
'U0CBK5KAT': 1,
|
||||
'U09TYF5Sk': 3}
|
||||
test_added_users = {'U08RGD1RD': 1,
|
||||
'U0CBK5KAT': 0,
|
||||
'U09TYF5Sk': 2}
|
||||
slack_data_dir = './random_path'
|
||||
timestamp = int(timezone_now().timestamp())
|
||||
zerver_userprofile, avatar_list, added_users = users_to_zerver_userprofile(
|
||||
|
@ -186,7 +178,7 @@ class SlackImporter(ZulipTestCase):
|
|||
|
||||
self.assertEqual(zerver_userprofile[1]['id'], test_added_users['U0CBK5KAT'])
|
||||
self.assertEqual(len(zerver_userprofile), 3)
|
||||
self.assertEqual(zerver_userprofile[1]['id'], 1)
|
||||
self.assertEqual(zerver_userprofile[1]['id'], 0)
|
||||
self.assertEqual(zerver_userprofile[1]['is_realm_admin'], True)
|
||||
self.assertEqual(zerver_userprofile[1]['is_staff'], False)
|
||||
self.assertEqual(zerver_userprofile[1]['is_active'], True)
|
||||
|
@ -226,27 +218,24 @@ class SlackImporter(ZulipTestCase):
|
|||
channel_members = ["U061A1R2R", "U061A3E0G", "U061A5N1G", "U064KUGRJ"]
|
||||
added_users = {"U061A1R2R": 1, "U061A3E0G": 8, "U061A5N1G": 7, "U064KUGRJ": 5}
|
||||
subscription_id_count = 0
|
||||
subscription_id_list = [7, 8, 9, 23]
|
||||
recipient_id = 12
|
||||
zerver_subscription = [] # type: List[Dict[str, Any]]
|
||||
final_subscription_id = build_subscription(channel_members, zerver_subscription,
|
||||
recipient_id, added_users,
|
||||
subscription_id_list, subscription_id_count)
|
||||
subscription_id_count)
|
||||
# sanity checks
|
||||
self.assertEqual(final_subscription_id, 4)
|
||||
self.assertEqual(zerver_subscription[0]['recipient'], 12)
|
||||
self.assertEqual(zerver_subscription[0]['id'], 7)
|
||||
self.assertEqual(zerver_subscription[0]['id'], 0)
|
||||
self.assertEqual(zerver_subscription[0]['user_profile'], added_users[channel_members[0]])
|
||||
self.assertEqual(zerver_subscription[2]['user_profile'], added_users[channel_members[2]])
|
||||
self.assertEqual(zerver_subscription[3]['id'], 23)
|
||||
self.assertEqual(zerver_subscription[3]['id'], 3)
|
||||
self.assertEqual(zerver_subscription[1]['recipient'],
|
||||
zerver_subscription[3]['recipient'])
|
||||
self.assertEqual(zerver_subscription[1]['pin_to_top'], False)
|
||||
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.get_data_file")
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.allocate_ids")
|
||||
def test_channels_to_zerver_stream(self, mock_allocate_ids: mock.Mock,
|
||||
mock_get_data_file: mock.Mock) -> None:
|
||||
def test_channels_to_zerver_stream(self, mock_get_data_file: mock.Mock) -> None:
|
||||
|
||||
added_users = {"U061A1R2R": 1, "U061A3E0G": 8, "U061A5N1G": 7, "U064KUGRJ": 5}
|
||||
zerver_userprofile = [{'id': 1}, {'id': 8}, {'id': 7}, {'id': 5}]
|
||||
|
@ -268,10 +257,6 @@ class SlackImporter(ZulipTestCase):
|
|||
'is_general': False, 'members': ['U061A3E0G'], 'is_archived': False,
|
||||
'topic': {'value': ''}, 'purpose': {'value': ''}}]
|
||||
mock_get_data_file.return_value = channel_data
|
||||
mock_allocate_ids.side_effect = [[1, 2, 3, 4], # For stream
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], # For subscription
|
||||
[1, 2, 3, 4, 5, 6, 7, 8], # For recipient
|
||||
[1, 2]] # For defaultstream
|
||||
|
||||
channel_to_zerver_stream_output = channels_to_zerver_stream('./random_path', realm_id, added_users,
|
||||
zerver_userprofile)
|
||||
|
@ -282,12 +267,12 @@ class SlackImporter(ZulipTestCase):
|
|||
zerver_recipient = channel_to_zerver_stream_output[4]
|
||||
added_recipient = channel_to_zerver_stream_output[5]
|
||||
|
||||
test_added_channels = {'feedback': 4, 'general': 2, 'general1': 3, 'random': 1}
|
||||
test_added_recipient = {'feedback': 4, 'general': 2, 'general1': 3, 'random': 1}
|
||||
test_added_channels = {'feedback': 3, 'general': 1, 'general1': 2, 'random': 0}
|
||||
test_added_recipient = {'feedback': 3, 'general': 1, 'general1': 2, 'random': 0}
|
||||
|
||||
# zerver defaultstream already tested in helper functions
|
||||
self.assertEqual(zerver_defaultstream, [{'id': 1, 'realm': 3, 'stream': 1},
|
||||
{'id': 2, 'realm': 3, 'stream': 2}])
|
||||
self.assertEqual(zerver_defaultstream, [{'id': 0, 'realm': 3, 'stream': 0},
|
||||
{'id': 1, 'realm': 3, 'stream': 1}])
|
||||
|
||||
self.assertDictEqual(test_added_channels, added_channels)
|
||||
self.assertDictEqual(test_added_recipient, added_recipient)
|
||||
|
@ -295,10 +280,10 @@ class SlackImporter(ZulipTestCase):
|
|||
# functioning of zerver subscriptions are already tested in the helper functions
|
||||
# This is to check the concatenation of the output lists from the helper functions
|
||||
# subscriptions for stream
|
||||
self.assertEqual(zerver_subscription[3]['recipient'], 2)
|
||||
self.assertEqual(zerver_subscription[5]['recipient'], 3)
|
||||
self.assertEqual(zerver_subscription[3]['recipient'], 1)
|
||||
self.assertEqual(zerver_subscription[5]['recipient'], 2)
|
||||
# subscription for users
|
||||
self.assertEqual(zerver_subscription[6]['recipient'], 4)
|
||||
self.assertEqual(zerver_subscription[6]['recipient'], 3)
|
||||
self.assertEqual(zerver_subscription[7]['user_profile'], 1)
|
||||
|
||||
# recipients for stream
|
||||
|
@ -346,26 +331,6 @@ class SlackImporter(ZulipTestCase):
|
|||
self.assertEqual(realm['zerver_userprofile'], [])
|
||||
self.assertEqual(realm['zerver_realm'], [{}])
|
||||
|
||||
def test_get_total_messages_and_attachments(self) -> None:
|
||||
messages = [{"text": "<@U8VAHEVUY> has joined the channel", "subtype": "channel_join",
|
||||
"channel_name": "random"},
|
||||
{"text": "message", "channel_name": "random", "subtype": "file_share"},
|
||||
{"text": "random", "channel_name": "random"},
|
||||
{"text": "test messsage", "channel_name": "general"},
|
||||
{"text": "test message 2", "subtype": "channel_leave", "channel_name": "general"},
|
||||
{"text": "random test", "channel_name": "general"},
|
||||
{"text": "message", "subtype": "channel_name", "channel_name": "general"}]
|
||||
|
||||
added_recipient = {'random': 2, 'general': 4}
|
||||
zerver_subscription = [{'recipient': 2}, {'recipient': 4}, {'recipient': 2}]
|
||||
|
||||
total_messages, total_usermessages, total_attachments = get_total_messages_and_attachments(
|
||||
zerver_subscription, added_recipient, messages)
|
||||
# subtype: channel_join, channel_leave are filtered out
|
||||
self.assertEqual(total_messages, 4)
|
||||
self.assertEqual(total_attachments, 1)
|
||||
self.assertEqual(total_usermessages, 6)
|
||||
|
||||
def test_get_message_sending_user(self) -> None:
|
||||
message_with_file = {'subtype': 'file', 'type': 'message',
|
||||
'file': {'user': 'U064KUGRJ'}}
|
||||
|
@ -379,7 +344,6 @@ class SlackImporter(ZulipTestCase):
|
|||
def test_build_zerver_message(self) -> None:
|
||||
zerver_usermessage = [] # type: List[Dict[str, Any]]
|
||||
usermessage_id_count = 0
|
||||
usermessage_id_list = [3, 7, 8, 11]
|
||||
zerver_subscription = [{'recipient': 2, 'user_profile': 7},
|
||||
{'recipient': 4, 'user_profile': 12},
|
||||
{'recipient': 2, 'user_profile': 16},
|
||||
|
@ -390,17 +354,17 @@ class SlackImporter(ZulipTestCase):
|
|||
message_id = 9
|
||||
|
||||
test_usermessage_id = build_zerver_usermessage(zerver_usermessage, usermessage_id_count,
|
||||
usermessage_id_list, zerver_subscription,
|
||||
recipient_id, mentioned_users_id, message_id)
|
||||
zerver_subscription, recipient_id,
|
||||
mentioned_users_id, message_id)
|
||||
self.assertEqual(test_usermessage_id, 4)
|
||||
|
||||
self.assertEqual(zerver_usermessage[0]['flags_mask'], 1)
|
||||
self.assertEqual(zerver_usermessage[0]['id'], 3)
|
||||
self.assertEqual(zerver_usermessage[0]['id'], 0)
|
||||
self.assertEqual(zerver_usermessage[0]['message'], message_id)
|
||||
self.assertEqual(zerver_usermessage[1]['user_profile'],
|
||||
zerver_subscription[2]['user_profile'])
|
||||
self.assertEqual(zerver_usermessage[1]['flags_mask'], 9)
|
||||
self.assertEqual(zerver_usermessage[3]['id'], 11)
|
||||
self.assertEqual(zerver_usermessage[3]['id'], 3)
|
||||
self.assertEqual(zerver_usermessage[3]['message'], message_id)
|
||||
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.build_zerver_usermessage", return_value = 2)
|
||||
|
@ -428,13 +392,11 @@ class SlackImporter(ZulipTestCase):
|
|||
"ts": "1433868669.000012", "channel_name": "general"}] # type: List[Dict[str, Any]]
|
||||
|
||||
added_recipient = {'random': 2, 'general': 1}
|
||||
ids = [[3, 4, 5, 6, 7], [], []]
|
||||
|
||||
zerver_usermessage = [] # type: List[Dict[str, Any]]
|
||||
zerver_subscription = [] # type: List[Dict[str, Any]]
|
||||
zerver_message, zerver_usermessage, attachment, uploads = channel_message_to_zerver_message(
|
||||
1, user_data, added_users, added_recipient, all_messages, zerver_subscription,
|
||||
'domain', ids)
|
||||
1, user_data, added_users, added_recipient, all_messages, zerver_subscription, 'domain')
|
||||
# functioning already tested in helper function
|
||||
self.assertEqual(zerver_usermessage, [])
|
||||
# subtype: channel_join is filtered
|
||||
|
@ -454,8 +416,8 @@ class SlackImporter(ZulipTestCase):
|
|||
self.assertEqual(zerver_message[2]['subject'], 'imported from slack')
|
||||
self.assertEqual(zerver_message[1]['recipient'], added_recipient['random'])
|
||||
|
||||
self.assertEqual(zerver_message[1]['id'], 4)
|
||||
self.assertEqual(zerver_message[4]['id'], 7)
|
||||
self.assertEqual(zerver_message[1]['id'], 1)
|
||||
self.assertEqual(zerver_message[4]['id'], 4)
|
||||
|
||||
self.assertIsNone(zerver_message[3]['rendered_content'])
|
||||
self.assertEqual(zerver_message[0]['has_image'], False)
|
||||
|
@ -466,11 +428,8 @@ class SlackImporter(ZulipTestCase):
|
|||
self.assertEqual(zerver_message[3]['sender'], 24)
|
||||
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.channel_message_to_zerver_message")
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.allocate_ids")
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.get_all_messages")
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.get_total_messages_and_attachments", return_value=[2, 4, 1])
|
||||
def test_convert_slack_workspace_messages(self, mock_get_total_messages_and_attachments: mock.Mock,
|
||||
mock_get_all_messages: mock.Mock, mock_allocate_ids: mock.Mock,
|
||||
def test_convert_slack_workspace_messages(self, mock_get_all_messages: mock.Mock,
|
||||
mock_message: mock.Mock) -> None:
|
||||
added_channels = {'random': 1, 'general': 2}
|
||||
zerver_message = [{'id': 1}, {'id': 5}]
|
||||
|
|
Loading…
Reference in New Issue