Skip to content

Commit 9f7e1ac

Browse files
committed
Add test file for Result class.
1 parent aadb6a2 commit 9f7e1ac

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

speech/unit_tests/test_result.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2017 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 TestResult(unittest.TestCase):
19+
@staticmethod
20+
def _get_target_class():
21+
from google.cloud.speech.result import Result
22+
23+
return Result
24+
25+
def _make_one(self, *args, **kw):
26+
return self._get_target_class()(*args, **kw)
27+
28+
def test_ctor(self):
29+
result = self._make_one([])
30+
self.assertIsInstance(result, self._get_target_class())
31+
32+
def test_from_pb(self):
33+
from google.cloud.proto.speech.v1beta1 import cloud_speech_pb2
34+
35+
confidence = 0.625
36+
transcript = 'this is a test transcript'
37+
alternative = cloud_speech_pb2.SpeechRecognitionAlternative(
38+
transcript=transcript, confidence=confidence)
39+
result_pb = cloud_speech_pb2.SpeechRecognitionResult(
40+
alternatives=[alternative])
41+
42+
result = self._get_target_class().from_pb(result_pb)
43+
self.assertEqual(result.confidence, confidence)
44+
self.assertEqual(result.transcript, transcript)
45+
46+
def test_from_api_repr(self):
47+
confidence = 0.625
48+
transcript = 'this is a test'
49+
response = {
50+
'alternatives': [
51+
{
52+
'confidence': confidence,
53+
'transcript': transcript,
54+
},
55+
],
56+
}
57+
58+
result = self._get_target_class().from_api_repr(response)
59+
self.assertEqual(result.confidence, confidence)
60+
self.assertEqual(result.transcript, transcript)

0 commit comments

Comments
 (0)