typing: Add none-checks for db queries.

Signed-off-by: Zixuan James Li <359101898@qq.com>
This commit is contained in:
Zixuan James Li 2022-05-30 19:34:34 -04:00 committed by Tim Abbott
parent 4a5043dd6e
commit c572d9be5a
10 changed files with 39 additions and 13 deletions

View File

@ -504,7 +504,9 @@ def make_end_of_cycle_updates_if_needed(
standard_plan_last_ledger = (
LicenseLedger.objects.filter(plan=standard_plan).order_by("id").last()
)
assert standard_plan_last_ledger is not None
licenses_for_plus_plan = standard_plan_last_ledger.licenses_at_next_renewal
assert licenses_for_plus_plan is not None
plus_plan_ledger_entry = LicenseLedger.objects.create(
plan=plus_plan,
is_renewal=True,
@ -1040,6 +1042,7 @@ def estimate_annual_recurring_revenue_by_realm() -> Dict[str, int]: # nocoverag
if plan.billing_schedule == CustomerPlan.MONTHLY:
renewal_cents *= 12
# TODO: Decimal stuff
assert plan.customer.realm is not None
annual_revenue[plan.customer.realm.string_id] = int(renewal_cents / 100)
return annual_revenue
@ -1048,6 +1051,7 @@ def get_realms_to_default_discount_dict() -> Dict[str, Decimal]:
realms_to_default_discount: Dict[str, Any] = {}
customers = Customer.objects.exclude(default_discount=None).exclude(default_discount=0)
for customer in customers:
assert customer.realm is not None
realms_to_default_discount[customer.realm.string_id] = assert_is_not_none(
customer.default_discount
)
@ -1116,6 +1120,7 @@ def downgrade_small_realms_behind_on_payments_as_needed() -> None:
customers = Customer.objects.all().exclude(stripe_customer_id=None)
for customer in customers:
realm = customer.realm
assert realm is not None
# For larger realms, we generally want to talk to the customer
# before downgrading or cancelling invoices; so this logic only applies with 5.
@ -1172,6 +1177,7 @@ def switch_realm_from_standard_to_plus_plan(realm: Realm) -> None:
standard_plan_last_renewal_ledger = (
LicenseLedger.objects.filter(is_renewal=True, plan=standard_plan).order_by("id").last()
)
assert standard_plan_last_renewal_ledger is not None
standard_plan_last_renewal_amount = (
standard_plan_last_renewal_ledger.licenses * standard_plan.price_per_license
)

View File

@ -583,10 +583,12 @@ class StripeTestCase(ZulipTestCase):
else:
expected_session_details["type"] = "free_trial_upgrade_from_billing_page"
else:
last_stripe_payment_intent = PaymentIntent.objects.last()
assert last_stripe_payment_intent is not None
expected_session_details["type"] = "upgrade_from_billing_page"
expected_session_details[
"stripe_payment_intent_id"
] = PaymentIntent.objects.last().stripe_payment_intent_id
] = last_stripe_payment_intent.stripe_payment_intent_id
self.assert_details_of_valid_session_from_event_status_endpoint(
upgrade_json_response.json()["stripe_session_id"], expected_session_details

View File

@ -1029,6 +1029,7 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
)
for realm_emoji in RealmEmoji.objects.filter(realm=realm):
if realm_emoji.author_id is None:
assert first_user_profile is not None
realm_emoji.author_id = first_user_profile.id
realm_emoji.save(update_fields=["author_id"])

View File

@ -567,6 +567,7 @@ class TestEmailMirrorMessagesWithAttachments(ZulipTestCase):
message = most_recent_message(user_profile)
attachment = Attachment.objects.last()
assert attachment is not None
self.assertEqual(list(attachment.messages.values_list("id", flat=True)), [message.id])
self.assertEqual(
message.sender, get_system_bot(settings.EMAIL_GATEWAY_BOT, stream.realm_id)

View File

@ -338,6 +338,7 @@ class TestFollowupEmails(ZulipTestCase):
cordelia = self.example_user("cordelia")
enqueue_welcome_emails(self.example_user("cordelia"), realm_creation=True)
scheduled_email = ScheduledEmail.objects.filter(users=cordelia).last()
assert scheduled_email is not None
self.assertEqual(
orjson.loads(scheduled_email.data)["template_prefix"], "zerver/emails/followup_day1"
)
@ -359,6 +360,7 @@ class TestFollowupEmails(ZulipTestCase):
cordelia.realm.save()
enqueue_welcome_emails(self.example_user("cordelia"), realm_creation=True)
scheduled_email = ScheduledEmail.objects.filter(users=cordelia).last()
assert scheduled_email is not None
self.assertEqual(
orjson.loads(scheduled_email.data)["template_prefix"], "zerver/emails/followup_day1"
)

View File

@ -223,6 +223,7 @@ class ExportFile(ZulipTestCase):
realm_emoji = RealmEmoji.objects.get(author=user)
file_name = realm_emoji.file_name
assert file_name is not None
assert file_name.endswith(".png")
emoji_path = f"{realm.id}/emoji/images/{file_name}"
@ -510,6 +511,7 @@ class RealmImportExportTest(ExportFile):
content="Thumbs up for export",
)
message = Message.objects.last()
assert message is not None
consented_user_ids = [self.example_user(user).id for user in ["iago", "hamlet"]]
do_add_reaction(
self.example_user("iago"), message, "outbox", "1f4e4", Reaction.UNICODE_EMOJI
@ -1517,10 +1519,10 @@ class SingleUserExportTest(ExportFile):
reaction_type=None,
)
reaction = Reaction.objects.order_by("id").last()
assert reaction
@checker
def zerver_reaction(records: List[Record]) -> None:
assert reaction
(exported_reaction,) = records
self.assertEqual(
exported_reaction,

View File

@ -373,6 +373,7 @@ class PreviewTestCase(ZulipTestCase):
embedded_link = f'<a href="{url}" title="The Rock">The Rock</a>'
msg = Message.objects.select_related("sender").get(id=msg_id)
assert msg.rendered_content is not None
self.assertIn(embedded_link, msg.rendered_content)
@responses.activate
@ -462,6 +463,7 @@ class PreviewTestCase(ZulipTestCase):
# up-to-date event for edited_url.
queue_json_publish(*args, **kwargs)
msg = Message.objects.select_related("sender").get(id=msg_id)
assert msg.rendered_content is not None
self.assertIn(
f'<a href="{edited_url}" title="The Rock">The Rock</a>',
msg.rendered_content,
@ -821,6 +823,7 @@ class PreviewTestCase(ZulipTestCase):
assert cached_data is not None
msg = Message.objects.select_related("sender").get(id=msg_id)
assert msg.rendered_content is not None
self.assertIn(cached_data.title, msg.rendered_content)
assert cached_data.image is not None
self.assertIn(re.sub(r"([^\w-])", r"\\\1", cached_data.image), msg.rendered_content)
@ -946,6 +949,7 @@ class PreviewTestCase(ZulipTestCase):
self.assertEqual(cached_data, mocked_data)
msg.refresh_from_db()
assert msg.rendered_content is not None
self.assertIn(f'a data-id="{escape(mocked_data.html)}"', msg.rendered_content)
@responses.activate

View File

@ -1195,6 +1195,7 @@ class MarkdownTest(ZulipTestCase):
realm_emoji = RealmEmoji.objects.filter(
realm=realm, name="green_tick", deactivated=False
).get()
assert realm_emoji.file_name is not None
self.assertEqual(
converted.rendered_content,
"<p>{}</p>".format(emoji_img(":green_tick:", realm_emoji.file_name, realm.id)),

View File

@ -28,6 +28,7 @@ from zerver.lib.user_topics import (
set_topic_mutes,
topic_is_muted,
)
from zerver.lib.utils import assert_is_not_none
from zerver.models import Message, Realm, Stream, UserMessage, UserProfile, get_realm, get_stream
@ -767,7 +768,7 @@ class EditMessageTest(EditMessageTestCase):
},
)
self.assert_json_success(result)
history = orjson.loads(Message.objects.get(id=msg_id).edit_history)
history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))
self.assertEqual(history[0]["prev_content"], "content 1")
self.assertEqual(history[0]["user_id"], hamlet.id)
self.assertEqual(
@ -788,7 +789,7 @@ class EditMessageTest(EditMessageTestCase):
},
)
self.assert_json_success(result)
history = orjson.loads(Message.objects.get(id=msg_id).edit_history)
history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))
self.assertEqual(history[0]["prev_topic"], "topic 1")
self.assertEqual(history[0]["topic"], "topic 2")
self.assertEqual(history[0]["user_id"], hamlet.id)
@ -805,7 +806,7 @@ class EditMessageTest(EditMessageTestCase):
},
)
self.assert_json_success(result)
history = orjson.loads(Message.objects.get(id=msg_id).edit_history)
history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))
self.assertEqual(history[0]["prev_stream"], stream_1.id)
self.assertEqual(history[0]["stream"], stream_2.id)
self.assertEqual(history[0]["user_id"], self.example_user("iago").id)
@ -820,7 +821,7 @@ class EditMessageTest(EditMessageTestCase):
},
)
self.assert_json_success(result)
history = orjson.loads(Message.objects.get(id=msg_id).edit_history)
history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))
self.assertEqual(history[0]["prev_content"], "content 2")
self.assertEqual(history[0]["prev_topic"], "topic 2")
self.assertEqual(history[0]["topic"], "topic 3")
@ -845,7 +846,7 @@ class EditMessageTest(EditMessageTestCase):
},
)
self.assert_json_success(result)
history = orjson.loads(Message.objects.get(id=msg_id).edit_history)
history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))
self.assertEqual(history[0]["prev_content"], "content 3")
self.assertEqual(history[0]["user_id"], hamlet.id)
@ -858,7 +859,7 @@ class EditMessageTest(EditMessageTestCase):
},
)
self.assert_json_success(result)
history = orjson.loads(Message.objects.get(id=msg_id).edit_history)
history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))
self.assertEqual(history[0]["prev_topic"], "topic 3")
self.assertEqual(history[0]["topic"], "topic 4")
self.assertEqual(history[0]["prev_stream"], stream_2.id)
@ -878,7 +879,7 @@ class EditMessageTest(EditMessageTestCase):
# Now, we verify that all of the edits stored in the message.edit_history
# have the correct data structure
history = orjson.loads(Message.objects.get(id=msg_id).edit_history)
history = orjson.loads(assert_is_not_none(Message.objects.get(id=msg_id).edit_history))
self.assertEqual(history[0]["prev_topic"], "topic 3")
self.assertEqual(history[0]["topic"], "topic 4")
@ -1588,7 +1589,9 @@ class EditMessageTest(EditMessageTestCase):
# Since edit history is being generated by do_update_message,
# it's contents can vary over time; So, to keep this test
# future proof, we only verify it's length.
self.assert_length(orjson.loads(msg.edit_history), len_edit_history)
self.assert_length(
orjson.loads(assert_is_not_none(msg.edit_history)), len_edit_history
)
for msg_id in [id3, id4]:
msg = Message.objects.get(id=msg_id)
@ -1642,18 +1645,20 @@ class EditMessageTest(EditMessageTestCase):
msg2 = Message.objects.get(id=id2)
msg3 = Message.objects.get(id=id3)
msg1_edit_history = orjson.loads(msg1.edit_history)
msg1_edit_history = orjson.loads(assert_is_not_none(msg1.edit_history))
self.assertTrue("prev_content" in msg1_edit_history[0].keys())
for msg in [msg2, msg3]:
self.assertFalse("prev_content" in orjson.loads(msg.edit_history)[0].keys())
self.assertFalse(
"prev_content" in orjson.loads(assert_is_not_none(msg.edit_history))[0].keys()
)
for msg in [msg1, msg2, msg3]:
self.assertEqual(
new_topic,
msg.topic_name(),
)
self.assert_length(orjson.loads(msg.edit_history), 1)
self.assert_length(orjson.loads(assert_is_not_none(msg.edit_history)), 1)
def test_propagate_topic_forward(self) -> None:
self.login("hamlet")

View File

@ -287,6 +287,7 @@ class TestSCIMUser(SCIMTestCase):
self.assertEqual(new_user_count, original_user_count + 1)
new_user = UserProfile.objects.last()
assert new_user is not None
self.assertEqual(new_user.delivery_email, "newuser@zulip.com")
self.assertEqual(new_user.full_name, "New User")
@ -315,6 +316,7 @@ class TestSCIMUser(SCIMTestCase):
self.assertEqual(new_user_count, original_user_count + 1)
new_user = UserProfile.objects.last()
assert new_user is not None
self.assertEqual(new_user.delivery_email, "newuser@zulip.com")
self.assertEqual(new_user.full_name, "New User")