Debian Codegraph @

t/unit/bin/proj/pyramid_celery_app.py

raw
1 from unittest.mock import MagicMock, Mock
2
3 from click import Option
4
5 from celery import Celery
6
7 # This module defines a mocked Celery application to replicate
8 # the behavior of Pyramid-Celery's configuration by preload options.
9 # Preload options should propagate to commands like shell and purge etc.
10 #
11 # The Pyramid-Celery project https://github.com/sontek/pyramid_celery
12 # assumes that you want to configure Celery via an ini settings file.
13 # The .ini files are the standard configuration file for Pyramid
14 # applications.
15 # See https://docs.pylonsproject.org/projects/pyramid/en/latest/quick_tutorial/ini.html
16 #
17
18 app = Celery(set_as_current=False)
19 app.config_from_object("t.integration.test_worker_config")
20
21
22 class PurgeMock:
23 def queue_purge(self, queue):
24 return 0
25
26
27 class ConnMock:
28 default_channel = PurgeMock()
29 channel_errors = KeyError
30
31
32 mock = Mock()
33 mock.__enter__ = Mock(return_value=ConnMock())
34 mock.__exit__ = Mock(return_value=False)
35
36 app.connection_for_write = MagicMock(return_value=mock)
37
38 # Below are taken from pyramid-celery's __init__.py
39 # Ref: https://github.com/sontek/pyramid_celery/blob/cf8aa80980e42f7235ad361874d3c35e19963b60/pyramid_celery/__init__.py#L25-L36 # noqa: E501
40 ini_option = Option(
41 (
42 "--ini",
43 "-i",
44 ),
45 help="Paste ini configuration file.",
46 )
47
48 ini_var_option = Option(
49 ("--ini-var",), help="Comma separated list of key=value to pass to ini."
50 )
51
52 app.user_options["preload"].add(ini_option)
53 app.user_options["preload"].add(ini_var_option)