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.

74 lines
2.0 KiB

  1. # coding: utf-8
  2. # Copyright (C) 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
  3. # @ 2015 Valentin CHEMIERE @ Akretion
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. import mock
  6. from contextlib import contextmanager
  7. from collections import defaultdict
  8. class MultiResponse(dict):
  9. pass
  10. class ConnMock(object):
  11. def __init__(self, response):
  12. self.response = response
  13. self._calls = []
  14. self.call_count = defaultdict(int)
  15. def __getattribute__(self, method):
  16. if method not in ('_calls', 'response', 'call_count'):
  17. def callable(*args, **kwargs):
  18. self._calls.append({
  19. 'method': method,
  20. 'args': args,
  21. 'kwargs': kwargs,
  22. })
  23. call = self.response[method]
  24. if isinstance(call, MultiResponse):
  25. call = call[self.call_count[method]]
  26. self.call_count[method] += 1
  27. return call
  28. return callable
  29. else:
  30. return super(ConnMock, self).__getattribute__(method)
  31. def __call__(self, *args, **kwargs):
  32. return self
  33. def __enter__(self, *args, **kwargs):
  34. return self
  35. def __exit__(self, *args, **kwargs):
  36. pass
  37. def __repr__(self, *args, **kwargs):
  38. return self
  39. def __getitem__(self, key):
  40. return
  41. @contextmanager
  42. def server_mock_sftp(response):
  43. with mock.patch('openerp.addons.external_file_location.tasks.sftp.'
  44. 'SftpTask', ConnMock(response)) as SFTPFS:
  45. yield SFTPFS._calls
  46. @contextmanager
  47. def server_mock_ftp(response):
  48. with mock.patch('openerp.addons.external_file_location.tasks.ftp.'
  49. 'FtpTask', ConnMock(response)) as FTPFS:
  50. yield FTPFS._calls
  51. @contextmanager
  52. def server_mock_filestore(response):
  53. with mock.patch('openerp.addons.external_file_location.tasks.filestore.'
  54. 'FileStoreTask', ConnMock(response)) as FTPFS:
  55. yield FTPFS._calls