Skip to content

Commit

Permalink
Merge pull request #1160 from CBroz1/1159
Browse files Browse the repository at this point in the history
Cascade restriction attribute
  • Loading branch information
dimitri-yatsenko authored Jul 15, 2024
2 parents b42b239 + 5328326 commit 685b42c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Added - Missing tests for set_password - PR [#1106](https://github.com/datajoint/datajoint-python/pull/1106)
- Changed - Returning success count after the .populate() call - PR [#1050](https://github.com/datajoint/datajoint-python/pull/1050)
- Fixed - `Autopopulate.populate` excludes `reserved` jobs in addition to `ignore` and `error` jobs
- Fixed - Issue [#1159]((https://github.com/datajoint/datajoint-python/pull/1159) (cascading delete) - PR [#1160](https://github.com/datajoint/datajoint-python/pull/1160)

### 0.14.1 -- Jun 02, 2023
- Fixed - Fix altering a part table that uses the "master" keyword - PR [#991](https://github.com/datajoint/datajoint-python/pull/991)
Expand Down
2 changes: 1 addition & 1 deletion datajoint/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ class U:
>>> dj.U().aggr(expr, n='count(*)')
The following expressions both yield one element containing the number `n` of distinct values of attribute `attr` in
query expressio `expr`.
query expression `expr`.
>>> dj.U().aggr(expr, n='count(distinct attr)')
>>> dj.U().aggr(dj.U('attr').aggr(expr), 'n=count(*)')
Expand Down
1 change: 1 addition & 0 deletions datajoint/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ def cascade(table):
and match["fk_attrs"] == match["pk_attrs"]
):
child._restriction = table._restriction
child._restriction_attributes = table.restriction_attributes
elif match["fk_attrs"] != match["pk_attrs"]:
child &= table.proj(
**dict(zip(match["fk_attrs"], match["pk_attrs"]))
Expand Down
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ def schema_any(connection_test, prefix):
schema_any(schema.ThingA)
schema_any(schema.ThingB)
schema_any(schema.ThingC)
schema_any(schema.ThingD)
schema_any(schema.ThingE)
schema_any(schema.Parent)
schema_any(schema.Child)
schema_any(schema.ComplexParent)
Expand All @@ -303,6 +305,26 @@ def schema_any(connection_test, prefix):
schema_any.drop()


@pytest.fixture
def thing_tables(schema_any):
a = schema.ThingA()
b = schema.ThingB()
c = schema.ThingC()
d = schema.ThingD()
e = schema.ThingE()

# clear previous contents if any.
c.delete_quick()
b.delete_quick()
a.delete_quick()

a.insert(dict(a=a) for a in range(7))
b.insert1(dict(b1=1, b2=1, b3=100))
b.insert1(dict(b1=1, b2=2, b3=100))

yield a, b, c, d, e


@pytest.fixture
def schema_simp(connection_test, prefix):
schema = dj.Schema(
Expand Down
15 changes: 15 additions & 0 deletions tests/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ class ThingC(dj.Manual):
"""


# Additional tables for #1159
class ThingD(dj.Manual):
definition = """
d: int
---
-> ThingC
"""


class ThingE(dj.Manual):
definition = """
-> ThingD
"""


class Parent(dj.Lookup):
definition = """
parent_id: int
Expand Down
13 changes: 13 additions & 0 deletions tests/test_cascading_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,16 @@ def test_drop_part(schema_simp_pop):
"""test issue #374"""
with pytest.raises(dj.DataJointError):
Website().drop()


def test_delete_1159(thing_tables):
tbl_a, tbl_c, tbl_c, tbl_d, tbl_e = thing_tables

tbl_c.insert([dict(a=i) for i in range(6)])
tbl_d.insert([dict(a=i, d=i) for i in range(5)])
tbl_e.insert([dict(d=i) for i in range(4)])

(tbl_a & "a=3").delete()

assert len(tbl_a) == 6, "Failed to cascade restriction attributes"
assert len(tbl_e) == 3, "Failed to cascade restriction attributes"
35 changes: 4 additions & 31 deletions tests/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import datajoint as dj
from datajoint import errors
from pytest import raises
from datajoint.dependencies import unite_master_parts
from .schema import *


def test_unite_master_parts():
Expand Down Expand Up @@ -50,22 +48,10 @@ def test_unite_master_parts():
]


def test_nullable_dependency(schema_any):
def test_nullable_dependency(thing_tables):
"""test nullable unique foreign key"""
# Thing C has a nullable dependency on B whose primary key is composite
a = ThingA()
b = ThingB()
c = ThingC()

# clear previous contents if any.
c.delete_quick()
b.delete_quick()
a.delete_quick()

a.insert(dict(a=a) for a in range(7))

b.insert1(dict(b1=1, b2=1, b3=100))
b.insert1(dict(b1=1, b2=2, b3=100))
_, _, c, _, _ = thing_tables

# missing foreign key attributes = ok
c.insert1(dict(a=0))
Expand All @@ -79,23 +65,10 @@ def test_nullable_dependency(schema_any):
assert len(c) == len(c.fetch()) == 5


def test_unique_dependency(schema_any):
def test_unique_dependency(thing_tables):
"""test nullable unique foreign key"""

# Thing C has a nullable dependency on B whose primary key is composite
a = ThingA()
b = ThingB()
c = ThingC()

# clear previous contents if any.
c.delete_quick()
b.delete_quick()
a.delete_quick()

a.insert(dict(a=a) for a in range(7))

b.insert1(dict(b1=1, b2=1, b3=100))
b.insert1(dict(b1=1, b2=2, b3=100))
_, _, c, _, _ = thing_tables

c.insert1(dict(a=0, b1=1, b2=1))
# duplicate foreign key attributes = not ok
Expand Down

0 comments on commit 685b42c

Please sign in to comment.