Skip to content

Commit c75e05c

Browse files
committed
Re-factoring usage of mocks in language.
This way repeated code is reduced to a single call site.
1 parent 8d45b39 commit c75e05c

2 files changed

Lines changed: 32 additions & 53 deletions

File tree

language/unit_tests/test_client.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
import unittest
1616

1717

18+
def make_mock_credentials():
19+
import mock
20+
from oauth2client.client import GoogleCredentials
21+
22+
credentials = mock.Mock(spec=GoogleCredentials)
23+
credentials.create_scoped_required.return_value = False
24+
return credentials
25+
26+
1827
class TestClient(unittest.TestCase):
1928

2029
@staticmethod
@@ -26,25 +35,19 @@ def _make_one(self, *args, **kw):
2635
return self._get_target_class()(*args, **kw)
2736

2837
def test_ctor(self):
29-
import mock
30-
from oauth2client.client import GoogleCredentials
3138
from google.cloud.language.connection import Connection
3239

33-
creds = mock.Mock(spec=GoogleCredentials)
34-
creds.create_scoped_required.return_value = False
40+
creds = make_mock_credentials()
3541
http = object()
3642
client = self._make_one(credentials=creds, http=http)
3743
self.assertIsInstance(client._connection, Connection)
3844
self.assertIs(client._connection.credentials, creds)
3945
self.assertIs(client._connection.http, http)
4046

4147
def test_document_from_text_factory(self):
42-
import mock
43-
from oauth2client.client import GoogleCredentials
4448
from google.cloud.language.document import Document
4549

46-
creds = mock.Mock(spec=GoogleCredentials)
47-
creds.create_scoped_required.return_value = False
50+
creds = make_mock_credentials()
4851
client = self._make_one(credentials=creds, http=object())
4952

5053
content = 'abc'
@@ -59,23 +62,16 @@ def test_document_from_text_factory(self):
5962
self.assertEqual(document.language, language)
6063

6164
def test_document_from_text_factory_failure(self):
62-
import mock
63-
from oauth2client.client import GoogleCredentials
64-
65-
creds = mock.Mock(spec=GoogleCredentials)
66-
creds.create_scoped_required.return_value = False
65+
creds = make_mock_credentials()
6766
client = self._make_one(credentials=creds, http=object())
6867

6968
with self.assertRaises(TypeError):
7069
client.document_from_text('abc', doc_type='foo')
7170

7271
def test_document_from_html_factory(self):
73-
import mock
74-
from oauth2client.client import GoogleCredentials
7572
from google.cloud.language.document import Document
7673

77-
creds = mock.Mock(spec=GoogleCredentials)
78-
creds.create_scoped_required.return_value = False
74+
creds = make_mock_credentials()
7975
client = self._make_one(credentials=creds, http=object())
8076

8177
content = '<html>abc</html>'
@@ -90,23 +86,16 @@ def test_document_from_html_factory(self):
9086
self.assertEqual(document.language, language)
9187

9288
def test_document_from_html_factory_failure(self):
93-
import mock
94-
from oauth2client.client import GoogleCredentials
95-
96-
creds = mock.Mock(spec=GoogleCredentials)
97-
creds.create_scoped_required.return_value = False
89+
creds = make_mock_credentials()
9890
client = self._make_one(credentials=creds, http=object())
9991

10092
with self.assertRaises(TypeError):
10193
client.document_from_html('abc', doc_type='foo')
10294

10395
def test_document_from_url_factory(self):
104-
import mock
105-
from oauth2client.client import GoogleCredentials
10696
from google.cloud.language.document import Document
10797

108-
creds = mock.Mock(spec=GoogleCredentials)
109-
creds.create_scoped_required.return_value = False
98+
creds = make_mock_credentials()
11099
client = self._make_one(credentials=creds, http=object())
111100

112101
gcs_url = 'gs://my-text-bucket/sentiment-me.txt'
@@ -118,13 +107,10 @@ def test_document_from_url_factory(self):
118107
self.assertEqual(document.doc_type, Document.PLAIN_TEXT)
119108

120109
def test_document_from_url_factory_explicit(self):
121-
import mock
122-
from oauth2client.client import GoogleCredentials
123110
from google.cloud.language.document import Document
124111
from google.cloud.language.document import Encoding
125112

126-
creds = mock.Mock(spec=GoogleCredentials)
127-
creds.create_scoped_required.return_value = False
113+
creds = make_mock_credentials()
128114
client = self._make_one(credentials=creds, http=object())
129115

