FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
datajoint-python/tests/test_json.py at master · sfernandopeiris/datajoint-python · GitHub
sfernandopeiris
datajoint-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
datajoint-python
/
tests
/
test_json.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
219 lines (186 loc) · 7.25 KB
Breadcrumbs
datajoint-python
/
tests
/
test_json.py
Copy path
File metadata and controls
219 lines (186 loc) · 7.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import
inspect
from
datajoint
.
declare
import
declare
import
datajoint
as
dj
import
numpy
as
np
from
distutils
.
version
import
LooseVersion
from
.
import
PREFIX
if
LooseVersion
(
dj
.
conn
().
query
(
"select @@version;"
).
fetchone
()[
0
])
>=
LooseVersion
(
"8.0.0"
):
schema
=
dj
.
Schema
(
PREFIX
+
"_json"
)
Team
=
None
def
setup
():
global
Team
@
schema
class
Team
(
dj
.
Lookup
):
definition
=
"""
name: varchar(40)
---
car=null: json
unique index(car.name:char(20))
uniQue inDex ( name, car.name:char(20), (json_value(`car`, _utf8mb4'$.length' returning decimal(4, 1))) )
"""
contents
=
[
(
"engineering"
,
{
"name"
:
"Rever"
,
"length"
:
20.5
,
"inspected"
:
True
,
"tire_pressure"
: [
32
,
31
,
33
,
34
],
"headlights"
: [
{
"side"
:
"left"
,
"hyper_white"
:
None
,
},
{
"side"
:
"right"
,
"hyper_white"
:
None
,
},
],
},
),
(
"business"
,
{
"name"
:
"Chaching"
,
"length"
:
100
,
"safety_inspected"
:
False
,
"tire_pressure"
: [
34
,
30
,
27
,
32
],
"headlights"
: [
{
"side"
:
"left"
,
"hyper_white"
:
True
,
},
{
"side"
:
"right"
,
"hyper_white"
:
True
,
},
],
},
),
(
"marketing"
,
None
,
),
]
def
teardown
():
schema
.
drop
()
def
test_insert_update
():
car
=
{
"name"
:
"Discovery"
,
"length"
:
22.9
,
"inspected"
:
None
,
"tire_pressure"
: [
35
,
36
,
34
,
37
],
"headlights"
: [
{
"side"
:
"left"
,
"hyper_white"
:
True
,
},
{
"side"
:
"right"
,
"hyper_white"
:
True
,
},
],
}
Team
.
insert1
({
"name"
:
"research"
,
"car"
:
car
})
q
=
Team
&
{
"name"
:
"research"
}
assert
q
.
fetch1
(
"car"
)
==
car
car
.
update
({
"length"
:
23
})
Team
.
update1
({
"name"
:
"research"
,
"car"
:
car
})
assert
q
.
fetch1
(
"car"
)
==
car
try
:
Team
.
insert1
({
"name"
:
"hr"
,
"car"
:
car
})
raise
Exception
(
"Inserted non-unique car name."
)
except
dj
.
DataJointError
:
pass
q
.
delete_quick
()
assert
not
q
def
test_describe
():
rel
=
Team
()
context
=
inspect
.
currentframe
().
f_globals
s1
=
declare
(
rel
.
full_table_name
,
rel
.
definition
,
context
)
s2
=
declare
(
rel
.
full_table_name
,
rel
.
describe
(),
context
)
assert
s1
==
s2
def
test_restrict
():
# dict
assert
(
Team
&
{
"car.name"
:
"Chaching"
}).
fetch1
(
"name"
)
==
"business"
assert
(
Team
&
{
"car.length"
:
20.5
}).
fetch1
(
"name"
)
==
"engineering"
assert
(
Team
&
{
"car.inspected"
:
"true"
}).
fetch1
(
"name"
)
==
"engineering"
assert
(
Team
&
{
"car.inspected:unsigned"
:
True
}).
fetch1
(
"name"
)
==
"engineering"
assert
(
Team
&
{
"car.safety_inspected"
:
"false"
}).
fetch1
(
"name"
)
==
"business"
assert
(
Team
&
{
"car.safety_inspected:unsigned"
:
False
}).
fetch1
(
"name"
)
==
"business"
assert
(
Team
&
{
"car.headlights[0].hyper_white"
:
None
}).
fetch
(
"name"
,
order_by
=
"name"
,
as_dict
=
True
)
==
[
{
"name"
:
"engineering"
},
{
"name"
:
"marketing"
},
]
# if entire record missing, JSON key is missing, or value set to JSON null
assert
(
Team
&
{
"car"
:
None
}).
fetch1
(
"name"
)
==
"marketing"
assert
(
Team
&
{
"car.tire_pressure"
: [
34
,
30
,
27
,
32
]}).
fetch1
(
"name"
)
==
"business"
assert
(
Team
&
{
"car.headlights[1]"
: {
"side"
:
"right"
,
"hyper_white"
:
True
}}
).
fetch1
(
"name"
)
==
"business"
# sql operators
assert
(
Team
&
"`car`->>'$.name' LIKE '%ching%'"
).
fetch1
(
"name"
)
==
"business"
,
"Missing substring"
assert
(
Team
&
"`car`->>'$.length' > 30"
).
fetch1
(
"name"
)
==
"business"
,
"<= 30"
assert
(
Team
&
"JSON_VALUE(`car`, '$.safety_inspected' RETURNING UNSIGNED) = 0"
).
fetch1
(
"name"
)
==
"business"
,
"Has `safety_inspected` set to `true`"
assert
(
Team
&
"`car`->>'$.headlights[0].hyper_white' = 'null'"
).
fetch1
(
"name"
)
==
"engineering"
,
"Has 1st `headlight` with `hyper_white` not set to `null`"
assert
(
Team
&
"`car`->>'$.inspected' IS NOT NULL"
).
fetch1
(
"name"
)
==
"engineering"
,
"Missing `inspected` key"
assert
(
Team
&
"`car`->>'$.tire_pressure' = '[34, 30, 27, 32]'"
).
fetch1
(
"name"
)
==
"business"
,
"`tire_pressure` array did not match"
assert
(
Team
&
"""`car`->>'$.headlights[1]' = '{"side": "right", "hyper_white": true}'"""
).
fetch1
(
"name"
)
==
"business"
,
"2nd `headlight` object did not match"
def
test_proj
():
# proj necessary since we need to rename indexed value into a proper attribute name
assert
Team
.
proj
(
car_length
=
"car.length"
).
fetch
(
as_dict
=
True
,
order_by
=
"car_length"
)
==
[
{
"name"
:
"marketing"
,
"car_length"
:
None
},
{
"name"
:
"business"
,
"car_length"
:
"100"
},
{
"name"
:
"engineering"
,
"car_length"
:
"20.5"
},
]
assert
Team
.
proj
(
car_length
=
"car.length:decimal(4, 1)"
).
fetch
(
as_dict
=
True
,
order_by
=
"car_length"
)
==
[
{
"name"
:
"marketing"
,
"car_length"
:
None
},
{
"name"
:
"engineering"
,
"car_length"
:
20.5
},
{
"name"
:
"business"
,
"car_length"
:
100.0
},
]
assert
Team
.
proj
(
car_width
=
"JSON_VALUE(`car`, '$.length' RETURNING float) - 15"
).
fetch
(
as_dict
=
True
,
order_by
=
"car_width"
)
==
[
{
"name"
:
"marketing"
,
"car_width"
:
None
},
{
"name"
:
"engineering"
,
"car_width"
:
5.5
},
{
"name"
:
"business"
,
"car_width"
:
85.0
},
]
assert
(
(
Team
&
{
"name"
:
"engineering"
}).
proj
(
car_tire_pressure
=
"car.tire_pressure"
)
).
fetch1
(
"car_tire_pressure"
)
==
"[32, 31, 33, 34]"
assert
np
.
array_equal
(
Team
.
proj
(
car_inspected
=
"car.inspected"
).
fetch
(
"car_inspected"
,
order_by
=
"name"
),
np
.
array
([
None
,
"true"
,
None
]),
)
assert
np
.
array_equal
(
Team
.
proj
(
car_inspected
=
"car.inspected:unsigned"
).
fetch
(
"car_inspected"
,
order_by
=
"name"
),
np
.
array
([
None
,
1
,
None
]),
)
Back
|
FazBrowse Home
|
New Git URL