Repository: zulip/zulip

Issue:
Get `zerver/actions/*.py` to 100% test coverage.
Lines missing coverage:

```
ERROR: zerver/actions/create_realm.py no longer has complete backend test coverage
  Lines missing coverage: [167, 194]

ERROR: zerver/actions/message_delete.py no longer has complete backend test coverage
  Lines missing coverage: [27]

ERROR: zerver/actions/message_edit.py no longer has complete backend test coverage
  Lines missing coverage: [925, 1098, 1166]

ERROR: zerver/actions/presence.py no longer has complete backend test coverage
  Lines missing coverage: [119]

ERROR: zerver/actions/scheduled_messages.py no longer has complete backend test coverage
  Lines missing coverage: [111]
```


Discussion:
Hello @zulip/server-testing members, this issue was labeled with the "area: testing-coverage" label, so you may want to check it out!

<!-- areaLabelAddition -->

The `message_delete.py` one is already fixed by https://github.com/zulip/zulip/pull/25494.
@zulipbot claim
Welcome to Zulip, @CertifiedJoon! We just sent you an invite to collaborate on this repository at https://github.com/zulip/zulip/invitations. Please accept this invite in order to claim this issue and begin a fun, rewarding experience contributing to Zulip!

Here's some tips to get you off to a good start:

