From 05a4fe34081a57bade369a0fa6b97ec45fbbefea Mon Sep 17 00:00:00 2001 From: dchhabda Date: Mon, 4 Nov 2024 18:30:12 -0500 Subject: [PATCH] fix tests --- pybossa/repositories/task_repository.py | 17 ++++++++++ test/test_api/test_task_api.py | 33 +++++++++++++------- test/test_repository/test_task_repository.py | 12 ++++--- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/pybossa/repositories/task_repository.py b/pybossa/repositories/task_repository.py index d75d3271a..1293e401a 100644 --- a/pybossa/repositories/task_repository.py +++ b/pybossa/repositories/task_repository.py @@ -201,7 +201,24 @@ def update(self, element): except IntegrityError as e: raise DBIntegrityError(e) + def _delete(self, element): + table = element.__class__ + inst = self.db.session.query(table).filter(table.id==element.id).first() + self.db.session.delete(inst) + + def delete_taskrun(self, element): + self._delete(element) + self.db.session.commit() + cached_projects.clean_project(element.project_id) + def delete(self, element): + # task repo is shared between task and taskun + # call taskrun specific delete for taskrun deletes + self._validate_can_be('deleted', element) + if element.__tablename__ == "task_run": + self.delete_taskrun(element) + return + tstart = time.perf_counter() self._validate_can_be('deleted', element) tend = time.perf_counter() diff --git a/test/test_api/test_task_api.py b/test/test_api/test_task_api.py index 1795b1b5a..94aa92bca 100644 --- a/test/test_api/test_task_api.py +++ b/test/test_api/test_task_api.py @@ -950,20 +950,23 @@ def test_task_delete(self): root_task = TaskFactory.create(project=project) ## anonymous - res = self.app.delete('/api/task/%s' % task.id) + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + res = self.app.delete('/api/task/%s' % task.id) error_msg = 'Anonymous should not be allowed to delete' print(res.status) assert_equal(res.status, '401 UNAUTHORIZED', error_msg) ### real user but not allowed as not owner! url = '/api/task/%s?api_key=%s' % (task.id, non_owner.api_key) - res = self.app.delete(url) + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + res = self.app.delete(url) error_msg = 'Should not be able to update tasks of others' assert_equal(res.status, '403 FORBIDDEN', error_msg) #### real user # DELETE with not allowed args - res = self.app.delete(url + "&foo=bar") + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + res = self.app.delete(url + "&foo=bar") err = json.loads(res.data) assert res.status_code == 415, err assert err['status'] == 'failed', err @@ -973,13 +976,15 @@ def test_task_delete(self): # DELETE returns 204 url = '/api/task/%s?api_key=%s' % (task.id, user.api_key) - res = self.app.delete(url) + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + res = self.app.delete(url) assert_equal(res.status, '204 NO CONTENT', res.data) assert res.data == b'', res.data # res.data is bytes type #### root user url = '/api/task/%s?api_key=%s' % (root_task.id, admin.api_key) - res = self.app.delete(url) + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + res = self.app.delete(url) assert_equal(res.status, '204 NO CONTENT', res.data) tasks = task_repo.filter_tasks_by(project_id=project.id) @@ -992,10 +997,12 @@ def test_delete_task_cascade(self): task = TaskFactory.create() task_runs = TaskRunFactory.create_batch(3, task=task) url = '/api/task/%s?api_key=%s' % (task.id, task.project.owner.api_key) - res = self.app.delete(url) + task_id = task.id + with patch.dict(self.flask_app.config, {'SESSION_REPLICATION_ROLE_DISABLED': True}): + res = self.app.delete(url) assert_equal(res.status, '204 NO CONTENT', res.data) - task_runs = task_repo.filter_task_runs_by(task_id=task.id) + task_runs = task_repo.filter_task_runs_by(task_id=task_id) assert len(task_runs) == 0, "There should not be any task run for task" @with_context @@ -1034,7 +1041,8 @@ def test_delete_task_when_result_associated_admin(self): url = '/api/task/%s?api_key=%s' % (result.task_id, admin.api_key) - res = self.app.delete(url) + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + res = self.app.delete(url) assert_equal(res.status, '204 NO CONTENT', res.status) @with_context @@ -1051,7 +1059,8 @@ def test_delete_task_when_result_associated_variation(self): url = '/api/task/%s?api_key=%s' % (result.task_id, admin.api_key) - res = self.app.delete(url) + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + res = self.app.delete(url) assert_equal(res.status, '204 NO CONTENT', res.status) @with_context @@ -1068,7 +1077,8 @@ def test_counter_table(self): assert len(items) == 10 task_id = task.id - task_repo.delete(task) + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + task_repo.delete(task) items = db.session.query(Counter).filter_by(project_id=project.id).all() assert len(items) == 9 items = db.session.query(Counter).filter_by(task_id=task_id).all() @@ -1103,7 +1113,8 @@ def test_counter_table_api(self): items = db.session.query(Counter).filter_by(project_id=project.id).all() assert len(items) == 10, len(items) - res = self.app.delete('/api/task/%s?api_key=%s' % (created_task['id'], + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + res = self.app.delete('/api/task/%s?api_key=%s' % (created_task['id'], project.owner.api_key)) items = db.session.query(Counter).filter_by(project_id=project.id).all() assert len(items) == 9 diff --git a/test/test_repository/test_task_repository.py b/test/test_repository/test_task_repository.py index abd8d2dfd..efb0b8dfb 100644 --- a/test/test_repository/test_task_repository.py +++ b/test/test_repository/test_task_repository.py @@ -721,9 +721,11 @@ def test_delete_task(self): """Test delete removes the Task instance""" task = TaskFactory.create() + task_id = task.id - self.task_repo.delete(task) - deleted = self.task_repo.get_task(task.id) + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + self.task_repo.delete(task) + deleted = self.task_repo.get_task(task_id) assert deleted is None, deleted @@ -734,9 +736,11 @@ def test_delete_task_deletes_dependent_taskruns(self): task = TaskFactory.create() taskrun = TaskRunFactory.create(task=task) + taskrun_id = taskrun.id - self.task_repo.delete(task) - deleted = self.task_repo.get_task_run(taskrun.id) + with patch.dict(self.flask_app.config, {'SQLALCHEMY_BINDS': {'bulkdel': "dbconn"}}): + self.task_repo.delete(task) + deleted = self.task_repo.get_task_run(taskrun_id) assert deleted is None, deleted