blob: d550f53630fc5642042924ae3043fa28a6f60791 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Unittests for monorail.tracker.rerank_helpers."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
11
12from framework import exceptions
13from testing import fake
14from tracker import rerank_helpers
15
16
17rerank_helpers.MAX_RANKING = 10
18
19
20class Rerank_HelpersTest(unittest.TestCase):
21
22 def setUp(self):
23 self.PAST_TIME = 12345
24 hotlist_item_fields = [
25 (78904, 31, 111, self.PAST_TIME, 'note'),
26 (78903, 21, 222, self.PAST_TIME, 'note'),
27 (78902, 11, 111, self.PAST_TIME, 'note'),
28 (78901, 1, 222, self.PAST_TIME, 'note')]
29 self.hotlist = fake.Hotlist(
30 'hotlist_name', 1234, hotlist_item_fields=hotlist_item_fields)
31
32 # Tested in tests for RerankHotlistItems.
33 def testGetHotlistRerankChanges_FirstPosition(self):
34 moved_issue_ids = [78903, 78902]
35 target_position = 0
36 changed_ranks = rerank_helpers.GetHotlistRerankChanges(
37 self.hotlist.items, moved_issue_ids, target_position)
38 self.assertEqual(changed_ranks, [(78903, 5), (78902, 15), (78901, 25)])
39
40 def testGetHotlistRerankChanges_LastPosition(self):
41 moved_issue_ids = [78903, 78902]
42 target_position = 2
43 changed_ranks = rerank_helpers.GetHotlistRerankChanges(
44 self.hotlist.items, moved_issue_ids, target_position)
45 self.assertEqual(changed_ranks, [(78904, 3), (78903, 6), (78902, 9)])
46
47 def testGetHotlistRerankChanges_Middle(self):
48 moved_issue_ids = [78903]
49 target_position = 1
50 changed_ranks = rerank_helpers.GetHotlistRerankChanges(
51 self.hotlist.items, moved_issue_ids, target_position)
52 self.assertEqual(changed_ranks, [(78903, 6)])
53
54
55 def testGetHotlistRerankChanges_NewMoveIds(self):
56 "We can handle reranking for inserting new issues."
57 moved_issue_ids = [78909, 78910, 78903]
58 target_position = 0
59 changed_ranks = rerank_helpers.GetHotlistRerankChanges(
60 self.hotlist.items, moved_issue_ids, target_position)
61 self.assertEqual(
62 changed_ranks, [(78909, 1), (78910, 3), (78903, 5), (78901, 7)])
63
64 def testGetHotlistRerankChanges_InvalidMovedIds(self):
65 moved_issue_ids = [78903]
66 target_position = -1
67 with self.assertRaises(exceptions.InputException):
68 rerank_helpers.GetHotlistRerankChanges(
69 self.hotlist.items, moved_issue_ids, target_position)
70
71 def testGetHotlistRerankChanges_InvalidPosition(self):
72 moved_issue_ids = [78909]
73 target_position = 8
74 with self.assertRaises(exceptions.InputException):
75 rerank_helpers.GetHotlistRerankChanges(
76 self.hotlist.items, moved_issue_ids, target_position)
77
78 def testGetInsertRankings(self):
79 lower = [(1, 0)]
80 higher = [(2, 10)]
81 moved_ids = [3]
82 ret = rerank_helpers.GetInsertRankings(lower, higher, moved_ids)
83 self.assertEqual(ret, [(3, 5)])
84
85 def testGetInsertRankings_Below(self):
86 lower = []
87 higher = [(1, 2)]
88 moved_ids = [2]
89 ret = rerank_helpers.GetInsertRankings(lower, higher, moved_ids)
90 self.assertEqual(ret, [(2, 1)])
91
92 def testGetInsertRankings_Above(self):
93 lower = [(1, 0)]
94 higher = []
95 moved_ids = [2]
96 ret = rerank_helpers.GetInsertRankings(lower, higher, moved_ids)
97 self.assertEqual(ret, [(2, 5)])
98
99 def testGetInsertRankings_Multiple(self):
100 lower = [(1, 0)]
101 higher = [(2, 10)]
102 moved_ids = [3,4,5]
103 ret = rerank_helpers.GetInsertRankings(lower, higher, moved_ids)
104 self.assertEqual(ret, [(3, 2), (4, 5), (5, 8)])
105
106 def testGetInsertRankings_SplitLow(self):
107 lower = [(1, 0), (2, 5)]
108 higher = [(3, 6), (4, 10)]
109 moved_ids = [5]
110 ret = rerank_helpers.GetInsertRankings(lower, higher, moved_ids)
111 self.assertEqual(ret, [(2, 2), (5, 5)])
112
113 def testGetInsertRankings_SplitHigh(self):
114 lower = [(1, 0), (2, 4)]
115 higher = [(3, 5), (4, 10)]
116 moved_ids = [5]
117 ret = rerank_helpers.GetInsertRankings(lower, higher, moved_ids)
118 self.assertEqual(ret, [(5, 6), (3, 9)])
119
120 def testGetInsertRankings_NoLower(self):
121 lower = []
122 higher = [(1, 1)]
123 moved_ids = [2]
124 ret = rerank_helpers.GetInsertRankings(lower, higher, moved_ids)
125 self.assertEqual(ret, [(2, 3), (1, 8)])
126
127 def testGetInsertRankings_NoRoom(self):
128 max_ranking, rerank_helpers.MAX_RANKING = rerank_helpers.MAX_RANKING, 1
129 lower = [(1, 0)]
130 higher = [(2, 1)]
131 moved_ids = [3]
132 ret = rerank_helpers.GetInsertRankings(lower, higher, moved_ids)
133 self.assertIsNone(ret)
134 rerank_helpers.MAX_RANKING = max_ranking