Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Fix tests for existing 'Adding time_precision optional option to SeriesHelper' PR #719

Merged
merged 13 commits into from
Apr 10, 2020
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
20 changes: 17 additions & 3 deletions influxdb/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class Meta:
# Only applicable if autocommit is True.
autocommit = True
# If True and no bulk_size, then will set bulk_size to 1.
time_precision = "h"|"m"|s"|"ms"|"u"|"ns"
# Default is ns (nanoseconds)
# Setting time precision while writing point
# You should also make sure time is set in the given precision

"""

Expand Down Expand Up @@ -71,6 +75,13 @@ def __new__(cls, *args, **kwargs):
cls.__name__))

cls._autocommit = getattr(_meta, 'autocommit', False)
cls._time_precision = getattr(_meta, 'time_precision', None)

allowed_time_precisions = ['h', 'm', 's', 'ms', 'u', 'ns', None]
if cls._time_precision not in allowed_time_precisions:
raise AttributeError(
'In {0}, time_precision is set, but invalid use any of {}.'
.format(cls.__name__, ','.join(allowed_time_precisions)))

cls._client = getattr(_meta, 'client', None)
if cls._autocommit and not cls._client:
Expand Down Expand Up @@ -116,11 +127,11 @@ def __init__(self, **kw):
keys = set(kw.keys())

# all tags should be passed, and keys - tags should be a subset of keys
if not(tags <= keys):
if not (tags <= keys):
raise NameError(
'Expected arguments to contain all tags {0}, instead got {1}.'
.format(cls._tags, kw.keys()))
if not(keys - tags <= fields):
if not (keys - tags <= fields):
raise NameError('Got arguments not in tags or fields: {0}'
.format(keys - tags - fields))

Expand All @@ -143,7 +154,10 @@ def commit(cls, client=None):
"""
if not client:
client = cls._client
rtn = client.write_points(cls._json_body_())
rtn = client.write_points(
cls._json_body_(),
time_precision=cls._time_precision)
# will be None if not set and will default to ns
cls._reset_()
return rtn

Expand Down
13 changes: 12 additions & 1 deletion influxdb/tests/helper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,19 @@ class Meta:

series_name = 'events.stats.{server_name}'

class InvalidTimePrecision(SeriesHelper):
"""Define instance of SeriesHelper for invalid time precision."""

class Meta:
"""Define metadata for InvalidTimePrecision."""

series_name = 'events.stats.{server_name}'
time_precision = "ks"
fields = ['time', 'server_name']
autocommit = True

for cls in [MissingMeta, MissingClient, MissingFields,
MissingSeriesName]:
MissingSeriesName, InvalidTimePrecision]:
self.assertRaises(
AttributeError, cls, **{'time': 159,
'server_name': 'us.east-1'})
Expand Down