File: test_django.py

package info (click to toggle)
python-carrot 0.10.5-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 492 kB
  • ctags: 508
  • sloc: python: 2,477; makefile: 75
file content (72 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import sys
import unittest
import pickle
import time
sys.path.insert(0, os.pardir)
sys.path.append(os.getcwd())

from tests.utils import BROKER_HOST, BROKER_PORT, BROKER_VHOST, \
                        BROKER_USER, BROKER_PASSWORD
from carrot.connection import DjangoBrokerConnection, BrokerConnection
from UserDict import UserDict

CARROT_BACKEND = "amqp"


class DictWrapper(UserDict):

    def __init__(self, data):
        self.data = data

    def __getattr__(self, key):
        try:
            return self.data[key]
        except KeyError:
            raise AttributeError("'%s' object has no attribute '%s'" % (
                self.__class__.__name__, key))


def configured_or_configure(settings, **conf):
    if settings.configured:
        for conf_name, conf_value in conf.items():
            setattr(settings, conf_name, conf_value)
    else:
        settings.configure(default_settings=DictWrapper(conf))


class TestDjangoSpecific(unittest.TestCase):

    def test_DjangoBrokerConnection(self):
        try:
            from django.conf import settings
        except ImportError:
            sys.stderr.write(
                "Django is not installed. \
                Not testing django specific features.\n")
            return
        configured_or_configure(settings,
                CARROT_BACKEND=CARROT_BACKEND,
                BROKER_HOST=BROKER_HOST,
                BROKER_PORT=BROKER_PORT,
                BROKER_VHOST=BROKER_VHOST,
                BROKER_USER=BROKER_USER,
                BROKER_PASSWORD=BROKER_PASSWORD)

        expected_values = {
            "backend_cls": CARROT_BACKEND,
            "hostname": BROKER_HOST,
            "port": BROKER_PORT,
            "virtual_host": BROKER_VHOST,
            "userid": BROKER_USER,
            "password": BROKER_PASSWORD}

        conn = DjangoBrokerConnection()
        self.assertTrue(isinstance(conn, BrokerConnection))

        for val_name, val_value in expected_values.items():
            self.assertEquals(getattr(conn, val_name, None), val_value)


if __name__ == '__main__':
    unittest.main()