3
3
import platform
4
4
import random
5
5
import string
6
+
6
7
import riak .pb .riak_ts_pb2
7
8
8
9
from riak import RiakError
9
10
from riak .table import Table
10
11
from riak .ts_object import TsObject
11
12
from riak .transports .pbc .codec import RiakPbcCodec
12
- from riak .util import str_to_bytes , bytes_to_str
13
+ from riak .util import str_to_bytes , bytes_to_str , \
14
+ is_timeseries_supported
13
15
from riak .tests import RUN_TIMESERIES
14
16
from riak .tests .base import IntegrationTestBase
15
17
from riak .pb .riak_ts_pb2 import TsColumnType
25
27
bd1 = 'временные ряды'
26
28
27
29
fiveMins = datetime .timedelta (0 , 300 )
28
- ts0 = datetime .datetime (2015 , 1 , 1 , 12 , 0 , 0 )
30
+ # NB: last arg is microseconds, 987ms expressed
31
+ ts0 = datetime .datetime (2015 , 1 , 1 , 12 , 0 , 0 , 987000 )
32
+ ex0ms = 1420113600987
33
+
29
34
ts1 = ts0 + fiveMins
35
+ ex1ms = 1420113900987
30
36
31
37
38
+ @unittest .skipUnless (is_timeseries_supported (), "Timeseries not supported" )
32
39
class TimeseriesUnitTests (unittest .TestCase ):
33
- def setUp (self ):
34
- self .c = RiakPbcCodec ()
35
- self .ts0ms = self .c ._unix_time_millis (ts0 )
36
- self .ts1ms = self .c ._unix_time_millis (ts1 )
37
- self .rows = [
40
+ @classmethod
41
+ def setUpClass (cls ):
42
+ cls .c = RiakPbcCodec ()
43
+
44
+ cls .ts0ms = cls .c ._unix_time_millis (ts0 )
45
+ if cls .ts0ms != ex0ms :
46
+ raise AssertionError (
47
+ 'expected {:d} to equal {:d}' .format (cls .ts0ms , ex0ms ))
48
+
49
+ cls .ts1ms = cls .c ._unix_time_millis (ts1 )
50
+ if cls .ts1ms != ex1ms :
51
+ raise AssertionError (
52
+ 'expected {:d} to equal {:d}' .format (cls .ts1ms , ex1ms ))
53
+
54
+ cls .rows = [
38
55
[bd0 , 0 , 1.2 , ts0 , True ],
39
56
[bd1 , 3 , 4.5 , ts1 , False ]
40
57
]
41
- self .test_key = ['hash1' , 'user2' , ts0 ]
42
- self .table = Table (None , 'test-table' )
58
+ cls .test_key = ['hash1' , 'user2' , ts0 ]
59
+ cls .table = Table (None , 'test-table' )
43
60
44
61
def validate_keyreq (self , req ):
45
62
self .assertEqual (self .table .name , bytes_to_str (req .table ))
@@ -48,6 +65,12 @@ def validate_keyreq(self, req):
48
65
self .assertEqual ('user2' , bytes_to_str (req .key [1 ].varchar_value ))
49
66
self .assertEqual (self .ts0ms , req .key [2 ].timestamp_value )
50
67
68
+ def test_encode_decode_timestamp (self ):
69
+ ts0ms = self .c ._unix_time_millis (ts0 )
70
+ self .assertEqual (ts0ms , ex0ms )
71
+ ts0_d = self .c ._datetime_from_unix_time_millis (ts0ms )
72
+ self .assertEqual (ts0 , ts0_d )
73
+
51
74
def test_encode_data_for_get (self ):
52
75
req = riak .pb .riak_ts_pb2 .TsGetReq ()
53
76
self .c ._encode_timeseries_keyreq (self .table , self .test_key , req )
@@ -166,12 +189,13 @@ def test_decode_data_from_query(self):
166
189
self .assertEqual (r1 [4 ], self .rows [1 ][4 ])
167
190
168
191
169
- @unittest .skipUnless (RUN_TIMESERIES , 'RUN_TIMESERIES is 0' )
192
+ @unittest .skipUnless (is_timeseries_supported () and RUN_TIMESERIES ,
193
+ 'Timeseries not supported or RUN_TIMESERIES is 0' )
170
194
class TimeseriesTests (IntegrationTestBase , unittest .TestCase ):
171
195
@classmethod
172
196
def setUpClass (cls ):
173
197
super (TimeseriesTests , cls ).setUpClass ()
174
- cls .now = datetime .datetime .utcfromtimestamp (144379690 )
198
+ cls .now = datetime .datetime .utcfromtimestamp (144379690.987000 )
175
199
fiveMinsAgo = cls .now - fiveMins
176
200
tenMinsAgo = fiveMinsAgo - fiveMins
177
201
fifteenMinsAgo = tenMinsAgo - fiveMins
@@ -211,6 +235,7 @@ def validate_data(self, ts_obj):
211
235
self .assertEqual (row [0 ], 'hash1' )
212
236
self .assertEqual (row [1 ], 'user2' )
213
237
self .assertEqual (row [2 ], self .fiveMinsAgo )
238
+ self .assertEqual (row [2 ].microsecond , 987000 )
214
239
self .assertEqual (row [3 ], 'wind' )
215
240
self .assertIsNone (row [4 ])
216
241
0 commit comments