#!/usr/bin/env python
# -*- coding: utf-8 -*-


# Path hack.
import sys
import os
sys.path.insert(0, os.path.abspath('..'))

import server

import unittest

class ServerTestSuite(unittest.TestCase):
    '''StatsD server test cases'''
    def setUp(self):
        server.clear_metrics()
    def test_clean_key(self):
        key = server.clean_key('test key')
        self.assertEqual(key, 'test_key')

        key = server.clean_key('test/key')
        self.assertEqual(key, 'test-key')

        key = server.clean_key('test!key')
        self.assertEqual(key, 'testkey')

        key = server.clean_key('test.key')
        self.assertEqual(key, 'test.key')

        key = server.clean_key('TESTKEY')
        self.assertEqual(key, 'testkey')

    def test_parse_line_invalid(self):
        self.assertRaises(ValueError, server.parse_line, 'keyonly')
        self.assertRaises(ValueError, server.parse_line, 'noval:')
        self.assertRaises(ValueError, server.parse_line, 'nocolon 1|c')
        self.assertRaises(ValueError, server.parse_line, 'notype:1')
        self.assertRaises(ValueError, server.parse_line, 'notype:1|')
        self.assertRaises(ValueError, server.parse_line, 'nosep:1 c')
        self.assertRaises(ValueError, server.parse_line, 'badtype:1|a')
        self.assertRaises(ValueError, server.parse_line, 'badval:help|c')

    def test_parse_line_counter(self):
        metric = server.parse_line('test.key: 1|c')
        self.assertEqual(metric, ('test.key', 1.0, 'c', 1.0))

        metric = server.parse_line('test.key: 1|c|@0.1')
        self.assertEqual(metric, (('test.key', 1.0, 'c', 0.1)))

        line = 'test.key: 1|c|2'
        self.assertRaises(ValueError, server.parse_line, line)

    def test_parse_line_gauge(self):
        metric = server.parse_line('test.key: 5|g')
        self.assertEqual(metric, ('test.key', 5.0, 'g', 1.0))

        self.assertRaises(ValueError, server.parse_line, 'test:5|g|@0.1')

    def test_parse_line_duration(self):
        metric = server.parse_line('test.key: 34.5|ms')
        self.assertEqual(metric, ('test.key', 34.5, 'ms', 1.0))

        self.assertRaises(ValueError, server.parse_line, 'test:5|ms|@0.1')

    def test_add_metric(self):
        server.add_metric('test.key', 1.0, 'c', 1.0)
        self.assertEqual(server.metrics['c']['test.key'], [1.0])

        server.add_metric('test.key', 1.0, 'c', 0.1)
        self.assertEqual(server.metrics['c']['test.key'], [1.0, 10.0])

        server.add_metric('test.key', 1.0, 'ms', 1.0)
        self.assertEqual(server.metrics['ms']['test.key'], [1.0])
        server.add_metric('test.key', 1.0, 'ms', 1.0)
        self.assertEqual(server.metrics['ms']['test.key'], [1.0, 1.0])


if __name__ == '__main__':
    unittest.main()

@@ -2696,16 +2696,376 @@
  1.0%5D)%0A%0A
+    def test_process_lines(self):%0A        lines = %5B%0A            'test.key: 1%7Cc%5Cn',%0A            'badline',%0A            'test.key: 2%7Cc%5Cn',%0A            'test.key2: 20%7Cms%5Cn',%0A            %5D%0A        server.process_lines(lines)%0A        self.assertEqual(server.metrics%5B'c'%5D%5B'test.key'%5D, %5B1.0, 2.0%5D)%0A        self.assertEqual(server.metrics%5B'ms'%5D%5B'test.key2'%5D, %5B20.0%5D)%0A%0A
 %0Aif __na

Add tests for `process_lines`