Skip to content

Commit 149555c

Browse files
committed
cp to tests
1 parent fe51002 commit 149555c

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

tests/test_cascading_delete.py

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

0 commit comments

Comments
 (0)