def create_diff_comment(self, review, filediff, interfilediff=None, text='My comment', issue_opened=False, first_line=1, num_lines=5, extra_fields=None, reply_to=None, **kwargs): """Creates a Comment for testing. The comment is tied to the given Review and FileDiff (and, optionally, an interfilediff). It's populated with default data that can be overridden by the caller. """ if issue_opened: issue_status = Comment.OPEN else: issue_status = None comment = Comment( filediff=filediff, interfilediff=interfilediff, first_line=first_line, num_lines=num_lines, text=text, issue_opened=issue_opened, issue_status=issue_status, reply_to=reply_to, **kwargs) if extra_fields: comment.extra_data = extra_fields comment.save() review.comments.add(comment) return comment
def create_diff_comment(self, review, filediff, interfilediff=None, text='My comment', issue_opened=False, issue_status=None, first_line=1, num_lines=5, extra_fields=None, reply_to=None, **kwargs): """Creates a Comment for testing. The comment is tied to the given Review and FileDiff (and, optionally, an interfilediff). It's populated with default data that can be overridden by the caller. """ if issue_opened and not issue_status: issue_status = Comment.OPEN comment = Comment( filediff=filediff, interfilediff=interfilediff, first_line=first_line, num_lines=num_lines, text=text, issue_opened=issue_opened, issue_status=issue_status, reply_to=reply_to, **kwargs) if extra_fields: comment.extra_data = extra_fields comment.save() review.comments.add(comment) return comment
def test_spanning_last_chunk(self): """Testing diff_comment_line_numbers with spanning chunks through last chunk """ t = Template('{% load reviewtags %}' '{% diff_comment_line_numbers chunks comment %}') result = t.render( Context({ 'comment': Comment(first_line=20, num_lines=50), 'chunks': [ { 'change': 'delete', 'lines': [ (10, 20, 'deleted line', [], '', '', [], False), # ... (50, 60, 'deleted line', [], '', '', [], False), ], }, { 'change': 'insert', 'lines': [ (51, '', '', [], 61, 'inserted line', [], False), # ... (100, '', '', [], 110, 'inserted line', [], False), ], }, ], })) self.assertEqual(result, 'Lines 30-60 (original), 61-79 (patched)')
def test_fake_equal_patched(self): """Testing diff_comment_line_numbers with fake equal from patched side of interdiff """ t = Template('{% load reviewtags %}' '{% diff_comment_line_numbers chunks comment %}') result = t.render( Context({ 'comment': Comment(first_line=20, num_lines=2), 'chunks': [ { 'change': 'equal', 'lines': [ (10, 20, 'deleted line', [], '', '', [], False), # ... (50, 60, 'deleted line', [], '', '', [], False), ], }, ], })) self.assertEqual(result, 'Lines 30-31 (original)')
def test_insert_multiple_lines(self): """Testing diff_comment_line_numbers with insert chunk and multiple commented lines """ t = Template('{% load reviewtags %}' '{% diff_comment_line_numbers chunks comment %}') result = t.render( Context({ 'comment': Comment(first_line=20, num_lines=2), 'chunks': [ { 'change': 'insert', 'lines': [ (10, '', '', [], 20, 'inserted line', [], False), # ... (50, '', '', [], 60, 'inserted line', [], False), ], }, ], })) self.assertEqual(result, 'Lines 30-31 (patched)')
def test_replace_single_line(self): """Testing diff_comment_line_numbers with replace chunk and single commented line """ t = Template('{% load reviewtags %}' '{% diff_comment_line_numbers chunks comment %}') result = t.render( Context({ 'comment': Comment(first_line=20, num_lines=1), 'chunks': [ { 'change': 'replace', 'lines': [ (10, 20, 'foo', [], 20, 'replaced line', [], False), # ... (50, 60, 'foo', [], 60, 'replaced line', [], False ), ], }, ], })) self.assertEqual(result, 'Line 30 (original), 30 (patched)')
def review_reply_draft(request, review_request_id, review_id): source_review = _get_and_validate_review(request, review_request_id, review_id) if isinstance(source_review, WebAPIResponseError): return source_review context_type = request.POST['type'] value = request.POST['value'] reply, reply_is_new = Review.objects.get_or_create( review_request=source_review.review_request, user=request.user, public=False, base_reply_to=source_review) result = {} if context_type == "comment": context_id = request.POST['id'] context_comment = Comment.objects.get(pk=context_id) try: comment = Comment.objects.get(review=reply, reply_to=context_comment) comment_is_new = False except Comment.DoesNotExist: comment = Comment(reply_to=context_comment, filediff=context_comment.filediff, interfilediff=context_comment.interfilediff, first_line=context_comment.first_line, num_lines=context_comment.num_lines) comment_is_new = True comment.text = value comment.timestamp = datetime.now() if value == "" and not comment_is_new: comment.delete() else: comment.save() result['comment'] = comment if comment_is_new: reply.comments.add(comment) elif context_type == "screenshot_comment": context_id = request.POST['id'] context_comment = ScreenshotComment.objects.get(pk=context_id) try: comment = ScreenshotComment.objects.get(review=reply, reply_to=context_comment) comment_is_new = False except ScreenshotComment.DoesNotExist: comment = ScreenshotComment(reply_to=context_comment, screenshot=context_comment.screenshot, x=context_comment.x, y=context_comment.y, w=context_comment.w, h=context_comment.h) comment_is_new = True comment.text = value comment.timestamp = datetime.now() if value == "" and not comment_is_new: comment.delete() else: comment.save() result['screenshot_comment'] = comment if comment_is_new: reply.screenshot_comments.add(comment) elif context_type == "body_top": reply.body_top = value if value == "": reply.body_top_reply_to = None else: reply.body_top_reply_to = source_review elif context_type == "body_bottom": reply.body_bottom = value if value == "": reply.body_bottom_reply_to = None else: reply.body_bottom_reply_to = source_review else: raise HttpResponseForbidden() if reply.body_top == "" and reply.body_bottom == "" and \ reply.comments.count() == 0 and reply.screenshot_comments.count() == 0: reply.delete() result['reply'] = None result['discarded'] = True else: reply.save() result['reply'] = reply return WebAPIResponse(request, result)
def create_diff_comment(self, review, filediff, interfilediff=None, text='My comment', issue_opened=False, issue_status=None, first_line=1, num_lines=5, extra_fields=None, reply_to=None, **kwargs): """Create a Comment for testing. The comment is tied to the given Review and FileDiff (and, optionally, an interfilediff). It's populated with default data that can be overridden by the caller. Args: review (reviewboard.reviews.models.review.Review): The review associated with the comment. filediff (reviewboard.diffviewer.models.FileDiff): The FileDiff associated with the comment. interfilediff (reviewboard.diffviewer.models.FileDiff, optional): The FileDiff used for the end of an interdiff range associated with the comment. text (unicode): The text for the comment. issue_opened (bool, optional): Whether an issue is to be opened for the comment. issue_status (unicode, optional): The issue status to set, if an issue is opened. Defaults to being an open issue. first_line (int, optional): The first line (0-based) of the comment range. num_lines (int, optional): The number of lines in the comment. extra_fields (dict, optional): Extra data to set on the comment. reply_to (reviewboard.reviews.models.diff_comment.Comment, optional): The comment this comment replies to. **kwargs (dict): Additional model attributes to set on the comment. Returns: reviewboard.reviews.models.diff_comment.Comment: The resulting comment. """ if issue_opened and not issue_status: issue_status = Comment.OPEN comment = Comment(filediff=filediff, interfilediff=interfilediff, first_line=first_line, num_lines=num_lines, text=text, issue_opened=issue_opened, issue_status=issue_status, reply_to=reply_to, **kwargs) if extra_fields: comment.extra_data = extra_fields comment.save() review.comments.add(comment) return comment
def create_diff_comment(self, review, filediff, interfilediff=None, text='My comment', issue_opened=False, issue_status=None, first_line=1, num_lines=5, extra_fields=None, reply_to=None, **kwargs): """Create a Comment for testing. The comment is tied to the given Review and FileDiff (and, optionally, an interfilediff). It's populated with default data that can be overridden by the caller. Args: review (reviewboard.reviews.models.review.Review): The review associated with the comment. filediff (reviewboard.diffviewer.models.FileDiff): The FileDiff associated with the comment. interfilediff (reviewboard.diffviewer.models.FileDiff, optional): The FileDiff used for the end of an interdiff range associated with the comment. text (unicode): The text for the comment. issue_opened (bool, optional): Whether an issue is to be opened for the comment. issue_status (unicode, optional): The issue status to set, if an issue is opened. Defaults to being an open issue. first_line (int, optional): The first line (0-based) of the comment range. num_lines (int, optional): The number of lines in the comment. extra_fields (dict, optional): Extra data to set on the comment. reply_to (reviewboard.reviews.models.diff_comment.Comment, optional): The comment this comment replies to. **kwargs (dict): Additional model attributes to set on the comment. Returns: reviewboard.reviews.models.diff_comment.Comment: The resulting comment. """ if issue_opened and not issue_status: issue_status = Comment.OPEN comment = Comment( filediff=filediff, interfilediff=interfilediff, first_line=first_line, num_lines=num_lines, text=text, issue_opened=issue_opened, issue_status=issue_status, reply_to=reply_to, **kwargs) if extra_fields: comment.extra_data = extra_fields comment.save() review.comments.add(comment) return comment