def testFailedJsonParse(self, mock_req): mock_req.return_value = test_utils.GetTestResponse() mock_req.return_value.text = '{"Invalid": "JSON}' ctx = context.Context('foo.corn', 'foo', 1) with self.assertRaises(excs.RequestError): ctx.ExecuteRequest('GET')
def testNotFound(self, mock_req): mock_req.return_value = test_utils.GetTestResponse( status_code=httplib.NOT_FOUND) ctx = context.Context('foo.corn', 'foo', 1) with self.assertRaises(excs.NotFoundError): ctx.ExecuteRequest('GET')
def testServerError(self, mock_req): mock_req.return_value = test_utils.GetTestResponse( status_code=httplib.INTERNAL_SERVER_ERROR) ctx = context.Context('foo.corn', 'foo', 1) with self.assertRaises(excs.RequestError): ctx.ExecuteRequest('GET')
def testClientError(self, mock_req): mock_req.return_value = test_utils.GetTestResponse( status_code=httplib.BAD_REQUEST) ctx = context.Context('foo.corn', 'foo', 1) with self.assertRaises(excs.RequestError): ctx.ExecuteRequest('GET')
def testEmptyResponse(self, mock_req): mock_req.return_value = test_utils.GetTestResponse( status_code=httplib.OK) mock_req.return_value.text = None ctx = context.Context('foo.corn', 'foo', 1) with self.assertRaises(excs.RequestError): ctx.ExecuteRequest('GET')
def testNoRoute(self, mock_req): ctx = context.Context('foo.corn', 'foo', 1) ctx.ExecuteRequest('GET') mock_req.assert_called_once_with( 'GET', 'https://foo.corn/api/bit9platform/v1', headers=mock.ANY, json=None, verify=mock.ANY, timeout=mock.ANY)
def testWithPath(self, mock_req): ctx = context.Context('foo.corn/other/path', 'foo', 1) ctx.ExecuteRequest('GET', api_route='abc') mock_req.assert_called_once_with( 'GET', 'https://foo.corn/other/path/api/bit9platform/v1/abc', headers=mock.ANY, json=None, verify=mock.ANY, timeout=mock.ANY)
def testHeaders(self, mock_req): ctx = context.Context('foo.corn', 'foo', 1) ctx.ExecuteRequest('GET') expected_headers = { 'X-Auth-Token': 'foo', 'Content-Type': 'application/json' } mock_req.assert_called_once_with('GET', mock.ANY, headers=expected_headers, json=None, verify=True, timeout=1)
def testRequestError(self, mock_req): mock_req.side_effect = requests.RequestException ctx = context.Context('foo.corn', 'foo', 1) with self.assertRaises(excs.RequestError): ctx.ExecuteRequest('GET')
def testBadTimeout(self, _): with self.assertRaises(ValueError): context.Context('foo.corn', 'foo', -1, version='v2') with self.assertRaises(ValueError): context.Context('foo.corn', 'foo', 'foo', version='v2')
def testBadVersion(self, _): with self.assertRaises(ValueError): context.Context('foo.corn', 'foo', 1, version='v2')
ROUTE = 'abcd' foo = model.StringProperty('foo', allow_update=True, expands_to=AModel) bar = model.Int32Property('bar') baz = model.StringProperty('baz', allow_update=True, expands_to=AModel) class OtherTestModel(model.Model): ROUTE = 'efgh' foo = model.StringProperty('foo', allow_update=True, expands_to=TestModel) bar = model.Int32Property('bar') _TEST_URL = 'https://foo.corn' _TEST_CTX = context.Context(_TEST_URL, 'abc', 123) _TEST_API_ADDR = _TEST_URL + '/api/bit9platform/v1/' @mock.patch.object(requests, 'request', return_value=test_utils.GetTestResponse(data={})) class QueryTest(absltest.TestCase): def testEmpty(self, mock_req): query.Query(TestModel).execute(_TEST_CTX) mock_req.assert_called_once_with('GET', _TEST_API_ADDR + 'abcd', headers=mock.ANY, json=None, verify=mock.ANY, timeout=mock.ANY)