Repository navigation
Expand file tree
/
Copy pathtest_client.py
More file actions
166 lines (135 loc) · 5.9 KB
/
Copy pathtest_client.py
File metadata and controls
166 lines (135 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import mock
def _make_credentials():
import google.auth.credentials
return mock.Mock(spec=google.auth.credentials.Credentials)
class TestClient(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.error_reporting.client import Client
return Client
def _getHttpContext(self):
from google.cloud.error_reporting.client import HTTPContext
return HTTPContext
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
def _makeHTTP(self, *args, **kw):
return self._getHttpContext()(*args, **kw)
PROJECT = 'PROJECT'
SERVICE = 'SERVICE'
VERSION = 'myversion'
@mock.patch(
'google.cloud.error_reporting.client._determine_default_project')
def test_ctor_default(self, _):
CREDENTIALS = _make_credentials()
target = self._make_one(credentials=CREDENTIALS)
self.assertEquals(target.service, target.DEFAULT_SERVICE)
self.assertEquals(target.version, None)
def test_ctor_params(self):
CREDENTIALS = _make_credentials()
target = self._make_one(project=self.PROJECT,
credentials=CREDENTIALS,
service=self.SERVICE,
version=self.VERSION)
self.assertEquals(target.service, self.SERVICE)
self.assertEquals(target.version, self.VERSION)
def test_report_exception_with_gax(self):
CREDENTIALS = _make_credentials()
target = self._make_one(project=self.PROJECT,
credentials=CREDENTIALS)
patch = mock.patch(
'google.cloud.error_reporting.client.make_report_error_api')
with patch as make_api:
try:
raise NameError
except NameError:
target.report_exception()
payload = make_api.return_value.report_error_event.call_args[0][0]
self.assertEquals(payload['serviceContext'], {
'service': target.DEFAULT_SERVICE,
})
self.assertIn('test_report', payload['message'])
self.assertIn('test_client.py', payload['message'])
def test_report_exception_wo_gax(self):
CREDENTIALS = _make_credentials()
target = self._make_one(project=self.PROJECT,
credentials=CREDENTIALS,
use_gax=False)
patch = mock.patch(
'google.cloud.error_reporting.client._ErrorReportingLoggingAPI'
)
with patch as _error_api:
try:
raise NameError
except NameError:
target.report_exception()
mock_report = _error_api.return_value.report_error_event
payload = mock_report.call_args[0][0]
self.assertEquals(payload['serviceContext'], {
'service': target.DEFAULT_SERVICE,
})
self.assertIn('test_report', payload['message'])
self.assertIn('test_client.py', payload['message'])
self.assertIsNotNone(target.report_errors_api)
@mock.patch('google.cloud.error_reporting.client.make_report_error_api')
def test_report_exception_with_service_version_in_constructor(
self,
make_client):
CREDENTIALS = _make_credentials()
SERVICE = "notdefault"
VERSION = "notdefaultversion"
target = self._make_one(project=self.PROJECT,
credentials=CREDENTIALS,
service=SERVICE,
version=VERSION)
http_context = self._makeHTTP(method="GET", response_status_code=500)
USER = "user@gmail.com"
client = mock.Mock()
make_client.return_value = client
try:
raise NameError
except NameError:
target.report_exception(http_context=http_context, user=USER)
payload = client.report_error_event.call_args[0][0]
self.assertEquals(payload['serviceContext'], {
'service': SERVICE,
'version': VERSION
})
self.assertIn(
'test_report_exception_with_service_version_in_constructor',
payload['message'])
self.assertIn('test_client.py', payload['message'])
self.assertEquals(
payload['context']['httpContext']['responseStatusCode'], 500)
self.assertEquals(
payload['context']['httpContext']['method'], 'GET')
self.assertEquals(payload['context']['user'], USER)
@mock.patch('google.cloud.error_reporting.client.make_report_error_api')
def test_report(self, make_client):
CREDENTIALS = _make_credentials()
target = self._make_one(project=self.PROJECT,
credentials=CREDENTIALS)
client = mock.Mock()
make_client.return_value = client
MESSAGE = 'this is an error'
target.report(MESSAGE)
payload = client.report_error_event.call_args[0][0]
self.assertEquals(payload['message'], MESSAGE)
report_location = payload['context']['reportLocation']
self.assertIn('test_client.py', report_location['filePath'])
self.assertEqual(report_location['functionName'], 'test_report')
self.assertGreater(report_location['lineNumber'], 100)
self.assertLess(report_location['lineNumber'], 250)