Debian Codegraph @

influxdb/tests/test_line_protocol.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # -*- coding: utf-8 -*-
2 """Define the line protocol test module."""
3
4 from __future__ import absolute_import
5 from __future__ import division
6 from __future__ import print_function
7 from __future__ import unicode_literals
8
9 import unittest
10
11 from datetime import datetime
12 from decimal import Decimal
13
14 from pytz import UTC, timezone
15 from influxdb import line_protocol
16
17
18 class TestLineProtocol(unittest.TestCase):
19 """Define the LineProtocol test object."""
20
21 def test_make_lines(self):
22 """Test make new lines in TestLineProtocol object."""
23 data = {
24 "tags": {
25 "empty_tag": "",
26 "none_tag": None,
27 "backslash_tag": "C:\\",
28 "integer_tag": 2,
29 "string_tag": "hello"
30 },
31 "points": [
32 {
33 "measurement": "test",
34 "fields": {
35 "string_val": "hello!",
36 "int_val": 1,
37 "float_val": 1.1,
38 "none_field": None,
39 "bool_val": True,
40 }
41 }
42 ]
43 }
44
45 self.assertEqual(
46 line_protocol.make_lines(data),
47 'test,backslash_tag=C:\\\\,integer_tag=2,string_tag=hello '
48 'bool_val=True,float_val=1.1,int_val=1i,string_val="hello!"\n'
49 )
50
51 def test_timezone(self):
52 """Test timezone in TestLineProtocol object."""
53 dt = datetime(2009, 11, 10, 23, 0, 0, 123456)
54 utc = UTC.localize(dt)
55 berlin = timezone('Europe/Berlin').localize(dt)
56 eastern = berlin.astimezone(timezone('US/Eastern'))
57 data = {
58 "points": [
59 {"measurement": "A", "fields": {"val": 1},
60 "time": 0},
61 {"measurement": "A", "fields": {"val": 1},
62 "time": "2009-11-10T23:00:00.123456Z"},
63 {"measurement": "A", "fields": {"val": 1}, "time": dt},
64 {"measurement": "A", "fields": {"val": 1}, "time": utc},
65 {"measurement": "A", "fields": {"val": 1}, "time": berlin},
66 {"measurement": "A", "fields": {"val": 1}, "time": eastern},
67 ]
68 }
69 self.assertEqual(
70 line_protocol.make_lines(data),
71 '\n'.join([
72 'A val=1i 0',
73 'A val=1i 1257894000123456000',
74 'A val=1i 1257894000123456000',
75 'A val=1i 1257894000123456000',
76 'A val=1i 1257890400123456000',
77 'A val=1i 1257890400123456000',
78 ]) + '\n'
79 )
80
81 def test_string_val_newline(self):
82 """Test string value with newline in TestLineProtocol object."""
83 data = {
84 "points": [
85 {
86 "measurement": "m1",
87 "fields": {
88 "multi_line": "line1\nline1\nline3"
89 }
90 }
91 ]
92 }
93
94 self.assertEqual(
95 line_protocol.make_lines(data),
96 'm1 multi_line="line1\\nline1\\nline3"\n'
97 )
98
99 def test_make_lines_unicode(self):
100 """Test make unicode lines in TestLineProtocol object."""
101 data = {
102 "tags": {
103 "unicode_tag": "\'Привет!\'" # Hello! in Russian
104 },
105 "points": [
106 {
107 "measurement": "test",
108 "fields": {
109 "unicode_val": "Привет!", # Hello! in Russian
110 }
111 }
112 ]
113 }
114
115 self.assertEqual(
116 line_protocol.make_lines(data),
117 'test,unicode_tag=\'Привет!\' unicode_val="Привет!"\n'
118 )
119
120 def test_make_lines_empty_field_string(self):
121 """Test make lines with an empty string field."""
122 data = {
123 "points": [
124 {
125 "measurement": "test",
126 "fields": {
127 "string": "",
128 }
129 }
130 ]
131 }
132
133 self.assertEqual(
134 line_protocol.make_lines(data),
135 'test string=""\n'
136 )
137
138 def test_tag_value_newline(self):
139 """Test make lines with tag value contains newline."""
140 data = {
141 "tags": {
142 "t1": "line1\nline2"
143 },
144 "points": [
145 {
146 "measurement": "test",
147 "fields": {
148 "val": "hello"
149 }
150 }
151 ]
152 }
153
154 self.assertEqual(
155 line_protocol.make_lines(data),
156 'test,t1=line1\\nline2 val="hello"\n'
157 )
158
159 def test_quote_ident(self):
160 """Test quote indentation in TestLineProtocol object."""
161 self.assertEqual(
162 line_protocol.quote_ident(r"""\foo ' bar " Örf"""),
163 r'''"\\foo ' bar \" Örf"'''
164 )
165
166 def test_quote_literal(self):
167 """Test quote literal in TestLineProtocol object."""
168 self.assertEqual(
169 line_protocol.quote_literal(r"""\foo ' bar " Örf"""),
170 r"""'\\foo \' bar " Örf'"""
171 )
172
173 def test_float_with_long_decimal_fraction(self):
174 """Ensure precision is preserved when casting floats into strings."""
175 data = {
176 "points": [
177 {
178 "measurement": "test",
179 "fields": {
180 "float_val": 1.0000000000000009,
181 }
182 }
183 ]
184 }
185 self.assertEqual(
186 line_protocol.make_lines(data),
187 'test float_val=1.0000000000000009\n'
188 )
189
190 def test_float_with_long_decimal_fraction_as_type_decimal(self):
191 """Ensure precision is preserved when casting Decimal into strings."""
192 data = {
193 "points": [
194 {
195 "measurement": "test",
196 "fields": {
197 "float_val": Decimal(0.8289445733333332),
198 }
199 }
200 ]
201 }
202 self.assertEqual(
203 line_protocol.make_lines(data),
204 'test float_val=0.8289445733333332\n'
205 )