test_line_protocol.py 6.2 KB

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