Skip to content

Commit 218cb2f

Browse files
committed
unit testing! yay!
1 parent d7b2727 commit 218cb2f

File tree

8 files changed

+473
-11
lines changed

8 files changed

+473
-11
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: project-requirements requirements run deploy
1+
.PHONY: project-requirements requirements run deploy test
22

33
project-requirements:
44
cd hotasballs; pip-compile --no-annotate
@@ -10,5 +10,8 @@ requirements: project-requirements
1010
run:
1111
cd hotasballs; lambda invoke -v
1212

13+
test:
14+
green -vvv
15+
1316
deploy:
1417
cd hotasballs; lambda deploy --requirements requirements.txt

__init__.py

Whitespace-only changes.

hotasballs/__init__.py

Whitespace-only changes.

requirements.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
-r hotasballs/requirements.txt
1+
-r hotasballs/requirements.in
22
python-lambda
33
pylint
4-
autopep8
4+
autopep8
5+
python-dotenv
6+
green
7+
requests-mock

requirements.txt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,34 @@ astroid==2.2.5 # via pylint
88
autopep8==1.4.4
99
boto3==1.9.162 # via python-lambda
1010
botocore==1.12.162 # via boto3, s3transfer
11-
certifi==2019.3.9
12-
chardet==3.0.4
11+
certifi==2019.3.9 # via requests
12+
chardet==3.0.4 # via requests
1313
click==7.0 # via python-lambda
14+
colorama==0.4.1 # via green
15+
coverage==4.5.3 # via green
1416
docutils==0.14 # via botocore
15-
idna==2.8
17+
green==2.16.1
18+
idna==2.8 # via requests
1619
isort==4.3.20 # via pylint
1720
jmespath==0.9.4 # via boto3, botocore
1821
lazy-object-proxy==1.4.1 # via astroid
22+
lxml==4.3.3 # via green
1923
mccabe==0.6.1 # via pylint
20-
oauthlib==3.0.1
24+
oauthlib==3.0.1 # via requests-oauthlib
2125
pycodestyle==2.5.0 # via autopep8
2226
pylint==2.3.1
23-
pysocks==1.7.0
27+
pysocks==1.7.0 # via tweepy
2428
python-dateutil==2.8.0 # via botocore
29+
python-dotenv==0.10.3
2530
python-lambda==3.2.6
2631
pyyaml==5.1 # via python-lambda
27-
requests-oauthlib==1.2.0
32+
requests-mock==1.6.0
33+
requests-oauthlib==1.2.0 # via tweepy
2834
requests==2.22.0
2935
s3transfer==0.2.1 # via boto3
30-
six==1.12.0
36+
six==1.12.0 # via astroid, python-dateutil, requests-mock, tweepy
3137
tweepy==3.7.0
3238
typed-ast==1.4.0 # via astroid
33-
urllib3==1.25.3
39+
unidecode==1.0.23 # via green
40+
urllib3==1.25.3 # via botocore, requests
3441
wrapt==1.11.1 # via astroid

test/__init__.py

Whitespace-only changes.

test/test_service.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)