From b54bd49cf44ab7b8760f8f869bdb3f1c632b5b2c Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Mon, 30 Dec 2019 13:03:57 -0500 Subject: [PATCH 1/3] Allow for clearing out the database. --- crc/__init__.py | 1 + example_data.py | 7 +++++++ tests/base_test.py | 12 ++++-------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/crc/__init__.py b/crc/__init__.py index 610db60a..954864bc 100644 --- a/crc/__init__.py +++ b/crc/__init__.py @@ -33,4 +33,5 @@ cors = CORS(connexion_app.app) def load_example_data(): """Load example data into the database.""" from example_data import ExampleDataLoader + ExampleDataLoader.clean_db() ExampleDataLoader().load_all() diff --git a/example_data.py b/example_data.py index 2f1ce25f..3543d3f8 100644 --- a/example_data.py +++ b/example_data.py @@ -50,6 +50,13 @@ class ExampleDataLoader: file = open(filename, "rb") workflow_data = [FileDataModel(data=file.read(), file_model=workflow_spec_files[0])] + @staticmethod + def clean_db(): + db.session.flush() # Clear out any transactions before deleting it all to avoid spurious errors. + for table in reversed(db.metadata.sorted_tables): + db.session.execute(table.delete()) + db.session.flush() + def load_all(self): db.session.bulk_save_objects(ExampleDataLoader.studies) db.session.bulk_save_objects(ExampleDataLoader.workflow_specs) diff --git a/tests/base_test.py b/tests/base_test.py index df1f3211..a13b2ba8 100644 --- a/tests/base_test.py +++ b/tests/base_test.py @@ -5,7 +5,7 @@ import os os.environ["TESTING"] = "true" from crc import app, db - +from example_data import ExampleDataLoader # UNCOMMENT THIS FOR DEBUGGING SQL ALCHEMY QUERIES # import logging @@ -13,11 +13,7 @@ from crc import app, db # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) -def clean_db(): - db.session.flush() # Clear out any transactions before deleting it all to avoid spurious errors. - for table in reversed(db.metadata.sorted_tables): - db.session.execute(table.delete()) - db.session.flush() + # Great class to inherit from, as it sets up and tears down @@ -43,12 +39,12 @@ class BaseTest: self.ctx.push() def tearDown(self): - clean_db() # This does not seem to work, some colision of sessions. + ExampleDataLoader.clean_db() # This does not seem to work, some colision of sessions. self.ctx.pop() self.auths = {} def load_example_data(self): - clean_db() + ExampleDataLoader.clean_db() from example_data import ExampleDataLoader ExampleDataLoader().load_all() From fb465531cad60c1084e0230395675b74a327e64d Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Mon, 30 Dec 2019 13:15:39 -0500 Subject: [PATCH 2/3] Add the ability to delete a workflow. --- crc/api.yml | 20 ++++++++++++++------ crc/api/study.py | 2 +- crc/api/workflow.py | 2 ++ tests/base_test.py | 2 +- tests/test_api.py | 14 ++++++++++++++ 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/crc/api.yml b/crc/api.yml index 0e7608a2..80bc9f05 100644 --- a/crc/api.yml +++ b/crc/api.yml @@ -290,12 +290,7 @@ paths: example: '' # /v1.0/workflow/0 /workflow/{workflow_id}: - get: - operationId: crc.api.workflow.get_workflow - summary: Detailed information for a specific workflow instance - tags: - - Workflows and Tasks - parameters: + parameters: - name: workflow_id in: path required: true @@ -303,6 +298,11 @@ paths: schema: type: integer format: int32 + get: + operationId: crc.api.workflow.get_workflow + summary: Detailed information for a specific workflow instance + tags: + - Workflows and Tasks responses: '200': description: Returns details about the workflows state and current task @@ -316,6 +316,14 @@ paths: application/json: schema: $ref: "#/components/schemas/Error" + delete: + operationId: crc.api.workflow.delete + summary: Removes an existing workflow + tags: + - Files + responses: + '204': + description: The workflow was removed /workflow/{workflow_id}/tasks: get: operationId: crc.api.workflow.get_tasks diff --git a/crc/api/study.py b/crc/api/study.py index 46545071..da8d1257 100644 --- a/crc/api/study.py +++ b/crc/api/study.py @@ -50,4 +50,4 @@ def add_workflow_to_study(study_id, body): workflow_spec_id=workflow_spec_model.id) db.session.add(workflow) db.session.commit() - return get_study_workflows(study_id) + return WorkflowSchema().dump(workflow) diff --git a/crc/api/workflow.py b/crc/api/workflow.py index 9bf55965..b9be5e8c 100644 --- a/crc/api/workflow.py +++ b/crc/api/workflow.py @@ -14,6 +14,8 @@ def get_workflow(workflow_id): workflow = db.session.query(WorkflowModel).filter_by(id=workflow_id).first() return schema.dump(workflow) +def delete(workflow_id): + db.session.query(WorkflowModel).filter_by(id=workflow_id).delete() def get_tasks(workflow_id): workflow = db.session.query(WorkflowModel).filter_by(id=workflow_id).first() diff --git a/tests/base_test.py b/tests/base_test.py index a13b2ba8..19c117cf 100644 --- a/tests/base_test.py +++ b/tests/base_test.py @@ -44,8 +44,8 @@ class BaseTest: self.auths = {} def load_example_data(self): - ExampleDataLoader.clean_db() from example_data import ExampleDataLoader + ExampleDataLoader.clean_db() ExampleDataLoader().load_all() def assert_success(self, rv, msg=""): diff --git a/tests/test_api.py b/tests/test_api.py index 5c736864..0ea76d0c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -68,6 +68,20 @@ class TestStudy(BaseTest, unittest.TestCase): workflows = WorkflowSchema(many=True).load(json_data, session=db.session) self.assertEqual(workflows[0].id, workflow.id) + def test_delete_workflow(self): + self.load_example_data() + study = db.session.query(StudyModel).first() + spec = db.session.query(WorkflowSpecModel).first() + rv = self.app.post('/v1.0/study/%i/workflows' % study.id,content_type="application/json", + data=json.dumps(WorkflowSpecSchema().dump(spec))) + self.assertEqual(1, db.session.query(WorkflowModel).count()) + json_data = json.loads(rv.get_data(as_text=True)) + workflow = WorkflowSchema().load(json_data, session=db.session) + rv = self.app.delete('/v1.0/workflow/%i' % workflow.id) + self.assert_success(rv) + self.assertEqual(0, db.session.query(WorkflowModel).count()) + + def test_get_current_user_tasks(self): self.load_example_data() study = db.session.query(StudyModel).first() From 115a88347f1d73fc1acc0f31753b6c91051285f3 Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Mon, 30 Dec 2019 13:34:18 -0500 Subject: [PATCH 3/3] Stupid tag --- crc/api.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crc/api.yml b/crc/api.yml index 80bc9f05..8c3cf19e 100644 --- a/crc/api.yml +++ b/crc/api.yml @@ -320,7 +320,7 @@ paths: operationId: crc.api.workflow.delete summary: Removes an existing workflow tags: - - Files + - Workflows and Tasks responses: '204': description: The workflow was removed