Skip to content

Commit 2077548

Browse files
committed
Use mock library in natural language tests.
1 parent 039f1cc commit 2077548

File tree

3 files changed

+96
-62
lines changed

3 files changed

+96
-62
lines changed

language/tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ envlist =
66
localdeps =
77
pip install --upgrade {toxinidir}/../core
88
deps =
9+
mock
910
pytest
1011
covercmd =
1112
py.test --quiet \
@@ -28,6 +29,6 @@ commands =
2829
{[testing]localdeps}
2930
{[testing]covercmd}
3031
deps =
31-
{[testenv]deps}
32+
{[testing]deps}
3233
coverage
3334
pytest-cov

language/unit_tests/test_client.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,25 @@ def _make_one(self, *args, **kw):
2626
return self._get_target_class()(*args, **kw)
2727

2828
def test_ctor(self):
29+
import mock
30+
from oauth2client.client import GoogleCredentials
2931
from google.cloud.language.connection import Connection
3032

31-
creds = _Credentials()
33+
creds = mock.Mock(spec=GoogleCredentials)
34+
creds.create_scoped_required.return_value = False
3235
http = object()
3336
client = self._make_one(credentials=creds, http=http)
3437
self.assertIsInstance(client.connection, Connection)
3538
self.assertIs(client.connection.credentials, creds)
3639
self.assertIs(client.connection.http, http)
3740

3841
def test_document_from_text_factory(self):
42+
import mock
43+
from oauth2client.client import GoogleCredentials
3944
from google.cloud.language.document import Document
4045

41-
creds = _Credentials()
46+
creds = mock.Mock(spec=GoogleCredentials)
47+
creds.create_scoped_required.return_value = False
4248
client = self._make_one(credentials=creds, http=object())
4349

4450
content = 'abc'
@@ -53,16 +59,23 @@ def test_document_from_text_factory(self):
5359
self.assertEqual(document.language, language)
5460

5561
def test_document_from_text_factory_failure(self):
56-
creds = _Credentials()
62+
import mock
63+
from oauth2client.client import GoogleCredentials
64+
65+
creds = mock.Mock(spec=GoogleCredentials)
66+
creds.create_scoped_required.return_value = False
5767
client = self._make_one(credentials=creds, http=object())
5868

5969
with self.assertRaises(TypeError):
6070
client.document_from_text('abc', doc_type='foo')
6171

6272
def test_document_from_html_factory(self):
73+
import mock
74+
from oauth2client.client import GoogleCredentials
6375
from google.cloud.language.document import Document
6476

65-
creds = _Credentials()
77+
creds = mock.Mock(spec=GoogleCredentials)
78+
creds.create_scoped_required.return_value = False
6679
client = self._make_one(credentials=creds, http=object())
6780

6881
content = '<html>abc</html>'
@@ -77,16 +90,23 @@ def test_document_from_html_factory(self):
7790
self.assertEqual(document.language, language)
7891

7992
def test_document_from_html_factory_failure(self):
80-
creds = _Credentials()
93+
import mock
94+
from oauth2client.client import GoogleCredentials
95+
96+
creds = mock.Mock(spec=GoogleCredentials)
97+
creds.create_scoped_required.return_value = False
8198
client = self._make_one(credentials=creds, http=object())
8299

83100
with self.assertRaises(TypeError):
84101
client.document_from_html('abc', doc_type='foo')
85102

86103
def test_document_from_url_factory(self):
104+
import mock
105+
from oauth2client.client import GoogleCredentials
87106
from google.cloud.language.document import Document
88107

89-
creds = _Credentials()
108+
creds = mock.Mock(spec=GoogleCredentials)
109+
creds.create_scoped_required.return_value = False
90110
client = self._make_one(credentials=creds, http=object())
91111

92112
gcs_url = 'gs://my-text-bucket/sentiment-me.txt'
@@ -98,10 +118,13 @@ def test_document_from_url_factory(self):
98118
self.assertEqual(document.doc_type, Document.PLAIN_TEXT)
99119

