You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. ###############################################################################
  3. #
  4. # Copyright (C) 2015 Akretion (http://www.akretion.com).
  5. # @author Valentin CHEMIERE <valentin.chemiere@akretion.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ###############################################################################
  21. import mock
  22. from contextlib import contextmanager
  23. from collections import defaultdict
  24. class MultiResponse(dict):
  25. pass
  26. class ConnMock(object):
  27. def __init__(self, response):
  28. self.response = response
  29. self._calls = []
  30. self.call_count = defaultdict(int)
  31. def __getattribute__(self, method):
  32. if method not in ('_calls', 'response', 'call_count'):
  33. def callable(*args, **kwargs):
  34. self._calls.append({
  35. 'method': method,
  36. 'args': args,
  37. 'kwargs': kwargs,
  38. })
  39. call = self.response[method]
  40. if isinstance(call, MultiResponse):
  41. call = call[self.call_count[method]]
  42. self.call_count[method] += 1
  43. return call
  44. return callable
  45. else:
  46. return super(ConnMock, self).__getattribute__(method)
  47. def __call__(self, *args, **kwargs):
  48. return self
  49. def __enter__(self, *args, **kwargs):
  50. return self
  51. def __exit__(self, *args, **kwargs):
  52. pass
  53. def __repr__(self, *args, **kwargs):
  54. return self
  55. def __getitem__(self, key):
  56. return
  57. @contextmanager
  58. def server_mock(response):
  59. with mock.patch('fs.sftpfs.SFTPFS', ConnMock(response)) as SFTPFS:
  60. yield SFTPFS._calls