1 # Copyright 2015 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import unittest 6 7 from dashboard import find_change_points 8 from dashboard import find_change_points_exp 9 from dashboard import testing_common 10 from dashboard.models import graph_data 11 12 13 class FindChangePointsExpTest(testing_common.TestCase): 14 15 def _MakeSampleTest(self): 16 """Makes a TestMetadata entity to be used in the tests below.""" 17 return graph_data.TestMetadata(id='m/b/suite/foo') 18 19 def testGetLastWindow_EmptyList_ReturnsEmptyList(self): 20 self.assertEqual([], find_change_points_exp._GetLastWindow([], 50)) 21 22 def testGetLastWindow_NoWindowSize_ReturnsFullSeries(self): 23 series = [(1, 2), (2, 4), (3, 8), (4, 16), (5, 32), (6, 64), (7, 128)] 24 self.assertEqual( 25 series, find_change_points_exp._GetLastWindow(series, None)) 26 self.assertEqual(series, find_change_points_exp._GetLastWindow(series, 0)) 27 28 def testGetLastWindow_SmallWindowSize_ReturnsCorrectSizeSubList(self): 29 series = [(1, 2), (2, 4), (3, 8), (4, 16), (5, 32), (6, 64), (7, 128)] 30 self.assertEqual( 31 [(7, 128)], find_change_points_exp._GetLastWindow(series, 1)) 32 33 def testGetLastWindow_BigWindowSize_ReturnsEntireSeries(self): 34 series = [(1, 2), (2, 4), (3, 8), (4, 16), (5, 32), (6, 64), (7, 128)] 35 self.assertEqual(series, find_change_points_exp._GetLastWindow(series, 50)) 36 37 def testRemoveKnownAnomalies_NoPriorAnomalies_ReturnsEmptyList(self): 38 test = self._MakeSampleTest() 39 self.assertEqual( 40 [], find_change_points_exp._RemoveKnownAnomalies(test, [])) 41 # The TestMetadata entity is never put(). 42 self.assertIsNone(test.key.get()) 43 test.put() 44 self.assertIsNotNone(test.key.get()) 45 46 def testRemoveKnownAnomalies_SomePriorAnomalies_ReturnsFilteredList(self): 47 test = self._MakeSampleTest() 48 test.last_alerted_revision = 3 49 series = [(i, i) for i in range(0, 6)] 50 change_points = [find_change_points.MakeChangePoint(series, i) 51 for i in [2, 3, 4]] 52 filtered = find_change_points_exp._RemoveKnownAnomalies(test, change_points) 53 # Only entries for after the last_alerted_revision are kept. 54 self.assertEqual(change_points[2:], filtered) 55 # The last_alerted_revision property of the TestMetadata is updated. 56 self.assertEqual(4, test.last_alerted_revision) 57 # The TestMetadata entity is never put(). 58 self.assertIsNone(test.key.get()) 59 60 61 if __name__ == '__main__': 62 unittest.main() 63