diff --git a/tildes/tests/test_group.py b/tildes/tests/test_group.py index 45849df..a956f86 100644 --- a/tildes/tests/test_group.py +++ b/tildes/tests/test_group.py @@ -81,3 +81,30 @@ def test_duplicate_group(db): with raises(IntegrityError): db.commit() + + +def test_is_subgroup(): + """Ensure is_subgroup_of() works with an obvious subgroup.""" + assert Group("test.subgroup").is_subgroup_of(Group("test")) + + +def test_is_subgroup_equal(): + """Ensure is_subgroup_of() returns False with the same groups.""" + group = Group("testing") + assert not group.is_subgroup_of(group) + + +def test_is_subgroup_backwards(): + """Ensure is_subgroup_of() returns False if called "backwards".""" + assert not Group("test").is_subgroup_of(Group("test.subgroup")) + + +def test_is_subgroup_multilevel(): + """Ensure is_subgroup_of() works with "deeper" subgroups.""" + path = "test.one.two.three" + group = Group(path) + path_parts = path.split(".") + + # tests each of the "higher" groups (test, test.one, test.one.two) + for level in range(1, len(path_parts)): + assert group.is_subgroup_of(Group(".".join(path_parts[:level]))) diff --git a/tildes/tildes/models/group/group.py b/tildes/tildes/models/group/group.py index 15f7759..09cd025 100644 --- a/tildes/tildes/models/group/group.py +++ b/tildes/tildes/models/group/group.py @@ -94,3 +94,11 @@ class Group(DatabaseModel): acl.append(DENY_ALL) return acl + + def is_subgroup_of(self, other: "Group") -> bool: + """Return whether this group is a sub-group of the other one.""" + # descendant_of() returns True if the ltrees are equal, so avoid that + if self.path == other.path: + return False + + return self.path.descendant_of(other.path)