Skip to content

Commit a5eb6fb

Browse files
committed
Migrate most tests from #1084 to pytest
1 parent 88783f0 commit a5eb6fb

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ def schema_simp(connection_test, prefix):
330330
schema = dj.Schema(
331331
prefix + "_relational", schema_simple.LOCALS_SIMPLE, connection=connection_test
332332
)
333+
schema(schema_simple.SelectPK)
334+
schema(schema_simple.KeyPK)
333335
schema(schema_simple.IJ)
334336
schema(schema_simple.JI)
335337
schema(schema_simple.A)

tests/schema_simple.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@
1313
import inspect
1414

1515

16+
@schema
17+
class SelectPK(dj.Lookup):
18+
definition = """ # tests sql keyword escaping
19+
id: int
20+
select : int
21+
"""
22+
contents = list(dict(id=i, select=i * j)
23+
for i in range(3) for j in range(4, 0, -1))
24+
25+
26+
@schema
27+
class KeyPK(dj.Lookup):
28+
definition = """ # tests sql keyword escaping
29+
id : int
30+
key : int
31+
"""
32+
contents = list(dict(id=i, key=i + j)
33+
for i in range(3) for j in range(4, 0, -1))
34+
35+
1636
class IJ(dj.Lookup):
1737
definition = """ # tests restrictions
1838
i : int

tests/test_declare.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,17 @@ class WithSuchALongPartNameThatItCrashesMySQL(dj.Part):
339339
schema_any(WhyWouldAnyoneCreateATableNameThisLong)
340340

341341

342+
def test_regex_mismatch(schema_any):
343+
344+
class IndexAttribute(dj.Manual):
345+
definition = """
346+
index: int
347+
"""
348+
349+
with pytest.raises(dj.DataJointError):
350+
schema_any(IndexAttribute)
351+
352+
342353
def test_table_name_with_underscores(schema_any):
343354
"""
344355
Test issue #1150 -- Reject table names containing underscores. Tables should be in strict

tests/test_relational_operand.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import datetime
66
import numpy as np
77
import datajoint as dj
8+
from datajoint.errors import DataJointError
89
from .schema_simple import *
910
from .schema import *
1011

@@ -570,3 +571,94 @@ def test_union_multiple(schema_simp_pop):
570571
y = set(zip(*q2.fetch("i", "j")))
571572
assert x == y
572573
assert q1.fetch(as_dict=True) == q2.fetch(as_dict=True)
574+
575+
576+
class TestDjTop:
577+
"""TODO: migrate"""
578+
579+
def test_restrictions_by_top(self):
580+
a = L() & dj.Top()
581+
b = L() & dj.Top(order_by=["cond_in_l", "KEY"])
582+
x = L() & dj.Top(5, "id_l desc", 4) & "cond_in_l=1"
583+
y = L() & "cond_in_l=1" & dj.Top(5, "id_l desc", 4)
584+
z = (
585+
L()
586+
& dj.Top(None, order_by="id_l desc")
587+
& "cond_in_l=1"
588+
& dj.Top(5, "id_l desc")
589+
& ("id_l=20", "id_l=16", "id_l=17")
590+
& dj.Top(2, "id_l asc", 1)
591+
)
592+
assert len(a) == 1
593+
assert len(b) == 1
594+
assert len(x) == 1
595+
assert len(y) == 5
596+
assert len(z) == 2
597+
assert a.fetch(as_dict=True) == [
598+
{"id_l": 0, "cond_in_l": 1},
599+
]
600+
assert b.fetch(as_dict=True) == [
601+
{"id_l": 3, "cond_in_l": 0},
602+
]
603+
assert x.fetch(as_dict=True) == [{"id_l": 25, "cond_in_l": 1}]
604+
assert y.fetch(as_dict=True) == [
605+
{"id_l": 16, "cond_in_l": 1},
606+
{"id_l": 15, "cond_in_l": 1},
607+
{"id_l": 11, "cond_in_l": 1},
608+
{"id_l": 10, "cond_in_l": 1},
609+
{"id_l": 5, "cond_in_l": 1},
610+
]
611+
assert z.fetch(as_dict=True) == [
612+
{"id_l": 17, "cond_in_l": 1},
613+
{"id_l": 20, "cond_in_l": 1},
614+
]
615+
616+
def test_top_restriction_with_keywords(self):
617+
select = SelectPK() & dj.Top(limit=9, order_by=["select desc"])
618+
key = KeyPK() & dj.Top(limit=9, order_by="key desc")
619+
assert select.fetch(as_dict=True) == [
620+
{"id": 2, "select": 8},
621+
{"id": 2, "select": 6},
622+
{"id": 1, "select": 4},
623+
{"id": 2, "select": 4},
624+
{"id": 1, "select": 3},
625+
{"id": 1, "select": 2},
626+
{"id": 2, "select": 2},
627+
{"id": 1, "select": 1},
628+
{"id": 0, "select": 0},
629+
]
630+
assert key.fetch(as_dict=True) == [
631+
{"id": 2, "key": 6},
632+
{"id": 2, "key": 5},
633+
{"id": 1, "key": 5},
634+
{"id": 0, "key": 4},
635+
{"id": 1, "key": 4},
636+
{"id": 2, "key": 4},
637+
{"id": 0, "key": 3},
638+
{"id": 1, "key": 3},
639+
{"id": 2, "key": 3},
640+
]
641+
642+
def test_top_errors(self):
643+
with assert_raises(DataJointError) as err1:
644+
L() & ("cond_in_l=1", dj.Top())
645+
with assert_raises(DataJointError) as err2:
646+
L() & dj.AndList(["cond_in_l=1", dj.Top()])
647+
with assert_raises(TypeError) as err3:
648+
L() & dj.Top(limit="1")
649+
with assert_raises(TypeError) as err4:
650+
L() & dj.Top(order_by=1)
651+
with assert_raises(TypeError) as err5:
652+
L() & dj.Top(offset="1")
653+
assert (
654+
"Invalid restriction type Top(limit=1, order_by=['KEY'], offset=0)"
655+
== str(err1.exception)
656+
)
657+
assert (
658+
"Invalid restriction type Top(limit=1, order_by=['KEY'], offset=0)"
659+
== str(err2.exception)
660+
)
661+
assert "Top limit must be an integer" == str(err3.exception)
662+
assert "Top order_by attributes must all be strings" == str(
663+
err4.exception)
664+
assert "The offset argument must be an integer" == str(err5.exception)

tests/test_schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ def test_list_tables(schema_simp):
210210
"#website",
211211
"profile",
212212
"profile__website",
213+
"#select_p_k",
214+
"#key_p_k",
213215
]
214216
)
215217
actual = set(schema_simp.list_tables())

0 commit comments

Comments
 (0)