100120
def test_document_from_url_factory_explicit(self):
121+
import mock
122+
from oauth2client.client import GoogleCredentials
101123
from google.cloud.language.document import Document
102124
from google.cloud.language.document import Encoding
103125

104-
creds = _Credentials()
126+
creds = mock.Mock(spec=GoogleCredentials)
127+
creds.create_scoped_required.return_value = False
105128
client = self._make_one(credentials=creds, http=object())
106129

107130
encoding = Encoding.UTF32
@@ -114,16 +137,3 @@ def test_document_from_url_factory_explicit(self):
114137
self.assertEqual(document.gcs_url, gcs_url)
115138
self.assertEqual(document.doc_type, Document.HTML)
116139
self.assertEqual(document.encoding, encoding)
117-
118-
119-
class _Credentials(object):
120-
121-
_scopes = None
122-
123-
@staticmethod
124-
def create_scoped_required():
125-
return True
126-
127-
def create_scoped(self, scope):
128-
self._scopes = scope
129-
return self

language/unit_tests/test_document.py

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,39 @@ def _verify_entity(self, entity, name, entity_type, wiki_url, salience):
187187
self.assertEqual(entity.salience, salience)
188188
self.assertEqual(entity.mentions, [name])
189189

190+
@staticmethod
191+
def _expected_data(content, encoding_type=None,
192+
extract_sentiment=False,
193+
extract_entities=False,
194+
extract_syntax=False):
195+
from google.cloud.language.document import DEFAULT_LANGUAGE
196+
from google.cloud.language.document import Document
197+
198+
expected = {
199+
'document': {
200+
'language': DEFAULT_LANGUAGE,
201+
'type': Document.PLAIN_TEXT,
202+
'content': content,
203+
},
204+
}
205+
if encoding_type is not None:
206+
expected['encodingType'] = encoding_type
207+
if extract_sentiment:
208+
features = expected.setdefault('features', {})
209+
features['extractDocumentSentiment'] = True
210+
if extract_entities:
211+
features = expected.setdefault('features', {})
212+
features['extractEntities'] = True
213+
if extract_syntax:
214+
features = expected.setdefault('features', {})
215+
features['extractSyntax'] = True
216+
return expected
217+
190218
def test_analyze_entities(self):
219+
import mock
220+
from google.cloud.language.connection import Connection
221+
from google.cloud.language.client import Client
222+
from google.cloud.language.document import Encoding
191223
from google.cloud.language.entity import EntityType
192224

193225
name1 = 'R-O-C-K'
@@ -229,8 +261,9 @@ def test_analyze_entities(self):
229261
],
230262
'language': 'en-US',
231263
}
232-
connection = _Connection(response)
233-
client = _Client(connection=connection)
264+
connection = mock.Mock(spec=Connection)
265+
connection.api_request.return_value = response
266+
client = mock.Mock(connection=connection, spec=Client)
234267
document = self._make_one(client, content)
235268

236269
entities = document.analyze_entities()
@@ -243,10 +276,10 @@ def test_analyze_entities(self):
243276
wiki2, salience2)
244277

245278
# Verify the request.
246-
self.assertEqual(len(connection._requested), 1)
247-
req = connection._requested[0]
248-
self.assertEqual(req['path'], 'analyzeEntities')
249-
self.assertEqual(req['method'], 'POST')
279+
expected = self._expected_data(
280+
content, encoding_type=Encoding.UTF8)
281+
connection.api_request.assert_called_once_with(
282+
path='analyzeEntities', method='POST', data=expected)
250283

251284
def _verify_sentiment(self, sentiment, polarity, magnitude):
252285
from google.cloud.language.sentiment import Sentiment
@@ -256,6 +289,10 @@ def _verify_sentiment(self, sentiment, polarity, magnitude):
256289
self.assertEqual(sentiment.magnitude, magnitude)
257290

