get_or_create_permission

This commit is contained in:
SvyatoslavArtymovych 2023-05-24 14:49:04 +03:00
parent 0cf87d7304
commit cfd05b8cd9
2 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,10 @@
from app import models as m
def get_or_create_permission(access: int, entity: m.Permission.Entity):
permission: m.Permission = m.Permission.query.filter_by(
access=access, entity=entity
).first()
if not permission:
permission: m.Permission = m.Permission(access=access, entity=entity).save()
return permission

View File

@ -0,0 +1,30 @@
from app import models as m
from app.controllers.get_or_create_permission import get_or_create_permission
def test_get_or_create_permission(client):
access = m.Permission.Access
entity = m.Permission.Entity
book_u: m.Permission = m.Permission.query.filter_by(
access=access.U, entity=entity.BOOK
).first()
assert not book_u
assert not m.Permission.query.count()
book_u: m.Permission = get_or_create_permission(access=access.U, entity=entity.BOOK)
assert book_u
assert book_u.access == access.U
assert book_u.entity == entity.BOOK
assert m.Permission.query.count() == 1
book_u: m.Permission = m.Permission.query.filter_by(
access=access.U, entity=entity.BOOK
).first()
assert book_u
assert book_u.access == access.U
assert book_u.entity == entity.BOOK
get_or_create_permission(access=access.U, entity=entity.BOOK)
assert m.Permission.query.count() == 1