Skip to content

Commit

Permalink
Refs #203. Implemented hard delete for participant
Browse files Browse the repository at this point in the history
  • Loading branch information
SBriere committed Jul 13, 2023
1 parent 703faee commit 912fc54
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 5 deletions.
5 changes: 5 additions & 0 deletions teraserver/python/opentera/db/models/TeraParticipant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
99 changes: 94 additions & 5 deletions teraserver/python/tests/opentera/db/models/test_TeraParticipant.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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))

Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 912fc54

Please sign in to comment.