From 64b3cbe3ace3fb95ede7e820918ee012197abbc9 Mon Sep 17 00:00:00 2001 From: MoshiMoshi0 Date: Tue, 24 Dec 2024 00:30:58 +0100 Subject: [PATCH] Remove possibility of unreachable code --- changedetectionio/model/Watch.py | 9 ++++----- changedetectionio/tests/unit/test_watch_model.py | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/changedetectionio/model/Watch.py b/changedetectionio/model/Watch.py index 7604c843..2fc82616 100644 --- a/changedetectionio/model/Watch.py +++ b/changedetectionio/model/Watch.py @@ -255,6 +255,8 @@ class model(watch_base): keys = list(self.history.keys()) if not keys: return None + if len(keys) == 1: + return keys[0] last_viewed = int(self.get('last_viewed')) sorted_keys = sorted(keys, key=lambda x: int(x)) @@ -264,15 +266,12 @@ class model(watch_base): if last_viewed >= int(sorted_keys[0]): return sorted_keys[1] - # When the 'last viewed' timestamp is less than or equal the oldest snapshot, return oldest - if last_viewed <= int(sorted_keys[-1]): - return sorted_keys[-1] - + # When the 'last viewed' timestamp is between snapshots, return the older snapshot for newer, older in list(zip(sorted_keys[0:], sorted_keys[1:])): if last_viewed < int(newer) and last_viewed >= int(older): return older - # Unreachable, return oldest + # When the 'last viewed' timestamp is less than the oldest snapshot, return oldest return sorted_keys[-1] def get_history_snapshot(self, timestamp): diff --git a/changedetectionio/tests/unit/test_watch_model.py b/changedetectionio/tests/unit/test_watch_model.py index de3e023c..1856b0dd 100644 --- a/changedetectionio/tests/unit/test_watch_model.py +++ b/changedetectionio/tests/unit/test_watch_model.py @@ -57,5 +57,9 @@ class TestDiffBuilder(unittest.TestCase): p = watch.get_from_version_based_on_last_viewed assert p == "100", "Correct with only one history snapshot" + watch['last_viewed'] = 200 + p = watch.get_from_version_based_on_last_viewed + assert p == "100", "Correct with only one history snapshot" + if __name__ == '__main__': unittest.main()