Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
chore(line_protocol): update repr value of floats to properly handle …
Browse files Browse the repository at this point in the history
…precision. Closes #488
  • Loading branch information
sebito91 committed Apr 9, 2020
1 parent 57c1408 commit 76e0125
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion influxdb/line_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _escape_value(value):
return str(value) + 'i'

if _is_float(value):
return repr(value)
return repr(float(value))

return str(value)

Expand Down
23 changes: 21 additions & 2 deletions influxdb/tests/test_line_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from __future__ import print_function
from __future__ import unicode_literals

from datetime import datetime
import unittest
from pytz import UTC, timezone

from datetime import datetime
from decimal import Decimal

from pytz import UTC, timezone
from influxdb import line_protocol


Expand Down Expand Up @@ -166,3 +168,20 @@ def test_float_with_long_decimal_fraction(self):
line_protocol.make_lines(data),
'test float_val=1.0000000000000009\n'
)

def test_float_with_long_decimal_fraction_as_type_decimal(self):
"""Ensure precision is preserved when casting Decimal into strings."""
data = {
"points": [
{
"measurement": "test",
"fields": {
"float_val": Decimal(1.0000000000000009),
}
}
]
}
self.assertEqual(
line_protocol.make_lines(data),
'test float_val=1.0000000000000009\n'
)

0 comments on commit 76e0125

Please sign in to comment.