| 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 ReviewCommentModel, ReviewFileModel, PeerReviewModelProvider |
|---|
| 29 | from codereview.tests.util import prepare_comments, prepare_file_data |
|---|
| 30 | from trac.test import EnvironmentStub, Mock, MockPerm |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | class TestReviewCommentModel(unittest.TestCase): |
|---|
| 34 | |
|---|
| 35 | def setUp(self): |
|---|
| 36 | self.env = EnvironmentStub(default_data=True, enable=['trac.*', |
|---|
| 37 | 'codereview.model.*', |
|---|
| 38 | 'codereview.peerreviewnew.*', |
|---|
| 39 | 'codereview.peerreviewmain.*', |
|---|
| 40 | 'codereview.tracgenericclass.*']) |
|---|
| 41 | PeerReviewModelProvider(self.env).environment_created() |
|---|
| 42 | self.req = Mock(href=Mock(), perm=MockPerm(), args={}, authname="Tester") |
|---|
| 43 | prepare_file_data(self.env) |
|---|
| 44 | prepare_comments(self.env) |
|---|
| 45 | |
|---|
| 46 | def tearDown(self): |
|---|
| 47 | self.env.shutdown() |
|---|
| 48 | |
|---|
| 49 | def test_comments_by_file_id(self): |
|---|
| 50 | # this is a dict, key: file_id as int, val: list of comment ids |
|---|
| 51 | comments = ReviewCommentModel.comment_ids_by_file_id(self.env) |
|---|
| 52 | self.assertEqual(3, len(comments)) |
|---|
| 53 | self.assertEqual(2, len(comments[1])) |
|---|
| 54 | self.assertEqual(3, len(comments[2])) |
|---|
| 55 | self.assertEqual(2, len(comments[3])) |
|---|
| 56 | |
|---|
| 57 | def test_select_by_file_id_no_file(self): |
|---|
| 58 | comments = list(ReviewCommentModel.select_by_file_id(self.env, 100)) |
|---|
| 59 | self.assertEqual(0, len(comments)) |
|---|
| 60 | |
|---|
| 61 | def test_select_by_file_id_parent_comments(self): |
|---|
| 62 | """Test with comment and additional comment tree""" |
|---|
| 63 | # Note that this file has three comments. Two of them are part of a comment tree on line 13 |
|---|
| 64 | comments = list(ReviewCommentModel.select_by_file_id(self.env, 2)) |
|---|
| 65 | self.assertEqual(3, len(comments)) |
|---|
| 66 | |
|---|
| 67 | lines = [c['line_num'] for c in comments] |
|---|
| 68 | self.assertEqual(3, len(lines)) |
|---|
| 69 | # There're two comments on line 13 |
|---|
| 70 | res = {13: 0, 12: 0} |
|---|
| 71 | for line in lines: |
|---|
| 72 | res[line] += 1 |
|---|
| 73 | self.assertEqual(2, res[13]) |
|---|
| 74 | self.assertEqual(1, res[12]) |
|---|
| 75 | |
|---|
| 76 | def test_select_by_file_id_comments(self): |
|---|
| 77 | """Test with two comments on different lines.""" |
|---|
| 78 | # Note that this file has three comments. Two of them are part of a comment tree on line 13 |
|---|
| 79 | comments = list(ReviewCommentModel.select_by_file_id(self.env, 3)) |
|---|
| 80 | self.assertEqual(2, len(comments)) |
|---|
| 81 | |
|---|
| 82 | lines = [c['line_num'] for c in comments] |
|---|
| 83 | self.assertEqual(2, len(lines)) |
|---|
| 84 | res = {15: 0, 16: 0} |
|---|
| 85 | for line in lines: |
|---|
| 86 | res[line] += 1 |
|---|
| 87 | self.assertEqual(1, res[15]) |
|---|
| 88 | self.assertEqual(1, res[16]) |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | def test_suite(): |
|---|
| 92 | suite = unittest.TestSuite() |
|---|
| 93 | suite.addTest(unittest.makeSuite(TestReviewCommentModel)) |
|---|
| 94 | return suite |
|---|