@@ -95,6 +95,16 @@ def _get_entities(include_entities):
9595 return entities
9696
9797
98+ def make_mock_client (response ):
99+ import mock
100+ from google .cloud .language .connection import Connection
101+ from google .cloud .language .client import Client
102+
103+ connection = mock .Mock (spec = Connection )
104+ connection .api_request .return_value = response
105+ return mock .Mock (_connection = connection , spec = Client )
106+
107+
98108class TestDocument (unittest .TestCase ):
99109
100110 @staticmethod
@@ -216,9 +226,6 @@ def _expected_data(content, encoding_type=None,
216226 return expected
217227
218228 def test_analyze_entities (self ):
219- import mock
220- from google .cloud .language .connection import Connection
221- from google .cloud .language .client import Client
222229 from google .cloud .language .document import Encoding
223230 from google .cloud .language .entity import EntityType
224231
@@ -261,9 +268,7 @@ def test_analyze_entities(self):
261268 ],
262269 'language' : 'en-US' ,
263270 }
264- connection = mock .Mock (spec = Connection )
265- connection .api_request .return_value = response
266- client = mock .Mock (_connection = connection , spec = Client )
271+ client = make_mock_client (response )
267272 document = self ._make_one (client , content )
268273
269274 entities = document .analyze_entities ()
@@ -278,7 +283,7 @@ def test_analyze_entities(self):
278283 # Verify the request.
279284 expected = self ._expected_data (
280285 content , encoding_type = Encoding .UTF8 )
281- connection .api_request .assert_called_once_with (
286+ client . _connection .api_request .assert_called_once_with (
282287 path = 'analyzeEntities' , method = 'POST' , data = expected )
283288
284289 def _verify_sentiment (self , sentiment , polarity , magnitude ):
@@ -289,10 +294,6 @@ def _verify_sentiment(self, sentiment, polarity, magnitude):
289294 self .assertEqual (sentiment .magnitude , magnitude )
290295
291296 def test_analyze_sentiment (self ):
292- import mock
293- from google .cloud .language .connection import Connection
294- from google .cloud .language .client import Client
295-
296297 content = 'All the pretty horses.'
297298 polarity = 1
298299 magnitude = 0.6
@@ -303,17 +304,15 @@ def test_analyze_sentiment(self):
303304 },
304305 'language' : 'en-US' ,
305306 }
306- connection = mock .Mock (spec = Connection )
307- connection .api_request .return_value = response
308- client = mock .Mock (_connection = connection , spec = Client )
307+ client = make_mock_client (response )
309308 document = self ._make_one (client , content )
310309
311310 sentiment = document .analyze_sentiment ()
312311 self ._verify_sentiment (sentiment , polarity , magnitude )
313312
314313 # Verify the request.
315314 expected = self ._expected_data (content )
316- connection .api_request .assert_called_once_with (
315+ client . _connection .api_request .assert_called_once_with (
317316 path = 'analyzeSentiment' , method = 'POST' , data = expected )
318317
319318 def _verify_sentences (self , include_syntax , annotations ):
@@ -343,10 +342,6 @@ def _verify_tokens(self, annotations, token_info):
343342
344343 def _annotate_text_helper (self , include_sentiment ,
345344 include_entities , include_syntax ):
346- import mock
347-
348- from google .cloud .language .connection import Connection
349- from google .cloud .language .client import Client
350345 from google .cloud .language .document import Annotations
351346 from google .cloud .language .document import Encoding
352347 from google .cloud .language .entity import EntityType
@@ -366,9 +361,7 @@ def _annotate_text_helper(self, include_sentiment,
366361 'magnitude' : ANNOTATE_MAGNITUDE ,
367362 }
368363
369- connection = mock .Mock (spec = Connection )
370- connection .api_request .return_value = response
371- client = mock .Mock (_connection = connection , spec = Client )
364+ client = make_mock_client (response )
372365 document = self ._make_one (client , ANNOTATE_CONTENT )
373366
374367 annotations = document .annotate_text (
@@ -400,7 +393,7 @@ def _annotate_text_helper(self, include_sentiment,
400393 extract_sentiment = include_sentiment ,
401394 extract_entities = include_entities ,
402395 extract_syntax = include_syntax )
403- connection .api_request .assert_called_once_with (
396+ client . _connection .api_request .assert_called_once_with (
404397 path = 'annotateText' , method = 'POST' , data = expected )
405398
406399 def test_annotate_text (self ):
0 commit comments