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