1+ import pytest
12import datajoint as dj
23import timeit
34import numpy as np
1011from .schema import Longblob
1112
1213
14+ @pytest .fixture
15+ def enable_feature_32bit_dims ():
16+ dj .blob .use_32bit_dims = True
17+ yield
18+ dj .blob .use_32bit_dims = False
19+
20+
1321def test_pack ():
1422 for x in (
1523 32 ,
@@ -180,6 +188,8 @@ def test_insert_longblob(schema_any):
180188 assert (Longblob & "id=1" ).fetch1 ()["data" ].all () == query_mym_blob ["data" ].all ()
181189 (Longblob & "id=1" ).delete ()
182190
191+
192+ def test_insert_longblob_32bit (schema_any , enable_feature_32bit_dims ):
183193 query_32_blob = (
184194 "INSERT INTO djtest_test1.longblob (id, data) VALUES (1, "
185195 "X'6D596D00530200000001000000010000000400000068697473007369646573007461736B73007374"
@@ -190,7 +200,6 @@ def test_insert_longblob(schema_any):
190200 "00000041020000000100000008000000040000000000000053007400610067006500200031003000')"
191201 )
192202 dj .conn ().query (query_32_blob ).fetchall ()
193- dj .blob .use_32bit_dims = True
194203 fetched = (Longblob & "id=1" ).fetch1 ()
195204 expected = {
196205 "id" : 1 ,
@@ -211,25 +220,31 @@ def test_insert_longblob(schema_any):
211220 assert fetched ["id" ] == expected ["id" ]
212221 assert np .array_equal (fetched ["data" ], expected ["data" ])
213222 (Longblob & "id=1" ).delete ()
214- dj .blob .use_32bit_dims = False
215223
216224
217225def test_datetime_serialization_speed ():
218226 # If this fails that means for some reason deserializing/serializing
219227 # np arrays of np.datetime64 types is now slower than regular arrays of datetime
228+ assert not dj .blob .use_32bit_dims , "32 bit dims should be off for this test"
229+ context = dict (
230+ np = np ,
231+ datetime = datetime ,
232+ pack = pack ,
233+ unpack = unpack ,
234+ )
220235
221236 optimized_exe_time = timeit .timeit (
222237 setup = "myarr=pack(np.array([np.datetime64('2022-10-13 03:03:13') for _ in range(0, 10000)]))" ,
223238 stmt = "unpack(myarr)" ,
224239 number = 10 ,
225- globals = globals () ,
240+ globals = context ,
226241 )
227242 print (f"np time { optimized_exe_time } " )
228243 baseline_exe_time = timeit .timeit (
229244 setup = "myarr2=pack(np.array([datetime(2022,10,13,3,3,13) for _ in range (0, 10000)]))" ,
230245 stmt = "unpack(myarr2)" ,
231246 number = 10 ,
232- globals = globals () ,
247+ globals = context ,
233248 )
234249 print (f"python time { baseline_exe_time } " )
235250
0 commit comments