diff --git a/changedetectionio/model/Watch.py b/changedetectionio/model/Watch.py index 8964e4ea..50cbb6b3 100644 --- a/changedetectionio/model/Watch.py +++ b/changedetectionio/model/Watch.py @@ -271,14 +271,20 @@ class model(dict): """Unfortunately for now timestamp is stored as string key""" keys = list(self.history.keys()) + if not keys: + return None - last_viewed = self.get('last_viewed') + last_viewed = int(self.get('last_viewed')) prev_k = keys[0] sorted_keys = sorted(keys, key=lambda x: int(x)) sorted_keys.reverse() + # When the 'last viewed' timestamp is greater than the newest snapshot, return second last + if last_viewed > int(sorted_keys[0]): + return sorted_keys[1] + for k in sorted_keys: - if int(k) < int(last_viewed): + if int(k) < last_viewed: if prev_k == sorted_keys[0]: # Return the second last one so we dont recommend the same version compares itself return sorted_keys[1] diff --git a/changedetectionio/tests/unit/test_watch_model.py b/changedetectionio/tests/unit/test_watch_model.py index 8e01bafe..6c7f15ae 100644 --- a/changedetectionio/tests/unit/test_watch_model.py +++ b/changedetectionio/tests/unit/test_watch_model.py @@ -11,7 +11,7 @@ from changedetectionio.model import Watch # mostly class TestDiffBuilder(unittest.TestCase): - def test_watch_module(self): + def test_watch_get_suggested_from_diff_timestamp(self): import uuid as uuid_builder watch = Watch.model(datastore_path='/tmp', default={}) watch.ensure_data_dir_exists() @@ -33,6 +33,17 @@ class TestDiffBuilder(unittest.TestCase): p = watch.get_next_snapshot_key_to_last_viewed assert p == "115", "Correct 'second last' last-viewed timestamp was detected when using the last timestamp" + watch['last_viewed'] = 99 + p = watch.get_next_snapshot_key_to_last_viewed + assert p == "100" + + watch['last_viewed'] = 200 + p = watch.get_next_snapshot_key_to_last_viewed + assert p == "115", "When the 'last viewed' timestamp is greater than the newest snapshot, return second last " + + watch = Watch.model(datastore_path='/tmp', default={}) + p = watch.get_next_snapshot_key_to_last_viewed + assert p == None, "None when no history available" if __name__ == '__main__': unittest.main()