Skip to content

Commit 00c666a

Browse files
committed
Re-add nosetests that were removed in #1114
1 parent 73945d2 commit 00c666a

16 files changed

+1302
-0
lines changed

tests_old/test_blob.py

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
import datajoint as dj
2+
import timeit
3+
import numpy as np
4+
import uuid
5+
from . import schema
6+
from decimal import Decimal
7+
from datetime import datetime
8+
from datajoint.blob import pack, unpack
9+
from numpy.testing import assert_array_equal
10+
from nose.tools import (
11+
assert_equal,
12+
assert_true,
13+
assert_false,
14+
assert_list_equal,
15+
assert_set_equal,
16+
assert_tuple_equal,
17+
assert_dict_equal,
18+
)
19+
20+
21+
def test_pack():
22+
for x in (
23+
32,
24+
-3.7e-2,
25+
np.float64(3e31),
26+
-np.inf,
27+
np.int8(-3),
28+
np.uint8(-1),
29+
np.int16(-33),
30+
np.uint16(-33),
31+
np.int32(-3),
32+
np.uint32(-1),
33+
np.int64(373),
34+
np.uint64(-3),
35+
):
36+
assert_equal(x, unpack(pack(x)), "Scalars don't match!")
37+
38+
x = np.nan
39+
assert_true(np.isnan(unpack(pack(x))), "nan scalar did not match!")
40+
41+
x = np.random.randn(8, 10)
42+
assert_array_equal(x, unpack(pack(x)), "Arrays do not match!")
43+
44+
x = np.random.randn(10)
45+
assert_array_equal(x, unpack(pack(x)), "Arrays do not match!")
46+
47+
x = 7j
48+
assert_equal(x, unpack(pack(x)), "Complex scalar does not match")
49+
50+
x = np.float32(np.random.randn(3, 4, 5))
51+
assert_array_equal(x, unpack(pack(x)), "Arrays do not match!")
52+
53+
x = np.int16(np.random.randn(1, 2, 3))
54+
assert_array_equal(x, unpack(pack(x)), "Arrays do not match!")
55+
56+
x = None
57+
assert_true(unpack(pack(x)) is None, "None did not match")
58+
59+
x = -255
60+
y = unpack(pack(x))
61+
assert_true(
62+
x == y and isinstance(y, int) and not isinstance(y, np.ndarray),
63+
"Scalar int did not match",
64+
)
65+
66+
x = -25523987234234287910987234987098245697129798713407812347
67+
y = unpack(pack(x))
68+
assert_true(
69+
x == y and isinstance(y, int) and not isinstance(y, np.ndarray),
70+
"Unbounded int did not match",
71+
)
72+
73+
x = 7.0
74+
y = unpack(pack(x))
75+
assert_true(
76+
x == y and isinstance(y, float) and not isinstance(y, np.ndarray),
77+
"Scalar float did not match",
78+
)
79+
80+
x = 7j
81+
y = unpack(pack(x))
82+
assert_true(
83+
x == y and isinstance(y, complex) and not isinstance(y, np.ndarray),
84+
"Complex scalar did not match",
85+
)
86+
87+
x = True
88+
assert_true(unpack(pack(x)) is True, "Scalar bool did not match")
89+
90+
x = [None]
91+
assert_list_equal(x, unpack(pack(x)))
92+
93+
x = {
94+
"name": "Anonymous",
95+
"age": 15,
96+
99: datetime.now(),
97+
"range": [110, 190],
98+
(11, 12): None,
99+
}
100+
y = unpack(pack(x))
101+
assert_dict_equal(x, y, "Dict do not match!")
102+
assert_false(
103+
isinstance(["range"][0], np.ndarray), "Scalar int was coerced into array."
104+
)
105+
106+
x = uuid.uuid4()
107+
assert_equal(x, unpack(pack(x)), "UUID did not match")
108+
109+
x = Decimal("-112122121.000003000")
110+
assert_equal(x, unpack(pack(x)), "Decimal did not pack/unpack correctly")
111+
112+
x = [1, datetime.now(), {1: "one", "two": 2}, (1, 2)]
113+
assert_list_equal(x, unpack(pack(x)), "List did not pack/unpack correctly")
114+
115+
x = (1, datetime.now(), {1: "one", "two": 2}, (uuid.uuid4(), 2))
116+
assert_tuple_equal(x, unpack(pack(x)), "Tuple did not pack/unpack correctly")
117+
118+
x = (
119+
1,
120+
{datetime.now().date(): "today", "now": datetime.now().date()},
121+
{"yes!": [1, 2, np.array((3, 4))]},
122+
)
123+
y = unpack(pack(x))
124+
assert_dict_equal(x[1], y[1])
125+
assert_array_equal(x[2]["yes!"][2], y[2]["yes!"][2])
126+
127+
x = {"elephant"}
128+
assert_set_equal(x, unpack(pack(x)), "Set did not pack/unpack correctly")
129+
130+
x = tuple(range(10))
131+
assert_tuple_equal(
132+
x, unpack(pack(range(10))), "Iterator did not pack/unpack correctly"
133+
)
134+
135+
x = Decimal("1.24")
136+
assert_true(x == unpack(pack(x)), "Decimal object did not pack/unpack correctly")
137+
138+
x = datetime.now()
139+
assert_true(x == unpack(pack(x)), "Datetime object did not pack/unpack correctly")
140+
141+
x = np.bool_(True)
142+
assert_true(x == unpack(pack(x)), "Numpy bool object did not pack/unpack correctly")
143+
144+
x = "test"
145+
assert_true(x == unpack(pack(x)), "String object did not pack/unpack correctly")
146+
147+
x = np.array(["yes"])
148+
assert_true(
149+
x == unpack(pack(x)), "Numpy string array object did not pack/unpack correctly"
150+
)
151+
152+
x = np.datetime64("1998").astype("datetime64[us]")
153+
assert_true(x == unpack(pack(x)))
154+
155+
156+
def test_recarrays():
157+
x = np.array([(1.0, 2), (3.0, 4)], dtype=[("x", float), ("y", int)])
158+
assert_array_equal(x, unpack(pack(x)))
159+
160+
x = x.view(np.recarray)
161+
assert_array_equal(x, unpack(pack(x)))
162+
163+
x = np.array([(3, 4)], dtype=[("tmp0", float), ("tmp1", "O")]).view(np.recarray)
164+
assert_array_equal(x, unpack(pack(x)))
165+
166+
167+
def test_object_arrays():
168+
x = np.array(((1, 2, 3), True), dtype="object")
169+
assert_array_equal(x, unpack(pack(x)), "Object array did not serialize correctly")
170+
171+
172+
def test_complex():
173+
z = np.random.randn(8, 10) + 1j * np.random.randn(8, 10)
174+
assert_array_equal(z, unpack(pack(z)), "Arrays do not match!")
175+
176+
z = np.random.randn(10) + 1j * np.random.randn(10)
177+
assert_array_equal(z, unpack(pack(z)), "Arrays do not match!")
178+
179+
x = np.float32(np.random.randn(3, 4, 5)) + 1j * np.float32(np.random.randn(3, 4, 5))
180+
assert_array_equal(x, unpack(pack(x)), "Arrays do not match!")
181+
182+
x = np.int16(np.random.randn(1, 2, 3)) + 1j * np.int16(np.random.randn(1, 2, 3))
183+
assert_array_equal(x, unpack(pack(x)), "Arrays do not match!")
184+
185+
186+
def test_insert_longblob():
187+
insert_dj_blob = {"id": 1, "data": [1, 2, 3]}
188+
schema.Longblob.insert1(insert_dj_blob)
189+
assert (schema.Longblob & "id=1").fetch1() == insert_dj_blob
190+
(schema.Longblob & "id=1").delete()
191+
192+
query_mym_blob = {"id": 1, "data": np.array([1, 2, 3])}
193+
schema.Longblob.insert1(query_mym_blob)
194+
assert (schema.Longblob & "id=1").fetch1()["data"].all() == query_mym_blob[
195+
"data"
196+
].all()
197+
(schema.Longblob & "id=1").delete()
198+
199+
query_32_blob = (
200+
"INSERT INTO djtest_test1.longblob (id, data) VALUES (1, "
201+
"X'6D596D00530200000001000000010000000400000068697473007369646573007461736B73007374"
202+
"616765004D000000410200000001000000070000000600000000000000000000000000F8FF00000000"
203+
"0000F03F000000000000F03F0000000000000000000000000000F03F00000000000000000000000000"
204+
"00F8FF230000004102000000010000000700000004000000000000006C006C006C006C00720072006C"
205+
"0023000000410200000001000000070000000400000000000000640064006400640064006400640025"
206+
"00000041020000000100000008000000040000000000000053007400610067006500200031003000')"
207+
)
208+
dj.conn().query(query_32_blob).fetchall()
209+
dj.blob.use_32bit_dims = True
210+
assert (schema.Longblob & "id=1").fetch1() == {
211+
"id": 1,
212+
"data": np.rec.array(
213+
[
214+
[
215+
(
216+
np.array([[np.nan, 1.0, 1.0, 0.0, 1.0, 0.0, np.nan]]),
217+
np.array(["llllrrl"], dtype="<U7"),
218+
np.array(["ddddddd"], dtype="<U7"),
219+
np.array(["Stage 10"], dtype="<U8"),
220+
)
221+
]
222+
],
223+
dtype=[("hits", "O"), ("sides", "O"), ("tasks", "O"), ("stage", "O")],
224+
),
225+
}
226+
(schema.Longblob & "id=1").delete()
227+
dj.blob.use_32bit_dims = False
228+
229+
230+
def test_datetime_serialization_speed():
231+
# If this fails that means for some reason deserializing/serializing
232+
# np arrays of np.datetime64 types is now slower than regular arrays of datetime
233+
234+
optimized_exe_time = timeit.timeit(
235+
setup="myarr=pack(np.array([np.datetime64('2022-10-13 03:03:13') for _ in range(0, 10000)]))",
236+
stmt="unpack(myarr)",
237+
number=10,
238+
globals=globals(),
239+
)
240+
print(f"np time {optimized_exe_time}")
241+
baseline_exe_time = timeit.timeit(
242+
setup="myarr2=pack(np.array([datetime(2022,10,13,3,3,13) for _ in range (0, 10000)]))",
243+
stmt="unpack(myarr2)",
244+
number=10,
245+
globals=globals(),
246+
)
247+
print(f"python time {baseline_exe_time}")
248+
249+
assert optimized_exe_time * 900 < baseline_exe_time

0 commit comments

Comments
 (0)