Skip to content

Commit c0bca14

Browse files
author
Matthew Shirtliffe
committed
Who is in space with tests
1 parent ae434b8 commit c0bca14

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed

whos_in_space/app.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import requests
2+
3+
4+
def print_header():
5+
""" Print header
6+
"""
7+
header_text = ''
8+
TEXT = f' Who\'s in space? \n'
9+
line = '-' * len(TEXT)
10+
line += '\n'
11+
12+
header_text += line
13+
header_text += TEXT
14+
header_text += line
15+
print(header_text)
16+
17+
18+
def get_who_is_in_space():
19+
response = requests.get('http://api.open-notify.org/astros.json')
20+
# response.raise_for_status
21+
if response.ok:
22+
return response
23+
24+
def print_people_in_space(json):
25+
people = json['people']
26+
print(f'The are {len(people)} people in space right now:\n')
27+
print('Name | Craft')
28+
for value in people:
29+
name = value['name']
30+
craft = value['craft']
31+
print(f'{name} | {craft}')
32+
33+
if __name__ == "__main__":
34+
print_header()
35+
response = get_who_is_in_space()
36+
json = response.json()
37+
print_people_in_space(json)

whos_in_space/tests/__init__.py

Whitespace-only changes.

whos_in_space/tests/system/__init__.py

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch, call
3+
import app
4+
5+
class AppTest(TestCase):
6+
7+
def test_print_header(self):
8+
9+
expected = '------------------\n Who\'s in space? \n------------------\n'
10+
with patch('builtins.print') as mocked_print:
11+
app.print_header()
12+
mocked_print.assert_called_with(expected)
13+
14+
15+
def test_get_who_is_in_space(self):
16+
17+
response_json = {'message': 'success', 'people': [{'name': 'Alexey Ovchinin', 'craft': 'ISS'}, {'name': 'Nick Hague', 'craft': 'ISS'}, {'name': 'Christina Koch', 'craft': 'ISS'}, {'name': 'Alexander Skvortsov', 'craft': 'ISS'}, {'name': 'Luca Parmitano', 'craft': 'ISS'}, {'name': 'Andrew Morgan', 'craft': 'ISS'}], 'number': 6}
18+
with patch('requests.get') as mocked_request_get:
19+
mocked_request_get.return_value.status_code = 200
20+
mocked_request_get.return_value.json.return_value = response_json
21+
response = app.get_who_is_in_space()
22+
self.assertEqual(response.status_code, 200)
23+
mocked_request_get.assert_called_with('http://api.open-notify.org/astros.json')
24+
self.assertEqual(response.json(), response_json)
25+
26+
27+
def test_print_people_in_space(self):
28+
29+
expected = [
30+
call('The are 6 people in space right now:\n'),
31+
call('Name | Craft'),
32+
call('Alexey Ovchinin | ISS'),
33+
call('Nick Hague | ISS'),
34+
call('Christina Koch | ISS'),
35+
call('Alexander Skvortsov | ISS'),
36+
call('Luca Parmitano | ISS'),
37+
call('Andrew Morgan | ISS')
38+
]
39+
with patch('builtins.print') as mocked_print:
40+
response_json = {'message': 'success', 'people': [{'name': 'Alexey Ovchinin', 'craft': 'ISS'}, {'name': 'Nick Hague', 'craft': 'ISS'}, {'name': 'Christina Koch', 'craft': 'ISS'}, {'name': 'Alexander Skvortsov', 'craft': 'ISS'}, {'name': 'Luca Parmitano', 'craft': 'ISS'}, {'name': 'Andrew Morgan', 'craft': 'ISS'}], 'number': 6}
41+
app.print_people_in_space(response_json)
42+
mocked_print.assert_has_calls(expected)

whos_in_space/tests/unit/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from unittest import TestCase
2+
import app
3+
4+
class FilteringRecordsTest(TestCase):
5+
pass

whos_in_space/words.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
badger badger badger badger mushroom mushroom
2+
snake badger badger badger

0 commit comments

Comments
 (0)