~launchpad-pqm/launchpad/devel

7675.916.98 by Henning Eggers
Merged db-stable at r10026 (recife roll-back) but without accepting the changes.
1
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
8687.15.18 by Karl Fogel
Add the copyright header block to files under lib/canonical/.
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3691.27.2 by Guilherme Salgado
Refactor some karma tests and move some stuff from person.txt to person-karma.txt
3
4
"""Helper functions/classes to be used when testing the karma framework."""
5
6
__metaclass__ = type
7675.916.98 by Henning Eggers
Merged db-stable at r10026 (recife roll-back) but without accepting the changes.
7
__all__ = [
8
    'KarmaAssignedEventListener',
9
    'KarmaRecorder',
10
    ]
3691.27.2 by Guilherme Salgado
Refactor some karma tests and move some stuff from person.txt to person-karma.txt
11
12
from canonical.launchpad.ftests.event import TestEventListener
13130.1.14 by Curtis Hovey
Moved launchpad.event into registry interfaces.
13
from lp.registry.interfaces.karma import IKarmaAssignedEvent
10320.1.1 by Curtis Hovey
Ran migrator to move registry modules to lp.regisrty.
14
from lp.registry.interfaces.person import IPerson
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
15
16
7675.916.98 by Henning Eggers
Merged db-stable at r10026 (recife roll-back) but without accepting the changes.
17
class KarmaRecorder:
18
    """Helper that records selected karma events.
19
20
    Install with `register` (and don't forget to uninstall later with
21
    `unregister`).
22
23
    A list of karma events is accumulated in the `karma_events`
24
    property.
25
    """
26
27
    def __init__(self, person=None, action_name=None, product=None,
28
                 distribution=None, sourcepackagename=None):
29
        """Create a `KarmaRecorder`, but do not activate it yet.
30
31
        :param person: If given, record only karma for this `Person`.
32
        :param action_name: If given, record only karma with this action
33
            name (e.g. questionasked, sponsoruploadaccepted, bugfixed).
34
        :param product: If given, record only karma related to this
35
            `Product`.
36
        :param distribution: If given, record only karma related to this
37
            `Distribution`.
38
        :param sourcepackagename: If given, record only karma related to
39
            this `SourcePackageName`.
40
        """
41
        self.person = person
42
        self.action_name = action_name
43
        self.product = product
44
        self.distribution = distribution
45
        self.sourcepackagename = sourcepackagename
46
47
        self.karma_events = []
48
49
    def _filterFor(self, filter_value, event_value):
50
        """Does an event property value pass our filter for that property?"""
51
        return filter_value is None or event_value == filter_value
52
53
    def filter(self, karma):
54
        """Does `karma` match our filters?"""
55
        return (
56
            self._filterFor(self.person, karma.person) and
57
            self._filterFor(self.action_name, karma.action.name) and
58
            self._filterFor(self.product, karma.product) and
59
            self._filterFor(self.distribution, karma.distribution) and
60
            self._filterFor(self.sourcepackagename, karma.sourcepackagename))
61
62
    def record(self, karma):
63
        """Overridable: record the assignment of karma.
64
65
        The default action to record the karma object in
66
        `self.karma_events`, but feel free to override this with your
67
        own handler.
68
        """
69
        self.karma_events.append(karma)
70
71
    def receive(self, obj, event):
72
        """Process a karma event.
73
74
        Runs `filter` on the event and if it passes, `record`s it.
75
        """
76
        if self.filter(event.karma):
77
            self.record(event.karma)
78
79
    def register_listener(self):
80
        """Register listener.  Must be `unregister`ed later."""
81
        self.listener = TestEventListener(
82
            IPerson, IKarmaAssignedEvent, self.receive)
83
84
    def unregister_listener(self):
85
        """Unregister listener after `register`."""
86
        self.listener.unregister()
87
88
89
class KarmaAssignedEventListener(KarmaRecorder):
3691.27.2 by Guilherme Salgado
Refactor some karma tests and move some stuff from person.txt to person-karma.txt
90
    """Test helper class that registers a listener printing information
91
    whenever Karma is assigned.
92
93
    No karma assignments will be printed until the register_listener()
3691.197.19 by Francis J. Lacoste
Added a show_person argument
94
    method is called.
3691.27.2 by Guilherme Salgado
Refactor some karma tests and move some stuff from person.txt to person-karma.txt
95
96
    Each time Karma is assigned to a Person, a line in the following format
97
    will be printed:
98
3691.27.8 by Guilherme Salgado
Update all Person.assignKarma() callsites to pass the product/distribution as the method wants
99
        Karma added: action=<action>, [product|distribution]=<contextname>
3691.27.2 by Guilherme Salgado
Refactor some karma tests and move some stuff from person.txt to person-karma.txt
100
3691.197.19 by Francis J. Lacoste
Added a show_person argument
101
    If show_person is set to True, the name of the person to whom karma is
102
    granted will also be shown like this (on one line):
103
104
        Karma added: action=<action>, [product|distribution]=<contextname>,
105
        person=<name>
106
3691.27.2 by Guilherme Salgado
Refactor some karma tests and move some stuff from person.txt to person-karma.txt
107
    A set of KarmaAction objects assigned since the register_listener()
108
    method was called is available in the added_listener_actions property.
109
    """
7675.939.1 by Jeroen Vermeulen
Test KarmaRecorder, and add missing filtering on person.
110
3691.197.19 by Francis J. Lacoste
Added a show_person argument
111
    def __init__(self, show_person=False):
7675.916.98 by Henning Eggers
Merged db-stable at r10026 (recife roll-back) but without accepting the changes.
112
        super(KarmaAssignedEventListener, self).__init__()
3691.27.2 by Guilherme Salgado
Refactor some karma tests and move some stuff from person.txt to person-karma.txt
113
        self.added_karma_actions = set()
3691.197.19 by Francis J. Lacoste
Added a show_person argument
114
        self.show_person = show_person
3691.27.2 by Guilherme Salgado
Refactor some karma tests and move some stuff from person.txt to person-karma.txt
115
7675.916.98 by Henning Eggers
Merged db-stable at r10026 (recife roll-back) but without accepting the changes.
116
    def record(self, karma):
117
        action = karma.action
3691.27.2 by Guilherme Salgado
Refactor some karma tests and move some stuff from person.txt to person-karma.txt
118
        self.added_karma_actions.add(action)
3691.27.8 by Guilherme Salgado
Update all Person.assignKarma() callsites to pass the product/distribution as the method wants
119
        text = "Karma added: action=%s," % action.name
7675.916.98 by Henning Eggers
Merged db-stable at r10026 (recife roll-back) but without accepting the changes.
120
        if karma.product is not None:
121
            text += " product=%s" % karma.product.name
122
        elif karma.distribution is not None:
123
            text += " distribution=%s" % karma.distribution.name
3691.197.19 by Francis J. Lacoste
Added a show_person argument
124
        if self.show_person:
7675.916.98 by Henning Eggers
Merged db-stable at r10026 (recife roll-back) but without accepting the changes.
125
            text += ", person=%s" % karma.person.name
3691.27.8 by Guilherme Salgado
Update all Person.assignKarma() callsites to pass the product/distribution as the method wants
126
        print text