@@ -27,6 +27,10 @@ def _make_one(self, *args, **kw):
2727 return self ._get_target_class ()(* args , ** kw )
2828
2929 def test_constructor_defaults (self ):
30+ from google .cloud .language .entity import Mention
31+ from google .cloud .language .entity import MentionType
32+ from google .cloud .language .entity import TextSpan
33+
3034 name = 'Italian'
3135 entity_type = 'LOCATION'
3236 wiki_url = 'http://en.wikipedia.org/wiki/Italy'
@@ -35,7 +39,10 @@ def test_constructor_defaults(self):
3539 'wikipedia_url' : wiki_url ,
3640 }
3741 salience = 0.19960518
38- mentions = ['Italian' ]
42+ mentions = [Mention (
43+ mention_type = MentionType .PROPER ,
44+ text = TextSpan (content = 'Italian' , begin_offset = 0 ),
45+ )]
3946 entity = self ._make_one (name , entity_type , metadata ,
4047 salience , mentions )
4148 self .assertEqual (entity .name , name )
@@ -45,9 +52,13 @@ def test_constructor_defaults(self):
4552 self .assertEqual (entity .mentions , mentions )
4653
4754 def test_from_api_repr (self ):
55+ from google .cloud .language .entity import EntityType
56+ from google .cloud .language .entity import Mention
57+ from google .cloud .language .entity import MentionType
58+
4859 klass = self ._get_target_class ()
4960 name = 'Italy'
50- entity_type = ' LOCATION'
61+ entity_type = EntityType . LOCATION
5162 salience = 0.223
5263 wiki_url = 'http://en.wikipedia.org/wiki/Italy'
5364 mention1 = 'Italy'
@@ -59,14 +70,95 @@ def test_from_api_repr(self):
5970 'salience' : salience ,
6071 'metadata' : {'wikipedia_url' : wiki_url },
6172 'mentions' : [
62- {'text' : {'content' : mention1 }},
63- {'text' : {'content' : mention2 }},
64- {'text' : {'content' : mention3 }},
73+ {'text' : {'content' : mention1 , 'beginOffset' : 3 },
74+ 'type' : 'PROPER' },
75+ {'text' : {'content' : mention2 , 'beginOffset' : 5 },
76+ 'type' : 'PROPER' },
77+ {'text' : {'content' : mention3 , 'beginOffset' : 8 },
78+ 'type' : 'PROPER' },
6579 ],
6680 }
6781 entity = klass .from_api_repr (payload )
6882 self .assertEqual (entity .name , name )
6983 self .assertEqual (entity .entity_type , entity_type )
7084 self .assertEqual (entity .salience , salience )
7185 self .assertEqual (entity .metadata , {'wikipedia_url' : wiki_url })
72- self .assertEqual (entity .mentions , [mention1 , mention2 , mention3 ])
86+
87+ # Assert that we got back Mention objects for each mention.
88+ self .assertIsInstance (entity .mentions [0 ], Mention )
89+ self .assertIsInstance (entity .mentions [1 ], Mention )
90+ self .assertIsInstance (entity .mentions [2 ], Mention )
91+
92+ # Assert that the text (and string coercison) are correct.
93+ self .assertEqual ([str (i ) for i in entity .mentions ],
94+ [mention1 , mention2 , mention3 ])
95+
96+ # Assert that the begin offsets are preserved.
97+ self .assertEqual ([i .text .begin_offset for i in entity .mentions ],
98+ [3 , 5 , 8 ])
99+
100+ # Assert that the mention types are preserved.
101+ for mention in entity .mentions :
102+ self .assertEqual (mention .mention_type , MentionType .PROPER )
103+
104+
105+ class TestMention (unittest .TestCase ):
106+ PAYLOAD = {
107+ 'text' : {'content' : 'Greece' , 'beginOffset' : 42 },
108+ 'type' : 'PROPER' ,
109+ }
110+
111+ def test_constructor (self ):
112+ from google .cloud .language .entity import Mention
113+ from google .cloud .language .entity import MentionType
114+ from google .cloud .language .entity import TextSpan
115+
116+ mention = Mention (
117+ text = TextSpan (content = 'snails' , begin_offset = 90 ),
118+ mention_type = MentionType .COMMON ,
119+ )
120+
121+ self .assertIsInstance (mention .text , TextSpan )
122+ self .assertEqual (mention .text .content , 'snails' )
123+ self .assertEqual (mention .text .begin_offset , 90 )
124+ self .assertEqual (mention .mention_type , MentionType .COMMON )
125+
126+ def test_from_api_repr (self ):
127+ from google .cloud .language .entity import Mention
128+ from google .cloud .language .entity import MentionType
129+ from google .cloud .language .entity import TextSpan
130+
131+ mention = Mention .from_api_repr (self .PAYLOAD )
132+
133+ self .assertIsInstance (mention , Mention )
134+ self .assertIsInstance (mention .text , TextSpan )
135+ self .assertEqual (mention .text .content , 'Greece' )
136+ self .assertEqual (mention .text .begin_offset , 42 )
137+ self .assertEqual (mention .mention_type , MentionType .PROPER )
138+
139+ def test_dunder_str (self ):
140+ from google .cloud .language .entity import Mention
141+
142+ mention = Mention .from_api_repr (self .PAYLOAD )
143+ self .assertEqual (str (mention ), 'Greece' )
144+
145+
146+ class TestTextSpan (unittest .TestCase ):
147+ def test_constructor (self ):
148+ from google .cloud .language .entity import TextSpan
149+
150+ text = TextSpan (content = 'Winston Churchill' , begin_offset = 1945 )
151+ self .assertIsInstance (text , TextSpan )
152+ self .assertEqual (text .content , str (text ), 'Winston Churchill' )
153+ self .assertEqual (text .begin_offset , 1945 )
154+
155+ def test_from_api_repr (self ):
156+ from google .cloud .language .entity import TextSpan
157+
158+ text = TextSpan .from_api_repr ({
159+ 'beginOffset' : 1953 ,
160+ 'content' : 'Queen Elizabeth' ,
161+ })
162+ self .assertIsInstance (text , TextSpan )
163+ self .assertEqual (text .content , str (text ), 'Queen Elizabeth' )
164+ self .assertEqual (text .begin_offset , 1953 )
0 commit comments