From 912fc54e00ac5f8a444e0b0e40901f1a764b3058 Mon Sep 17 00:00:00 2001 From: Simon Briere Date: Thu, 13 Jul 2023 07:55:50 -0400 Subject: [PATCH] Refs #203. Implemented hard delete for participant --- .../opentera/db/models/TeraParticipant.py | 5 + .../opentera/db/models/BaseModelsTest.py | 1 + .../db/models/test_TeraParticipant.py | 99 ++++++++++++++++++- .../db/models/test_TeraParticipantGroup.py | 26 +++++ 4 files changed, 126 insertions(+), 5 deletions(-) diff --git a/teraserver/python/opentera/db/models/TeraParticipant.py b/teraserver/python/opentera/db/models/TeraParticipant.py index 9118369ce..0dd145d4d 100644 --- a/teraserver/python/opentera/db/models/TeraParticipant.py +++ b/teraserver/python/opentera/db/models/TeraParticipant.py @@ -407,3 +407,8 @@ def delete_check_integrity(self, with_deleted: bool = False) -> IntegrityError | return IntegrityError('Participant still has created tests', self.id_participant, 't_tests') return None + + def hard_delete_before(self): + # Delete sessions that we are part of since they will not be deleted otherwise + for ses in self.participant_sessions: + ses.hard_delete() diff --git a/teraserver/python/tests/opentera/db/models/BaseModelsTest.py b/teraserver/python/tests/opentera/db/models/BaseModelsTest.py index a9e41ad0d..81c384c21 100644 --- a/teraserver/python/tests/opentera/db/models/BaseModelsTest.py +++ b/teraserver/python/tests/opentera/db/models/BaseModelsTest.py @@ -15,6 +15,7 @@ def setUpClass(cls): cls._flask_app.config.update({'PROPAGATE_EXCEPTIONS': True}) cls._db_man = DBManager(cls._config, app=cls._flask_app) # Setup DB in RAM + # cls._db_man.open_local({'filename': 'D:\\temp\\opentera.db'}, echo=False, ram=False) cls._db_man.open_local({}, echo=False, ram=True) # Creating default users / tests. Time-consuming, only once per test file. diff --git a/teraserver/python/tests/opentera/db/models/test_TeraParticipant.py b/teraserver/python/tests/opentera/db/models/test_TeraParticipant.py index f3d8d3628..468287a58 100644 --- a/teraserver/python/tests/opentera/db/models/test_TeraParticipant.py +++ b/teraserver/python/tests/opentera/db/models/test_TeraParticipant.py @@ -1,5 +1,10 @@ from opentera.db.models.TeraParticipant import TeraParticipant from opentera.db.models.TeraParticipantGroup import TeraParticipantGroup +from opentera.db.models.TeraSession import TeraSession +from opentera.db.models.TeraAsset import TeraAsset +from opentera.db.models.TeraTest import TeraTest +from opentera.db.models.TeraService import TeraService + import uuid from tests.opentera.db.models.BaseModelsTest import BaseModelsTest @@ -34,12 +39,96 @@ def test_token(self): loadedParticipant = TeraParticipant.get_participant_by_token(token) self.assertEqual(loadedParticipant.participant_uuid, participant.participant_uuid) - def test_json(self): + def test_soft_delete(self): with self._flask_app.app_context(): - return - participant = TeraParticipant.get_participant_by_name('Participant #1') + # Create a new participant + participant = TeraParticipant() + participant.participant_name = "Test participant" + participant.id_project = 1 + TeraParticipant.insert(participant) + self.assertIsNotNone(participant.id_participant) + id_participant = participant.id_participant + + # Delete participant + TeraParticipant.delete(id_participant) + # Make sure participant is deleted + self.assertIsNone(TeraParticipant.get_participant_by_id(id_participant)) + + # Query, with soft delete flag + participant = TeraParticipant.query.filter_by(id_participant=id_participant)\ + .execution_options(include_deleted=True).first() + self.assertIsNotNone(participant) + self.assertIsNotNone(participant.deleted_at) + + def test_hard_delete(self): + with self._flask_app.app_context(): + # Create a new participant + participant = TeraParticipant() + participant.participant_name = "Test participant" + participant.id_project = 1 + TeraParticipant.insert(participant) + self.assertIsNotNone(participant.id_participant) + id_participant = participant.id_participant + + # Assign participant to sessions + part_session = TeraSession() + part_session.id_creator_participant = id_participant + part_session.id_session_type = 1 + part_session.session_name = 'Creator participant session' + TeraSession.insert(part_session) + id_session = part_session.id_session + + part_session = TeraSession() + part_session.id_creator_service = 1 + part_session.id_session_type = 1 + part_session.session_name = "Participant invitee session" + part_session.session_participants = [participant] + TeraSession.insert(part_session) + id_session_invitee = part_session.id_session + + # Attach asset + asset = TeraAsset() + asset.asset_name = "Participant asset test" + asset.id_participant = id_participant + asset.id_session = id_session + asset.asset_service_uuid = TeraService.get_openteraserver_service().service_uuid + asset.asset_type = 'Test' + TeraAsset.insert(asset) + id_asset = asset.id_asset + + # ... and test + test = TeraTest() + test.id_participant = id_participant + test.id_session = id_session + test.id_test_type = 1 + test.test_name = "Device test test!" + TeraTest.insert(test) + id_test = test.id_test + + # Soft delete device to prevent relationship integrity errors as we want to test hard-delete cascade here + TeraSession.delete(id_session) + TeraSession.delete(id_session_invitee) + TeraParticipant.delete(id_participant) + + # Check that device and relationships are still there + self.assertIsNone(TeraParticipant.get_participant_by_id(id_participant)) + self.assertIsNotNone(TeraParticipant.get_participant_by_id(id_participant, True)) + self.assertIsNone(TeraSession.get_session_by_id(id_session)) + self.assertIsNotNone(TeraSession.get_session_by_id(id_session, True)) + self.assertIsNone(TeraSession.get_session_by_id(id_session_invitee)) + self.assertIsNotNone(TeraSession.get_session_by_id(id_session_invitee, True)) + self.assertIsNone(TeraAsset.get_asset_by_id(id_asset)) + self.assertIsNotNone(TeraAsset.get_asset_by_id(id_asset, True)) + self.assertIsNone(TeraTest.get_test_by_id(id_test)) + self.assertIsNotNone(TeraTest.get_test_by_id(id_test, True)) - json = participant.to_json() + # Hard delete participant + TeraParticipant.delete(participant.id_participant, hard_delete=True) - print(json) + # Make sure device and associations are deleted + self.assertIsNone(TeraParticipant.get_participant_by_id(id_participant, True)) + self.assertIsNone(TeraSession.get_session_by_id(id_session, True)) + self.assertIsNone(TeraSession.get_session_by_id(id_session_invitee, True)) + self.assertIsNone(TeraAsset.get_asset_by_id(id_asset, True)) + self.assertIsNone(TeraTest.get_test_by_id(id_test, True)) diff --git a/teraserver/python/tests/opentera/db/models/test_TeraParticipantGroup.py b/teraserver/python/tests/opentera/db/models/test_TeraParticipantGroup.py index 7051964ef..4fed7a124 100644 --- a/teraserver/python/tests/opentera/db/models/test_TeraParticipantGroup.py +++ b/teraserver/python/tests/opentera/db/models/test_TeraParticipantGroup.py @@ -1,7 +1,33 @@ from tests.opentera.db.models.BaseModelsTest import BaseModelsTest +from opentera.db.models.TeraParticipantGroup import TeraParticipantGroup + class TeraParticipantGroupTest(BaseModelsTest): def test_defaults(self): pass + + def test_soft_delete(self): + with self._flask_app.app_context(): + # Create a new participant group + group = TeraParticipantGroup() + group.participant_group_name = "Test participant group" + group.id_project = 1 + TeraParticipantGroup.insert(group) + self.assertIsNotNone(group.id_participant_group) + id_participant_group = group.id_participant_group + + # Delete participant group + TeraParticipantGroup.delete(id_participant_group) + # Make sure participant group is deleted + self.assertIsNone(TeraParticipantGroup.get_participant_group_by_id(id_participant_group)) + + # Query, with soft delete flag + group = TeraParticipantGroup.query.filter_by(id_participant_group=id_participant_group) \ + .execution_options(include_deleted=True).first() + self.assertIsNotNone(group) + self.assertIsNotNone(group.deleted_at) + + def test_hard_delete(self): + pass