diff --git a/whos_in_space/app.py b/whos_in_space/app.py new file mode 100644 index 0000000..27930fb --- /dev/null +++ b/whos_in_space/app.py @@ -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) diff --git a/whos_in_space/tests/__init__.py b/whos_in_space/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/whos_in_space/tests/system/__init__.py b/whos_in_space/tests/system/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/whos_in_space/tests/system/app_test.py b/whos_in_space/tests/system/app_test.py new file mode 100644 index 0000000..042a917 --- /dev/null +++ b/whos_in_space/tests/system/app_test.py @@ -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) \ No newline at end of file diff --git a/whos_in_space/tests/unit/__init__.py b/whos_in_space/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/whos_in_space/tests/unit/unit_test.py b/whos_in_space/tests/unit/unit_test.py new file mode 100644 index 0000000..4918ff7 --- /dev/null +++ b/whos_in_space/tests/unit/unit_test.py @@ -0,0 +1,5 @@ +from unittest import TestCase +import app + +class FilteringRecordsTest(TestCase): + pass \ No newline at end of file diff --git a/whos_in_space/words.txt b/whos_in_space/words.txt new file mode 100644 index 0000000..350cca9 --- /dev/null +++ b/whos_in_space/words.txt @@ -0,0 +1,2 @@ +badger badger badger badger mushroom mushroom +snake badger badger badger \ No newline at end of file