Skip to content

Commit b195e38

Browse files
committed
[FIX] unitests for (influxdata#649) ns precision timestamps
1 parent b3b6fe7 commit b195e38

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

influxdb/tests/client_test.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def setUp(self):
9090
"host": "server01",
9191
"region": "us-west"
9292
},
93-
"time": "2009-11-10T23:00:00.123456Z",
93+
"time": "2009-11-10 23:10:55.123456789",
9494
"fields": {
9595
"value": 0.64
9696
}
@@ -202,7 +202,7 @@ def test_write_points(self):
202202
)
203203
self.assertEqual(
204204
'cpu_load_short,host=server01,region=us-west '
205-
'value=0.64 1257894000123456000\n',
205+
'value=0.64 1257894655123456789\n',
206206
m.last_request.body.decode('utf-8'),
207207
)
208208

@@ -224,7 +224,7 @@ def test_write_points_toplevel_attributes(self):
224224
)
225225
self.assertEqual(
226226
'cpu_load_short,host=server01,region=us-west,tag=hello '
227-
'value=0.64 1257894000123456000\n',
227+
'value=0.64 1257894655123456789\n',
228228
m.last_request.body.decode('utf-8'),
229229
)
230230

@@ -273,7 +273,7 @@ def test_write_points_udp(self):
273273

274274
self.assertEqual(
275275
'cpu_load_short,host=server01,region=us-west '
276-
'value=0.64 1257894000123456000\n',
276+
'value=0.64 1257894655123456789\n',
277277
received_data.decode()
278278
)
279279

@@ -298,35 +298,35 @@ def test_write_points_with_precision(self):
298298
cli.write_points(self.dummy_points, time_precision='n')
299299
self.assertEqual(
300300
b'cpu_load_short,host=server01,region=us-west '
301-
b'value=0.64 1257894000123456000\n',
301+
b'value=0.64 1257894655123456789\n',
302302
m.last_request.body,
303303
)
304304

305305
cli.write_points(self.dummy_points, time_precision='u')
306306
self.assertEqual(
307307
b'cpu_load_short,host=server01,region=us-west '
308-
b'value=0.64 1257894000123456\n',
308+
b'value=0.64 1257894655123456\n',
309309
m.last_request.body,
310310
)
311311

312312
cli.write_points(self.dummy_points, time_precision='ms')
313313
self.assertEqual(
314314
b'cpu_load_short,host=server01,region=us-west '
315-
b'value=0.64 1257894000123\n',
315+
b'value=0.64 1257894655123\n',
316316
m.last_request.body,
317317
)
318318

319319
cli.write_points(self.dummy_points, time_precision='s')
320320
self.assertEqual(
321321
b"cpu_load_short,host=server01,region=us-west "
322-
b"value=0.64 1257894000\n",
322+
b"value=0.64 1257894655\n",
323323
m.last_request.body,
324324
)
325325

326326
cli.write_points(self.dummy_points, time_precision='m')
327327
self.assertEqual(
328328
b'cpu_load_short,host=server01,region=us-west '
329-
b'value=0.64 20964900\n',
329+
b'value=0.64 20964910\n',
330330
m.last_request.body,
331331
)
332332

@@ -352,39 +352,39 @@ def test_write_points_with_precision_udp(self):
352352
received_data, addr = s.recvfrom(1024)
353353
self.assertEqual(
354354
b'cpu_load_short,host=server01,region=us-west '
355-
b'value=0.64 1257894000123456000\n',
355+
b'value=0.64 1257894655123456789\n',
356356
received_data,
357357
)
358358

359359
cli.write_points(self.dummy_points, time_precision='u')
360360
received_data, addr = s.recvfrom(1024)
361361
self.assertEqual(
362362
b'cpu_load_short,host=server01,region=us-west '
363-
b'value=0.64 1257894000123456\n',
363+
b'value=0.64 1257894655123456\n',
364364
received_data,
365365
)
366366

367367
cli.write_points(self.dummy_points, time_precision='ms')
368368
received_data, addr = s.recvfrom(1024)
369369
self.assertEqual(
370370
b'cpu_load_short,host=server01,region=us-west '
371-
b'value=0.64 1257894000123\n',
371+
b'value=0.64 1257894655123\n',
372372
received_data,
373373
)
374374

375375
cli.write_points(self.dummy_points, time_precision='s')
376376
received_data, addr = s.recvfrom(1024)
377377
self.assertEqual(
378378
b"cpu_load_short,host=server01,region=us-west "
379-
b"value=0.64 1257894000\n",
379+
b"value=0.64 1257894655\n",
380380
received_data,
381381
)
382382

383383
cli.write_points(self.dummy_points, time_precision='m')
384384
received_data, addr = s.recvfrom(1024)
385385
self.assertEqual(
386386
b'cpu_load_short,host=server01,region=us-west '
387-
b'value=0.64 20964900\n',
387+
b'value=0.64 20964910\n',
388388
received_data,
389389
)
390390

