<commit_before>import logging
import sys
import unittest
from tempfile import NamedTemporaryFile

from checks.datadog import Dogstream

class TestDogstream(unittest.TestCase):
    def setUp(self):
        self.log_file = NamedTemporaryFile()
        
        self.config = {
            'dogstream_log': self.log_file.name
        }
        
        self.dogstream = Dogstream(logging.getLogger('test.dogstream'))
        
    def _write_log(self, log_data):
        for data in log_data:
            print >> self.log_file, data
        self.log_file.flush()
    
    def tearDown(self):
        self.log_file.close()
    
    def test_dogstream(self):
        log_data = [
            ('test.metric.a', '1000000000', '1', 'metric_type=counter'),
            ('test.metric.b', '1000000000', '1', 'metric_type=gauge'),
            ('test.metric.c', '1000000000', '1', 'metric_type=counter'),

            ('test.metric.a', '1000000001', '1', 'metric_type=counter'),
            ('test.metric.b', '1000000001', '2', 'metric_type=gauge'),
            ('test.metric.c', '1000000001', '2', 'metric_type=counter'),

            ('test.metric.a', '1000000002', '1', 'metric_type=counter'),
            ('test.metric.b', '1000000002', '3', 'metric_type=gauge'),
            ('test.metric.c', '1000000002', '4', 'metric_type=counter'),

            ('test.metric.a', '1000000003', '1', 'metric_type=counter'),
            ('test.metric.b', '1000000003', '4', 'metric_type=gauge'),
            ('test.metric.c', '1000000003', '8', 'metric_type=counter'),

            ('test.metric.a', '1000000004', '1', 'metric_type=counter'),

            ('test.metric.a', '1000000005', '0', 'metric_type=counter'),
            ('test.metric.b', '1000000005', '0', 'metric_type=gauge'),
            ('test.metric.c', '1000000005', '32', 'metric_type=counter'),
        ]
        
        expected_output = {
            'test.metric.a': (1000000002.5, 5),
            'test.metric.b': (1000000002.2, 2),
            'test.metric.c': (1000000002.2, 47),
        }
        
        self._write_log((' '.join(data) for data in log_data))

        actual_output = self.dogstream.check(self.config, move_end=False)
        self.assertEquals(expected_output, actual_output)
    
    def test_dogstream_bad_input(self):
        log_data = [
            ('test.metric.e1000000000 1metric_type=gauge'),
            ('1000000001 1 metric_type=gauge tag=staging'),
            ('test_metric.e 1 1000000002 metric_type=gauge'),
            ('test_metric.e 1000000002 10 metric_type=gauge'),
        ]
        expected_output = {
            'test_metric.e': (1000000002, 10)
        }
        
        self._write_log(log_data)
        
        actual_output = self.dogstream.check(self.config, move_end=False)
        self.assertEquals(expected_output, actual_output)
        


<commit_msg>make sure the test actually works<commit_after>import logging
import sys
import unittest
from tempfile import NamedTemporaryFile

from checks.datadog import Dogstream

class TestDogstream(unittest.TestCase):
    def setUp(self):
        self.log_file = NamedTemporaryFile()
        
        self.config = {
            'dogstream_log': self.log_file.name
        }
        
        self.dogstream = Dogstream(logging.getLogger('test.dogstream'), 
            self.config)
        
    def _write_log(self, log_data):
        for data in log_data:
            print >> self.log_file, data
        self.log_file.flush()
    
    def tearDown(self):
        self.log_file.close()
    
    def test_dogstream(self):
        log_data = [
            ('test.metric.a', '1000000000', '1', 'metric_type=counter'),
            ('test.metric.b', '1000000000', '1', 'metric_type=gauge'),
            ('test.metric.c', '1000000000', '1', 'metric_type=counter'),

            ('test.metric.a', '1000000001', '1', 'metric_type=counter'),
            ('test.metric.b', '1000000001', '2', 'metric_type=gauge'),
            ('test.metric.c', '1000000001', '2', 'metric_type=counter'),

            ('test.metric.a', '1000000002', '1', 'metric_type=counter'),
            ('test.metric.b', '1000000002', '3', 'metric_type=gauge'),
            ('test.metric.c', '1000000002', '4', 'metric_type=counter'),

            ('test.metric.a', '1000000003', '1', 'metric_type=counter'),
            ('test.metric.b', '1000000003', '4', 'metric_type=gauge'),
            ('test.metric.c', '1000000003', '8', 'metric_type=counter'),

            ('test.metric.a', '1000000004', '1', 'metric_type=counter'),

            ('test.metric.a', '1000000005', '0', 'metric_type=counter'),
            ('test.metric.b', '1000000005', '0', 'metric_type=gauge'),
            ('test.metric.c', '1000000005', '32', 'metric_type=counter'),
        ]
        
        expected_output = {
            'test.metric.a': 5,
            'test.metric.b': 2,
            'test.metric.c': 47,
        }
        
        self._write_log((' '.join(data) for data in log_data))

        actual_output = self.dogstream.check(self.config, move_end=False)
        self.assertEquals(expected_output, actual_output)
    
    def test_dogstream_bad_input(self):
        log_data = [
            ('test.metric.e1000000000 1metric_type=gauge'),
            ('1000000001 1 metric_type=gauge tag=staging'),
            ('test_metric.e 1 1000000002 metric_type=gauge'),
            ('test_metric.e 1000000002 10 metric_type=gauge'),
        ]
        expected_output = {
            'test_metric.e': 10
        }
        
        self._write_log(log_data)
        
        actual_output = self.dogstream.check(self.config, move_end=False)
        self.assertEquals(expected_output, actual_output)
        
if __name__ == '__main__':
    unittest.main()



{'fraction_non_alphanumeric': 0.15468940316686966, 'fraction_numerical': 0.08682791021402471, 'mean_word_length': 2.3653395784543325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 44, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3910834', 'n_tokens_mistral': 2172, 'n_tokens_neox': 1799, 'n_words': 314}