TestCollectdGraphiteEncoder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 44
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 2 1
A test_encode_simple() 0 4 1
A test_encode_multiple_values() 0 10 1
A test_encode_with_prefix() 0 18 3
A test_invalid_messages() 0 5 2
1
import unittest
2
from kafka_influxdb.encoder import collectd_graphite_encoder
3
4
5
class TestCollectdGraphiteEncoder(unittest.TestCase):
6
    def setUp(self):
7
        self.encoder = collectd_graphite_encoder.Encoder()
8
9
    def test_encode_simple(self):
10
        msg = b'myhost.load.load.shortterm 0.05 1436357630'
11
        expected = ['load_load_shortterm,host=myhost value=0.05 1436357630']
12
        self.assertEqual(self.encoder.encode(msg), expected)
13
14
    def test_encode_with_prefix(self):
15
        msg = b'mydatacenter.myhost.load.load.shortterm 0.45 1436357630'
16
17
        # The official documentation states that tags should be sorted for performance reasons.
18
        # As of now they will be sorted on the InfluxDB side anyway (which is probably faster).
19
        # (See https://docs.influxdata.com/influxdb/v1.1/write_protocols/line_protocol_tutorial/#tag-set for more info)
20
        # So we don't sort the tags to make the encoder faster.
21
        # As a consequence they can appear in any order. Test for all combinations.
22
        expected1 = [
23
            'load_load_shortterm,datacenter=mydatacenter,host=myhost value=0.45 1436357630']
24
        expected2 = [
25
            'load_load_shortterm,host=myhost,datacenter=mydatacenter value=0.45 1436357630']
26
27
        if self.encoder.encode(msg, prefix="mydatacenter.", prefix_tag="datacenter") == expected1:
28
            return
29
        if self.encoder.encode(msg, prefix="mydatacenter.", prefix_tag="datacenter") == expected2:
30
            return
31
        raise self.failureException()
32
33
    def test_encode_multiple_values(self):
34
        msg = b'26f2fc918f50.load.load.shortterm 0.05 1436357630\n' \
35
              b'26f2fc918f50.load.load.midterm 0.06 1436357631\n' \
36
              b'26f2fc918f50.load.load.longterm 0.07 1436357632'
37
        expected = [
38
            'load_load_shortterm,host=26f2fc918f50 value=0.05 1436357630',
39
            'load_load_midterm,host=26f2fc918f50 value=0.06 1436357631',
40
            'load_load_longterm,host=26f2fc918f50 value=0.07 1436357632',
41
        ]
42
        self.assertEqual(self.encoder.encode(msg), expected)
43
44
    def test_invalid_messages(self):
45
        invalid_messages = [b'', b'\n', b'bla', b'foo\nbar\nbaz']
46
        for msg in invalid_messages:
47
            self.assertEqual(self.encoder.encode(
48
                msg, prefix="mydatacenter.", prefix_tag="datacenter"), [])
49