From 8e1b7fcbfde5774dd973d0f8208ede5774ad19bc Mon Sep 17 00:00:00 2001 From: Spyros Zoupanos Date: Mon, 27 Nov 2017 16:47:55 +0100 Subject: [PATCH 1/3] Returning the ctime of a Workflow object for both backends. --- aiida/orm/implementation/django/workflow.py | 4 ++++ aiida/orm/implementation/general/workflow.py | 7 +++++++ aiida/orm/implementation/sqlalchemy/workflow.py | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/aiida/orm/implementation/django/workflow.py b/aiida/orm/implementation/django/workflow.py index 13f05ad0ee..02830be9fb 100644 --- a/aiida/orm/implementation/django/workflow.py +++ b/aiida/orm/implementation/django/workflow.py @@ -157,6 +157,10 @@ def label(self): def label(self, label): self._update_db_label_field(label) + @property + def ctime(self): + return self.dbworkflowinstance.ctime + def _update_db_label_field(self, field_value): """ Safety method to store the label of the workflow diff --git a/aiida/orm/implementation/general/workflow.py b/aiida/orm/implementation/general/workflow.py index e490017abc..17ed732e1d 100644 --- a/aiida/orm/implementation/general/workflow.py +++ b/aiida/orm/implementation/general/workflow.py @@ -137,6 +137,13 @@ def label(self, label): """ pass + @abstractproperty + def ctime(self): + """ + Get the creation time of the workflow + """ + pass + @abstractmethod def _update_db_label_field(self, field_value): """ diff --git a/aiida/orm/implementation/sqlalchemy/workflow.py b/aiida/orm/implementation/sqlalchemy/workflow.py index 0d8d01d16a..66eac9a83b 100644 --- a/aiida/orm/implementation/sqlalchemy/workflow.py +++ b/aiida/orm/implementation/sqlalchemy/workflow.py @@ -156,6 +156,10 @@ def label(self): def label(self, label): self._update_db_label_field(label) + @property + def ctime(self): + return self.dbworkflowinstance.ctime + def _update_db_label_field(self, field_value): """ Safety method to store the label of the workflow From c9a35508fe66e02c7e0e5a95274f09b1c2c6fd31 Mon Sep 17 00:00:00 2001 From: Spyros Zoupanos Date: Mon, 27 Nov 2017 17:03:31 +0100 Subject: [PATCH 2/3] Addding a small test --- aiida/backends/tests/workflows.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/aiida/backends/tests/workflows.py b/aiida/backends/tests/workflows.py index 78e6157319..365c1018ef 100644 --- a/aiida/backends/tests/workflows.py +++ b/aiida/backends/tests/workflows.py @@ -152,6 +152,27 @@ def test_wf_get_state(self): # it is a valid state self.assertIn(wf.get_state(), wf_states) + def test_wf_ctime(self): + import datetime + import pytz + + # Get the current datetime (before the creation of the workflow) + dt_before = datetime.datetime.now(pytz.utc) + + # Creating a simple workflow & storing it + wf = WFTestEmpty() + wf.store() + + # Get the current datetime (after the creation of the workflow) + dt_after = datetime.datetime.now(pytz.utc) + + self.assertLessEqual(dt_before, wf.ctime, "The workflow doesn't have" + "a valid creation time") + + self.assertGreaterEqual(dt_before, wf.ctime, "The workflow doesn't " + "have a valid creation " + "time") + def test_failing_calc_in_wf(self): """ This test checks that a workflow (but also a workflow with From d11b5053784a583b05dda5a5f4c2160a4cfb5e1a Mon Sep 17 00:00:00 2001 From: Spyros Zoupanos Date: Mon, 27 Nov 2017 18:00:40 +0100 Subject: [PATCH 3/3] Some minor corrections to the test aiida.backends.tests.workflows.TestWorkflowBasic#test_wf_ctime & corrections for the aiida.orm.implementation.sqlalchemy.workflow.Workflow#get_state --- aiida/backends/tests/workflows.py | 32 +++++++++++++++---- .../orm/implementation/sqlalchemy/workflow.py | 2 +- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/aiida/backends/tests/workflows.py b/aiida/backends/tests/workflows.py index 365c1018ef..9caa7d9d26 100644 --- a/aiida/backends/tests/workflows.py +++ b/aiida/backends/tests/workflows.py @@ -144,13 +144,31 @@ def test_workflow_info(self): get_workflow_info(w) def test_wf_get_state(self): - # Creating a simple workflow & storing it - wf = WFTestEmpty() - wf.store() + """ + Simple test that checks the state of the workflows. We create two + workflows since the test order in the SQLA was influencing the value + of aiida.backends.sqlalchemy.models.workflow.DbWorkflow.state which + should be a Choice object, according to the SQLA doc. Sometimes it + was automatically converted to unicode. + + Since we are interested to get a unicode from + aiida.orm.implementation.general.workflow.AbstractWorkflow#get_state + we enforce this conversion at + aiida.orm.implementation.sqlalchemy.workflow.Workflow#get_state + + For more info, check issue #951 + """ + # Creating two simple workflows & storing them + wf1 = WFTestEmpty() + wf1.store() + + wf2 = WFTestEmpty() + wf2.store() # Checking that the get_state doesn't throw exceptions and that # it is a valid state - self.assertIn(wf.get_state(), wf_states) + self.assertIn(wf1.get_state(), wf_states) + self.assertIn(wf2.get_state(), wf_states) def test_wf_ctime(self): import datetime @@ -169,9 +187,9 @@ def test_wf_ctime(self): self.assertLessEqual(dt_before, wf.ctime, "The workflow doesn't have" "a valid creation time") - self.assertGreaterEqual(dt_before, wf.ctime, "The workflow doesn't " - "have a valid creation " - "time") + self.assertGreaterEqual(dt_after, wf.ctime, "The workflow doesn't " + "have a valid creation " + "time") def test_failing_calc_in_wf(self): """ diff --git a/aiida/orm/implementation/sqlalchemy/workflow.py b/aiida/orm/implementation/sqlalchemy/workflow.py index 66eac9a83b..54ec82b25f 100644 --- a/aiida/orm/implementation/sqlalchemy/workflow.py +++ b/aiida/orm/implementation/sqlalchemy/workflow.py @@ -407,7 +407,7 @@ def get_state(self): Get the Workflow's state :return: a state from wf_states in aiida.common.datastructures """ - return self.dbworkflowinstance.state + return unicode(self.dbworkflowinstance.state) def set_state(self, state): """