@@ -96,12 +96,12 @@ def _get_entities(include_entities):
9696 return entities
9797
9898
99- def make_mock_client (response ):
99+ def make_mock_client (response , api_version = 'v1' ):
100100 import mock
101- from google .cloud .language ._http import Connection
102101 from google .cloud .language .client import Client
103102
104- connection = mock .Mock (spec = Connection )
103+ connection = mock .Mock (spec = Client ._CONNECTION_CLASSES [api_version ])
104+ connection .API_VERSION = api_version
105105 connection .api_request .return_value = response
106106 return mock .Mock (_connection = connection , spec = Client )
107107
@@ -205,7 +205,8 @@ def test__to_dict_with_no_content(self):
205205 'type' : klass .PLAIN_TEXT ,
206206 })
207207
208- def _verify_entity (self , entity , name , entity_type , wiki_url , salience ):
208+ def _verify_entity (self , entity , name , entity_type , wiki_url , salience ,
209+ sentiment = None ):
209210 from google .cloud .language .entity import Entity
210211
211212 self .assertIsInstance (entity , Entity )
@@ -218,6 +219,10 @@ def _verify_entity(self, entity, name, entity_type, wiki_url, salience):
218219 self .assertEqual (entity .salience , salience )
219220 self .assertEqual (len (entity .mentions ), 1 )
220221 self .assertEqual (entity .mentions [0 ].text .content , name )
222+ if sentiment :
223+ self .assertEqual (entity .sentiment .score , sentiment .score )
224+ self .assertAlmostEqual (entity .sentiment .magnitude ,
225+ sentiment .magnitude )
221226
222227 @staticmethod
223228 def _expected_data (content , encoding_type = None ,
@@ -308,6 +313,85 @@ def test_analyze_entities(self):
308313 client ._connection .api_request .assert_called_once_with (
309314 path = 'analyzeEntities' , method = 'POST' , data = expected )
310315
316+ def test_analyze_entity_sentiment_v1_error (self ):
317+ client = make_mock_client ({})
318+ document = self ._make_one (client , 'foo bar baz' )
319+ with self .assertRaises (NotImplementedError ):
320+ entity_response = document .analyze_entity_sentiment ()
321+
322+ def test_analyze_entity_sentiment (self ):
323+ from google .cloud .language .document import Encoding
324+ from google .cloud .language .entity import EntityType
325+ from google .cloud .language .sentiment import Sentiment
326+
327+ name1 = 'R-O-C-K'
328+ name2 = 'USA'
329+ content = name1 + ' in the ' + name2
330+ wiki2 = 'http://en.wikipedia.org/wiki/United_States'
331+ salience1 = 0.91391456
332+ salience2 = 0.086085409
333+ sentiment = Sentiment (score = 0.15 , magnitude = 42 )
334+ response = {
335+ 'entities' : [
336+ {
337+ 'name' : name1 ,
338+ 'type' : EntityType .OTHER ,
339+ 'metadata' : {},
340+ 'salience' : salience1 ,
341+ 'mentions' : [
342+ {
343+ 'text' : {
344+ 'content' : name1 ,
345+ 'beginOffset' : - 1
346+ },
347+ 'type' : 'TYPE_UNKNOWN' ,
348+ }
349+ ],
350+ 'sentiment' : {
351+ 'score' : 0.15 ,
352+ 'magnitude' : 42 ,
353+ }
354+ },
355+ {
356+ 'name' : name2 ,
357+ 'type' : EntityType .LOCATION ,
358+ 'metadata' : {'wikipedia_url' : wiki2 },
359+ 'salience' : salience2 ,
360+ 'mentions' : [
361+ {
362+ 'text' : {
363+ 'content' : name2 ,
364+ 'beginOffset' : - 1 ,
365+ },
366+ 'type' : 'PROPER' ,
367+ },
368+ ],
369+ 'sentiment' : {
370+ 'score' : 0.15 ,
371+ 'magnitude' : 42 ,
372+ }
373+ },
374+ ],
375+ 'language' : 'en-US' ,
376+ }
377+ client = make_mock_client (response , api_version = 'v1beta2' )
378+ document = self ._make_one (client , content )
379+
380+ entity_response = document .analyze_entity_sentiment ()
381+ self .assertEqual (len (entity_response .entities ), 2 )
382+ entity1 = entity_response .entities [0 ]
383+ self ._verify_entity (entity1 , name1 , EntityType .OTHER ,
384+ None , salience1 , sentiment )
385+ entity2 = entity_response .entities [1 ]
386+ self ._verify_entity (entity2 , name2 , EntityType .LOCATION ,
387+ wiki2 , salience2 , sentiment )
388+
389+ # Verify the request.
390+ expected = self ._expected_data (
391+ content , encoding_type = Encoding .get_default ())
392+ client ._connection .api_request .assert_called_once_with (
393+ path = 'analyzeEntitySentiment' , method = 'POST' , data = expected )
394+
311395 def _verify_sentiment (self , sentiment , score , magnitude ):
312396 from google .cloud .language .sentiment import Sentiment
313397
0 commit comments