| 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, ReviewFileModel |
|---|
| 29 | from codereview.peerReviewMain import PeerReviewMain |
|---|
| 30 | from datetime import datetime |
|---|
| 31 | from trac.resource import Resource, ResourceNotFound |
|---|
| 32 | from trac.test import EnvironmentStub |
|---|
| 33 | from trac.util.datefmt import to_datetime, to_utimestamp |
|---|
| 34 | from trac.web.href import Href |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | def _prepare_review_data(env): |
|---|
| 38 | # owner, status, created, name, notes, parent_id |
|---|
| 39 | revs = [ |
|---|
| 40 | ['Rev1', 'bar', to_utimestamp(to_datetime(datetime(2019, 2, 4))), 'name1', 'note1', 0], |
|---|
| 41 | ['Rev1', 'closed', to_utimestamp(to_datetime(datetime(2019, 3, 4))), 'name2', 'note2', 0], |
|---|
| 42 | ['Rev2', 'bar', to_utimestamp(to_datetime(datetime(2019, 3, 14))), 'name3', 'note3', 1], |
|---|
| 43 | ['Rev3', 'foo', to_utimestamp(to_datetime(datetime(2019, 4, 4))), 'name4', 'note4', 2] |
|---|
| 44 | ] |
|---|
| 45 | |
|---|
| 46 | with env.db_transaction as db: |
|---|
| 47 | cursor = db.cursor() |
|---|
| 48 | for rev in revs: |
|---|
| 49 | cursor.execute("INSERT INTO peerreview (owner, status, created, name, notes, parent_id) " |
|---|
| 50 | "VALUES (%s,%s,%s,%s,%s,%s)", rev) |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | def _prepare_file_data(env): |
|---|
| 54 | # review_id, path, start, end, revision, status |
|---|
| 55 | files = [ |
|---|
| 56 | [1, '/foo/bar', 5, 100, '1234', 'new', None, 'repo1'], |
|---|
| 57 | [1, '/foo/bar2', 6, 101, '1234', 'new', None, 'repo1'], |
|---|
| 58 | [2, '/foo/bar', 5, 100, '1234', 'closed', None, 'repo1'], |
|---|
| 59 | [2, '/foo/bar2', 6, 101, '12346', 'closed', None, 'repo1'], |
|---|
| 60 | [2, '/foo/bar3', 7, 102, '12347', 'closed', None, 'repo1'], |
|---|
| 61 | [3, '/foo/bar2', 6, 101, '1234', 'new', None, 'repo1'], |
|---|
| 62 | [4, '/foo/bar', 5, 100, '1234', 'new', None, 'repo1'], |
|---|
| 63 | [4, '/foo/bar2', 6, 101, '1234', 'new', None, 'repo1'], |
|---|
| 64 | # File list data for several projects |
|---|
| 65 | [0, '/foo/bar', 5, 100, '1234', 'new', 'PrjFoo', 'repo1'], |
|---|
| 66 | [0, '/foo/bar2', 6, 101, '1234', 'new', 'PrjFoo', 'repo1'], |
|---|
| 67 | [0, '/foo/bar', 5, 100, '1234', 'new', 'PrjBar', 'repo1'], |
|---|
| 68 | [0, '/foo/bar2', 6, 101, '12346', 'new', 'PrjBar', 'repo1'], |
|---|
| 69 | [0, '/foo/bar3', 7, 102, '12347', 'new', 'PrjFoo', 'repo1'], |
|---|
| 70 | [0, '/foo/bar/baz', 6, 101, '1234', 'new', 'PrjBar', 'repo1'], |
|---|
| 71 | [0, '/foo/bar', 5, 100, '1234', 'new', 'PrjBaz', 'repo1'], |
|---|
| 72 | [0, '/foo/bar2', 6, 101, '1234', 'new', 'PrjBaz', 'repo1'], |
|---|
| 73 | ] |
|---|
| 74 | for f in files: |
|---|
| 75 | rfm = ReviewFileModel(env) |
|---|
| 76 | rfm['review_id'] = f[0] |
|---|
| 77 | rfm['path'] = f[1] |
|---|
| 78 | rfm['line_start'] = f[2] |
|---|
| 79 | rfm['line_end'] = f[3] |
|---|
| 80 | rfm['revision'] = f[4] |
|---|
| 81 | rfm['status'] = f[5] |
|---|
| 82 | rfm['project'] = f[6] |
|---|
| 83 | rfm['repo'] = f[7] |
|---|
| 84 | rfm.insert() |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | class TestResource(unittest.TestCase): |
|---|
| 88 | |
|---|
| 89 | def setUp(self): |
|---|
| 90 | self.env = EnvironmentStub(default_data=True, enable=['trac.*', |
|---|
| 91 | 'codereview.model.*', |
|---|
| 92 | 'codereview.peerreviewnew.*', |
|---|
| 93 | 'codereview.peerreviewmain.*', |
|---|
| 94 | 'codereview.tracgenericclass.*']) |
|---|
| 95 | PeerReviewModelProvider(self.env).environment_created() |
|---|
| 96 | self.plugin = PeerReviewMain(self.env) |
|---|
| 97 | _prepare_review_data(self.env) |
|---|
| 98 | _prepare_file_data(self.env) |
|---|
| 99 | |
|---|
| 100 | def tearDown(self): |
|---|
| 101 | self.env.shutdown() |
|---|
| 102 | |
|---|
| 103 | def test_get_resourece_realms(self): |
|---|
| 104 | realms = list(self.plugin.get_resource_realms()) |
|---|
| 105 | realms = list(set(realms)) |
|---|
| 106 | self.assertEqual(2, len(realms)) |
|---|
| 107 | for item in realms: |
|---|
| 108 | self.assertIn(item, ('peerreview', 'peerreviewfile')) |
|---|
| 109 | |
|---|
| 110 | def test_get_resource_description(self): |
|---|
| 111 | resource = Resource('peerreview', 1) |
|---|
| 112 | self.assertEqual('Review 1', self.plugin.get_resource_description(resource)) |
|---|
| 113 | self.assertEqual('review:1', self.plugin.get_resource_description(resource, 'compact')) |
|---|
| 114 | resource = Resource('peerreviewfile', 1) |
|---|
| 115 | self.assertEqual('ReviewFile 1', self.plugin.get_resource_description(resource)) |
|---|
| 116 | self.assertEqual('rfile:1', self.plugin.get_resource_description(resource, 'compact')) |
|---|
| 117 | |
|---|
| 118 | def test_get_resource_description_wrong_resource(self): |
|---|
| 119 | resource = Resource('wiki', 'WikiStart') |
|---|
| 120 | self.assertEqual('', self.plugin.get_resource_description(resource)) |
|---|
| 121 | |
|---|
| 122 | def test_resource(self): |
|---|
| 123 | resource = Resource('peerreview', 1) |
|---|
| 124 | self.assertEqual("<Resource 'peerreview:1'>", str(resource).replace("u'", "'")) |
|---|
| 125 | res = Resource('peerreview', 1) |
|---|
| 126 | resource = Resource(res, 2) # This creates a copy |
|---|
| 127 | self.assertEqual("<Resource 'peerreview:2'>", str(resource).replace("u'", "'")) |
|---|
| 128 | |
|---|
| 129 | resource = Resource('peerreviewfile', 1) |
|---|
| 130 | self.assertEqual("<Resource 'peerreviewfile:1'>", str(resource).replace("u'", "'")) |
|---|
| 131 | res = Resource('peerreviewfile', 1) |
|---|
| 132 | resource = Resource(res, 2) # This creates a copy |
|---|
| 133 | self.assertEqual("<Resource 'peerreviewfile:2'>", str(resource).replace("u'", "'")) |
|---|
| 134 | |
|---|
| 135 | def test_resource_exists_peerreview(self): |
|---|
| 136 | for num in range(1, 5): |
|---|
| 137 | resource = Resource('peerreview', num) |
|---|
| 138 | self.assertTrue(self.plugin.resource_exists(resource)) |
|---|
| 139 | # Invalid review id |
|---|
| 140 | resource = Resource('peerreview', 12) |
|---|
| 141 | self.assertFalse(self.plugin.resource_exists(resource)) |
|---|
| 142 | |
|---|
| 143 | def test_resource_exists_peerreviewfile(self): |
|---|
| 144 | for num in range(1, 9): |
|---|
| 145 | resource = Resource('peerreviewfile', num) |
|---|
| 146 | self.assertTrue(self.plugin.resource_exists(resource)) |
|---|
| 147 | # Only files associated with a review are real peerreviewfiles |
|---|
| 148 | for num in range(9, 17): |
|---|
| 149 | resource = Resource('peerreviewfile', num) |
|---|
| 150 | self.assertFalse(self.plugin.resource_exists(resource)) |
|---|
| 151 | # Invalid file id |
|---|
| 152 | resource = Resource('peerreviewfile', 22) |
|---|
| 153 | self.assertFalse(self.plugin.resource_exists(resource)) |
|---|
| 154 | |
|---|
| 155 | def test_resource_exist_wrong_resource(self): |
|---|
| 156 | resource = Resource('wiki', 'WikiStart') |
|---|
| 157 | self.assertRaises(ResourceNotFound, self.plugin.resource_exists, resource) |
|---|
| 158 | |
|---|
| 159 | def test_get_resource_url(self): |
|---|
| 160 | href = Href('foo') |
|---|
| 161 | resource = Resource('peerreview', 1) |
|---|
| 162 | self.assertEqual('foo/peerreviewview/1' , self.plugin.get_resource_url(resource, href)) |
|---|
| 163 | |
|---|
| 164 | resource = Resource('peerreviewfile', 1) |
|---|
| 165 | self.assertEqual('foo/peerreviewfile/1' , self.plugin.get_resource_url(resource, href)) |
|---|
| 166 | |
|---|
| 167 | def test_suite(): |
|---|
| 168 | suite = unittest.TestSuite() |
|---|
| 169 | suite.addTest(unittest.makeSuite(TestResource)) |
|---|
| 170 | return suite |
|---|