diff options
| author | Romain Derie <rde@odoo.com> | 2019-06-26 13:48:56 +0000 |
|---|---|---|
| committer | Romain Derie <rde@odoo.com> | 2019-06-26 17:08:09 +0000 |
| commit | 6ebcb1db80eeb37ab7ac2f5a27a8c616f12b95b3 (patch) | |
| tree | e5b099e61983f969a205d593cdd31b557306b722 /addons/website/tools.py | |
| parent | bb81e4ccd7b0044bbe1ad45842a3f799ceac0838 (diff) | |
[IMP] tool, website, website_sale: move test utils functions
We have multiple ways of simulating a frontend context while running python
tests (mainly during TransactionCase):
- `MockObject` class from 8557bcfa30
- `simulate_frontend_context` method from a5a5a57e48
- `DotDict` from f8efc9a957c28
This commit moves `MockObject` to misc to be reusable and adapt it to be
accessible by dot notation with `DotDict`.
Part of #34396
Diffstat (limited to 'addons/website/tools.py')
| -rw-r--r-- | addons/website/tools.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/addons/website/tools.py b/addons/website/tools.py new file mode 100644 index 00000000000..fde1e94651d --- /dev/null +++ b/addons/website/tools.py @@ -0,0 +1,60 @@ +# -*- encoding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import odoo +import werkzeug + +from odoo.tools import DotDict + + +class MockObject(object): + _log_call = [] + + def __init__(self, *args, **kwargs): + self.__dict__ = kwargs + + def __call__(self, *args, **kwargs): + self._log_call.append((args, kwargs)) + return self + + def __getitem__(self, index): + return self + + +def werkzeugRaiseNotFound(*args, **kwargs): + raise werkzeug.exceptions.NotFound() + + +class MockRequest(object): + """ Class with context manager mocking odoo.http.request for tests """ + def __init__(self, env, **kw): + app = MockObject(routing={ + 'type': 'http', + 'website': True, + 'multilang': kw.get('multilang', True), + }) + app.get_db_router = app.bind = app.match = app + if not kw.get('routing', True): + app.match = werkzeugRaiseNotFound + self.request = DotDict({ + 'context': kw.get('context', {}), + 'db': None, + 'debug': False, + 'env': env, + 'httprequest': { + 'path': '/hello/', + 'app': app, + }, + 'redirect': werkzeug.utils.redirect, + 'session': { + 'sale_order_id': kw.get('sale_order_id'), + }, + 'website': kw.get('website'), + }) + odoo.http._request_stack.push(self.request) + + def __enter__(self): + return self.request + + def __exit__(self, exc_type, exc_value, traceback): + odoo.http._request_stack.pop() |
