Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: influxdb_client/client/write: strip .0 on float values #181

Merged
merged 1 commit into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ def _append_fields(fields):
if isinstance(value, float) or isinstance(value, Decimal):
if not math.isfinite(value):
continue
_return.append(f'{_escape_key(field)}={str(value)}')
s = str(value)
# It's common to represent whole numbers as floats
# and the trailing ".0" that Python produces is unnecessary
# in line-protocol, inconsistent with other line-protocol encoders,
# and takes more space than needed, so trim it off.
if s.endswith('.0'):
s = s[:-2]
_return.append(f'{_escape_key(field)}={s}')
elif isinstance(value, int) and not isinstance(value, bool):
_return.append(f'{_escape_key(field)}={str(value)}i')
elif isinstance(value, bool):
Expand Down
28 changes: 14 additions & 14 deletions tests/test_WriteApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def tearDown(self) -> None:
def test_write_line_protocol(self):
bucket = self.create_test_bucket()

record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
record = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
self.write_client.write(bucket.name, self.org, record)

result = self.query_api.query(f"from(bucket:\"{bucket.name}\") |> range(start: 1970-01-01T00:00:00.000000001Z)",
Expand Down Expand Up @@ -72,8 +72,8 @@ def test_write_precision(self):
def test_write_records_list(self):
bucket = self.create_test_bucket()

_record1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
_record2 = "h2o_feet,location=coyote_creek level\\ water_level=2.0 2"
_record1 = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
_record2 = "h2o_feet,location=coyote_creek level\\ water_level=2 2"

record_list = [_record1, _record2]

Expand Down Expand Up @@ -170,7 +170,7 @@ def test_write_using_default_tags(self):
def test_write_result(self):
_bucket = self.create_test_bucket()

_record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
_record = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
result = self.write_client.write(_bucket.name, self.org, _record)

# The success response is 204 - No Content
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_write_dictionary(self):

def test_write_bytes(self):
_bucket = self.create_test_bucket()
_bytes = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1".encode("utf-8")
_bytes = "h2o_feet,location=coyote_creek level\\ water_level=1 1".encode("utf-8")

self.write_client.write(_bucket.name, self.org, _bytes)

Expand All @@ -230,9 +230,9 @@ def test_write_bytes(self):
def test_write_tuple(self):
bucket = self.create_test_bucket()

_record1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
_record2 = "h2o_feet,location=coyote_creek level\\ water_level=2.0 2"
_bytes = "h2o_feet,location=coyote_creek level\\ water_level=3.0 3".encode("utf-8")
_record1 = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
_record2 = "h2o_feet,location=coyote_creek level\\ water_level=2 2"
_bytes = "h2o_feet,location=coyote_creek level\\ water_level=3 3".encode("utf-8")

p = (Point("h2o_feet").tag("location", "coyote_creek").field("level water_level", 4.0).time(4))

Expand Down Expand Up @@ -322,7 +322,7 @@ def test_write_data_frame_without_measurement_name(self):
def test_use_default_org(self):
bucket = self.create_test_bucket()

record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
record = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
self.write_client.write(bucket.name, record=record)

result = self.query_api.query(
Expand Down Expand Up @@ -485,7 +485,7 @@ def test_writes_synchronous_without_retry(self):

self.write_client = self.influxdb_client.write_api(write_options=SYNCHRONOUS)
with self.assertRaises(ApiException) as cm:
self.write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1.0 1")
self.write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1 1")
exception = cm.exception

self.assertEqual("Service Unavailable", exception.reason)
Expand All @@ -496,7 +496,7 @@ def test_writes_asynchronous_without_retry(self):

self.write_client = self.influxdb_client.write_api(write_options=ASYNCHRONOUS)
with self.assertRaises(ApiException) as cm:
self.write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1.0 1").get()
self.write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1 1").get()
exception = cm.exception

self.assertEqual("Service Unavailable", exception.reason)
Expand Down Expand Up @@ -527,7 +527,7 @@ def tearDown(self) -> None:
def test_write_result(self):
_bucket = self.create_test_bucket()

_record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
_record = "h2o_feet,location=coyote_creek level\\ water_level=1 1"
result = self.write_client.write(_bucket.name, self.org, _record)

self.assertEqual(ApplyResult, type(result))
Expand Down Expand Up @@ -649,8 +649,8 @@ def test_use_default_tags_with_data_frame(self):
def test_write_bytes(self):
bucket = self.create_test_bucket()

_bytes1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1".encode("utf-8")
_bytes2 = "h2o_feet,location=coyote_creek level\\ water_level=2.0 2".encode("utf-8")
_bytes1 = "h2o_feet,location=coyote_creek level\\ water_level=1 1".encode("utf-8")
_bytes2 = "h2o_feet,location=coyote_creek level\\ water_level=2 2".encode("utf-8")

_bytes_list = [_bytes1, _bytes2]

Expand Down
Loading