Skip to content

Commit

Permalink
Who is in space with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Shirtliffe committed Aug 5, 2019
1 parent ae434b8 commit c0bca14
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
37 changes: 37 additions & 0 deletions whos_in_space/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import requests


def print_header():
""" Print header
"""
header_text = ''
TEXT = f' Who\'s in space? \n'
line = '-' * len(TEXT)
line += '\n'

header_text += line
header_text += TEXT
header_text += line
print(header_text)


def get_who_is_in_space():
response = requests.get('http://api.open-notify.org/astros.json')
# response.raise_for_status
if response.ok:
return response

def print_people_in_space(json):
people = json['people']
print(f'The are {len(people)} people in space right now:\n')
print('Name | Craft')
for value in people:
name = value['name']
craft = value['craft']
print(f'{name} | {craft}')

if __name__ == "__main__":
print_header()
response = get_who_is_in_space()
json = response.json()
print_people_in_space(json)
Empty file added whos_in_space/tests/__init__.py
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions whos_in_space/tests/system/app_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from unittest import TestCase
from unittest.mock import patch, call
import app

class AppTest(TestCase):

def test_print_header(self):

expected = '------------------\n Who\'s in space? \n------------------\n'
with patch('builtins.print') as mocked_print:
app.print_header()
mocked_print.assert_called_with(expected)


def test_get_who_is_in_space(self):

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}
with patch('requests.get') as mocked_request_get:
mocked_request_get.return_value.status_code = 200
mocked_request_get.return_value.json.return_value = response_json
response = app.get_who_is_in_space()
self.assertEqual(response.status_code, 200)
mocked_request_get.assert_called_with('http://api.open-notify.org/astros.json')
self.assertEqual(response.json(), response_json)


def test_print_people_in_space(self):

expected = [
call('The are 6 people in space right now:\n'),
call('Name | Craft'),
call('Alexey Ovchinin | ISS'),
call('Nick Hague | ISS'),
call('Christina Koch | ISS'),
call('Alexander Skvortsov | ISS'),
call('Luca Parmitano | ISS'),
call('Andrew Morgan | ISS')
]
with patch('builtins.print') as mocked_print:
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}
app.print_people_in_space(response_json)
mocked_print.assert_has_calls(expected)
Empty file.
5 changes: 5 additions & 0 deletions whos_in_space/tests/unit/unit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from unittest import TestCase
import app

class FilteringRecordsTest(TestCase):
pass
2 changes: 2 additions & 0 deletions whos_in_space/words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
badger badger badger badger mushroom mushroom
snake badger badger badger

0 comments on commit c0bca14

Please sign in to comment.