def testUpdateFields(self, mock_req): obj = {'foo': 'def'} mock_req.side_effect = [ test_utils.GetTestResponse(data={ 'foo': 'abc', 'bar': 123 }), test_utils.GetTestResponse(data={ 'foo': 'def', 'bar': 123 }) ] TestModel.update('12345', obj, _TEST_CTX) expected_calls = [ mock.call('GET', _TEST_API_ADDR + 'abcd/12345', headers=mock.ANY, json=None, verify=mock.ANY, timeout=mock.ANY), mock.call('POST', _TEST_API_ADDR + 'abcd', headers=mock.ANY, json={ 'foo': 'def', 'bar': 123 }, verify=mock.ANY, timeout=mock.ANY) ] mock_req.assert_has_calls(expected_calls)
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 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 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 testCount(self, mock_req): mock_req.return_value = test_utils.GetTestResponse(data={'count': 1}) response = query.Query(TestModel).count(_TEST_CTX) self.assertEqual(1, response) mock_req.assert_called_once_with( 'GET', _TEST_API_ADDR + 'abcd?limit=-1', headers=mock.ANY, json=None, verify=mock.ANY, timeout=mock.ANY)
def testBuildQuery(self, mock_req): mock_req.return_value = test_utils.GetTestResponse(data={}) TestModel.query().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)
def testGet(self, mock_req): mock_req.return_value = test_utils.GetTestResponse(data={}) TestModel.get('123', _TEST_CTX) mock_req.assert_called_once_with('GET', _TEST_API_ADDR + 'abcd/123', headers=mock.ANY, json=None, verify=mock.ANY, timeout=mock.ANY)
def testPut(self, mock_req): obj = {'foo': 'abc', 'bar': 123} test_model = TestModel.from_dict(obj) mock_req.return_value = test_utils.GetTestResponse(data=obj) test_model.put(_TEST_CTX) mock_req.assert_called_once_with('POST', _TEST_API_ADDR + 'abcd', headers=mock.ANY, json=obj, verify=mock.ANY, timeout=mock.ANY)
def testDelete(self, mock_req): obj = {'foo': 'abc', 'bar': 123} test_model = TestModel.from_dict(obj) mock_req.return_value = test_utils.GetTestResponse(data=obj) test_model.delete(123, _TEST_CTX) mock_req.assert_called_once_with('DELETE', _TEST_API_ADDR + 'abcd/123', headers=mock.ANY, json=None, verify=mock.ANY, timeout=mock.ANY)
def testPut_ExtraQueryArgs(self, mock_req): obj = {'foo': 'abc', 'bar': 123} test_model = TestModel.from_dict(obj) mock_req.return_value = test_utils.GetTestResponse(data=obj) test_model.put(_TEST_CTX, {'resetCLIPassword': True}) mock_req.assert_called_once_with('POST', _TEST_API_ADDR + 'abcd?resetCLIPassword=True', headers=mock.ANY, json=obj, verify=mock.ANY, timeout=mock.ANY)
def testPut_ExpandedModel(self, mock_req): obj = {'foo': 'abc', 'foo_foo': 'def', 'bar': 123} test_model = TestModel.from_dict(obj) amodel = test_model.get_expand(TestModel.foo) # Update the expanded model. amodel.foo = 'ghi' mock_req.return_value = test_utils.GetTestResponse(data={'foo': 'ghi'}) new_model = amodel.put(_TEST_CTX) self.assertEqual(new_model, amodel) mock_req.assert_called_once_with('POST', _TEST_API_ADDR + '1234', headers=mock.ANY, json={'foo': 'ghi'}, verify=mock.ANY, timeout=mock.ANY)
def testPut_ModelWithExpand(self, mock_req): obj = {'foo': 'abc', 'foo_foo': 'def', 'bar': 123} test_model = TestModel.from_dict(obj) test_model.foo = 'def' mock_req.return_value = test_utils.GetTestResponse(data={ 'foo': 'def', 'bar': 123 }) new_model = test_model.put(_TEST_CTX) self.assertEqual(new_model, test_model) mock_req.assert_called_once_with('POST', _TEST_API_ADDR + 'abcd', headers=mock.ANY, json={ 'foo': 'def', 'bar': 123 }, verify=mock.ANY, timeout=mock.ANY)
('Hello\nthere', 'Hello there'), ('\nHello\nthere\n\n\n', 'Hello there'), (u'Nice string', 'Nice string'), ('Nicer string', 'Nicer string'), (u'foo\nbar', 'foo bar'), (None, '') ] for (str_input, str_output) in tests: ret = context.ToAsciiStr(str_input) self.assertEqual(str_output, ret) self.assertRaises(TypeError, context.ToAsciiStr, ['hello']) @mock.patch.object( requests, 'request', return_value=test_utils.GetTestResponse()) class ContextTest(absltest.TestCase): def testBadVersion(self, _): with self.assertRaises(ValueError): context.Context('foo.corn', 'foo', 1, version='v2') 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 testNoSchema(self, mock_req): ctx = context.Context('foo.corn', 'foo', 1)
self.assertEqual('TestModel', TestModel.foo.model_cls_name) self.assertEqual('TestModel', TestModel.bar.model_cls_name) def testNoRouteProvided(self): with self.assertRaises(excs.Error): class OtherModel(model.Model): # pylint: disable=unused-variable pass def testKindMap(self): self.assertEqual(TestModel, OtherTestModel._KIND_MAP['TestModel']) @mock.patch.object(requests, 'request', return_value=test_utils.GetTestResponse()) class ModelTest(absltest.TestCase): def testPut(self, mock_req): obj = {'foo': 'abc', 'bar': 123} test_model = TestModel.from_dict(obj) mock_req.return_value = test_utils.GetTestResponse(data=obj) test_model.put(_TEST_CTX) mock_req.assert_called_once_with('POST', _TEST_API_ADDR + 'abcd', headers=mock.ANY, json=obj, verify=mock.ANY, timeout=mock.ANY)