Skip to content

Commit

Permalink
Add unittest for Weather action
Browse files Browse the repository at this point in the history
Responses have been shortened to avoid big files in the repository.
  • Loading branch information
kh31d4r committed Nov 24, 2024
1 parent 7e262d4 commit 5494842
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 0 deletions.
24 changes: 24 additions & 0 deletions test_marvin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import json
import os

from datetime import date
from unittest import mock, TestCase
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 8 additions & 0 deletions weatherFiles/codes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"entry": [
{
"key": 100,
"value": "Inget signifikant väder observerat"
}
]
}
78 changes: 78 additions & 0 deletions weatherFiles/station.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
191 changes: 191 additions & 0 deletions weatherFiles/weather.json
Original file line number Diff line number Diff line change
@@ -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
]
}
]
}
]
}

0 comments on commit 5494842

Please sign in to comment.