|
| 1 | +import os |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | +from unittest import TestCase, mock |
| 5 | +from dotenv import load_dotenv |
| 6 | +import requests_mock |
| 7 | + |
| 8 | +load_dotenv() |
| 9 | + |
| 10 | +from hotasballs.hotasballs import service |
| 11 | + |
| 12 | + |
| 13 | +class HotAsBallsTest(TestCase): |
| 14 | + def setUp(self): |
| 15 | + self.weather_data = json.load(open(Path(__file__).parent / 'weather_data.json', 'r')) |
| 16 | + self.lat = 35.640432 |
| 17 | + self.lon = 139.728833 |
| 18 | + |
| 19 | + def testTestEnv(self): |
| 20 | + '''Test that we have proper environment variables setup.''' |
| 21 | + self.assertEqual(service.HOT_THRESHOLD, 35) |
| 22 | + self.assertEqual(service.DARKSKY_SECRET_KEY, 'darkskysecret') |
| 23 | + |
| 24 | + def testWeatherGet(self): |
| 25 | + '''Test that we can grab data from Darksky and format it.''' |
| 26 | + with requests_mock.Mocker() as mock: |
| 27 | + mock.get( |
| 28 | + f"https://api.darksky.net/forecast/{os.getenv('DARKSKY_SECRET_KEY')}/{self.lat},{self.lon}?units=si&exclude=hourly", |
| 29 | + json=self.weather_data |
| 30 | + ) |
| 31 | + weather = service.get_weather(self.lat, self.lon) |
| 32 | + self.assertEqual(weather.temperatureHigh, 23.4) |
| 33 | + self.assertEqual(weather.apparentTemperatureHigh, 24.0) |
| 34 | + self.assertEqual(weather.humidity, 85) |
| 35 | + |
| 36 | + def testColdMessage(self): |
| 37 | + '''Test message creation when too cold.''' |
| 38 | + w = service.Weather({ |
| 39 | + 'temperatureHigh': 23.4, |
| 40 | + 'apparentTemperatureHigh': 24.0, |
| 41 | + 'humidity': 85, |
| 42 | + }) |
| 43 | + self.assertEqual(service.generate_message(w, 'Tokyo'), None) |
| 44 | + |
| 45 | + def testWarmMessage(self): |
| 46 | + '''Test message creation when just warm.''' |
| 47 | + w = service.Weather({ |
| 48 | + 'temperatureHigh': 23.4, |
| 49 | + 'apparentTemperatureHigh': 29.3, |
| 50 | + 'humidity': 85, |
| 51 | + }) |
| 52 | + message = service.generate_message(w, 'Tokyo') |
| 53 | + self.assertIn('Tokyo', message) |
| 54 | + self.assertNotIn('Hot as balls', message) |
| 55 | + |
| 56 | + def testHotMessage(self): |
| 57 | + '''Test message creation when it's hot as balls.''' |
| 58 | + w = service.Weather({ |
| 59 | + 'temperatureHigh': 35.8, |
| 60 | + 'apparentTemperatureHigh': 36.3, |
| 61 | + 'humidity': 85, |
| 62 | + }) |
| 63 | + message = service.generate_message(w, 'Tokyo') |
| 64 | + self.assertIn('Tokyo', message) |
| 65 | + self.assertIn('hot as balls', message) |
| 66 | + self.assertNotIn('feels like', message) |
| 67 | + |
| 68 | + def testHotFeltMessage(self): |
| 69 | + '''Test message creation when it's hot as balls and apparent temp gap.''' |
| 70 | + w = service.Weather({ |
| 71 | + 'temperatureHigh': 29.4, |
| 72 | + 'apparentTemperatureHigh': 36.3, |
| 73 | + 'humidity': 85, |
| 74 | + }) |
| 75 | + message = service.generate_message(w, 'Tokyo') |
| 76 | + self.assertIn('Tokyo', message) |
| 77 | + self.assertIn('hot as balls', message) |
| 78 | + self.assertIn('feels like', message) |
0 commit comments