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 )
0 commit comments