~bjornt/lp2kanban/no-sync-lanes

« back to all changes in this revision

Viewing changes to src/lp2kanban/tests/test_cards2workitems.py

[r=benji] It's now possible to store JSON-encoded annotations in a card's description.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
    convert_cards_to_work_items,
12
12
    update_blueprints_from_work_items,
13
13
    )
14
 
from lp2kanban.kanban import Record
 
14
from lp2kanban.kanban import (
 
15
    Record,
 
16
    )
15
17
from lp2kanban.tests.common import (
16
18
    FauxBoard,
17
19
    FauxCard,
51
53
            "String \"{string}\" did not match pattern \"{pattern}\"".format(
52
54
                string=string, pattern=pattern))
53
55
 
54
 
    def _generateCard(self, board=None, lane=None, external_system_url=None,
55
 
                      assigned_user_id=0, title=None):
 
56
    def _generateCard(self, board=None, lane=None,
 
57
                     description_annotations=None, assigned_user_id=0,
 
58
                     title=None, external_system_url=None):
56
59
        if board is None:
57
60
            board = self.board
58
61
        if lane is None:
59
62
            lane = self.default_lane
60
 
        if external_system_url is None:
61
 
            external_system_url = self.blueprint_url
 
63
        if description_annotations is None:
 
64
            description_annotations = Record(
 
65
                blueprint_url=self.blueprint_url)
62
66
        if title is None:
63
67
            title = "A card"
64
68
        return FauxCard(
65
69
            title=title, lane=lane, assigned_user_id=assigned_user_id,
 
70
            description_annotations=description_annotations,
66
71
            external_system_url=external_system_url)
67
72
 
68
73
 
84
89
        # Each item returned by convert_cards_to_work_items() is of the
85
90
        # correct work item format.
86
91
        cards = [
87
 
            FauxCard(
88
 
                title="Test card 1", lane=FauxLane(type=1),
89
 
                external_system_url=self.blueprint_url),
90
 
            FauxCard(
 
92
            self._generateCard(
 
93
                title="Test card 1", lane=FauxLane(type=1)),
 
94
            self._generateCard(
91
95
                title="Test card 2",
92
96
                assigned_user_id=self.user_1.id,
93
 
                lane=FauxLane(type=2),
94
 
                external_system_url=self.blueprint_url),
95
 
            FauxCard(
96
 
                title="Test card 3", lane=FauxLane(type=3),
97
 
                external_system_url=self.blueprint_url),
 
97
                lane=FauxLane(type=2)),
 
98
            self._generateCard(
 
99
                title="Test card 3", lane=FauxLane(type=3)),
98
100
            ]
99
101
        work_items = convert_cards_to_work_items(
100
102
            cards, self.board, self.lp_users_for_board)
106
108
        # included in the set() of work items returned by
107
109
        # convert_cards_to_work_items()
108
110
        cards = [self._generateCard(title="Card %i" % id) for id in range(3)]
109
 
        cards.append(self._generateCard(external_system_url="http://nyan.cat"))
 
111
        cards.append(self._generateCard(
 
112
            description_annotations=Record(blueprint_url='http://nyan.cat')))
110
113
        work_items = convert_cards_to_work_items(
111
114
            cards, self.board, self.lp_users_for_board)
112
115
        self.assertEqual(len(cards) - 1, len(work_items))
170
173
            card, self.board, self.lp_users_for_board)
171
174
        self._assertMatchesWorkItemFormat(work_item.text)
172
175
 
173
 
    def test_blueprint_extracted_from_card_link(self):
 
176
    def test_blueprint_extracted_from_card_description_annotations(self):
174
177
        # The blueprint onto which to sync a card's work item can be
175
 
        # found by inspecting the card's external link. This is returned
176
 
        # as work_item.blueprint.
 
178
        # found by inspecting the card's description annotations. This
 
179
        # is returned as work_item.blueprint_url.
177
180
        card = self._generateCard()
178
181
        work_item = convert_card_to_work_item(
179
182
            card, self.board, self.lp_users_for_board)
180
 
        self.assertEqual(card.external_system_url, work_item.blueprint_url)
 
183
        self.assertEqual(
 
184
            card.description_annotations.blueprint_url, work_item.blueprint_url)
 
185
 
 
186
    def test_blueprint_extracted_from_card_external_link(self):
 
187
        # Cards can also be linked to blueprints by putting the
 
188
        # blueprint url in the card's external_system_url property
 
189
        # (via the web UI). If convert_card_to_work_item() encounters
 
190
        # this it will also store the blueprint URL in the card's
 
191
        # description annotations so that it's less likely to be
 
192
        # overwritten.
 
193
        card = self._generateCard(
 
194
            external_system_url=self.blueprint_url,
 
195
            description_annotations=Record(),
 
196
            )
 
197
        work_item = convert_card_to_work_item(
 
198
            card, self.board, self.lp_users_for_board)
 
199
        self.assertEqual(
 
200
            self.blueprint_url, card.description_annotations.blueprint_url)
181
201
 
182
202
    def test_non_blueprint_links_ignored(self):
183
203
        # If the card has some other external link attached to it that
184
204
        # isn't a blueprint, convert_card_to_work_item() will return
185
205
        # None.
186
206
        card = self._generateCard(
187
 
            external_system_url="https://nyancat.com")
 
207
            description_annotations=Record
 
208
                (blueprint_url= 'https://nyancat.com'))
188
209
        work_item = convert_card_to_work_item(
189
210
            card, self.board, self.lp_users_for_board)
190
211
        self.assertIsNone(work_item)
202
223
 
203
224
    def setUp(self):
204
225
        super(UpdateBluePrintsFromWorkItemsTestCase, self).setUp()
 
226
        blueprint_url_tpl = (
 
227
            "https://blueprints.launchpad.net/foo/+spec/bar%i")
205
228
        self.cards = [
206
229
            self._generateCard(
207
230
                title="Card %i" % id,
208
 
                external_system_url=(
209
 
                    "https://blueprints.launchpad.net/foo/+spec/bar%i" % id))
210
 
            for id in range(3)]
 
231
                description_annotations=Record(
 
232
                    blueprint_url=blueprint_url_tpl % id))
 
233
                for id in range(3)]
211
234
        self.work_items = convert_cards_to_work_items(
212
235
            self.cards, self.board, self.lp_users_for_board)
213
236
        self.blueprints = {}
214
237
        for work_item in self.work_items:
215
238
            self.blueprints[work_item.blueprint_url] = FauxBlueprint(
216
 
                workitems_text='', saved=False)
 
239
                workitems_text='', saved=False, name="blueprint")
217
240
 
218
241
    def test_fetches_blueprints(self):
219
242
        # update_blueprints_from_work_items() fetches the blueprints frm
223
246
        called_with_urls = []
224
247
        def fake_fetch_blueprint(lp, url):
225
248
            called_with_urls.append(url)
226
 
            return FauxBlueprint()
 
249
            return FauxBlueprint(name="somename")
227
250
        self.addCleanup(
228
251
            patch(cards2workitems, "fetch_blueprint", fake_fetch_blueprint))
229
252
        update_blueprints_from_work_items(lp=None, work_items=self.work_items)