mirror of
https://github.com/zulip/zulip.git
synced 2025-11-09 08:26:11 +00:00
tests: Extract and use assert_max_length test helper.
This makes it much more explicit in which cases we're checking equality or a maximum in our use of `assert_length`. Fixes #1400.
This commit is contained in:
@@ -486,12 +486,15 @@ class ZulipTestCase(TestCase):
|
|||||||
"""
|
"""
|
||||||
self.assertEqual(self.get_json_error(result, status_code=status_code), msg)
|
self.assertEqual(self.get_json_error(result, status_code=status_code), msg)
|
||||||
|
|
||||||
def assert_length(self, queries, count, exact=False):
|
def assert_length(self, queries, count):
|
||||||
# type: (Sized, int, bool) -> None
|
# type: (Sized, int) -> None
|
||||||
actual_count = len(queries)
|
actual_count = len(queries)
|
||||||
if exact:
|
return self.assertTrue(actual_count == count,
|
||||||
return self.assertTrue(actual_count == count,
|
|
||||||
"len(%s) == %s, != %s" % (queries, actual_count, count))
|
"len(%s) == %s, != %s" % (queries, actual_count, count))
|
||||||
|
|
||||||
|
def assert_max_length(self, queries, count):
|
||||||
|
# type: (Sized, int) -> None
|
||||||
|
actual_count = len(queries)
|
||||||
return self.assertTrue(actual_count <= count,
|
return self.assertTrue(actual_count <= count,
|
||||||
"len(%s) == %s, > %s" % (queries, actual_count, count))
|
"len(%s) == %s, > %s" % (queries, actual_count, count))
|
||||||
|
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ class GetEventsTest(ZulipTestCase):
|
|||||||
})
|
})
|
||||||
events = ujson.loads(result.content)["events"]
|
events = ujson.loads(result.content)["events"]
|
||||||
self.assert_json_success(result)
|
self.assert_json_success(result)
|
||||||
self.assert_length(events, 0, True)
|
self.assert_length(events, 0)
|
||||||
|
|
||||||
local_id = 10.01
|
local_id = 10.01
|
||||||
self.send_message(email, recipient_email, Recipient.PERSONAL, "hello", local_id=local_id, sender_queue_id=queue_id)
|
self.send_message(email, recipient_email, Recipient.PERSONAL, "hello", local_id=local_id, sender_queue_id=queue_id)
|
||||||
@@ -120,7 +120,7 @@ class GetEventsTest(ZulipTestCase):
|
|||||||
})
|
})
|
||||||
events = ujson.loads(result.content)["events"]
|
events = ujson.loads(result.content)["events"]
|
||||||
self.assert_json_success(result)
|
self.assert_json_success(result)
|
||||||
self.assert_length(events, 1, True)
|
self.assert_length(events, 1)
|
||||||
self.assertEqual(events[0]["type"], "message")
|
self.assertEqual(events[0]["type"], "message")
|
||||||
self.assertEqual(events[0]["message"]["sender_email"], email)
|
self.assertEqual(events[0]["message"]["sender_email"], email)
|
||||||
self.assertEqual(events[0]["local_message_id"], local_id)
|
self.assertEqual(events[0]["local_message_id"], local_id)
|
||||||
@@ -140,7 +140,7 @@ class GetEventsTest(ZulipTestCase):
|
|||||||
})
|
})
|
||||||
events = ujson.loads(result.content)["events"]
|
events = ujson.loads(result.content)["events"]
|
||||||
self.assert_json_success(result)
|
self.assert_json_success(result)
|
||||||
self.assert_length(events, 1, True)
|
self.assert_length(events, 1)
|
||||||
self.assertEqual(events[0]["type"], "message")
|
self.assertEqual(events[0]["type"], "message")
|
||||||
self.assertEqual(events[0]["message"]["sender_email"], email)
|
self.assertEqual(events[0]["message"]["sender_email"], email)
|
||||||
self.assertEqual(events[0]["local_message_id"], local_id)
|
self.assertEqual(events[0]["local_message_id"], local_id)
|
||||||
@@ -187,7 +187,7 @@ class GetEventsTest(ZulipTestCase):
|
|||||||
})
|
})
|
||||||
events = ujson.loads(result.content)["events"]
|
events = ujson.loads(result.content)["events"]
|
||||||
self.assert_json_success(result)
|
self.assert_json_success(result)
|
||||||
self.assert_length(events, 0, True)
|
self.assert_length(events, 0)
|
||||||
|
|
||||||
self.send_message(email, "othello@zulip.com", Recipient.PERSONAL, "hello")
|
self.send_message(email, "othello@zulip.com", Recipient.PERSONAL, "hello")
|
||||||
self.send_message(email, "Denmark", Recipient.STREAM, "hello")
|
self.send_message(email, "Denmark", Recipient.STREAM, "hello")
|
||||||
@@ -200,7 +200,7 @@ class GetEventsTest(ZulipTestCase):
|
|||||||
})
|
})
|
||||||
events = ujson.loads(result.content)["events"]
|
events = ujson.loads(result.content)["events"]
|
||||||
self.assert_json_success(result)
|
self.assert_json_success(result)
|
||||||
self.assert_length(events, 1, True)
|
self.assert_length(events, 1)
|
||||||
self.assertEqual(events[0]["type"], "message")
|
self.assertEqual(events[0]["type"], "message")
|
||||||
self.assertEqual(events[0]["message"]["display_recipient"], "Denmark")
|
self.assertEqual(events[0]["message"]["display_recipient"], "Denmark")
|
||||||
|
|
||||||
|
|||||||
@@ -346,7 +346,7 @@ class StreamMessagesTest(ZulipTestCase):
|
|||||||
with queries_captured() as queries:
|
with queries_captured() as queries:
|
||||||
send_message()
|
send_message()
|
||||||
|
|
||||||
self.assert_length(queries, 7)
|
self.assert_max_length(queries, 7)
|
||||||
|
|
||||||
def test_stream_message_unicode(self):
|
def test_stream_message_unicode(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
@@ -464,7 +464,7 @@ class MessageDictTest(ZulipTestCase):
|
|||||||
delay = time.time() - t
|
delay = time.time() - t
|
||||||
# Make sure we don't take longer than 1ms per message to extract messages.
|
# Make sure we don't take longer than 1ms per message to extract messages.
|
||||||
self.assertTrue(delay < 0.001 * num_ids)
|
self.assertTrue(delay < 0.001 * num_ids)
|
||||||
self.assert_length(queries, 7)
|
self.assert_max_length(queries, 7)
|
||||||
self.assertEqual(len(rows), num_ids)
|
self.assertEqual(len(rows), num_ids)
|
||||||
|
|
||||||
def test_applying_markdown(self):
|
def test_applying_markdown(self):
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class ActivityTest(ZulipTestCase):
|
|||||||
with queries_captured() as queries:
|
with queries_captured() as queries:
|
||||||
self.client_get('/activity')
|
self.client_get('/activity')
|
||||||
|
|
||||||
self.assert_length(queries, 13)
|
self.assert_max_length(queries, 13)
|
||||||
|
|
||||||
class UserPresenceTests(ZulipTestCase):
|
class UserPresenceTests(ZulipTestCase):
|
||||||
def test_get_empty(self):
|
def test_get_empty(self):
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ class LoginTest(ZulipTestCase):
|
|||||||
with queries_captured() as queries:
|
with queries_captured() as queries:
|
||||||
self.register("test", "test")
|
self.register("test", "test")
|
||||||
# Ensure the number of queries we make is not O(streams)
|
# Ensure the number of queries we make is not O(streams)
|
||||||
self.assert_length(queries, 67)
|
self.assert_max_length(queries, 67)
|
||||||
user_profile = get_user_profile_by_email('test@zulip.com')
|
user_profile = get_user_profile_by_email('test@zulip.com')
|
||||||
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
||||||
|
|
||||||
|
|||||||
@@ -1056,7 +1056,7 @@ class SubscriptionAPITest(ZulipTestCase):
|
|||||||
with tornado_redirected_to_list(events):
|
with tornado_redirected_to_list(events):
|
||||||
self.helper_check_subs_before_and_after_add(self.streams + add_streams, {},
|
self.helper_check_subs_before_and_after_add(self.streams + add_streams, {},
|
||||||
add_streams, self.streams, self.test_email, self.streams + add_streams)
|
add_streams, self.streams, self.test_email, self.streams + add_streams)
|
||||||
self.assert_length(events, 6, True)
|
self.assert_length(events, 6)
|
||||||
|
|
||||||
def test_successful_subscriptions_add_with_announce(self):
|
def test_successful_subscriptions_add_with_announce(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
@@ -1291,9 +1291,9 @@ class SubscriptionAPITest(ZulipTestCase):
|
|||||||
streams_to_sub,
|
streams_to_sub,
|
||||||
dict(principals=ujson.dumps([email1, email2])),
|
dict(principals=ujson.dumps([email1, email2])),
|
||||||
)
|
)
|
||||||
self.assert_length(queries, 43)
|
self.assert_max_length(queries, 43)
|
||||||
|
|
||||||
self.assert_length(events, 8, exact=True)
|
self.assert_length(events, 8)
|
||||||
for ev in [x for x in events if x['event']['type'] not in ('message', 'stream')]:
|
for ev in [x for x in events if x['event']['type'] not in ('message', 'stream')]:
|
||||||
if isinstance(ev['event']['subscriptions'][0], dict):
|
if isinstance(ev['event']['subscriptions'][0], dict):
|
||||||
self.assertEqual(ev['event']['op'], 'add')
|
self.assertEqual(ev['event']['op'], 'add')
|
||||||
@@ -1319,9 +1319,9 @@ class SubscriptionAPITest(ZulipTestCase):
|
|||||||
streams_to_sub,
|
streams_to_sub,
|
||||||
dict(principals=ujson.dumps([self.test_email])),
|
dict(principals=ujson.dumps([self.test_email])),
|
||||||
)
|
)
|
||||||
self.assert_length(queries, 8)
|
self.assert_max_length(queries, 8)
|
||||||
|
|
||||||
self.assert_length(events, 2, True)
|
self.assert_length(events, 2)
|
||||||
add_event, add_peer_event = events
|
add_event, add_peer_event = events
|
||||||
self.assertEqual(add_event['event']['type'], 'subscription')
|
self.assertEqual(add_event['event']['type'], 'subscription')
|
||||||
self.assertEqual(add_event['event']['op'], 'add')
|
self.assertEqual(add_event['event']['op'], 'add')
|
||||||
@@ -1347,7 +1347,7 @@ class SubscriptionAPITest(ZulipTestCase):
|
|||||||
with tornado_redirected_to_list(events):
|
with tornado_redirected_to_list(events):
|
||||||
do_add_subscription(user_profile, stream)
|
do_add_subscription(user_profile, stream)
|
||||||
|
|
||||||
self.assert_length(events, 2, True)
|
self.assert_length(events, 2)
|
||||||
add_event, add_peer_event = events
|
add_event, add_peer_event = events
|
||||||
|
|
||||||
self.assertEqual(add_event['event']['type'], 'subscription')
|
self.assertEqual(add_event['event']['type'], 'subscription')
|
||||||
@@ -1381,8 +1381,8 @@ class SubscriptionAPITest(ZulipTestCase):
|
|||||||
)
|
)
|
||||||
# Make sure Zephyr mirroring realms such as MIT do not get
|
# Make sure Zephyr mirroring realms such as MIT do not get
|
||||||
# any tornado subscription events
|
# any tornado subscription events
|
||||||
self.assert_length(events, 0, True)
|
self.assert_length(events, 0)
|
||||||
self.assert_length(queries, 7)
|
self.assert_max_length(queries, 7)
|
||||||
|
|
||||||
def test_bulk_subscribe_many(self):
|
def test_bulk_subscribe_many(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
@@ -1400,7 +1400,7 @@ class SubscriptionAPITest(ZulipTestCase):
|
|||||||
dict(principals=ujson.dumps([self.test_email])),
|
dict(principals=ujson.dumps([self.test_email])),
|
||||||
)
|
)
|
||||||
# Make sure we don't make O(streams) queries
|
# Make sure we don't make O(streams) queries
|
||||||
self.assert_length(queries, 9)
|
self.assert_max_length(queries, 9)
|
||||||
|
|
||||||
@slow("common_subscribe_to_streams is slow")
|
@slow("common_subscribe_to_streams is slow")
|
||||||
def test_subscriptions_add_for_principal(self):
|
def test_subscriptions_add_for_principal(self):
|
||||||
@@ -1874,7 +1874,7 @@ class GetSubscribersTest(ZulipTestCase):
|
|||||||
if not sub["name"].startswith("stream_"):
|
if not sub["name"].startswith("stream_"):
|
||||||
continue
|
continue
|
||||||
self.assertTrue(len(sub["subscribers"]) == len(users_to_subscribe))
|
self.assertTrue(len(sub["subscribers"]) == len(users_to_subscribe))
|
||||||
self.assert_length(queries, 4, exact=True)
|
self.assert_length(queries, 4)
|
||||||
|
|
||||||
@slow("common_subscribe_to_streams is slow")
|
@slow("common_subscribe_to_streams is slow")
|
||||||
def test_never_subscribed_streams(self):
|
def test_never_subscribed_streams(self):
|
||||||
@@ -1907,7 +1907,7 @@ class GetSubscribersTest(ZulipTestCase):
|
|||||||
if stream_dict["name"].startswith("stream_"):
|
if stream_dict["name"].startswith("stream_"):
|
||||||
self.assertFalse(stream_dict['name'] == "stream_invite_only_1")
|
self.assertFalse(stream_dict['name'] == "stream_invite_only_1")
|
||||||
self.assertTrue(len(stream_dict["subscribers"]) == len(users_to_subscribe))
|
self.assertTrue(len(stream_dict["subscribers"]) == len(users_to_subscribe))
|
||||||
self.assert_length(queries, 4, exact=True)
|
self.assert_length(queries, 4)
|
||||||
|
|
||||||
@slow("common_subscribe_to_streams is slow")
|
@slow("common_subscribe_to_streams is slow")
|
||||||
def test_gather_subscriptions_mit(self):
|
def test_gather_subscriptions_mit(self):
|
||||||
@@ -1938,7 +1938,7 @@ class GetSubscribersTest(ZulipTestCase):
|
|||||||
self.assertTrue(len(sub["subscribers"]) == len(users_to_subscribe))
|
self.assertTrue(len(sub["subscribers"]) == len(users_to_subscribe))
|
||||||
else:
|
else:
|
||||||
self.assertTrue(len(sub["subscribers"]) == 0)
|
self.assertTrue(len(sub["subscribers"]) == 0)
|
||||||
self.assert_length(queries, 4, exact=True)
|
self.assert_length(queries, 4)
|
||||||
|
|
||||||
def test_nonsubscriber(self):
|
def test_nonsubscriber(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
|
|||||||
@@ -854,7 +854,7 @@ class BotTest(ZulipTestCase):
|
|||||||
self.assert_json_success(result)
|
self.assert_json_success(result)
|
||||||
|
|
||||||
msg_event = [e for e in events if e['event']['type'] == 'message']
|
msg_event = [e for e in events if e['event']['type'] == 'message']
|
||||||
self.assert_length(msg_event, 1, exact=True) # Notification message event is sent.
|
self.assert_length(msg_event, 1) # Notification message event is sent.
|
||||||
|
|
||||||
# Create a bot.
|
# Create a bot.
|
||||||
self.assert_num_bots_equal(0)
|
self.assert_num_bots_equal(0)
|
||||||
@@ -872,7 +872,7 @@ class BotTest(ZulipTestCase):
|
|||||||
|
|
||||||
# No notification message event or invitation email is sent because of bot.
|
# No notification message event or invitation email is sent because of bot.
|
||||||
msg_event = [e for e in events_bot if e['event']['type'] == 'message']
|
msg_event = [e for e in events_bot if e['event']['type'] == 'message']
|
||||||
self.assert_length(msg_event, 0, exact=True)
|
self.assert_length(msg_event, 0)
|
||||||
self.assertEqual(len(events_bot), len(events) - 1)
|
self.assertEqual(len(events_bot), len(events) - 1)
|
||||||
|
|
||||||
# Test runner automatically redirects all sent email to a dummy 'outbox'.
|
# Test runner automatically redirects all sent email to a dummy 'outbox'.
|
||||||
@@ -1671,8 +1671,8 @@ class GetProfileTest(ZulipTestCase):
|
|||||||
with simulated_empty_cache() as cache_queries:
|
with simulated_empty_cache() as cache_queries:
|
||||||
user_profile = get_user_profile_by_email('hamlet@zulip.com')
|
user_profile = get_user_profile_by_email('hamlet@zulip.com')
|
||||||
|
|
||||||
self.assert_length(queries, 1)
|
self.assert_max_length(queries, 1)
|
||||||
self.assert_length(cache_queries, 1, exact=True)
|
self.assert_length(cache_queries, 1)
|
||||||
self.assertEqual(user_profile.email, 'hamlet@zulip.com')
|
self.assertEqual(user_profile.email, 'hamlet@zulip.com')
|
||||||
|
|
||||||
def test_api_get_empty_profile(self):
|
def test_api_get_empty_profile(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user