130116
encoding = Encoding.UTF32

language/unit_tests/test_document.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ def _get_entities(include_entities):
9595
return entities
9696

9797

98+
def make_mock_client(response):
99+
import mock
100+
from google.cloud.language.connection import Connection
101+
from google.cloud.language.client import Client
102+
103+
connection = mock.Mock(spec=Connection)
104+
connection.api_request.return_value = response
105+
return mock.Mock(_connection=connection, spec=Client)
106+
107+
98108
class TestDocument(unittest.TestCase):
99109

100110
@staticmethod
@@ -216,9 +226,6 @@ def _expected_data(content, encoding_type=None,
216226
return expected
217227

218228
def test_analyze_entities(self):
219-
import mock
220-
from google.cloud.language.connection import Connection
221-
from google.cloud.language.client import Client
222229
from google.cloud.language.document import Encoding
223230
from google.cloud.language.entity import EntityType
224231

@@ -261,9 +268,7 @@ def test_analyze_entities(self):
261268
],
262269
'language': 'en-US',
263270
}
264-
connection = mock.Mock(spec=Connection)
265-
connection.api_request.return_value = response
266-
client = mock.Mock(_connection=connection, spec=Client)
271+
client = make_mock_client(response)
267272
document = self._make_one(client, content)
268273

269274
entities = document.analyze_entities()
@@ -278,7 +283,7 @@ def test_analyze_entities(self):
278283
# Verify the request.
279284
expected = self._expected_data(
280285
content, encoding_type=Encoding.UTF8)
281-
connection.api_request.assert_called_once_with(
286+
client._connection.api_request.assert_called_once_with(
282287
path='analyzeEntities', method='POST', data=expected)
283288

284289
def _verify_sentiment(self, sentiment, polarity, magnitude):
@@ -289,10 +294,6 @@ def _verify_sentiment(self, sentiment, polarity, magnitude):
289294
self.assertEqual(sentiment.magnitude, magnitude)
290295

291296
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-
296297
content = 'All the pretty horses.'
297298
polarity = 1
298299
magnitude = 0.6
@@ -303,17 +304,15 @@ def test_analyze_sentiment(self):
303304
},
304305
'language': 'en-US',
305306
}
306-
connection = mock.Mock(spec=Connection)
307-
connection.api_request.return_value = response
308-
client = mock.Mock(_connection=connection, spec=Client)
307+
client = make_mock_client(response)
309308
document = self._make_one(client, content)
310309

311310
sentiment = document.analyze_sentiment()
312311
self._verify_sentiment(sentiment, polarity, magnitude)
313312

314313
# Verify the request.
315314
expected = self._expected_data(content)
316-
connection.api_request.assert_called_once_with(
315+
client._connection.api_request.assert_called_once_with(
317316
path='analyzeSentiment', method='POST', data=expected)
318317

319318
def _verify_sentences(self, include_syntax, annotations):
@@ -343,10 +342,6 @@ def _verify_tokens(self, annotations, token_info):
343342

344343
def _annotate_text_helper(self, include_sentiment,
345344
include_entities, include_syntax):
346-
import mock
347-
348-
from google.cloud.language.connection import Connection
349-
from google.cloud.language.client import Client
350345
from google.cloud.language.document import Annotations
351346
from google.cloud.language.document import Encoding
352347
from google.cloud.language.entity import EntityType
@@ -366,9 +361,7 @@ def _annotate_text_helper(self, include_sentiment,
366361
'magnitude': ANNOTATE_MAGNITUDE,
367362
}
368363

369-
connection = mock.Mock(spec=Connection)
370-
connection.api_request.return_value = response
371-
client = mock.Mock(_connection=connection, spec=Client)
364+
client = make_mock_client(response)
372365
document = self._make_one(client, ANNOTATE_CONTENT)
373366

374367
annotations = document.annotate_text(
@@ -400,7 +393,7 @@ def _annotate_text_helper(self, include_sentiment,
400393
extract_sentiment=include_sentiment,
401394
extract_entities=include_entities,
402395
extract_syntax=include_syntax)
403-
connection.api_request.assert_called_once_with(
396+
client._connection.api_request.assert_called_once_with(
404397
path='annotateText', method='POST', data=expected)
405398

406399
def test_annotate_text(self):

0 commit comments

Comments
 (0)