Skip to content

Commit

Permalink
Add testcase for 2x manytomany w/same tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Jul 1, 2023
1 parent c393925 commit 0bb93d9
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tests/manytomany.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,50 @@ def test_pk_is_fk(self):
def test_empty(self):
al = AccountList.create(name='empty')
self.assertEqual(list(al.accounts), [])


class Permission(TestModel):
name = TextField()

DeniedThroughDeferred = DeferredThroughModel()

class Visitor(TestModel):
name = TextField()
allowed = ManyToManyField(Permission)
denied = ManyToManyField(Permission, through_model=DeniedThroughDeferred)

class DeniedThrough(TestModel):
permission = ForeignKeyField(Permission)
visitor = ForeignKeyField(Visitor)

DeniedThroughDeferred.set_model(DeniedThrough)


class TestMultipleManyToManySameTables(ModelTestCase):
database = get_in_memory_db()
requires = [Permission, Visitor, Visitor.allowed.through_model,
Visitor.denied.through_model]

def test_multiple_manytomany_same_tables(self):
p1, p2, p3 = [Permission.create(name=n) for n in ('p1', 'p2', 'p3')]
v1, v2, v3 = [Visitor.create(name=n) for n in ('v1', 'v2', 'v3')]

v1.allowed.add([p1, p2, p3])
v2.allowed.add(p2)
v2.denied.add([p1, p3])
v3.allowed.add(p3)
v3.denied.add(p1)

accum = []
for v in Visitor.select().order_by(Visitor.name):
allowed, denied = [], []
for p in v.allowed.order_by(Permission.name):
allowed.append(p.name)
for p in v.denied.order_by(Permission.name):
denied.append(p.name)
accum.append((v.name, allowed, denied))

self.assertEqual(accum, [
('v1', ['p1', 'p2', 'p3'], []),
('v2', ['p2'], ['p1', 'p3']),
('v3', ['p3'], ['p1'])])

0 comments on commit 0bb93d9

Please sign in to comment.