From 3cc4188b99d63c3cd1205b548a8f9d41a006a596 Mon Sep 17 00:00:00 2001 From: Shrivaths Shyam Date: Sat, 2 Dec 2023 00:45:30 -0800 Subject: [PATCH] Better date comparison, more tests --- sleap/gui/web.py | 12 ++++++++---- .../test_bulletin.json | 2 +- tests/gui/test_web.py | 13 +++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/sleap/gui/web.py b/sleap/gui/web.py index a6fe79d1f..fa10242dd 100644 --- a/sleap/gui/web.py +++ b/sleap/gui/web.py @@ -6,6 +6,7 @@ from typing import List, Dict, Any, Optional, Tuple import json import os +from datetime import datetime REPO_ID = "talmolab/sleap" @@ -164,7 +165,7 @@ def previous_announcement_date(self): _previous_announcement_date = self.state["announcement last seen date"] return _previous_announcement_date - def _read_bulletin_data(self): + def _read_bulletin_data(self) -> Optional[Dict]: """Reads the bulletin data from the JSON file.""" try: with open(self.bulletin_json_path, "r") as jsf: @@ -176,14 +177,17 @@ def _read_bulletin_data(self): @property def new_announcement_available(self): self._read_bulletin_data() + latest_date = datetime.strptime(self._latest_data["date"], "%m/%d/%Y") + previous_date = datetime.strptime(self.previous_announcement_date, "%m/%d/%Y") if ( - self._latest_data and self.previous_announcement_date - and self._latest_data["date"] != self.previous_announcement_date + self._latest_data + and self.previous_announcement_date + and latest_date > previous_date ): return True return False - def get_latest_announcement(self) -> Optional[Tuple[str, str]]: + def get_latest_announcement(self) -> Optional[Tuple[str, str, str]]: """Return latest announcements on the releases page not seen by user.""" if self.new_announcement_available: return ( diff --git a/tests/data/announcement_checker_bulletin/test_bulletin.json b/tests/data/announcement_checker_bulletin/test_bulletin.json index a6dbf01a4..a4b2e60c8 100644 --- a/tests/data/announcement_checker_bulletin/test_bulletin.json +++ b/tests/data/announcement_checker_bulletin/test_bulletin.json @@ -1 +1 @@ -[{"title": "title1", "date": "10/12/2023", "content": "New announcement"}, {"title": "title2", "date": "10/07/2023", "content": "Old Announcment"}] \ No newline at end of file +[{"title": "title1", "date": "10/09/2023", "content": "New announcement"}, {"title": "title2", "date": "10/07/2023", "content": "Old Announcment"}] \ No newline at end of file diff --git a/tests/gui/test_web.py b/tests/gui/test_web.py index 942d4e84f..bd0d7d26e 100644 --- a/tests/gui/test_web.py +++ b/tests/gui/test_web.py @@ -107,10 +107,23 @@ def test_announcementchecker(bulletin_json_path): announcement = checker.get_latest_announcement() assert announcement == ("title1", "10/12/2023", "New announcement") + # Check if announcement is updated checker.update_announcement() assert context.state["announcement last seen date"] == "10/12/2023" assert context.state["announcement"] == "New announcement" + # Create dummy JSON file + bulletin_data = [ + {"title": "title1", "date": "10/09/2023", "content": "New announcement"}, + {"title": "title2", "date": "10/07/2023", "content": "Old Announcment"}, + ] + with open(bulletin_json_path, "w") as test_file: + json.dump(bulletin_data, test_file) + + # Check to ensure no new announcement is created + announcement = checker.get_latest_announcement() + assert announcement == None + def test_get_analytics_data(): analytics_data = get_analytics_data()