예제 #1
0
    def test_known_alert_not_updated(self, mocked_read_storage, mocked_write_storage):
        alert_id = "some_id"
        published = "2019-08-22T01:45:05Z"
        fs_path = "alerts.json"

        mocked_read_storage.return_value = {
            alert_id: Alert(
                id=alert_id,
                title="title",
                updated=published,
                published=published,
                summary="summary",
            )
        }

        alerts = [
            Alert(
                id=alert_id,
                title="title",
                updated=published,
                published=published,
                summary="summary",
            )
        ]

        alerter = Alerter(self.sms_client, fs_path, alerts)
        alerter.notify_and_store_alerts()

        self.sms_client.send.assert_not_called()
        mocked_write_storage.assert_not_called()
        self.assertTrue(alerter.alerts_log["some_id"].updated == published)
예제 #2
0
    def test_newly_found_alert(self, mocked_read_storage, mocked_write_storage):
        mocked_read_storage.return_value = {
            "foo": Alert(
                id="some_id",
                title="title",
                updated="2019-08-22T01:45:05Z",
                published="published",
                summary="summary",
            )
        }

        fs_path = "alerts.json"
        alerts = [
            Alert(
                id="some_other_id",
                title="title",
                updated="2019-08-22T01:45:05Z",
                published="published",
                summary="summary",
            )
        ]

        alerter = Alerter(self.sms_client, fs_path, alerts)
        alerter.notify_and_store_alerts()

        self.sms_client.send.assert_called_once()
        mocked_write_storage.assert_called_once()
        self.assertTrue(alerter.alerts_log["some_other_id"] is not None)
예제 #3
0
 def test_alert_sms(self):
     alert = Alert(
         id="id",
         title="title",
         updated="updated",
         published="published",
         summary="summary",
     )
     sms = alert.sms()
     self.assertEqual(sms, alert.summary)
예제 #4
0
 def test_alert_updated_date(self):
     alert = Alert(
         id="id",
         title="title",
         updated="2019-08-22T01:45:05Z",
         published="published",
         summary="summary",
     )
     updated_date = alert.updated_date()
     self.assertIsInstance(updated_date, datetime)
예제 #5
0
    def test_write_storage(self):
        alerts_log = {
            "tag:weather.gc.ca,2013-04-16:20190822014505": Alert(
                id="id",
                title="title",
                updated="updated",
                published="published",
                summary="summary",
            )
        }
        fs_path = "foo"

        with patch("builtins.open", new_callable=mock_open()) as mock_file, patch(
            "json.dump"
        ) as mock_json:
            AlertStorage.write_storage(fs_path, alerts_log)
            mock_file.assert_called_with(fs_path, "w")
            mock_json.assert_called_with(
                {
                    "tag:weather.gc.ca,2013-04-16:20190822014505": {
                        "id": "id",
                        "title": "title",
                        "updated": "updated",
                        "published": "published",
                        "summary": "summary",
                    }
                },
                mock_file.return_value.__enter__.return_value,
            )
예제 #6
0
 def test_alert_initialization(self):
     alert = Alert(
         id="id",
         title="title",
         updated="updated",
         published="published",
         summary="summary",
     )
     self.assertEqual(alert.id, "id")
     self.assertEqual(alert.title, "title")
     self.assertEqual(alert.updated, "updated")
     self.assertEqual(alert.published, "published")
     self.assertEqual(alert.summary, "summary")
예제 #7
0
 def parse(url: str) -> List[Alert]:
     """
     Parses the given url (which ought to be a GC rss feed url), returning a list of Alerts
     """
     logging.info(f"Parsing alerts from {url}")
     alerts = []
     d = feedparser.parse(url)
     entries = d.entries
     for entry in entries:
         alert = Alert(
             id=entry["id"],
             title=entry["title"],
             updated=entry["updated"],
             published=entry["published"],
             summary=entry["summary"],
         )
         alerts.append(alert)
     return alerts