- Join me on the [Zulip developers' server](https://chat.zulip.org), to get help, chat about this issue, and meet the other developers.
- [Unwatch this repository](https://help.github.com/articles/unwatching-repositories/), so that you don't get 100 emails a day.

As you work on this issue, you'll also want to refer to the [Zulip code contribution guide](https://zulip.readthedocs.io/en/latest/contributing/index.html), as well as the rest of the developer documentation on that site.

See you on the other side (that is, the pull request side)!

@adityanarayanm095 You have been unassigned from this issue because you have not made any updates for over 14 days. Please feel free to reclaim the issue if you decide to pick up again. Thanks!

@zulipbot abandon
@zulipbot claim
Welcome to Zulip, @adityanarayanm095! We just sent you an invite to collaborate on this repository at https://github.com/zulip/zulip/invitations. Please accept this invite in order to claim this issue and begin a fun, rewarding experience contributing to Zulip!

Here's some tips to get you off to a good start:

- Join me on the [Zulip developers' server](https://chat.zulip.org), to get help, chat about this issue, and meet the other developers.
- [Unwatch this repository](https://help.github.com/articles/unwatching-repositories/), so that you don't get 100 emails a day.

As you work on this issue, you'll also want to refer to the [Zulip code contribution guide](https://zulip.readthedocs.io/en/latest/contributing/index.html), as well as the rest of the developer documentation on that site.

See you on the other side (that is, the pull request side)!

@timabbott 
1) How to produce this error locally? to confirm after code change?
2) How to understand the method to write test-covergage for any particular file?
Thanks @adityanarayanm095 for picking up this issue. This issue is related to the coverage provided by the tests for some; whose details are in the issue description. 

You can check if your code works by checking out the test coverage by running `./tools/test-backend --coverage` -- this will generate an HTML page from which you can find out the current coverage for each file related to the backend.

Also, I see there are five files mentioned in the issue description, but I just generated the coverage report. It looks like only `zerver/actions/presence.py` is missing 100% test coverage, and the rest of the files in `zerver/actions` have got full test coverage. So you need to just get one file to full test coverage to solve this issue.

The tests are located in `zerver/tests` -- try taking a look at `zerver/tests/test_presence.py` to see how they are laid out. 
> Thanks @adityanarayanm095 for picking up this issue. This issue is related to the coverage provided by the tests for some; whose details are in the issue description.
> 
> You can check if your code works by checking out the test coverage by running `./tools/test-backend --coverage` -- this will generate an HTML page from which you can find out the current coverage for each file related to the backend.
> 
> Also, I see there are five files mentioned in the issue description, but I just generated the coverage report. It looks like only `zerver/actions/presence.py` is missing 100% test coverage, and the rest of the files in `zerver/actions` have got full test coverage. So you need to just get one file to full test coverage to solve this issue.
> 
> The tests are located in `zerver/tests` -- try taking a look at `zerver/tests/test_presence.py` to see how they are laid out.

@sbansal1999, Below and few other lines are not covered so how to get idea to write test coverage for them?, i have read `zerver/tests/test_presence.py` but not able to relate with the missing code coverage.
 
![image](https://github.com/zulip/zulip/assets/91023072/a28c76b5-bc41-4409-9390-be2ac1173785)

@adityanarayanm095  I ran the tests again just to double check and there is only one line (line no. 119) that is missing test coverage in `zerver/actions/presence.py`. 

I would recommend updating your fork as it could be a possible reason for the discrepancy. Read - [keeping your fork updated](https://zulip.readthedocs.io/en/latest/git/using.html#keep-your-fork-up-to-date). Also for help on writing tests, you can have a look at [this](https://zulip.readthedocs.io/en/latest/testing/testing-with-django.html#writing-tests) page.

Feel free to join [The Zulip development community](https://zulip.com/development-community/) to discuss related things.
@zulipbot claim

**ERROR:** Unexpected response from GitHub API.
@zulipbot claim
@adityanarayanm095 You have been unassigned from this issue because you have not made any updates for over 14 days. Please feel free to reclaim the issue if you decide to pick up again. Thanks!

@zulipbot claim
Welcome to Zulip, @aakash1408! We just sent you an invite to collaborate on this repository at https://github.com/zulip/zulip/invitations. Please accept this invite in order to claim this issue and begin a fun, rewarding experience contributing to Zulip!

Here's some tips to get you off to a good start:

- Join me on the [Zulip developers' server](https://chat.zulip.org), to get help, chat about this issue, and meet the other developers.
- [Unwatch this repository](https://help.github.com/articles/unwatching-repositories/), so that you don't get 100 emails a day.

As you work on this issue, you'll also want to refer to the [Zulip code contribution guide](https://zulip.readthedocs.io/en/latest/contributing/index.html), as well as the rest of the developer documentation on that site.

See you on the other side (that is, the pull request side)!

@Haider8 You have been unassigned from this issue because you have not made any updates for over 14 days. Please feel free to reclaim the issue if you decide to pick up again. Thanks!

@zulipbot abandon
@zulipbot claim
Welcome to Zulip, @Haider8! We just sent you an invite to collaborate on this repository at https://github.com/zulip/zulip/invitations. Please accept this invite in order to claim this issue and begin a fun, rewarding experience contributing to Zulip!

Here's some tips to get you off to a good start:

- Join me on the [Zulip developers' server](https://chat.zulip.org), to get help, chat about this issue, and meet the other developers.
- [Unwatch this repository](https://help.github.com/articles/unwatching-repositories/), so that you don't get 100 emails a day.

As you work on this issue, you'll also want to refer to the [Zulip code contribution guide](https://zulip.readthedocs.io/en/latest/contributing/index.html), as well as the rest of the developer documentation on that site.

See you on the other side (that is, the pull request side)!

@Haider8 Are you working on this issue?

Let me work on this @amanagr 

assign 
@zulipbot claim
Welcome to Zulip, @dhruvpal05! We just sent you an invite to collaborate on this repository at https://github.com/zulip/zulip/invitations. Please accept this invite in order to claim this issue and begin a fun, rewarding experience contributing to Zulip!

Here's some tips to get you off to a good start:

- Join me on the [Zulip developers' server](https://chat.zulip.org), to get help, chat about this issue, and meet the other developers.
- [Unwatch this repository](https://help.github.com/articles/unwatching-repositories/), so that you don't get 100 emails a day.

As you work on this issue, you'll also want to refer to the [Zulip code contribution guide](https://zulip.readthedocs.io/en/latest/contributing/index.html), as well as the rest of the developer documentation on that site.

See you on the other side (that is, the pull request side)!

@zulipbot claim

Welcome to Zulip, @ro4i7! We just sent you an invite to collaborate on this repository at https://github.com/zulip/zulip/invitations. Please accept this invite in order to claim this issue and begin a fun, rewarding experience contributing to Zulip!

Here's some tips to get you off to a good start:

- Join me on the [Zulip developers' server](https://chat.zulip.org), to get help, chat about this issue, and meet the other developers.
- [Unwatch this repository](https://help.github.com/articles/unwatching-repositories/), so that you don't get 100 emails a day.

As you work on this issue, you'll also want to refer to the [Zulip code contribution guide](https://zulip.readthedocs.io/en/latest/contributing/index.html), as well as the rest of the developer documentation on that site.

See you on the other side (that is, the pull request side)!

@sbansal1999 is this issue has been solved by #26511 or been solving by him?
@zulipbot abandon

@zulipbot claim

@vizerion07 You have been unassigned from this issue because you have not made any updates for over 14 days. Please feel free to reclaim the issue if you decide to pick up again. Thanks!

@zulipbot claim

Resolving patch:
diff --git a/zerver/actions/presence.py b/zerver/actions/presence.py
--- a/zerver/actions/presence.py
+++ b/zerver/actions/presence.py
@@ -113,20 +113,17 @@ def do_update_user_presence(
         defaults=defaults,
     )
 
+    # We initialize these values as a large delta so that if the user
+    # was never active, we always treat the user as newly online.
+    time_since_last_active_for_comparison = timedelta(days=1)
+    time_since_last_connected_for_comparison = timedelta(days=1)
     if presence.last_active_time is not None:
-        time_since_last_active = log_time - presence.last_active_time
-    else:
-        # The user was never active, so let's consider this large to go over any thresholds
-        # we may have.
-        time_since_last_active = timedelta(days=1)
+        time_since_last_active_for_comparison = log_time - presence.last_active_time
     if presence.last_connected_time is not None:
-        time_since_last_connected = log_time - presence.last_connected_time
-    else:
-        # Same approach as above.
-        time_since_last_connected = timedelta(days=1)
+        time_since_last_connected_for_comparison = log_time - presence.last_connected_time
 
     assert (3 * settings.PRESENCE_PING_INTERVAL_SECS + 20) <= settings.OFFLINE_THRESHOLD_SECS
-    now_online = time_since_last_active > timedelta(
+    now_online = time_since_last_active_for_comparison > timedelta(
         # Here, we decide whether the user is newly online, and we need to consider
         # sending an immediate presence update via the events system that this user is now online,
         # rather than waiting for other clients to poll the presence update.
@@ -148,7 +145,7 @@ def do_update_user_presence(
     # times per minute with multiple connected browser windows.
     # We also need to be careful not to wrongly "update" the timestamp if we actually already
     # have newer presence than the reported log_time.
-    if not created and time_since_last_connected > timedelta(
+    if not created and time_since_last_connected_for_comparison > timedelta(
         seconds=settings.PRESENCE_UPDATE_MIN_FREQ_SECONDS
     ):
         presence.last_connected_time = log_time
@@ -156,7 +153,8 @@ def do_update_user_presence(
     if (
         not created
         and status == UserPresence.LEGACY_STATUS_ACTIVE_INT
-        and time_since_last_active > timedelta(seconds=settings.PRESENCE_UPDATE_MIN_FREQ_SECONDS)
+        and time_since_last_active_for_comparison
+        > timedelta(seconds=settings.PRESENCE_UPDATE_MIN_FREQ_SECONDS)
     ):
         presence.last_active_time = log_time
         update_fields.append("last_active_time")


Test patch: