Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 1 | # Copyright 2016 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Tests for features bizobj functions.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 12 | from mrproto import features_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | from features import features_bizobj |
| 14 | from testing import fake |
| 15 | |
| 16 | class FeaturesBizobjTest(unittest.TestCase): |
| 17 | |
| 18 | def setUp(self): |
| 19 | self.local_ids = [1, 2, 3, 4, 5] |
| 20 | self.issues = [fake.MakeTestIssue(1000, local_id, '', 'New', 111) |
| 21 | for local_id in self.local_ids] |
| 22 | self.hotlistitems = [features_pb2.MakeHotlistItem( |
| 23 | issue.issue_id, rank=rank*10, adder_id=111, date_added=3) for |
| 24 | rank, issue in enumerate(self.issues)] |
| 25 | self.iids = [item.issue_id for item in self.hotlistitems] |
| 26 | |
| 27 | def testIssueIsInHotlist(self): |
| 28 | hotlist = features_pb2.Hotlist(items=self.hotlistitems) |
| 29 | for issue in self.issues: |
| 30 | self.assertTrue(features_bizobj.IssueIsInHotlist(hotlist, issue.issue_id)) |
| 31 | |
| 32 | self.assertFalse(features_bizobj.IssueIsInHotlist( |
| 33 | hotlist, fake.MakeTestIssue(1000, 9, '', 'New', 111))) |
| 34 | |
| 35 | def testSplitHotlistIssueRanks(self): |
| 36 | iid_rank_tuples = [(issue.issue_id, issue.rank) |
| 37 | for issue in self.hotlistitems] |
| 38 | iid_rank_tuples.reverse() |
| 39 | ret = features_bizobj.SplitHotlistIssueRanks( |
| 40 | 100003, True, iid_rank_tuples) |
| 41 | self.assertEqual(ret, (iid_rank_tuples[:2], iid_rank_tuples[2:])) |
| 42 | |
| 43 | iid_rank_tuples.reverse() |
| 44 | ret = features_bizobj.SplitHotlistIssueRanks( |
| 45 | 100003, False, iid_rank_tuples) |
| 46 | self.assertEqual(ret, (iid_rank_tuples[:3], iid_rank_tuples[3:])) |
| 47 | |
| 48 | # target issue not found |
| 49 | first_pairs, second_pairs = features_bizobj.SplitHotlistIssueRanks( |
| 50 | 100009, True, iid_rank_tuples) |
| 51 | self.assertEqual(iid_rank_tuples, first_pairs) |
| 52 | self.assertEqual(second_pairs, []) |
| 53 | |
| 54 | def testGetOwnerIds(self): |
| 55 | hotlist = features_pb2.Hotlist(owner_ids=[111]) |
| 56 | self.assertEqual(features_bizobj.GetOwnerIds(hotlist), [111]) |
| 57 | |
| 58 | def testUsersInvolvedInHotlists_Empty(self): |
| 59 | self.assertEqual(set(), features_bizobj.UsersInvolvedInHotlists([])) |
| 60 | |
| 61 | def testUsersInvolvedInHotlists_Normal(self): |
| 62 | hotlist1 = features_pb2.Hotlist( |
| 63 | owner_ids=[111, 222], editor_ids=[333, 444, 555], |
| 64 | follower_ids=[123]) |
| 65 | hotlist2 = features_pb2.Hotlist( |
| 66 | owner_ids=[111], editor_ids=[222, 123]) |
| 67 | self.assertEqual(set([111, 222, 333, 444, 555, 123]), |
| 68 | features_bizobj.UsersInvolvedInHotlists([hotlist1, |
| 69 | hotlist2])) |
| 70 | |
| 71 | def testUserIsInHotlist(self): |
| 72 | h = features_pb2.Hotlist() |
| 73 | self.assertFalse(features_bizobj.UserIsInHotlist(h, {9})) |
| 74 | self.assertFalse(features_bizobj.UserIsInHotlist(h, set())) |
| 75 | |
| 76 | h.owner_ids.extend([1, 2, 3]) |
| 77 | h.editor_ids.extend([4, 5, 6]) |
| 78 | h.follower_ids.extend([7, 8, 9]) |
| 79 | self.assertTrue(features_bizobj.UserIsInHotlist(h, {1})) |
| 80 | self.assertTrue(features_bizobj.UserIsInHotlist(h, {4})) |
| 81 | self.assertTrue(features_bizobj.UserIsInHotlist(h, {7})) |
| 82 | self.assertFalse(features_bizobj.UserIsInHotlist(h, {10})) |
| 83 | |
| 84 | # Membership via group membership |
| 85 | self.assertTrue(features_bizobj.UserIsInHotlist(h, {10, 4})) |
| 86 | |
| 87 | # Membership via several group memberships |
| 88 | self.assertTrue(features_bizobj.UserIsInHotlist(h, {1, 4})) |
| 89 | |
| 90 | # Several irrelevant group memberships |
| 91 | self.assertFalse(features_bizobj.UserIsInHotlist(h, {10, 11, 12})) |
| 92 | |
| 93 | def testDetermineHotlistIssuePosition(self): |
| 94 | # normal |
| 95 | prev_iid, index, next_iid = features_bizobj.DetermineHotlistIssuePosition( |
| 96 | self.issues[2], self.iids) |
| 97 | self.assertEqual(prev_iid, self.hotlistitems[1].issue_id) |
| 98 | self.assertEqual(index, 2) |
| 99 | self.assertEqual(next_iid, self.hotlistitems[3].issue_id) |
| 100 | |
| 101 | # end of list |
| 102 | prev_iid, index, next_iid = features_bizobj.DetermineHotlistIssuePosition( |
| 103 | self.issues[4], self.iids) |
| 104 | self.assertEqual(prev_iid, self.hotlistitems[3].issue_id) |
| 105 | self.assertEqual(index, 4) |
| 106 | self.assertEqual(next_iid, None) |
| 107 | |
| 108 | # beginning of list |
| 109 | prev_iid, index, next_iid = features_bizobj.DetermineHotlistIssuePosition( |
| 110 | self.issues[0], self.iids) |
| 111 | self.assertEqual(prev_iid, None) |
| 112 | self.assertEqual(index, 0) |
| 113 | self.assertEqual(next_iid, self.hotlistitems[1].issue_id) |
| 114 | |
| 115 | # one item in list |
| 116 | prev_iid, index, next_iid = features_bizobj.DetermineHotlistIssuePosition( |
| 117 | self.issues[2], [self.iids[2]]) |
| 118 | self.assertEqual(prev_iid, None) |
| 119 | self.assertEqual(index, 0) |
| 120 | self.assertEqual(next_iid, None) |
| 121 | |
| 122 | prev_iid, index, next_iid = features_bizobj.DetermineHotlistIssuePosition( |
| 123 | self.issues[2], [self.iids[3]]) |
| 124 | self.assertEqual(prev_iid, None) |
| 125 | self.assertEqual(index, None) |
| 126 | self.assertEqual(next_iid, None) |
| 127 | |
| 128 | #none |
| 129 | prev_iid, index, next_iid = features_bizobj.DetermineHotlistIssuePosition( |
| 130 | self.issues[2], []) |
| 131 | self.assertEqual(prev_iid, None) |
| 132 | self.assertEqual(index, None) |
| 133 | self.assertEqual(next_iid, None) |