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