Skip to content

Commit 2a7aed2

Browse files
Update test schemas to use core DataJoint types
- Replace int/smallint/tinyint with int32/int16/uint8 - Replace float/double with float32/float64 - Replace timestamp with datetime - Convert Auto from autoincrement to explicit Lookup values - Update fixture teardown to check schema.exists - Update test_alter_part regex to handle type comments 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4e6f230 commit 2a7aed2

File tree

5 files changed

+66
-67
lines changed

5 files changed

+66
-67
lines changed

tests/conftest.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,13 @@ def schema_any(connection_test, prefix):
574574
# Restore original config value after all tables are declared
575575
dj.config.jobs.allow_new_pk_fields_in_computed_tables = original_value
576576
yield schema_any
577-
# Clean up job tables before dropping schema
578-
for job in schema_any.jobs:
579-
try:
580-
job.delete()
581-
except DataJointError:
582-
pass
577+
# Clean up job tables before dropping schema (if schema still exists)
578+
if schema_any.exists:
579+
for job in schema_any.jobs:
580+
try:
581+
job.delete()
582+
except DataJointError:
583+
pass
583584
schema_any.drop()
584585

585586

@@ -638,12 +639,13 @@ def schema_any_fresh(connection_test, prefix):
638639
# Restore original config value after all tables are declared
639640
dj.config.jobs.allow_new_pk_fields_in_computed_tables = original_value
640641
yield schema_any
641-
# Clean up job tables before dropping schema
642-
for job in schema_any.jobs:
643-
try:
644-
job.delete()
645-
except DataJointError:
646-
pass
642+
# Clean up job tables before dropping schema (if schema still exists)
643+
if schema_any.exists:
644+
for job in schema_any.jobs:
645+
try:
646+
job.delete()
647+
except DataJointError:
648+
pass
647649
schema_any.drop()
648650

649651

tests/integration/test_alter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ def test_alter_part(self, schema_alter):
4545
"""
4646
https://github.com/datajoint/datajoint-python/issues/936
4747
"""
48-
self.verify_alter(schema_alter, table=Parent.Child, attribute_sql="`child_id` .* DEFAULT NULL")
48+
# Regex includes optional COMMENT for type annotations
49+
self.verify_alter(schema_alter, table=Parent.Child, attribute_sql=r"`child_id` .* DEFAULT NULL[^,]*")
4950
self.verify_alter(
5051
schema_alter,
5152
table=Parent.Grandchild,
52-
attribute_sql="`grandchild_id` .* DEFAULT NULL",
53+
attribute_sql=r"`grandchild_id` .* DEFAULT NULL[^,]*",
5354
)

tests/integration/test_declare.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,17 @@ class Type(dj.Part):
114114

115115
def test_attributes(schema_any):
116116
"""
117-
Test autoincrement declaration
117+
Test attribute declarations
118118
"""
119119
auto = Auto()
120-
auto.fill()
121120
subject = Subject()
122121
experiment = Experiment()
123122
trial = Trial()
124123
ephys = Ephys()
125124
channel = Ephys.Channel()
126125

127126
assert auto.heading.names == ["id", "name"]
128-
assert auto.heading.attributes["id"].autoincrement
127+
assert auto.heading.attributes["id"].numeric
129128

