| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # Copyright (c) 2019 Cinc |
|---|
| 3 | # All rights reserved. |
|---|
| 4 | # |
|---|
| 5 | # Redistribution and use in source and binary forms, with or without |
|---|
| 6 | # modification, are permitted provided that the following conditions |
|---|
| 7 | # are met: |
|---|
| 8 | # 1. Redistributions of source code must retain the above copyright |
|---|
| 9 | # notice, this list of conditions and the following disclaimer. |
|---|
| 10 | # 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 11 | # notice, this list of conditions and the following disclaimer in the |
|---|
| 12 | # documentation and/or other materials provided with the distribution. |
|---|
| 13 | # 3. The name of the author may not be used to endorse or promote products |
|---|
| 14 | # derived from this software without specific prior written permission. |
|---|
| 15 | # |
|---|
| 16 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 17 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|---|
| 18 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|---|
| 19 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 20 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|---|
| 21 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|---|
| 25 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 26 | |
|---|
| 27 | import unittest |
|---|
| 28 | from codereview.model import PeerReviewModelProvider |
|---|
| 29 | from codereview.peerReviewPerform import CommentAnnotator |
|---|
| 30 | from codereview.tests.util import prepare_comments, prepare_file_data, prepare_review_data |
|---|
| 31 | from trac.resource import Resource |
|---|
| 32 | from trac.test import EnvironmentStub, Mock, MockPerm, MockRequest |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | class TestCommentAnnotator(unittest.TestCase): |
|---|
| 36 | |
|---|
| 37 | def setUp(self): |
|---|
| 38 | self.env = EnvironmentStub(default_data=True, enable=['trac.*', |
|---|
| 39 | 'codereview.model.*', |
|---|
| 40 | 'codereview.peerreviewnew.*', |
|---|
| 41 | 'codereview.peerreviewmain.*', |
|---|
| 42 | 'codereview.tracgenericclass.*']) |
|---|
| 43 | PeerReviewModelProvider(self.env).environment_created() |
|---|
| 44 | self.req = Mock(href=Mock(), perm=MockPerm(), args={}, authname="Tester") |
|---|
| 45 | prepare_file_data(self.env) |
|---|
| 46 | prepare_review_data(self.env) |
|---|
| 47 | prepare_comments(self.env) |
|---|
| 48 | |
|---|
| 49 | def tearDown(self): |
|---|
| 50 | self.env.shutdown() |
|---|
| 51 | |
|---|
| 52 | def test_prep_peer(self): |
|---|
| 53 | # Note that this isn't testing all the permission or locked state variations yet. |
|---|
| 54 | # Because of the Mock* objects quite a few checks end with 'allowed' |
|---|
| 55 | # The method prep_peer() is indirectly tested by creating the CommentAnnotator object |
|---|
| 56 | # Note that this file has three comments. Two of them are part of a comment tree on line 13 |
|---|
| 57 | resource = Resource('peerreviewfile', 2) |
|---|
| 58 | context = Mock(req=MockRequest(self.env), resource=resource) |
|---|
| 59 | annotator = CommentAnnotator(self.env, context, 'path/file.png') |
|---|
| 60 | data = annotator.data |
|---|
| 61 | # annotator.data: [list of comment ids, PeerReviewModel, locked] |
|---|
| 62 | # The review is the one this file is associated with |
|---|
| 63 | self.assertEqual(3, len(data)) |
|---|
| 64 | self.assertEqual(1, data[1]['review_id']) |
|---|
| 65 | self.assertFalse(data[2]) |
|---|
| 66 | # Now check for correct comments |
|---|
| 67 | self.assertEqual(3, len(data[0])) # number of comment lines |
|---|
| 68 | # There're two comments on line 13 |
|---|
| 69 | res = {13: 0, 12: 0} |
|---|
| 70 | for line in data[0]: |
|---|
| 71 | res[line] += 1 |
|---|
| 72 | self.assertEqual(2, res[13]) |
|---|
| 73 | self.assertEqual(1, res[12]) |
|---|
| 74 | |
|---|
| 75 | # File for a locked review (closed) |
|---|
| 76 | # Note that the finishing states are defined in one of the loaded plugins |
|---|
| 77 | resource = Resource('peerreviewfile', 3) |
|---|
| 78 | context = Mock(req=MockRequest(self.env), resource=resource) |
|---|
| 79 | annotator = CommentAnnotator(self.env, context, 'path/file.png') |
|---|
| 80 | data = annotator.data |
|---|
| 81 | # annotator.data: [list of comment ids, PeerReviewModel, locked] |
|---|
| 82 | # The review is the one this file is associated with |
|---|
| 83 | self.assertEqual(3, len(annotator.data)) |
|---|
| 84 | self.assertEqual(2, annotator.data[1]['review_id']) |
|---|
| 85 | self.assertTrue(annotator.data[2]) |
|---|
| 86 | # Check comments |
|---|
| 87 | self.assertEqual(2, len(annotator.data[0])) # number of comments |
|---|
| 88 | res = {15: 0, 16: 0} |
|---|
| 89 | for line in data[0]: |
|---|
| 90 | res[line] += 1 |
|---|
| 91 | self.assertEqual(1, res[15]) |
|---|
| 92 | self.assertEqual(1, res[16]) |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | def test_suite(): |
|---|
| 96 | suite = unittest.TestSuite() |
|---|
| 97 | suite.addTest(unittest.makeSuite(TestCommentAnnotator)) |
|---|
| 98 | return suite |
|---|