~launchpad-pqm/python-oops-tools/trunk

« back to all changes in this revision

Viewing changes to src/oopstools/oops/test/fakes.py

  • Committer: Robert Collins
  • Date: 2011-10-13 20:18:51 UTC
  • Revision ID: robertc@robertcollins.net-20111013201851-ym8jmdhoeol3p83s
Export of cruft-deleted tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2005-2011 Canonical Ltd.  All rights reserved.
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Affero General Public License as published by
 
5
# the Free Software Foundation, either version 3 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Affero General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Affero General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
import tarfile
 
17
import tempfile
 
18
import os
 
19
 
 
20
from bzrlib.branch import Branch
 
21
 
 
22
LPLIB_ROOT="https://api.edge.launchpad.net/beta"
 
23
 
 
24
 
 
25
class SampleBranch:
 
26
    """Convenience class to untar the sample data branch."""
 
27
 
 
28
    # XXX: fix this hardcoded path
 
29
    TEST_DATA = "tests/sample_data/branch_data.tar.bz2"
 
30
 
 
31
    def __init__(self):
 
32
        tar_file = tarfile.open(self.TEST_DATA)
 
33
        tmp_dir = tempfile.mkdtemp()
 
34
        tar_file.extractall(tmp_dir)
 
35
        self.path = tmp_dir
 
36
        self.branch_name = 'devel'
 
37
        self.branch_path = os.path.join(tmp_dir, self.branch_name)
 
38
        self.branch = None
 
39
 
 
40
    def open_branch(self):
 
41
        self.branch = Branch.open(self.branch_path)
 
42
        return self.branch
 
43
 
 
44
 
 
45
class FakeBug:
 
46
 
 
47
    def __init__(self, bug_id):
 
48
        self.id = bug_id
 
49
        self.title = ""
 
50
        self.bug_tasks = []
 
51
        self.messages = []
 
52
        self.self_link = "%s/bugs/%s" % (LPLIB_ROOT, bug_id)
 
53
 
 
54
    def addBugTask(self, bug_id, target_name, status):
 
55
        bug_task = FakeBugTask(bug_id, target_name, status)
 
56
        bug_task.bug = self
 
57
        bug_task.addTarget(target_name)
 
58
        self.bug_tasks.append(bug_task)
 
59
 
 
60
    def newMessage(self, subject, content):
 
61
        self.messages.append((subject, content))
 
62
 
 
63
    def lp_save(self):
 
64
        pass
 
65
 
 
66
 
 
67
class FakeBugTask:
 
68
 
 
69
    def __init__(self, bug_id, target_name, status=u"Fix Committed"):
 
70
        self._bug_id = bug_id
 
71
        self.bug = None
 
72
        self.bug_target_name = target_name
 
73
        self.status = status
 
74
        self.assignee = None
 
75
        self.target = None
 
76
        self.milestone = None
 
77
        self.tags = []
 
78
        self.self_link = "%s/%s/+bug/%s" % (LPLIB_ROOT, target_name, bug_id)
 
79
 
 
80
    def addTarget(self, target_name):
 
81
        self.target = FakeTarget(target_name)
 
82
 
 
83
    def transitionToAssignee(self, assignee):
 
84
        self.assignee = assignee
 
85
 
 
86
    def transitionToMilestone(self, milestone):
 
87
        self.milestone = milestone
 
88
 
 
89
 
 
90
class FakeTarget:
 
91
 
 
92
    def __init__(self, target_name):
 
93
        self.self_link = "%s/%s" % (LPLIB_ROOT, target_name)
 
94
 
 
95
 
 
96
class FakePeople:
 
97
 
 
98
    def __init__(self):
 
99
        self._people = {}
 
100
 
 
101
    def getByEmail(self, email):
 
102
        try:
 
103
            person = self._people[email]
 
104
            return person
 
105
        except:
 
106
            return None
 
107
 
 
108
 
 
109
class FakePerson:
 
110
 
 
111
    def __init__(self, name, display_name, email):
 
112
        self.name = name
 
113
        self.display_name = display_name
 
114
        self.preferred_email = email
 
115
        self.self_link = "%s/~%s" % (LPLIB_ROOT, name)
 
116
 
 
117
 
 
118
class FakeBranches:
 
119
 
 
120
    def __init__(self):
 
121
        self._branches = {}
 
122
 
 
123
    def getByUrl(self, url):
 
124
        try:
 
125
            branch = self._branches[url]
 
126
            return branch
 
127
        except:
 
128
            return None
 
129
 
 
130
 
 
131
class FakeBranch:
 
132
 
 
133
    def __init__(self, branch_nick, owner, project, linked_bugs):
 
134
        self.owner = owner
 
135
        self.url = "lp:%s/%s/%s" % (owner.name, project, branch_nick)
 
136
        self.project = project
 
137
        self.linked_bugs = linked_bugs
 
138
        self.properties = {}
 
139
        self.properties["branch-nick"] = branch_nick
 
140
 
 
141
 
 
142
class FakeMilestone:
 
143
 
 
144
    def __init__(self, milestone_name, target, active=True):
 
145
        self.name = milestone_name
 
146
        self.target = target
 
147
        self.is_active = active
 
148
 
 
149
 
 
150
class FakeProject:
 
151
 
 
152
    def __init__(self, project_name, milestone=None):
 
153
        self.name = project_name
 
154
        self.milestone = milestone
 
155
        self.self_link = "%s/%s" % (LPLIB_ROOT, project_name)
 
156
 
 
157
    def getMilestone(self, name):
 
158
        return FakeMilestone(name, self, True)
 
159
 
 
160
 
 
161
class FakeLP:
 
162
 
 
163
    def __init__(self):
 
164
        self.people = FakePeople()
 
165
        self.bugs = {}
 
166
        self.branches = FakeBranches()
 
167
        self.projects = {}
 
168
 
 
169
    def createBugWithBugTask(self, bug_id, target_name, status):
 
170
        fake_bug = FakeBug(bug_id)
 
171
        fake_bug.addBugTask(bug_id, target_name, status)
 
172
        self.bugs[bug_id] = fake_bug
 
173
        return fake_bug
 
174
 
 
175
    def createPerson(self, name, display_name, email):
 
176
        fake_person = FakePerson(name, display_name, email)
 
177
        self.people._people[email] = fake_person
 
178
        return fake_person
 
179
 
 
180
    def createBranch(self, branch_nick, owner, project, linked_bugs):
 
181
        fake_branch = FakeBranch(branch_nick, owner, project, linked_bugs)
 
182
        self.branches._branches[fake_branch.url] = fake_branch
 
183
        return fake_branch
 
184
 
 
185
    def createProject(self, name):
 
186
        fake_project = FakeProject(name)
 
187
        self.projects[name] = fake_project
 
188
        return fake_project