mock_utils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (C) 2017 YouCompleteMe contributors
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. import json
  18. from unittest import mock
  19. HTTP_OK = 200
  20. class FakeResponse:
  21. """A fake version of a requests response object, just about suitable for
  22. mocking a server response. Not usually used directly. See
  23. MockServerResponse* methods"""
  24. def __init__( self, response, exception ):
  25. self._json = response
  26. self._exception = exception
  27. self.code = HTTP_OK
  28. def read( self ):
  29. if self._exception:
  30. raise self._exception
  31. return json.dumps( self._json ).encode( 'utf-8' )
  32. def close( self ):
  33. pass
  34. class FakeFuture:
  35. """A fake version of a future response object, just about suitable for
  36. mocking a server response as generated by PostDataToHandlerAsync.
  37. Not usually used directly. See MockAsyncServerResponse* methods"""
  38. def __init__( self, done, response = None, exception = None ):
  39. self._done = done
  40. if not done:
  41. self._result = None
  42. else:
  43. self._result = FakeResponse( response, exception )
  44. def done( self ):
  45. return self._done
  46. def result( self ):
  47. return self._result
  48. def MockAsyncServerResponseDone( response ):
  49. """Return a MessagePoll containing a fake future object that is complete with
  50. the supplied response message. Suitable for mocking a response future within
  51. a client request. For example:
  52. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  53. mock_response = MockAsyncServerResponseDone( response )
  54. with patch.dict( ycm._message_poll_requests, {} ):
  55. ycm._message_poll_requests[ filetype ] = MessagesPoll( v.current.buffer )
  56. ycm._message_poll_requests[ filetype ]._response_future = mock_response
  57. # Needed to keep a reference to the mocked dictionary
  58. mock_future = ycm._message_poll_requests[ filetype ]._response_future
  59. ycm.OnPeriodicTick() # Uses ycm._message_poll_requests[ filetype ] ...
  60. """
  61. return mock.MagicMock( wraps = FakeFuture( True, response ) )
  62. def MockAsyncServerResponseInProgress():
  63. """Return a fake future object that is incomplete. Suitable for mocking a
  64. response future within a client request. For example:
  65. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  66. mock_response = MockAsyncServerResponseInProgress()
  67. with patch.dict( ycm._message_poll_requests, {} ):
  68. ycm._message_poll_requests[ filetype ] = MessagesPoll( v.current.buffer )
  69. ycm._message_poll_requests[ filetype ]._response_future = mock_response
  70. # Needed to keep a reference to the mocked dictionary
  71. mock_future = ycm._message_poll_requests[ filetype ]._response_future
  72. ycm.OnPeriodicTick() # Uses ycm._message_poll_requests[ filetype ] ...
  73. """
  74. return mock.MagicMock( wraps = FakeFuture( False ) )
  75. def MockAsyncServerResponseException( exception ):
  76. """Return a fake future object that is complete, but raises an exception.
  77. Suitable for mocking a response future within a client request. For example:
  78. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 1 ) ) as v:
  79. mock_response = MockAsyncServerResponseException( exception )
  80. with patch.dict( ycm._message_poll_requests, {} ):
  81. ycm._message_poll_requests[ filetype ] = MessagesPoll( v.current.buffer )
  82. ycm._message_poll_requests[ filetype ]._response_future = mock_response
  83. # Needed to keep a reference to the mocked dictionary
  84. mock_future = ycm._message_poll_requests[ filetype ]._response_future
  85. ycm.OnPeriodicTick() # Uses ycm._message_poll_requests[ filetype ] ...
  86. """
  87. return mock.MagicMock( wraps = FakeFuture( True, None, exception ) )
  88. # TODO: In future, implement MockServerResponse and MockServerResponseException
  89. # for synchronous cases when such test cases are needed.