|
| 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 TestSafeSearchAnnotation(unittest.TestCase): |
| 19 | + @staticmethod |
| 20 | + def _get_target_class(): |
| 21 | + from google.cloud.vision.safe_search import SafeSearchAnnotation |
| 22 | + return SafeSearchAnnotation |
| 23 | + |
| 24 | + def test_safe_search_annotation(self): |
| 25 | + from google.cloud.vision.likelihood import Likelihood |
| 26 | + from unit_tests._fixtures import SAFE_SEARCH_DETECTION_RESPONSE |
| 27 | + |
| 28 | + response = SAFE_SEARCH_DETECTION_RESPONSE['responses'][0] |
| 29 | + safe_search_response = response['safeSearchAnnotation'] |
| 30 | + |
| 31 | + safe_search = self._get_target_class().from_api_repr( |
| 32 | + safe_search_response) |
| 33 | + |
| 34 | + self.assertIs(safe_search.adult, Likelihood.VERY_UNLIKELY) |
| 35 | + self.assertIs(safe_search.spoof, Likelihood.UNLIKELY) |
| 36 | + self.assertIs(safe_search.medical, Likelihood.POSSIBLE) |
| 37 | + self.assertIs(safe_search.violence, Likelihood.VERY_UNLIKELY) |
| 38 | + |
| 39 | + def test_pb_safe_search_annotation(self): |
| 40 | + from google.cloud.vision.likelihood import Likelihood |
| 41 | + from google.cloud.grpc.vision.v1.image_annotator_pb2 import ( |
| 42 | + Likelihood as LikelihoodPB) |
| 43 | + from google.cloud.grpc.vision.v1 import image_annotator_pb2 |
| 44 | + |
| 45 | + possible = LikelihoodPB.Value('POSSIBLE') |
| 46 | + possible_name = Likelihood.POSSIBLE |
| 47 | + safe_search_annotation = image_annotator_pb2.SafeSearchAnnotation( |
| 48 | + adult=possible, spoof=possible, medical=possible, violence=possible |
| 49 | + ) |
| 50 | + |
| 51 | + safe_search = self._get_target_class().from_pb(safe_search_annotation) |
| 52 | + |
| 53 | + self.assertIs(safe_search.adult, possible_name) |
| 54 | + self.assertIs(safe_search.spoof, possible_name) |
| 55 | + self.assertIs(safe_search.medical, possible_name) |
| 56 | + self.assertIs(safe_search.violence, possible_name) |
| 57 | + |
| 58 | + def test_empty_pb_safe_search_annotation(self): |
| 59 | + from google.cloud.vision.likelihood import Likelihood |
| 60 | + from google.cloud.grpc.vision.v1 import image_annotator_pb2 |
| 61 | + |
| 62 | + unknown = Likelihood.UNKNOWN |
| 63 | + safe_search_annotation = image_annotator_pb2.SafeSearchAnnotation() |
| 64 | + |
| 65 | + safe_search = self._get_target_class().from_pb(safe_search_annotation) |
| 66 | + |
| 67 | + self.assertIs(safe_search.adult, unknown) |
| 68 | + self.assertIs(safe_search.spoof, unknown) |
| 69 | + self.assertIs(safe_search.medical, unknown) |
| 70 | + self.assertIs(safe_search.violence, unknown) |
0 commit comments