@@ -35,36 +35,53 @@ def child_service_url(self, child_service):
3535 return child_service ._BaseService__service_url ()
3636
3737 def test__read_api_key_from_env (self , child_service , mocker ):
38+ # Given
3839 env_key = "ABSTRACTAPI_{service_name}_API_KEY" .format (
3940 service_name = child_service ._service_name_env_var
4041 )
4142 value = "some-api-key"
4243 mocker .patch .dict (os .environ , {env_key : value })
43- assert child_service ._read_api_key_from_env () == value
44+
45+ # When
46+ api_key = child_service ._read_api_key_from_env ()
47+
48+ # Then
49+ assert api_key == value
4450
4551 def test_init_without_api_key (self , mocker ):
52+ # Given
4653 mocked_read = mocker .patch .object (
4754 _ChildService ,
4855 "_read_api_key_from_env" ,
4956 wraps = _ChildService ._read_api_key_from_env
5057 )
5158
59+ # Then
5260 with pytest .raises (ValueError ):
53- _ChildService ()
61+ _ChildService () # When
5462 mocked_read .assert_called_once ()
5563
5664 def test___service_url (self , base_url , mocker ):
65+ # Given
5766 service = "test"
5867 action = "testing"
5968 mocker .patch .object (BaseService , "_subdomain" , service , create = True )
60- assert BaseService ._BaseService__service_url (self = BaseService ) == base_url .format (subdomain = service )
61- assert BaseService ._BaseService__service_url (self = BaseService , action = action ) == base_url .format (subdomain = service ) + action
69+
70+ # When
71+ service_url = BaseService ._BaseService__service_url (self = BaseService )
72+ service_url_with_action = BaseService ._BaseService__service_url (self = BaseService , action = action )
73+
74+ # Then
75+ assert service_url == base_url .format (subdomain = service )
76+ assert service_url_with_action == base_url .format (subdomain = service ) + action
6277
6378 @pytest .mark .parametrize ("method" , ["PUT" , "PATCH" , "DELETE" ])
6479 def test__service_request_with_invalid_method (
6580 self , method , child_service ,
6681 ):
82+ # Then
6783 with pytest .raises (ClientRequestError ):
84+ # When
6885 child_service ._service_request (
6986 _response_class = _ChildServiceResponse ,
7087 _method = method
@@ -77,16 +94,19 @@ def test__service_request_get(
7794 requests_mock ,
7895 mocker
7996 ):
97+ # Given
8098 service_params = {"param1" : "value" , "param2" : None }
8199 requests_mock .get (child_service_url , status_code = requests .codes .OK )
82100 mocked_request = mocker .patch .object (
83101 requests , "request" , wraps = requests .request
84102 )
85103
104+ # When
86105 child_service ._service_request (
87106 _ChildServiceResponse , ** service_params
88107 )
89108
109+ # Then
90110 mocked_request .assert_called_once_with (
91111 method = "GET" ,
92112 url = child_service_url ,
@@ -113,6 +133,7 @@ def test__service_request_post(
113133 requests_mock ,
114134 mocker
115135 ):
136+ # Given
116137 requests_mock .post (child_service_url , content = b"" )
117138 mocked_request = mocker .patch .object (
118139 requests , "request" , wraps = requests .request
@@ -126,13 +147,17 @@ def test__service_request_post(
126147 request_kwargs ["_files" ] = files
127148 if body is not None :
128149 request_kwargs ["_body" ] = body
129- child_service ._service_request (** request_kwargs )
130150
131151 call_kwargs = {"method" : "POST" , "url" : child_service_url }
132152 if files is not None :
133153 call_kwargs ["files" ] = files
134154 if body is not None :
135155 call_kwargs ["json" ] = body
156+
157+ # When
158+ child_service ._service_request (** request_kwargs )
159+
160+ # Then
136161 mocked_request .assert_called_once_with (** call_kwargs )
137162
138163 @pytest .mark .parametrize (
@@ -145,10 +170,14 @@ def test__service_request_unaccepted_status_code(
145170 child_service_url ,
146171 requests_mock
147172 ):
173+ # Given
148174 requests_mock .get (
149175 child_service_url , status_code = status_code
150176 )
177+
178+ # Then
151179 with pytest .raises (APIRequestError ):
180+ # When
152181 child_service ._service_request (_ChildServiceResponse )
153182
154183 def test__service_request_parsing_error (
@@ -157,10 +186,13 @@ def test__service_request_parsing_error(
157186 blank_response ,
158187 mocker
159188 ):
189+ # Given
160190 mocked_request = mocker .patch .object (requests , "request" )
161191 mocked_request .return_value = blank_response
162192
193+ # Then
163194 with pytest .raises (ResponseParseError ):
195+ # When
164196 child_service ._service_request (
165197 _ChildServiceResponse ,
166198 _response_class_kwargs = {"key" : "unexpected" }
0 commit comments