influxdb/tests/dataframe_client_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -914,36 +914,36 @@ def test_get_list_database(self):
914914

915915
def test_datetime_to_epoch(self):
916916
"""Test convert datetime to epoch in TestDataFrameClient object."""
917-
timestamp = pd.Timestamp('2013-01-01 00:00:00.000+00:00')
917+
timestamp = pd.Timestamp('2013-01-01 23:10:55.123456789+00:00')
918918
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
919919

920920
self.assertEqual(
921921
cli._datetime_to_epoch(timestamp),
922-
1356998400.0
922+
1357081855
923923
)
924924
self.assertEqual(
925925
cli._datetime_to_epoch(timestamp, time_precision='h'),
926-
1356998400.0 / 3600
926+
1357081855 // 3600
927927
)
928928
self.assertEqual(
929929
cli._datetime_to_epoch(timestamp, time_precision='m'),
930-
1356998400.0 / 60
930+
1357081855 // 60
931931
)
932932
self.assertEqual(
933933
cli._datetime_to_epoch(timestamp, time_precision='s'),
934-
1356998400.0
934+
1357081855
935935
)
936936
self.assertEqual(
937937
cli._datetime_to_epoch(timestamp, time_precision='ms'),
938-
1356998400000.0
938+
1357081855123
939939
)
940940
self.assertEqual(
941941
cli._datetime_to_epoch(timestamp, time_precision='u'),
942-
1356998400000000.0
942+
1357081855123456
943943
)
944944
self.assertEqual(
945945
cli._datetime_to_epoch(timestamp, time_precision='n'),
946-
1356998400000000000.0
946+
1357081855123456789
947947
)
948948

949949
def test_dsn_constructor(self):

influxdb/tests/test_line_protocol.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pytz import UTC, timezone
1212

1313
from influxdb import line_protocol
14-
14+
import pandas as pd
1515

1616
class TestLineProtocol(unittest.TestCase):
1717
"""Define the LineProtocol test object."""
@@ -48,31 +48,54 @@ def test_make_lines(self):
4848

4949
def test_timezone(self):
5050
"""Test timezone in TestLineProtocol object."""
51+
# datetime tests
5152
dt = datetime(2009, 11, 10, 23, 0, 0, 123456)
5253
utc = UTC.localize(dt)
5354
berlin = timezone('Europe/Berlin').localize(dt)
5455
eastern = berlin.astimezone(timezone('US/Eastern'))
55-
data = {
56-
"points": [
56+
# pandas ns timestamp tests
57+
pddt = pd.Timestamp('2009-11-10 23:00:00.123456789')
58+
pdutc = pd.Timestamp(pddt, tz = 'UTC')
59+
pdberlin = pdutc.astimezone('Europe/Berlin')
60+
pdeastern = pdberlin.astimezone('US/Eastern')
61+
62+
data = { "points": [
5763
{"measurement": "A", "fields": {"val": 1},
58-
"time": 0},
64+
"time": 0},
65+
#string representations
5966
{"measurement": "A", "fields": {"val": 1},
60-
"time": "2009-11-10T23:00:00.123456Z"},
67+
"time": "2009-11-10T23:00:00.123456Z"}, # String version for datetime
68+
{"measurement": "A", "fields": {"val": 1},
69+
"time": "2009-11-10 23:00:00.123456789"}, # String version for pandas ns timestamp
70+
# datetime
6171
{"measurement": "A", "fields": {"val": 1}, "time": dt},
6272
{"measurement": "A", "fields": {"val": 1}, "time": utc},
6373
{"measurement": "A", "fields": {"val": 1}, "time": berlin},
6474
{"measurement": "A", "fields": {"val": 1}, "time": eastern},
65-
]
66-
}
75+
# pandas timestamp
76+
{"measurement": "A", "fields": {"val": 1}, "time": pddt},
77+
{"measurement": "A", "fields": {"val": 1}, "time": pdutc},
78+
{"measurement": "A", "fields": {"val": 1}, "time": pdberlin},
79+
{"measurement": "A", "fields": {"val": 1}, "time": pdeastern},
80+
]}
81+
82+
6783
self.assertEqual(
6884
line_protocol.make_lines(data),
6985
'\n'.join([
7086
'A val=1i 0',
7187
'A val=1i 1257894000123456000',
88+
'A val=1i 1257894000123456789',
89+
#datetime results
7290
'A val=1i 1257894000123456000',
7391
'A val=1i 1257894000123456000',
7492
'A val=1i 1257890400123456000',
7593
'A val=1i 1257890400123456000',
94+
#pandas ns timestamp results
95+
'A val=1i 1257894000123456789',
96+
'A val=1i 1257894000123456789',
97+
'A val=1i 1257894000123456789',
98+
'A val=1i 1257894000123456789',
7699
]) + '\n'
77100
)
78101

0 commit comments

Comments
 (0)