|
| 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') |
0 commit comments