~launchpad-qa/qa-shepherd/devel

7 by Maris Fogels
Added some inital objects and tests.
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
22 by Maris Fogels
Copied some launchpadlib fakes that we will need for testing.
4
"""Helpers for writing tests.
5
6
Borrowed from lp:~ursinha/lp-qa-tools/lplib-fakes
7
"""
7 by Maris Fogels
Added some inital objects and tests.
8
9
__metaclass__ = type
17 by Maris Fogels
Stripped out the now pointless object factory.
10
__all__ = []
11
12
23 by Maris Fogels
Start doing branch lookups using a fake launchpadlib.
13
import launchpadlib.uris
22 by Maris Fogels
Copied some launchpadlib fakes that we will need for testing.
14
15
16
LPLIB_ROOT = launchpadlib.uris.lookup_service_root('production')
17
18
19
class FakeBug:
20
21
    def __init__(self, bug_id):
22
        self.id = bug_id
23
        self.title = ""
24
        self.tags = []
25
        self.bug_tasks = []
26
        self.messages = []
27
        self.self_link = "%s/bugs/%s" % (LPLIB_ROOT, bug_id)
28
29
    def addBugTask(self, bug_id, project_name, status):
30
        bug_task = FakeBugTask(bug_id, project_name, status)
31
        bug_task.bug = self
32
        bug_task.addTarget(project_name)
33
        self.bug_tasks.append(bug_task)
34
35
    def newMessage(self, subject, content):
36
        self.messages.append((subject, content))
37
38
    def lp_save(self):
39
        pass
40
41
42
class FakeBugTask:
43
44
    def __init__(self, bug_id, project_name, status=u"Fix Committed"):
45
        self._bug_id = bug_id
46
        self.bug = None
47
        self._project_name = project_name
48
        self.status = status
49
        self.assignee = None
50
        self.target = None
51
        self.milestone = None
52
        self.tags = []
53
        self.self_link = "%s/%s/+bug/%s" % (LPLIB_ROOT, project_name, bug_id)
54
55
    def addTarget(self, project_name):
56
        self.target = FakeTarget(project_name)
57
58
    def transitionToAssignee(self, assignee):
59
        self.assignee = assignee
60
61
    def transitionToMilestone(self, milestone=None):
62
        self.milestone = milestone
63
64
    def transitionToStatus(self, status=None):
65
        self.status = status
66
67
68
class FakeTarget:
69
70
    def __init__(self, project_name):
71
        self.self_link = "%s/%s" % (LPLIB_ROOT, project_name)
72
73
74
class FakePeople:
75
76
    def __init__(self):
77
        self._people = {}
78
79
    def getByEmail(self, email):
80
        try:
81
            person = self._people[email]
82
            return person
83
        except:
84
            return None
85
86
87
class FakePerson:
88
89
    def __init__(self, name, display_name, email):
90
        self.name = name
91
        self.display_name = display_name
92
        self.preferred_email = email
93
        self.self_link = "%s/~%s" % (LPLIB_ROOT, name)
94
95
96
class FakeBranches:
97
98
    def __init__(self):
99
        self._branches = {}
100
23 by Maris Fogels
Start doing branch lookups using a fake launchpadlib.
101
    def getByUrl(self, *args, **kwargs):
102
        url = get_arg(kwargs, 'url')
22 by Maris Fogels
Copied some launchpadlib fakes that we will need for testing.
103
        try:
104
            branch = self._branches[url]
105
            return branch
106
        except:
107
            return None
108
109
110
class FakeBranch:
111
112
    def __init__(self, branch_nick, owner, project, linked_bugs):
113
        self.owner = owner
114
        self.project = project
115
        self.linked_bugs = linked_bugs
116
        self.properties = {}
23 by Maris Fogels
Start doing branch lookups using a fake launchpadlib.
117
        self._branch_nick = branch_nick
22 by Maris Fogels
Copied some launchpadlib fakes that we will need for testing.
118
        self.properties["branch-nick"] = branch_nick
23 by Maris Fogels
Start doing branch lookups using a fake launchpadlib.
119
        self.url = self.get_url()
120
121
122
    def get_url(self):
123
        """Return this branch's URL (not part of launchpadlib).
124
125
        Useful for testing.
126
        """
127
        return "lp:%s/%s/%s" % (
128
            self.owner.name,
129
            self.project,
130
            self._branch_nick)
22 by Maris Fogels
Copied some launchpadlib fakes that we will need for testing.
131
132
133
class FakeMilestone:
134
135
    def __init__(self, milestone_name, target, active=True):
136
        self.name = milestone_name
137
        self.target = target
138
        self.is_active = active
139
140
141
class FakeProject:
142
143
    def __init__(self, project_name, milestone=None):
144
        self.name = project_name
145
        self.milestone = milestone
146
        self.self_link = "%s/%s" % (LPLIB_ROOT, project_name)
147
148
    def getMilestone(self, name):
149
        return FakeMilestone(name, self, True)
150
151
152
class FakeLP:
153
154
    def __init__(self):
155
        self.people = FakePeople()
156
        self.bugs = {}
157
        self.branches = FakeBranches()
158
        self.projects = {}
159
160
    def createBugWithBugTask(self, bug_id, project_name, status):
161
        fake_bug = FakeBug(bug_id)
162
        fake_bug.addBugTask(bug_id, project_name, status)
163
        self.bugs[bug_id] = fake_bug
164
        return fake_bug
165
166
    def createPerson(self, name, display_name, email):
167
        fake_person = FakePerson(name, display_name, email)
168
        self.people._people[email] = fake_person
169
        return fake_person
170
171
    def createBranch(self, branch_nick, owner, project, linked_bugs):
172
        fake_branch = FakeBranch(branch_nick, owner, project, linked_bugs)
173
        self.branches._branches[fake_branch.url] = fake_branch
174
        return fake_branch
175
176
    def createProject(self, name):
177
        fake_project = FakeProject(name)
178
        self.projects[name] = fake_project
23 by Maris Fogels
Start doing branch lookups using a fake launchpadlib.
179
        return fake_project
180
181
182
def get_arg(kwargs, param_name):
183
    """A helper to pull a specific paramter from a keyword argument dict.
184
185
    We use this to simulate launchpadlib's requirement that operations
186
    must have a named keyword argument when you make a call like
187
    launchpad.getFoo(bar='baz')
188
    """
189
    try:
190
        return kwargs.pop(param_name)
191
    except KeyError:
192
        raise TypeError(
193
            "Method must be called with keyword arg '%s'." % param_name)