店播爬取Python脚本

service_reflection_test.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #! /usr/bin/env python
  2. #
  3. # Protocol Buffers - Google's data interchange format
  4. # Copyright 2008 Google Inc. All rights reserved.
  5. # https://developers.google.com/protocol-buffers/
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are
  9. # met:
  10. #
  11. # * Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # * Redistributions in binary form must reproduce the above
  14. # copyright notice, this list of conditions and the following disclaimer
  15. # in the documentation and/or other materials provided with the
  16. # distribution.
  17. # * Neither the name of Google Inc. nor the names of its
  18. # contributors may be used to endorse or promote products derived from
  19. # this software without specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. """Tests for google.protobuf.internal.service_reflection."""
  33. __author__ = 'petar@google.com (Petar Petrov)'
  34. try:
  35. import unittest2 as unittest #PY26
  36. except ImportError:
  37. import unittest
  38. from google.protobuf import unittest_pb2
  39. from google.protobuf import service_reflection
  40. from google.protobuf import service
  41. class FooUnitTest(unittest.TestCase):
  42. def testService(self):
  43. class MockRpcChannel(service.RpcChannel):
  44. def CallMethod(self, method, controller, request, response, callback):
  45. self.method = method
  46. self.controller = controller
  47. self.request = request
  48. callback(response)
  49. class MockRpcController(service.RpcController):
  50. def SetFailed(self, msg):
  51. self.failure_message = msg
  52. self.callback_response = None
  53. class MyService(unittest_pb2.TestService):
  54. pass
  55. self.callback_response = None
  56. def MyCallback(response):
  57. self.callback_response = response
  58. rpc_controller = MockRpcController()
  59. channel = MockRpcChannel()
  60. srvc = MyService()
  61. srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
  62. self.assertEqual('Method Foo not implemented.',
  63. rpc_controller.failure_message)
  64. self.assertEqual(None, self.callback_response)
  65. rpc_controller.failure_message = None
  66. service_descriptor = unittest_pb2.TestService.GetDescriptor()
  67. srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
  68. unittest_pb2.BarRequest(), MyCallback)
  69. self.assertTrue(srvc.GetRequestClass(service_descriptor.methods[1]) is
  70. unittest_pb2.BarRequest)
  71. self.assertTrue(srvc.GetResponseClass(service_descriptor.methods[1]) is
  72. unittest_pb2.BarResponse)
  73. self.assertEqual('Method Bar not implemented.',
  74. rpc_controller.failure_message)
  75. self.assertEqual(None, self.callback_response)
  76. class MyServiceImpl(unittest_pb2.TestService):
  77. def Foo(self, rpc_controller, request, done):
  78. self.foo_called = True
  79. def Bar(self, rpc_controller, request, done):
  80. self.bar_called = True
  81. srvc = MyServiceImpl()
  82. rpc_controller.failure_message = None
  83. srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
  84. self.assertEqual(None, rpc_controller.failure_message)
  85. self.assertEqual(True, srvc.foo_called)
  86. rpc_controller.failure_message = None
  87. srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
  88. unittest_pb2.BarRequest(), MyCallback)
  89. self.assertEqual(None, rpc_controller.failure_message)
  90. self.assertEqual(True, srvc.bar_called)
  91. def testServiceStub(self):
  92. class MockRpcChannel(service.RpcChannel):
  93. def CallMethod(self, method, controller, request,
  94. response_class, callback):
  95. self.method = method
  96. self.controller = controller
  97. self.request = request
  98. callback(response_class())
  99. self.callback_response = None
  100. def MyCallback(response):
  101. self.callback_response = response
  102. channel = MockRpcChannel()
  103. stub = unittest_pb2.TestService_Stub(channel)
  104. rpc_controller = 'controller'
  105. request = 'request'
  106. # GetDescriptor now static, still works as instance method for compatibility
  107. self.assertEqual(unittest_pb2.TestService_Stub.GetDescriptor(),
  108. stub.GetDescriptor())
  109. # Invoke method.
  110. stub.Foo(rpc_controller, request, MyCallback)
  111. self.assertIsInstance(self.callback_response, unittest_pb2.FooResponse)
  112. self.assertEqual(request, channel.request)
  113. self.assertEqual(rpc_controller, channel.controller)
  114. self.assertEqual(stub.GetDescriptor().methods[0], channel.method)
  115. if __name__ == '__main__':
  116. unittest.main()