-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,105 @@ | ||
"""QualiCharge auditable schemas tests.""" | ||
|
||
from sqlalchemy import func | ||
from sqlmodel import select | ||
|
||
from qualicharge.auth.factories import UserFactory | ||
from qualicharge.factories.static import OperateurFactory | ||
from qualicharge.schemas.audit import Audit | ||
from qualicharge.schemas.core import Operateur | ||
|
||
|
||
def test_auditable_schema_changes(db_session): | ||
"""Test an updated schema instance creates a new Audit entry.""" | ||
OperateurFactory.__session__ = db_session | ||
UserFactory.__session__ = db_session | ||
|
||
user = UserFactory.create_sync() | ||
|
||
# Check initial database state | ||
assert db_session.exec(select(func.count(Operateur.id))).one() == 0 | ||
assert db_session.exec(select(func.count(Audit.id))).one() == 0 | ||
|
||
# Persist an operateur with not creator or updator | ||
operateur = OperateurFactory.create_sync( | ||
nom_operateur="Doe inc.", | ||
contact_operateur="[email protected]", | ||
telephone_operateur="+33144276350", | ||
) | ||
|
||
# Check database state | ||
assert db_session.exec(select(func.count(Operateur.id))).one() == 1 | ||
assert db_session.exec(select(func.count(Audit.id))).one() == 0 | ||
|
||
# Update operateur without updator | ||
operateur.contact_operateur = "[email protected]" | ||
operateur.telephone_operateur = "+33144276351" | ||
db_session.add(operateur) | ||
|
||
# Check database state | ||
assert db_session.exec(select(func.count(Operateur.id))).one() == 1 | ||
assert db_session.exec(select(func.count(Audit.id))).one() == 0 | ||
|
||
# Now update operateur with an updator | ||
operateur.updated_by_id = user.id | ||
operateur.contact_operateur = "[email protected]" | ||
operateur.telephone_operateur = "+33144276352" | ||
db_session.add(operateur) | ||
|
||
# Check database state | ||
assert db_session.exec(select(func.count(Operateur.id))).one() == 1 | ||
assert db_session.exec(select(func.count(Audit.id))).one() == 1 | ||
audit = db_session.exec(select(Audit)).first() | ||
assert audit.table == "operateur" | ||
assert audit.author_id == user.id | ||
assert audit.target_id == operateur.id | ||
assert audit.updated_at == operateur.updated_at | ||
assert audit.changes == { | ||
"updated_by_id": ["None", str(user.id)], | ||
"contact_operateur": ["[email protected]", "[email protected]"], | ||
"telephone_operateur": ["tel:+33-1-44-27-63-51", "tel:+33-1-44-27-63-52"], | ||
} | ||
|
||
# Perform new updates | ||
operateur.contact_operateur = "[email protected]" | ||
operateur.telephone_operateur = "+33144276353" | ||
db_session.add(operateur) | ||
|
||
# Check database state | ||
expected_audits = 2 | ||
assert db_session.exec(select(func.count(Operateur.id))).one() == 1 | ||
assert db_session.exec(select(func.count(Audit.id))).one() == expected_audits | ||
audit = db_session.exec(select(Audit).order_by(Audit.updated_at.desc())).first() | ||
assert audit.table == "operateur" | ||
assert audit.author_id == user.id | ||
assert audit.target_id == operateur.id | ||
assert audit.updated_at == operateur.updated_at | ||
assert audit.changes == { | ||
"contact_operateur": ["[email protected]", "[email protected]"], | ||
"telephone_operateur": ["tel:+33-1-44-27-63-52", "tel:+33-1-44-27-63-53"], | ||
} | ||
|
||
|
||
def test_auditable_schema_changes_dynamic_fk(db_session): | ||
"""Test auditable schema dynamic audits foreign key.""" | ||
OperateurFactory.__session__ = db_session | ||
UserFactory.__session__ = db_session | ||
|
||
user = UserFactory.create_sync() | ||
operateur = OperateurFactory.create_sync( | ||
nom_operateur="Doe inc.", | ||
contact_operateur="[email protected]", | ||
telephone_operateur="+33144276350", | ||
updated_by_id=user.id | ||
) | ||
|
||
# Now update operateur twice | ||
operateur.contact_operateur = "[email protected]" | ||
operateur.telephone_operateur = "+33144276352" | ||
db_session.add(operateur) | ||
operateur.contact_operateur = "[email protected]" | ||
operateur.telephone_operateur = "+33144276353" | ||
db_session.add(operateur) | ||
|
||
# Test audits dymanic generic FK | ||
assert len(operateur.audits) == 2 |