Repository: Vaskivskyi/asusrouter (language: python)

Issue:
Update tools module



Discussion / hints:
## [Codecov](https://app.codecov.io/gh/Vaskivskyi/asusrouter/pull/373?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yevhenii+Vaskivskyi) Report
Attention: `1 lines` in your changes are missing coverage. Please review.
> Comparison is base [(`f516d21`)](https://app.codecov.io/gh/Vaskivskyi/asusrouter/commit/f516d21f2dafb0ea945774a7e9d63c0479397646?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yevhenii+Vaskivskyi) 30.23% compared to head [(`da3bfc6`)](https://app.codecov.io/gh/Vaskivskyi/asusrouter/pull/373?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yevhenii+Vaskivskyi) 30.43%.

| [Files](https://app.codecov.io/gh/Vaskivskyi/asusrouter/pull/373?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yevhenii+Vaskivskyi) | Patch % | Lines |
|---|---|---|
| [asusrouter/modules/connection.py](https://app.codecov.io/gh/Vaskivskyi/asusrouter/pull/373?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yevhenii+Vaskivskyi#diff-YXN1c3JvdXRlci9tb2R1bGVzL2Nvbm5lY3Rpb24ucHk=) | 50.00% | [1 Missing :warning: ](https://app.codecov.io/gh/Vaskivskyi/asusrouter/pull/373?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yevhenii+Vaskivskyi) |

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##              dev     #373      +/-   ##
==========================================
+ Coverage   30.23%   30.43%   +0.20%     
==========================================
  Files          58       57       -1     
  Lines        2967     2970       +3     
==========================================
+ Hits          897      904       +7     
+ Misses       2070     2066       -4     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/Vaskivskyi/asusrouter/pull/373?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yevhenii+Vaskivskyi).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yevhenii+Vaskivskyi).


Fix patch:
diff --git a/asusrouter/modules/client.py b/asusrouter/modules/client.py
index be481d6..e065481 100644
--- a/asusrouter/modules/client.py
+++ b/asusrouter/modules/client.py
@@ -210,7 +210,7 @@ def process_client_description(data: dict[str, Any]) -> AsusClientDescription:
         for pair in value:
             key_to_find, converter = safe_unpack_key(pair)
             item = data.get(key_to_find)
-            if item and item != str():
+            if item is not None and item != str():
                 setattr(
                     description,
                     key,
diff --git a/asusrouter/modules/connection.py b/asusrouter/modules/connection.py
index 1ad5c7d..bd43d11 100644
--- a/asusrouter/modules/connection.py
+++ b/asusrouter/modules/connection.py
@@ -66,6 +66,14 @@ def get_connection_type(value: Optional[int]) -> ConnectionType:
     """Get connection type."""
 
     # Check that it's actually an int
-    value = safe_int(value) or 0
+    # This would actually work with float as well doing direct conversion
+    value = safe_int(value)
 
-    return get_enum_key_by_value(ConnectionType, value, ConnectionType.WIRED)
+    # ------------------------------------------------------ #
+    # I am officially considering this a feature, not a bug. #
+    # ------------------------------------------------------ #
+
+    if value is None:
+        value = 0
+
+    return CONNECTION_TYPE.get(value, ConnectionType.WIRED)


Test patch:
diff --git a/tests/modules/test_connection.py b/tests/modules/test_connection.py
new file mode 100644
index 0000000..1968fd2
--- /dev/null
+++ b/tests/modules/test_connection.py
@@ -0,0 +1,57 @@
+"""Tests for the connection module."""
+
+import pytest
+
+from asusrouter.modules.connection import (
+    ConnectionType,
+    InternetMode,
+    get_connection_type,
+    get_internet_mode,
+)
+
+
+@pytest.mark.parametrize(
+    "value, result",
+    [
+        # Existing values of InternetMode
+        ("allow", InternetMode.ALLOW),
+        ("block", InternetMode.BLOCK),
+        ("time", InternetMode.TIME),
+        # Unknown values of the same type
+        ("unknown", InternetMode.UNKNOWN),
+        ("", InternetMode.UNKNOWN),
+        # Values of the wrong type
+        (None, InternetMode.UNKNOWN),
+        (1, InternetMode.UNKNOWN),
+    ],
+)
+def test_get_internet_mode(value, result):
+    """Test get_internet_mode."""
+
+    assert get_internet_mode(value) == result
+
+
+@pytest.mark.parametrize(
+    "value, result",
+    [
+        # Existing values of ConnectionType
+        (0, ConnectionType.WIRED),
+        (1, ConnectionType.WLAN_2G),
+        (2, ConnectionType.WLAN_5G),
+        (3, ConnectionType.WLAN_5G2),
+        (4, ConnectionType.WLAN_6G),
+        # Unknown values of the same type
+        (-1, ConnectionType.WIRED),
+        (5, ConnectionType.WIRED),
+        # Values of the wrong type
+        (None, ConnectionType.WIRED),
+        ("", ConnectionType.WIRED),
+        # Special cases which would still work
+        (1.0, ConnectionType.WLAN_2G),
+        (2.3, ConnectionType.WLAN_5G),
+    ],
+)
+def test_get_connection_type(value, result):
+    """Test get_connection_type."""
+
+    assert get_connection_type(value) == result