~launchpad-pqm/launchpad/devel

13333.6.35 by Gavin Panella
Move AdapterFixture to lp.testing.fixture.ZopeAdapterFixture.
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Tests for lp.testing.fixture."""
5
6
__metaclass__ = type
7
13333.6.50 by Gavin Panella
Set the service_config in a new RabbitServer fixture.
8
from textwrap import dedent
9
10
from fixtures import EnvironmentVariableFixture
13333.6.35 by Gavin Panella
Move AdapterFixture to lp.testing.fixture.ZopeAdapterFixture.
11
from zope.component import (
12
    adapts,
13
    queryAdapter,
14
    )
15
from zope.interface import (
16
    implements,
17
    Interface,
18
    )
19
13864.1.1 by William Grant
Revert r13863. The pgbouncer fixture relies on /usr/sbin being in PATH, which isn't the case everywhere (but most notably not on buildbot).
20
from canonical.testing.layers import BaseLayer
13333.6.35 by Gavin Panella
Move AdapterFixture to lp.testing.fixture.ZopeAdapterFixture.
21
from lp.testing import TestCase
13333.6.50 by Gavin Panella
Set the service_config in a new RabbitServer fixture.
22
from lp.testing.fixture import (
23
    RabbitServer,
24
    ZopeAdapterFixture,
25
    )
26
27
28
class TestRabbitServer(TestCase):
29
30
    layer = BaseLayer
31
32
    def test_service_config(self):
33
        # Rabbit needs to fully isolate itself: an existing per user
34
        # .erlange.cookie has to be ignored, and ditto bogus HOME if other
35
        # tests fail to cleanup.
36
        self.useFixture(EnvironmentVariableFixture('HOME', '/nonsense/value'))
37
38
        # RabbitServer pokes some .ini configuration into its config.
39
        with RabbitServer() as fixture:
40
            expected = dedent("""\
41
                [rabbitmq]
42
                host: localhost:%d
43
                userid: guest
44
                password: guest
45
                virtual_host: /
46
                """ % fixture.config.port)
47
            self.assertEqual(expected, fixture.config.service_config)
13333.6.35 by Gavin Panella
Move AdapterFixture to lp.testing.fixture.ZopeAdapterFixture.
48
49
50
class IFoo(Interface):
51
    pass
52
53
54
class IBar(Interface):
55
    pass
56
57
58
class Foo:
59
    implements(IFoo)
60
61
62
class Bar:
63
    implements(IBar)
64
65
66
class FooToBar:
67
68
    adapts(IFoo)
69
    implements(IBar)
70
71
    def __init__(self, foo):
72
        self.foo = foo
73
74
75
class TestZopeAdapterFixture(TestCase):
76
77
    layer = BaseLayer
78
79
    def test_register_and_unregister(self):
13333.6.49 by Gavin Panella
More comments.
80
        # Entering ZopeAdapterFixture's context registers the given adapter,
81
        # and exiting the context unregisters the adapter again.
13333.6.35 by Gavin Panella
Move AdapterFixture to lp.testing.fixture.ZopeAdapterFixture.
82
        context = Foo()
83
        # No adapter from Foo to Bar is registered.
84
        self.assertIs(None, queryAdapter(context, IBar))
85
        with ZopeAdapterFixture(FooToBar):
86
            # Now there is an adapter from Foo to Bar.
87
            adapter = queryAdapter(context, IBar)
88
            self.assertIsNot(None, adapter)
89
            self.assertIsInstance(adapter, FooToBar)
13333.6.49 by Gavin Panella
More comments.
90
        # The adapter is no longer registered.
13333.6.35 by Gavin Panella
Move AdapterFixture to lp.testing.fixture.ZopeAdapterFixture.
91
        self.assertIs(None, queryAdapter(context, IBar))