130129
# test attribute declarations
131130
assert subject.heading.names == [

tests/schema.py

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,36 @@ class TTest(dj.Lookup):
1616
"""
1717

1818
definition = """
19-
key : int # key
19+
key : int32 # key
2020
---
21-
value : int # value
21+
value : int32 # value
2222
"""
2323
contents = [(k, 2 * k) for k in range(10)]
2424

2525

2626
class TTest2(dj.Manual):
2727
definition = """
28-
key : int # key
28+
key : int32 # key
2929
---
30-
value : int # value
30+
value : int32 # value
3131
"""
3232

3333

3434
class TTest3(dj.Manual):
3535
definition = """
36-
key : int
36+
key : int32
3737
---
3838
value : varchar(300)
3939
"""
4040

4141

4242
class NullableNumbers(dj.Manual):
4343
definition = """
44-
key : int
44+
key : int32
4545
---
46-
fvalue = null : float
47-
dvalue = null : double
48-
ivalue = null : int
46+
fvalue = null : float32
47+
dvalue = null : float64
48+
ivalue = null : int32
4949
"""
5050

5151

@@ -54,7 +54,7 @@ class TTestExtra(dj.Manual):
5454
clone of Test but with an extra field
5555
"""
5656

57-
definition = TTest.definition + "\nextra : int # extra int\n"
57+
definition = TTest.definition + "\nextra : int32 # extra int\n"
5858

5959

6060
class TTestNoExtra(dj.Manual):
@@ -67,14 +67,11 @@ class TTestNoExtra(dj.Manual):
6767

6868
class Auto(dj.Lookup):
6969
definition = """
70-
id :int auto_increment
70+
id : uint8
7171
---
7272
name :varchar(12)
7373
"""
74-
75-
def fill(self):
76-
if not self:
77-
self.insert([dict(name="Godel"), dict(name="Escher"), dict(name="Bach")])
74+
contents = [(1, "Godel"), (2, "Escher"), (3, "Bach")]
7875

7976

8077
class User(dj.Lookup):
@@ -94,7 +91,7 @@ class User(dj.Lookup):
9491

9592
class Subject(dj.Lookup):
9693
definition = """ # Basic information about animal subjects used in experiments
97-
subject_id :int # unique subject id
94+
subject_id :int32 # unique subject id
9895
---
9996
real_id :varchar(40) # real-world name. Omit if the same as subject_id
10097
species = "mouse" :enum('mouse', 'monkey', 'human')
@@ -131,13 +128,13 @@ class Language(dj.Lookup):
131128
class Experiment(dj.Imported):
132129
definition = """ # information about experiments
133130
-> Subject
134-
experiment_id :smallint # experiment number for this subject
131+
experiment_id :int16 # experiment number for this subject
135132
---
136133
experiment_date :date # date when experiment was started
137134
-> [nullable] User
138135
data_path="" :varchar(255) # file path to recorded data
139136
notes="" :varchar(2048) # e.g. purpose of experiment
140-
entry_time=CURRENT_TIMESTAMP :timestamp # automatic timestamp
137+
entry_time=CURRENT_TIMESTAMP :datetime # automatic timestamp
141138
"""
142139

143140
fake_experiments_per_subject = 5
@@ -164,17 +161,17 @@ def make(self, key):
164161
class Trial(dj.Imported):
165162
definition = """ # a trial within an experiment
166163
-> Experiment.proj(animal='subject_id')
167-
trial_id :smallint # trial number
164+
trial_id :int16 # trial number
168165
---
169-
start_time :double # (s)
166+
start_time :float64 # (s)
170167
"""
171168

172169
class Condition(dj.Part):
173170
definition = """ # trial conditions
174171
-> Trial
175-
cond_idx : smallint # condition number
172+
cond_idx : int16 # condition number
176173
----
177-
orientation : float # degrees
174+
orientation : float32 # degrees
178175
"""
179176

180177
def make(self, key):
@@ -191,14 +188,14 @@ class Ephys(dj.Imported):
191188
definition = """ # some kind of electrophysiological recording
192189
-> Trial
193190
----
194-
sampling_frequency :double # (Hz)
191+
sampling_frequency :float64 # (Hz)
195192
duration :decimal(7,3) # (s)
196193
"""
197194

198195
class Channel(dj.Part):
199196
definition = """ # subtable containing individual channels
200197
-> master
201-
channel :tinyint unsigned # channel number within Ephys
198+
channel :uint8 # channel number within Ephys
202199
----
203200
voltage : <blob>
204201
current = null : <blob> # optional current to test null handling
@@ -226,15 +223,15 @@ def _make_tuples(self, key):
226223
class Image(dj.Manual):
227224
definition = """
228225
# table for testing blob inserts
229-
id : int # image identifier
226+
id : int32 # image identifier
230227
---
231228
img : <blob> # image
232229
"""
233230

234231

235232
class UberTrash(dj.Lookup):
236233
definition = """
237-
id : int
234+
id : int32
238235
---
239236
"""
240237
contents = [(1,)]
@@ -243,15 +240,15 @@ class UberTrash(dj.Lookup):
243240
class UnterTrash(dj.Lookup):
244241
definition = """
245242
-> UberTrash
246-
my_id : int
243+
my_id : int32
247244
---
248245
"""
249246
contents = [(1, 1), (1, 2)]
250247

251248

252249
class SimpleSource(dj.Lookup):
253250
definition = """
254-
id : int # id
251+
id : int32 # id
255252
"""
256253
contents = [(x,) for x in range(10)]
257254

@@ -311,24 +308,24 @@ class IndexRich(dj.Manual):
311308
---
312309
-> [unique, nullable] User.proj(first="username")
313310
first_date : date
314-
value : int
311+
value : int32
315312
index (first_date, value)
316313
"""
317314

318315

319316
# Schema for issue 656
320317
class ThingA(dj.Manual):
321318
definition = """
322-
a: int
319+
a: int32
323320
"""
324321

325322

326323
class ThingB(dj.Manual):
327324
definition = """
328-
b1: int
329-
b2: int
325+
b1: int32
326+
b2: int32
330327
---
331-
b3: int
328+
b3: int32
332329
"""
333330

334331

@@ -343,7 +340,7 @@ class ThingC(dj.Manual):
343340
# Additional tables for #1159
344341
class ThingD(dj.Manual):
345342
definition = """
346-
d: int
343+
d: int32
347344
---
348345
-> ThingC
349346
"""
@@ -357,7 +354,7 @@ class ThingE(dj.Manual):
357354

358355
class Parent(dj.Lookup):
359356
definition = """
360-
parent_id: int
357+
parent_id: int32
361358
---
362359
name: varchar(30)
363360
"""
@@ -367,7 +364,7 @@ class Parent(dj.Lookup):
367364
class Child(dj.Lookup):
368365
definition = """
369366
-> Parent
370-
child_id: int
367+
child_id: int32
371368
---
372369
name: varchar(30)
373370
"""
@@ -376,12 +373,12 @@ class Child(dj.Lookup):
376373

377374
# Related to issue #886 (8), #883 (5)
378375
class ComplexParent(dj.Lookup):
379-
definition = "\n".join(["parent_id_{}: int".format(i + 1) for i in range(8)])
376+
definition = "\n".join(["parent_id_{}: int32".format(i + 1) for i in range(8)])
380377
contents = [tuple(i for i in range(8))]
381378

382379

383380
class ComplexChild(dj.Lookup):
384-
definition = "\n".join(["-> ComplexParent"] + ["child_id_{}: int".format(i + 1) for i in range(1)])
381+
definition = "\n".join(["-> ComplexParent"] + ["child_id_{}: int32".format(i + 1) for i in range(1)])
385382
contents = [tuple(i for i in range(9))]
386383

387384

@@ -443,16 +440,16 @@ class SessionDateA(dj.Lookup):
443440

444441
class Stimulus(dj.Lookup):
445442
definition = """
446-
id: int
443+
id: int32
447444
---
448-
contrast: int
449-
brightness: int
445+
contrast: int32
446+
brightness: int32
450447
"""
451448

452449

453450
class Longblob(dj.Manual):
454451
definition = """
455-
id: int
452+
id: int32
456453
---
457454
data: <blob>
458455
"""

tests/schema_alter.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@
66
class Experiment(dj.Imported):
77
original_definition = """ # information about experiments
88
-> Subject
9-
experiment_id :smallint # experiment number for this subject
9+
experiment_id :int16 # experiment number for this subject
1010
---
1111
experiment_date :date # date when experiment was started
1212
-> [nullable] User
1313
data_path="" :varchar(255) # file path to recorded data
1414
notes="" :varchar(2048) # e.g. purpose of experiment
15-
entry_time=CURRENT_TIMESTAMP :timestamp # automatic timestamp
15+
entry_time=CURRENT_TIMESTAMP :datetime # automatic timestamp
1616
"""
1717

1818
definition1 = """ # Experiment
1919
-> Subject
20-
experiment_id :smallint # experiment number for this subject
20+
experiment_id :int16 # experiment number for this subject
2121
---
22-
data_path : int # some number
22+
data_path : int32 # some number
2323
extra=null : <blob> # just testing
2424
-> [nullable] User
2525
subject_notes=null :varchar(2048) # {notes} e.g. purpose of experiment
26-
entry_time=CURRENT_TIMESTAMP :timestamp # automatic timestamp
26+
entry_time=CURRENT_TIMESTAMP :datetime # automatic timestamp
2727
"""
2828

2929

3030
class Parent(dj.Manual):
3131
definition = """
32-
parent_id: int
32+
parent_id: int32
3333
"""
3434

3535
class Child(dj.Part):
@@ -39,7 +39,7 @@ class Child(dj.Part):
3939
definition_new = """
4040
-> master
4141
---
42-
child_id=null: int
42+
child_id=null: int32
4343
"""
4444

4545
class Grandchild(dj.Part):
@@ -49,7 +49,7 @@ class Grandchild(dj.Part):
4949
definition_new = """
5050
-> master.Child
5151
---
52-
grandchild_id=null: int
52+
grandchild_id=null: int32
5353
"""
5454

5555

0 commit comments

Comments
 (0)