예제 #1
0
    def test_various_data(self):
        """Test alert logic against data in the specified fixture.

        Note that the input data is actually divided by 100 before
        being input to the function under test. This slightly
        unfortunate design comes from wanting to exercise a couple of
        interesting edge cases without making the tests less readable
        by introducing extra floating points to the test input
        fixture.

        """
        with open(
                settings.SITE_ROOT + '/frontend/tests/fixtures/'
                'alert_test_cases.txt', 'rb') as expected:
            test_cases = expected.readlines()
        for test in each_cusum_test(test_cases):
            cusum = bookmark_utils.CUSUM(test['data'],
                                         window_size=3,
                                         sensitivity=5)
            cusum.work()
            new_result_formatted = extract_percentiles_for_alerts(
                cusum.as_dict())
            error_message = "In test '%s':\n" % test['name']
            error_message += "   Input values: %s\n" % test['data']
            error_message += "Expected alerts: %s\n" % test['expected']
            self.assertEqual(
                new_result_formatted, test['expected'],
                error_message + "            Got: %s" % new_result_formatted)
            info = cusum.get_last_alert_info()
            if info:
                change = deltawords(info['to'] * 100.0, info['from'] * 100.0)
                self.assertEqual(test['deltawords'], change)
            else:
                self.assertEqual(test['deltawords'], 'not at all')
예제 #2
0
 def test_alert_parsed_when_more_than_one_alert(self):
     cusum = bookmark_utils.CUSUM(['1', '2', '3', 'b'])
     cusum.alert_indices = [1, 3]
     cusum.target_means = ['a', 'a', 'a', 'a']
     self.assertDictEqual(cusum.get_last_alert_info(), {
         'from': 'a',
         'to': 'b',
         'period': 1
     })
예제 #3
0
 def test_period_parsed(self):
     cusum = bookmark_utils.CUSUM(['c', 'c', 'c'])
     cusum.alert_indices = [1, 2]
     cusum.target_means = ['a', 'a', 'a']
     self.assertDictEqual(cusum.get_last_alert_info(), {
         'from': 'a',
         'to': 'c',
         'period': 2
     })
예제 #4
0
 def test_alert_parsed_when_only_alert(self):
     cusum = bookmark_utils.CUSUM(['c', 'c', 'c'])
     cusum.target_means = ['b', 'b', 'b']
     cusum.alert_indices = [2]
     self.assertDictEqual(cusum.get_last_alert_info(), {
         'from': 'b',
         'to': 'c',
         'period': 1
     })
예제 #5
0
 def test_alert_parsed_when_more_than_one_alert(self):
     cusum = bookmark_utils.CUSUM(["1", "2", "3", "b"])
     cusum.alert_indices = [1, 3]
     cusum.target_means = ["a", "a", "a", "a"]
     self.assertDictEqual(cusum.get_last_alert_info(), {
         "from": "a",
         "to": "b",
         "period": 1
     })
예제 #6
0
 def test_period_parsed(self):
     cusum = bookmark_utils.CUSUM(["c", "c", "c"])
     cusum.alert_indices = [1, 2]
     cusum.target_means = ["a", "a", "a"]
     self.assertDictEqual(cusum.get_last_alert_info(), {
         "from": "a",
         "to": "c",
         "period": 2
     })
예제 #7
0
 def test_alert_parsed_when_only_alert(self):
     cusum = bookmark_utils.CUSUM(["c", "c", "c"])
     cusum.target_means = ["b", "b", "b"]
     cusum.alert_indices = [2]
     self.assertDictEqual(cusum.get_last_alert_info(), {
         "from": "b",
         "to": "c",
         "period": 1
     })
예제 #8
0
 def test_no_alert_when_alert_not_most_recent(self):
     cusum = bookmark_utils.CUSUM(['a', 'b'])
     cusum.alert_indices = [0]
     cusum.target_means = ['b']
     self.assertIsNone(cusum.get_last_alert_info(), None)
예제 #9
0
 def test_no_alert_when_empty(self):
     cusum = bookmark_utils.CUSUM(['a', 'b'])
     cusum.alert_indices = []
     self.assertIsNone(cusum.get_last_alert_info(), None)