Skip to content

Commit 1c6db0d

Browse files
author
Luke Sneeringer
committed
Added system tests and Entity Response tests.
Still need explicit SyntaxResponse and SentimentResponse tests.
1 parent c0448da commit 1c6db0d

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
18+
class TestEntityResponse(unittest.TestCase):
19+
ENTITY_DICT = {
20+
'mentions': [{'text': {'content': 'Italian'}}],
21+
'metadata': {'wikipedia_url': 'http://en.wikipedia.org/wiki/Italy'},
22+
'name': 'Italian',
23+
'salience': 0.15,
24+
'type': 'LOCATION',
25+
}
26+
27+
def test_constructor(self):
28+
from google.cloud.language.api_responses import EntityResponse
29+
from google.cloud.language.entity import Entity
30+
31+
entity_response = EntityResponse(
32+
entities=[Entity.from_api_repr(self.ENTITY_DICT)],
33+
language='en',
34+
)
35+
self._verify_entity_response(entity_response)
36+
37+
def test_api_repr_factory(self):
38+
from google.cloud.language.api_responses import EntityResponse
39+
40+
entity_response = EntityResponse.from_api_repr({
41+
'entities': [self.ENTITY_DICT],
42+
'language': 'en',
43+
})
44+
self._verify_entity_response(entity_response)
45+
46+
def _verify_entity_response(self, entity_response):
47+
from google.cloud.language.entity import EntityType
48+
49+
self.assertEqual(len(entity_response.entities), 1)
50+
entity = entity_response.entities[0]
51+
self.assertEqual(entity.name, 'Italian')
52+
self.assertEqual(len(entity.mentions), 1)
53+
self.assertEqual(entity.mentions[0], 'Italian')
54+
self.assertTrue(entity.metadata['wikipedia_url'].endswith('Italy'))
55+
self.assertAlmostEqual(entity.salience, 0.15)
56+
self.assertEqual(entity.entity_type, EntityType.LOCATION)
57+
self.assertEqual(entity_response.language, 'en')

system_tests/language.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ def _check_analyze_entities_result(self, entities):
100100

101101
def test_analyze_entities(self):
102102
document = Config.CLIENT.document_from_text(self.TEXT_CONTENT)
103-
entities = document.analyze_entities().entities
104-
self._check_analyze_entities_result(entities)
103+
response = document.analyze_entities()
104+
self.assertEqual(response.language, 'en')
105+
self._check_analyze_entities_result(response.entities)
105106

106107
def test_analyze_entities_from_blob(self):
107108
# Upload the text to a blob.

0 commit comments

Comments
 (0)