Skip to content

Commit 6ee5d20

Browse files
authored
Merge pull request #1125 from ethho/dev-tests-plat-151-cascade
PLAT-151: Migrate test_cascading_delete.py
2 parents e87a7d8 + 5af6e36 commit 6ee5d20

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

tests/test_cascading_delete.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import pytest
2+
import datajoint as dj
3+
from .schema_simple import A, B, D, E, L, Website, Profile
4+
from .schema import ComplexChild, ComplexParent
5+
6+
7+
@pytest.fixture
8+
def schema_simp_pop(schema_simp):
9+
A().insert(A.contents, skip_duplicates=True)
10+
L().insert(L.contents, skip_duplicates=True)
11+
B().populate()
12+
D().populate()
13+
E().populate()
14+
yield schema_simp
15+
16+
17+
class TestDelete:
18+
def test_delete_tree(self, schema_simp_pop):
19+
assert not dj.config["safemode"], "safemode must be off for testing"
20+
assert (
21+
L() and A() and B() and B.C() and D() and E() and E.F(),
22+
"schema is not populated",
23+
)
24+
A().delete()
25+
assert not A() or B() or B.C() or D() or E() or E.F(), "incomplete delete"
26+
27+
def test_stepwise_delete(self, schema_simp_pop):
28+
assert not dj.config["safemode"], "safemode must be off for testing"
29+
assert L() and A() and B() and B.C(), "schema population failed"
30+
B.C().delete(force=True)
31+
assert not B.C(), "failed to delete child tables"
32+
B().delete()
33+
assert (
34+
not B()
35+
), "failed to delete from the parent table following child table deletion"
36+
37+
def test_delete_tree_restricted(self, schema_simp_pop):
38+
assert not dj.config["safemode"], "safemode must be off for testing"
39+
assert (
40+
L() and A() and B() and B.C() and D() and E() and E.F()
41+
), "schema is not populated"
42+
cond = "cond_in_a"
43+
rel = A() & cond
44+
rest = dict(
45+
A=len(A()) - len(rel),
46+
B=len(B() - rel),
47+
C=len(B.C() - rel),
48+
D=len(D() - rel),
49+
E=len(E() - rel),
50+
F=len(E.F() - rel),
51+
)
52+
rel.delete()
53+
assert not (
54+
rel or B() & rel or B.C() & rel or D() & rel or E() & rel or (E.F() & rel)
55+
), "incomplete delete"
56+
assert len(A()) == rest["A"], "invalid delete restriction"
57+
assert len(B()) == rest["B"], "invalid delete restriction"
58+
assert len(B.C()) == rest["C"], "invalid delete restriction"
59+
assert len(D()) == rest["D"], "invalid delete restriction"
60+
assert len(E()) == rest["E"], "invalid delete restriction"
61+
assert len(E.F()) == rest["F"], "invalid delete restriction"
62+
63+
def test_delete_lookup(self, schema_simp_pop):
64+
assert not dj.config["safemode"], "safemode must be off for testing"
65+
assert (
66+
bool(L() and A() and B() and B.C() and D() and E() and E.F()),
67+
"schema is not populated",
68+
)
69+
L().delete()
70+
assert not bool(L() or D() or E() or E.F()), "incomplete delete"
71+
A().delete() # delete all is necessary because delete L deletes from subtables.
72+
73+
def test_delete_lookup_restricted(self, schema_simp_pop):
74+
assert not dj.config["safemode"], "safemode must be off for testing"
75+
assert (
76+
L() and A() and B() and B.C() and D() and E() and E.F(),
77+
"schema is not populated",
78+
)
79+
rel = L() & "cond_in_l"
80+
original_count = len(L())
81+
deleted_count = len(rel)
82+
rel.delete()
83+
assert len(L()) == original_count - deleted_count
84+
85+
def test_delete_complex_keys(self, schema_any):
86+
"""
87+
https://github.com/datajoint/datajoint-python/issues/883
88+
https://github.com/datajoint/datajoint-python/issues/886
89+
"""
90+
assert not dj.config["safemode"], "safemode must be off for testing"
91+
parent_key_count = 8
92+
child_key_count = 1
93+
restriction = dict(
94+
{"parent_id_{}".format(i + 1): i for i in range(parent_key_count)},
95+
**{
96+
"child_id_{}".format(i + 1): (i + parent_key_count)
97+
for i in range(child_key_count)
98+
}
99+
)
100+
assert len(ComplexParent & restriction) == 1, "Parent record missing"
101+
assert len(ComplexChild & restriction) == 1, "Child record missing"
102+
(ComplexParent & restriction).delete()
103+
assert len(ComplexParent & restriction) == 0, "Parent record was not deleted"
104+
assert len(ComplexChild & restriction) == 0, "Child record was not deleted"
105+
106+
def test_delete_master(self, schema_simp_pop):
107+
Profile().populate_random()
108+
Profile().delete()
109+
110+
def test_delete_parts(self, schema_simp_pop):
111+
"""test issue #151"""
112+
with pytest.raises(dj.DataJointError):
113+
Profile().populate_random()
114+
Website().delete()
115+
116+
def test_drop_part(self, schema_simp_pop):
117+
"""test issue #374"""
118+
with pytest.raises(dj.DataJointError):
119+
Website().drop()

0 commit comments

Comments
 (0)