From 5494842f4b74c2e9037f34f153ffc00c68a92954 Mon Sep 17 00:00:00 2001 From: Daniel Persson Date: Sun, 24 Nov 2024 18:12:19 +0100 Subject: [PATCH] Add unittest for Weather action Responses have been shortened to avoid big files in the repository. --- test_marvin_actions.py | 24 +++++ weatherFiles/codes.json | 8 ++ weatherFiles/station.json | 78 ++++++++++++++++ weatherFiles/weather.json | 191 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 301 insertions(+) create mode 100644 weatherFiles/codes.json create mode 100644 weatherFiles/station.json create mode 100644 weatherFiles/weather.json diff --git a/test_marvin_actions.py b/test_marvin_actions.py index 8475623..87d3dc6 100644 --- a/test_marvin_actions.py +++ b/test_marvin_actions.py @@ -6,6 +6,7 @@ """ import json +import os from datetime import date from unittest import mock, TestCase @@ -327,6 +328,29 @@ def testCommitResponse(self): expected = f"Använd detta meddelandet: '{message}'" self.assertActionOutput(marvin_actions.marvinCommit, "commit", expected) + def testWeatherRequest(self): + """Test that marvin sends the expected requests for weather info""" + with mock.patch("marvin_actions.requests") as r: + self.executeAction(marvin_actions.marvinWeather, "väder") + for url in ["https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/13/station/65090/period/latest-hour/data.json", + "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/13/codes.json", + "https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/15.5890/lat/56.1500/data.json"]: + self.assertTrue(mock.call(url, timeout=5) in r.get.call_args_list) + + def testWeatherResponse(self): + """Test that marvin properly parses weather responses""" + responses = [] + for responseFile in ["station.json", "codes.json", "weather.json"]: + with open(os.path.join("weatherFiles", responseFile), "r", encoding="UTF-8") as f: + response = requests.models.Response() + response._content = str.encode(json.dumps(json.load(f))) + responses.append(response) + + with mock.patch("marvin_actions.requests") as r: + r.get.side_effect = responses + expected = "Karlskrona just nu: 11.7 °C. Inget signifikant väder observerat." + self.assertActionOutput(marvin_actions.marvinWeather, "väder", expected) + def testCommitReaction(self): """Test that marvin only generates commit messages when asked""" self.assertActionSilent(marvin_actions.marvinCommit, "nocommit") diff --git a/weatherFiles/codes.json b/weatherFiles/codes.json new file mode 100644 index 0000000..e0407ae --- /dev/null +++ b/weatherFiles/codes.json @@ -0,0 +1,8 @@ +{ + "entry": [ + { + "key": 100, + "value": "Inget signifikant väder observerat" + } + ] +} diff --git a/weatherFiles/station.json b/weatherFiles/station.json new file mode 100644 index 0000000..96b4a53 --- /dev/null +++ b/weatherFiles/station.json @@ -0,0 +1,78 @@ +{ + "updated": 1729620000000, + "parameter": { + "key": "13", + "name": "Rådande väder", + "summary": "momentanvärde, 1 gång/tim resp 8 gånger/dygn", + "unit": "kod" + }, + "station": { + "key": "65090", + "name": "Karlskrona-Söderstjerna", + "owner": "Försvarsmakten", + "ownerCategory": "CLIMATE", + "measuringStations": "CORE", + "height": 2.0 + }, + "period": { + "key": "latest-hour", + "from": 1729616401000, + "to": 1729620000000, + "summary": "Data från senaste timmen", + "sampling": "Ej angivet" + }, + "position": [ + { + "from": 1267401600000, + "to": 1729621220000, + "height": 2.4, + "latitude": 56.15, + "longitude": 15.589 + } + ], + "link": [ + { + "href": "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/13/station/65090/period/latest-hour/data.json", + "rel": "data", + "type": "application/json" + }, + { + "href": "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/13/station/65090/period/latest-hour/data.xml", + "rel": "data", + "type": "application/xml" + }, + { + "href": "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/13/station/65090/period/latest-hour/data.csv", + "rel": "data", + "type": "text/plain" + }, + {}, + { + "href": "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/13/codes.json", + "rel": "codes", + "type": "application/json" + }, + { + "href": "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/13/codes.xml", + "rel": "codes", + "type": "application/xml" + }, + { + "href": "https://opendata-catalog.smhi.se/md/25080190-38ba-4279-a65d-d9ef8d0bf949", + "rel": "iso19139", + "type": "application/vnd.iso.19139+xml" + }, + { + "href": "https://opendata-catalog.smhi.se/md/b451cab6-2a8f-4b14-a125-fe0c49a6c9aa", + "rel": "iso19139", + "type": "application/vnd.iso.19139+xml" + } + ], + "value": [ + { + "date": 1729620000000, + "value": "100", + "quality": "G" + } + ] +} diff --git a/weatherFiles/weather.json b/weatherFiles/weather.json new file mode 100644 index 0000000..7f07039 --- /dev/null +++ b/weatherFiles/weather.json @@ -0,0 +1,191 @@ +{ + "approvedTime": "2024-10-23T19:30:09Z", + "referenceTime": "2024-10-23T19:00:00Z", + "geometry": { + "type": "Point", + "coordinates": [ + [ + 15.582146, + 56.14957 + ] + ] + }, + "timeSeries": [ + { + "validTime": "2024-10-23T20:00:00Z", + "parameters": [ + { + "name": "spp", + "levelType": "hl", + "level": 0, + "unit": "percent", + "values": [ + -9 + ] + }, + { + "name": "pcat", + "levelType": "hl", + "level": 0, + "unit": "category", + "values": [ + 0 + ] + }, + { + "name": "pmin", + "levelType": "hl", + "level": 0, + "unit": "kg/m2/h", + "values": [ + 0.0 + ] + }, + { + "name": "pmean", + "levelType": "hl", + "level": 0, + "unit": "kg/m2/h", + "values": [ + 0.0 + ] + }, + { + "name": "pmax", + "levelType": "hl", + "level": 0, + "unit": "kg/m2/h", + "values": [ + 0.0 + ] + }, + { + "name": "pmedian", + "levelType": "hl", + "level": 0, + "unit": "kg/m2/h", + "values": [ + 0.0 + ] + }, + { + "name": "tcc_mean", + "levelType": "hl", + "level": 0, + "unit": "octas", + "values": [ + 3 + ] + }, + { + "name": "lcc_mean", + "levelType": "hl", + "level": 0, + "unit": "octas", + "values": [ + 0 + ] + }, + { + "name": "mcc_mean", + "levelType": "hl", + "level": 0, + "unit": "octas", + "values": [ + 0 + ] + }, + { + "name": "hcc_mean", + "levelType": "hl", + "level": 0, + "unit": "octas", + "values": [ + 3 + ] + }, + { + "name": "t", + "levelType": "hl", + "level": 2, + "unit": "Cel", + "values": [ + 11.7 + ] + }, + { + "name": "msl", + "levelType": "hmsl", + "level": 0, + "unit": "hPa", + "values": [ + 1031.3 + ] + }, + { + "name": "vis", + "levelType": "hl", + "level": 2, + "unit": "km", + "values": [ + 38.5 + ] + }, + { + "name": "wd", + "levelType": "hl", + "level": 10, + "unit": "degree", + "values": [ + 260 + ] + }, + { + "name": "ws", + "levelType": "hl", + "level": 10, + "unit": "m/s", + "values": [ + 7.7 + ] + }, + { + "name": "r", + "levelType": "hl", + "level": 2, + "unit": "percent", + "values": [ + 83 + ] + }, + { + "name": "tstm", + "levelType": "hl", + "level": 0, + "unit": "percent", + "values": [ + 0 + ] + }, + { + "name": "gust", + "levelType": "hl", + "level": 10, + "unit": "m/s", + "values": [ + 10.1 + ] + }, + { + "name": "Wsymb2", + "levelType": "hl", + "level": 0, + "unit": "category", + "values": [ + 2 + ] + } + ] + } + ] +}