720
class TestMergeProposalGetPreviewDiff(TestCaseWithFactory):
721
"""Tests for `BranchMergeProposal.getPreviewDiff`."""
723
layer = LaunchpadFunctionalLayer
726
TestCaseWithFactory.setUp(self, user='foo.bar@canonical.com')
727
self.mp_one = self.factory.makeBranchMergeProposal()
728
self.mp_two = self.factory.makeBranchMergeProposal()
729
self.preview_diff = self.mp_one.updatePreviewDiff(
730
'Some diff', u"source_id", u"target_id")
733
def test_getPreviewDiff(self):
734
"""We can get a preview-diff."""
737
self.mp_one.getPreviewDiff(self.preview_diff.id))
739
def test_getPreviewDiff_NotFound(self):
740
"""DiffNotFound is raised if a PreviewDiff cannot be found."""
742
DiffNotFound, self.mp_one.getPreviewDiff, 1000)
744
def test_getPreviewDiffWrongBranchMergeProposal(self):
745
"""An error is raised if the given id does not match the MP."""
747
WrongBranchMergeProposal,
748
self.mp_two.getPreviewDiff, self.preview_diff.id)
720
751
class TestMergeProposalNotification(TestCaseWithFactory):
721
752
"""Test that events are created when merge proposals are manipulated"""
2059
2090
merge_proposal=self.bmp)
2060
2091
login_person(self.person)
2062
def getInlineComments(self, diff_timestamp=None):
2093
def getInlineComments(self, previewdiff_id=None):
2063
2094
# Return all published inline comments for the context BMP
2064
if diff_timestamp is None:
2065
diff_timestamp = self.previewdiff.date_created
2067
return self.bmp.getInlineComments(diff_timestamp)
2069
def getDraft(self, diff_timestamp=None, person=None):
2095
if previewdiff_id is None:
2096
previewdiff_id = self.previewdiff.id
2098
return self.bmp.getInlineComments(previewdiff_id)
2100
def getDraft(self, previewdiff_id=None, person=None):
2070
2101
# Return all draft inline comments for the context BMP
2071
if diff_timestamp is None:
2072
diff_timestamp = self.previewdiff.date_created
2102
if previewdiff_id is None:
2103
previewdiff_id = self.previewdiff.id
2073
2104
if person is None:
2074
2105
person = self.person
2076
2107
return self.bmp.getDraftInlineComments(
2077
diff_timestamp, person)
2108
previewdiff_id, person)
2080
2111
class TestBranchMergeProposalInlineCommentsDisabled(
2086
2117
# 'saveDraftInlineComment' does not record draft inline comments
2087
2118
# if the corresponding feature-flag is not set.
2088
2119
self.bmp.saveDraftInlineComment(
2089
diff_timestamp=self.previewdiff.date_created,
2120
previewdiff_id=self.previewdiff.id,
2090
2121
person=self.person,
2091
2122
comments={'10': 'No game'})
2092
2123
self.assertIsNone(self.getDraft())
2098
2129
self.bmp.createComment(
2099
2130
owner=self.bmp.registrant,
2100
2131
subject='Testing!',
2101
diff_timestamp=self.previewdiff.date_created,
2132
previewdiff_id=self.previewdiff.id,
2102
2133
inline_comments={'11': 'foo'},
2104
2135
self.assertEqual(0, len(self.getInlineComments()))
2122
2153
# `IBranchMergeProposal.saveDraftInlineComment`.
2123
2154
# see `ICoreReviewInlineCommentSet.ensureDraft` for details.
2124
2155
self.bmp.saveDraftInlineComment(
2125
diff_timestamp=self.previewdiff.date_created,
2156
previewdiff_id=self.previewdiff.id,
2126
2157
person=self.person,
2127
2158
comments={'10': 'DrAfT', '15': 'CoMmEnTs'})
2128
2159
self.assertEqual(2, len(self.getDraft()))
2134
2165
'person': self.person,
2135
2166
'comments': {'10': 'No diff'},
2136
'diff_timestamp': datetime(2001, 1, 1, 12, tzinfo=UTC),
2167
'previewdiff_id': 1000,
2138
2169
self.assertRaises(
2139
2170
DiffNotFound, self.bmp.saveDraftInlineComment, **kwargs)
2145
2176
self.bmp.createComment(
2146
2177
owner=self.bmp.registrant,
2147
2178
subject='Testing!',
2148
diff_timestamp=self.previewdiff.date_created,
2179
previewdiff_id=self.previewdiff.id,
2149
2180
inline_comments={'11': 'foo'},
2151
2182
self.assertEqual(1, len(self.getInlineComments()))
2157
2188
self.bmp.createComment(
2158
2189
owner=self.bmp.registrant,
2159
2190
subject='Testing!',
2160
diff_timestamp=self.previewdiff.date_created,
2191
previewdiff_id=self.previewdiff.id,
2161
2192
inline_comments=None,
2163
2194
self.assertEqual(0, len(self.getInlineComments()))
2169
2200
self.bmp.createComment(
2170
2201
owner=self.bmp.registrant,
2171
2202
subject='Testing!',
2172
diff_timestamp=self.previewdiff.date_created,
2203
previewdiff_id=self.previewdiff.id,
2173
2204
inline_comments={},
2175
2206
self.assertEqual(0, len(self.getInlineComments()))
2176
2207
self.assertEqual(1, self.bmp.all_comments.count())
2178
def test_publish_no_diff_timestamp(self):
2179
# If previewdiff creation timestamp is not given and there are
2209
def test_publish_no_previewdiff_id(self):
2210
# If previewdiff ID is not given and there are
2180
2211
# inline comments to publish, an `AssertioError` is raised
2182
2213
'owner': self.bmp.registrant,
2183
2214
'subject': 'Testing!',
2184
'diff_timestamp': None,
2215
'previewdiff_id': None,
2185
2216
'inline_comments': {'10': 'foo'},
2187
2218
self.assertRaises(
2188
2219
AssertionError, self.bmp.createComment, **kwargs)
2190
2221
def test_publish_no_diff_found(self):
2191
# If previewdiff creation timestamp does not correspond to
2222
# If previewdiff ID does not correspond to
2192
2223
# a context `PreviewDiff`, an `DiffNotFound` is raised.
2194
2225
'owner': self.bmp.registrant,
2195
2226
'subject': 'Testing!',
2196
'diff_timestamp': datetime(2001, 1, 1, 12, tzinfo=UTC),
2227
'previewdiff_id': 1000,
2197
2228
'inline_comments': {'10': 'foo'},
2199
2230
self.assertRaises(
2205
2236
comment = self.bmp.createComment(
2206
2237
owner=self.bmp.registrant,
2207
2238
subject='Testing!',
2208
diff_timestamp=self.previewdiff.date_created,
2239
previewdiff_id=self.previewdiff.id,
2209
2240
inline_comments={'11': 'eleven'},
2212
2243
published_comments = self.bmp.getInlineComments(
2213
self.previewdiff.date_created)
2244
self.previewdiff.id)
2214
2245
self.assertEqual(1, len(published_comments))
2215
2246
[published_comment] = published_comments
2225
2256
# Draft inline comments for an specific `PreviewDiff` and
2226
2257
# `IPerson` (author) can be retrieved via `getDraftInlineComments`.
2227
2258
self.bmp.saveDraftInlineComment(
2228
diff_timestamp=self.previewdiff.date_created,
2259
previewdiff_id=self.previewdiff.id,
2229
2260
person=self.person,
2230
2261
comments={'10': 'ten'})
2232
2263
draft_comments = self.bmp.getDraftInlineComments(
2233
self.previewdiff.date_created, self.person)
2264
self.previewdiff.id, self.person)
2235
2266
# A 'draft' inline comment is represented by a dictionary (object)
2236
2267
# with keyed by line numbers (as text) and the corresponding
2240
2271
def test_get_draft_different_users(self):
2241
2272
# Different users have different draft comments.
2242
2273
self.bmp.saveDraftInlineComment(
2243
diff_timestamp=self.previewdiff.date_created,
2274
previewdiff_id=self.previewdiff.id,
2244
2275
person=self.person,
2245
2276
comments={'1': 'zoing!'})
2247
2278
someone_else = self.factory.makePerson()
2248
2279
self.bmp.saveDraftInlineComment(
2249
diff_timestamp=self.previewdiff.date_created,
2280
previewdiff_id=self.previewdiff.id,
2250
2281
person=someone_else,
2251
2282
comments={'1': 'boing!'})
2256
2287
def test_get_diff_not_found(self):
2257
2288
# Trying to fetch inline comments (draft or published) with a
2258
# diff_timestamp that does not correspond to a context
2259
# `PreviewDiff` creation date raises `DiffNotFound`.
2289
# ID that does not correspond to a context `PreviewDiff`
2290
# raises `DiffNotFound`.
2260
2291
self.assertRaises(
2261
2292
DiffNotFound, self.bmp.getInlineComments,
2262
datetime(2001, 1, 1, 12, tzinfo=UTC))
2264
2295
self.assertRaises(
2265
2296
DiffNotFound, self.bmp.getDraftInlineComments,
2266
datetime(2001, 1, 1, 12, tzinfo=UTC), self.person)
2269
2300
class TestWebservice(WebServiceTestCase):
2329
2360
self.assertRaises(
2330
2361
BadRequest, ws_bmp.saveDraftInlineComment,
2331
diff_timestamp=datetime(2001, 1, 1, 12, tzinfo=UTC), comments={})
2362
previewdiff_id=1000, comments={})
2333
2364
def test_saveDraftInlineComment(self):
2334
2365
# Creating and retrieving draft inline comments.
2342
2373
ws_bmp = self.wsObject(previewdiff.branch_merge_proposal, user=user)
2344
2375
ws_bmp.saveDraftInlineComment(
2345
diff_timestamp=previewdiff.date_created,
2376
previewdiff_id=previewdiff.id,
2346
2377
comments={'2': 'foo'})
2347
2378
transaction.commit()
2349
2380
draft_comments = ws_bmp.getDraftInlineComments(
2350
diff_timestamp=previewdiff.date_created)
2381
previewdiff_id=previewdiff.id)
2351
2382
self.assertEqual({'2': 'foo'}, draft_comments)
2353
2384
def test_getInlineComment(self):
2364
2395
review_comment = ws_bmp.createComment(
2365
2396
subject='Testing!',
2366
diff_timestamp=previewdiff.date_created,
2397
previewdiff_id=previewdiff.id,
2367
2398
inline_comments={u'2': u'foo'})
2368
2399
transaction.commit()
2370
2401
inline_comments = ws_bmp.getInlineComments(
2371
diff_timestamp=previewdiff.date_created)
2402
previewdiff_id=previewdiff.id)
2373
2404
self.assertEqual(1, len(inline_comments))
2374
2405
[inline_comment] = inline_comments