258291
def test_analyze_sentiment(self):
292+
import mock
293+
from google.cloud.language.connection import Connection
294+
from google.cloud.language.client import Client
295+
259296
content = 'All the pretty horses.'
260297
polarity = 1
261298
magnitude = 0.6
@@ -266,18 +303,18 @@ def test_analyze_sentiment(self):
266303
},
267304
'language': 'en-US',
268305
}
269-
connection = _Connection(response)
270-
client = _Client(connection=connection)
306+
connection = mock.Mock(spec=Connection)
307+
connection.api_request.return_value = response
308+
client = mock.Mock(connection=connection, spec=Client)
271309
document = self._make_one(client, content)
272310

273311
sentiment = document.analyze_sentiment()
274312
self._verify_sentiment(sentiment, polarity, magnitude)
275313

276314
# Verify the request.
277-
self.assertEqual(len(connection._requested), 1)
278-
req = connection._requested[0]
279-
self.assertEqual(req['path'], 'analyzeSentiment')
280-
self.assertEqual(req['method'], 'POST')
315+
expected = self._expected_data(content)
316+
connection.api_request.assert_called_once_with(
317+
path='analyzeSentiment', method='POST', data=expected)
281318

282319
def _verify_sentences(self, include_syntax, annotations):
283320
from google.cloud.language.syntax import Sentence
@@ -306,7 +343,12 @@ def _verify_tokens(self, annotations, token_info):
306343

307344
def _annotate_text_helper(self, include_sentiment,
308345
include_entities, include_syntax):
346+
import mock
347+
348+
from google.cloud.language.connection import Connection
349+
from google.cloud.language.client import Client
309350
from google.cloud.language.document import Annotations
351+
from google.cloud.language.document import Encoding
310352
from google.cloud.language.entity import EntityType
311353

312354
token_info, sentences = _get_token_and_sentences(include_syntax)
@@ -324,8 +366,9 @@ def _annotate_text_helper(self, include_sentiment,
324366
'magnitude': ANNOTATE_MAGNITUDE,
325367
}
326368

327-
connection = _Connection(response)
328-
client = _Client(connection=connection)
369+
connection = mock.Mock(spec=Connection)
370+
connection.api_request.return_value = response
371+
client = mock.Mock(connection=connection, spec=Client)
329372
document = self._make_one(client, ANNOTATE_CONTENT)
330373

331374
annotations = document.annotate_text(
@@ -352,16 +395,13 @@ def _annotate_text_helper(self, include_sentiment,
352395
self.assertEqual(annotations.entities, [])
353396

354397
# Verify the request.
355-
self.assertEqual(len(connection._requested), 1)
356-
req = connection._requested[0]
357-
self.assertEqual(req['path'], 'annotateText')
358-
self.assertEqual(req['method'], 'POST')
359-
features = req['data']['features']
360-
self.assertEqual(features.get('extractDocumentSentiment', False),
361-
include_sentiment)
362-
self.assertEqual(features.get('extractEntities', False),
363-
include_entities)
364-
self.assertEqual(features.get('extractSyntax', False), include_syntax)
398+
expected = self._expected_data(
399+
ANNOTATE_CONTENT, encoding_type=Encoding.UTF8,
400+
extract_sentiment=include_sentiment,
401+
extract_entities=include_entities,
402+
extract_syntax=include_syntax)
403+
connection.api_request.assert_called_once_with(
404+
path='annotateText', method='POST', data=expected)
365405

366406
def test_annotate_text(self):
367407
self._annotate_text_helper(True, True, True)
@@ -374,20 +414,3 @@ def test_annotate_text_entities_only(self):
374414

375415
def test_annotate_text_syntax_only(self):
376416
self._annotate_text_helper(False, False, True)
377-
378-
379-
class _Connection(object):
380-
381-
def __init__(self, response):
382-
self._response = response
383-
self._requested = []
384-
385-
def api_request(self, **kwargs):
386-
self._requested.append(kwargs)
387-
return self._response
388-
389-
390-
class _Client(object):
391-
392-
def __init__(self, connection=None):
393-
self.connection = connection

0 commit comments

Comments
 (0)