12
12
__metaclass__ = type
15
"RabbitServerSettings",
19
"uses_rabbit_fixture",
22
from functools import wraps
24
from fixtures import Fixture
25
19
from rabbitfixture.server import RabbitServer
26
from testtools.monkey import MonkeyPatcher
29
class RabbitServerSettings(Fixture):
31
This patches the active Django settings to point the application at the
32
ephemeral RabbitMQ server specified by the given configuration.
35
def __init__(self, config):
36
super(RabbitServerSettings, self).__init__()
40
super(RabbitServerSettings, self).setUp()
41
from django.conf import settings
42
patcher = MonkeyPatcher()
44
settings, "RABBITMQ_HOST",
45
"%s:%d" % (self.config.hostname, self.config.port))
46
patcher.add_patch(settings, "RABBITMQ_USERID", "guest")
47
patcher.add_patch(settings, "RABBITMQ_PASSWORD", "guest")
48
patcher.add_patch(settings, "RABBITMQ_VIRTUAL_HOST", "/")
49
patcher.add_patch(settings, "RABBITMQ_PUBLISH", True)
50
self.addCleanup(patcher.restore)
54
21
# See {start,stop,get}_rabbit().
81
def use_rabbit_fixture(test):
82
"""Ensure that a :class:`RabbitServer` is started, and Django's setting
83
updated to point to it, and that Django's settings are returned to their
84
original values at the end.
86
config = get_rabbit().config
87
fixture = RabbitServerSettings(config)
88
test.useFixture(fixture)
91
def uses_rabbit_fixture(func):
92
"""Decorate a test function with `use_rabbit_fixture`."""
95
use_rabbit_fixture(self)