From 5c6ba7bde171c6e614142125e8629a5a6e4cdaa3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <henrik@trineo.org>
Date: Fri, 13 Dec 2019 13:10:14 +0100
Subject: [PATCH] MAINT: getting closer to Spec

- reordered Experiment and Analysis CFood
- change Project folder name to 2010_xxx
- adjusted test to specs
- added function to set description
- Source -> SourceCode
- added source
---
 integrationtests/full_test/crawl.py           |  2 +-
 .../2010_TestProject/2019-02-03/README.md     |  2 +-
 .../2019-02-03_something/README.md            |  2 +-
 .../2010_TestProject/2019-02-03/README.md     |  2 +-
 .../2019-02-03_really_cool_finding/README.md  |  2 +-
 .../2019-02-03_something/README.md            |  2 +-
 integrationtests/full_test/model.yml          |  4 +--
 integrationtests/full_test/test_crawler.py    | 36 ++++++++-----------
 src/caosadvancedtools/cfood.py                | 25 +++++++++++++
 src/caosadvancedtools/utils.py                |  2 +-
 10 files changed, 49 insertions(+), 30 deletions(-)

diff --git a/integrationtests/full_test/crawl.py b/integrationtests/full_test/crawl.py
index 62ca8eac..5742769c 100755
--- a/integrationtests/full_test/crawl.py
+++ b/integrationtests/full_test/crawl.py
@@ -58,7 +58,7 @@ if __name__ == "__main__":
     c = Crawler(use_cache=True, access=access,
                 verbosity=DEBUG,
                 food=[ProjectCFood,
-                      AnalysisCFood, ExperimentCFood,
+                      ExperimentCFood,AnalysisCFood, 
                       PublicationCFood, SimulationCFood,
                       ])
     c.crawl(files, interactive=False, security_level=UPDATE)
diff --git a/integrationtests/full_test/extroot/DataAnalysis/2010_TestProject/2019-02-03/README.md b/integrationtests/full_test/extroot/DataAnalysis/2010_TestProject/2019-02-03/README.md
index 1c3d6935..718098b2 100644
--- a/integrationtests/full_test/extroot/DataAnalysis/2010_TestProject/2019-02-03/README.md
+++ b/integrationtests/full_test/extroot/DataAnalysis/2010_TestProject/2019-02-03/README.md
@@ -4,7 +4,7 @@ responsible:
 description: 	A description of another example analysis.
 
 source:
-- filename:	"/ExperimentalData/TestProject/2019-02-03/*.dat"
+- filename:	"/ExperimentalData/2010_TestProject/2019-02-03/*.dat"
   description:  an example reference to a results file
 
 scripts:
diff --git a/integrationtests/full_test/extroot/DataAnalysis/2010_TestProject/2019-02-03_something/README.md b/integrationtests/full_test/extroot/DataAnalysis/2010_TestProject/2019-02-03_something/README.md
index c62e216b..67b81e22 100644
--- a/integrationtests/full_test/extroot/DataAnalysis/2010_TestProject/2019-02-03_something/README.md
+++ b/integrationtests/full_test/extroot/DataAnalysis/2010_TestProject/2019-02-03_something/README.md
@@ -8,7 +8,7 @@ results:
   description:  an example reference to a results file
 
 source:
-- filename: /ExperimentalData/TestProject/2019-02-03_something/
+- filename: /ExperimentalData/2010_TestProject/2019-02-03_something/
   description:  an example reference to an experiment
 
 revisionOf:
diff --git a/integrationtests/full_test/extroot/ExperimentalData/2010_TestProject/2019-02-03/README.md b/integrationtests/full_test/extroot/ExperimentalData/2010_TestProject/2019-02-03/README.md
index 348a6fec..d46cdb4f 100644
--- a/integrationtests/full_test/extroot/ExperimentalData/2010_TestProject/2019-02-03/README.md
+++ b/integrationtests/full_test/extroot/ExperimentalData/2010_TestProject/2019-02-03/README.md
@@ -4,6 +4,6 @@ responsible:
 description: 	A description of another example experiment.
 
 results:
-- filename:	"/ExperimentalData/TestProject/2019-02-03/*.dat"
+- filename:	"/ExperimentalData/2010_TestProject/2019-02-03/*.dat"
   description:  an example reference to a results file
 ...
diff --git a/integrationtests/full_test/extroot/Publications/Posters/2019-02-03_really_cool_finding/README.md b/integrationtests/full_test/extroot/Publications/Posters/2019-02-03_really_cool_finding/README.md
index 68bebf70..27196a3d 100644
--- a/integrationtests/full_test/extroot/Publications/Posters/2019-02-03_really_cool_finding/README.md
+++ b/integrationtests/full_test/extroot/Publications/Posters/2019-02-03_really_cool_finding/README.md
@@ -4,7 +4,7 @@ responsible:
 description: 	A description of another example experiment.
 
 source:
-- /home/professional/CaosDB/caosdb-deploy/profiles/empty/custom/extroot/DataAnalysis/TestProject/2019-02-03/results.pdf
+- /home/professional/CaosDB/caosdb-deploy/profiles/empty/custom/extroot/DataAnalysis/2010_TestProject/2019-02-03/results.pdf
 
 results:
 - "*.pdf"
diff --git a/integrationtests/full_test/extroot/SimulationData/2010_TestProject/2019-02-03_something/README.md b/integrationtests/full_test/extroot/SimulationData/2010_TestProject/2019-02-03_something/README.md
index 3c984cb2..feaf7e2a 100644
--- a/integrationtests/full_test/extroot/SimulationData/2010_TestProject/2019-02-03_something/README.md
+++ b/integrationtests/full_test/extroot/SimulationData/2010_TestProject/2019-02-03_something/README.md
@@ -16,7 +16,7 @@ revisonOf:
 - ../2019-02-03
 
 source:
-- /extroot/ExperimentalData/TestProject/2019-02-03
+- /extroot/ExperimentalData/2010_TestProject/2019-02-03
 
 tags:
 - collagen
diff --git a/integrationtests/full_test/model.yml b/integrationtests/full_test/model.yml
index 6fd888dc..03439408 100644
--- a/integrationtests/full_test/model.yml
+++ b/integrationtests/full_test/model.yml
@@ -23,11 +23,11 @@ responsible:
   datatype: REFERENCE
 results:
   datatype: REFERENCE
-data:
+source:
   datatype: REFERENCE
 scripts:
   datatype: REFERENCE
-Source:
+SourceCode:
 Simulation:
   obligatory_properties:
     date:
diff --git a/integrationtests/full_test/test_crawler.py b/integrationtests/full_test/test_crawler.py
index 70821deb..280ef262 100755
--- a/integrationtests/full_test/test_crawler.py
+++ b/integrationtests/full_test/test_crawler.py
@@ -24,7 +24,7 @@ class CrawlerTest(unittest.TestCase):
         # There should be a Project with name TestProject which is referenced
         project_id = exp.get_property("Project").value
         project = get_entity_with_id(project_id)
-        assert project.name == "TestProject"
+        assert project.name == "2010_TestProject"
         assert "Project" in [p.name for p in project.get_parents()]
 
         # There should be a datafile as result attached with path datafile.dat
@@ -86,27 +86,21 @@ class CrawlerTest(unittest.TestCase):
         datfile = get_entity_with_id(datfile_id)
         assert os.path.basename(datfile.path) == "plot.py"
 
-        # There should be a file as script attached with path plot.py
-        datfile_id = ana.get_property("source").value[0]
-        datfile = get_entity_with_id(datfile_id)
-        assert os.path.basename(datfile.path) == "datafile.dat"
-        self.assertEqual(datfile.description,
-                         "an example reference to a results file")
-
         # Should have a responsible person
         self.assertIsNotNone(ana.get_property("Responsible"))
-        person = db.Record(id=ana.get_property("Responsible").value)
+        person = db.Record(id=ana.get_property("Responsible").value[0])
         person.retrieve()
-        self.assertEqual("Only", person.get_property("FirstName").value)
+        print(person)
+        self.assertEqual("Only", person.get_property("firstname").value)
 
         # Should have a description
         self.assertIsNotNone(ana.description)
 
         # Should reference an experiment
         self.assertIsNotNone(ana.get_property("Experiment"))
-        person = db.Record(id=ana.get_property("Responsible").value)
-        person.retrieve()
-        self.assertEqual("2019-02-03", person.get_property("date").value)
+        exp = db.Record(id=ana.get_property("Experiment").value[0])
+        exp.retrieve()
+        self.assertEqual("2019-02-03", exp.get_property("date").value)
 
         #######################
         # # second analysis # #
@@ -125,9 +119,9 @@ class CrawlerTest(unittest.TestCase):
 
         # Should reference an experiment
         self.assertIsNotNone(ana.get_property("Experiment"))
-        person = db.Record(id=ana.get_property("Responsible").value)
-        person.retrieve()
-        self.assertEqual("2019-02-03", person.get_property("date").value)
+        exp = db.Record(id=ana.get_property("Experiment").value[0])
+        exp.retrieve()
+        self.assertEqual("2019-02-03", exp.get_property("date").value)
 
     def test_simulation(self):
         ######################
@@ -168,7 +162,7 @@ class CrawlerTest(unittest.TestCase):
             "FIND Simulation with date=2019-02-03 and identifier='something'",
             unique=True)
 
-        # There should be a file as script attached with path plot.py
+        # There should be a file as script attached
         exp = get_entity_with_id(sim.get_property("source").value)
         self.assertEqual(exp.parents[0].name, "Experiment")
 
@@ -215,8 +209,8 @@ class CrawlerTest(unittest.TestCase):
         datfile = get_entity_with_id(datfile_id)
         assert os.path.basename(datfile.path) == "poster.pdf"
 
-        # There should be a file as data attached with path plot.py
-        datfile_id = pub.get_property("sources").value[0]
+        # There should be a file as data attached
+        datfile_id = pub.get_property("source").value[0]
         datfile = get_entity_with_id(datfile_id)
         assert os.path.basename(datfile.path) == "results.pdf"
 
@@ -228,8 +222,8 @@ class CrawlerTest(unittest.TestCase):
         ##########################
         pub = db.execute_query("FIND *paper_on_exciting_stuff ", unique=True)
 
-        # There should be a file as data attached with path plot.py
-        datfile_id = pub.get_property("sources").value[0]
+        # There should be a file as data attached
+        datfile_id = pub.get_property("source").value[0]
         datfile = get_entity_with_id(datfile_id)
         assert os.path.basename(datfile.path) == "results.pdf"
 
diff --git a/src/caosadvancedtools/cfood.py b/src/caosadvancedtools/cfood.py
index d0bce391..c7a98594 100644
--- a/src/caosadvancedtools/cfood.py
+++ b/src/caosadvancedtools/cfood.py
@@ -287,6 +287,31 @@ def assure_object_is_in_list(obj, containing_object, property_name,
         to_be_updated.append(containing_object)
 
 
+def assure_has_description(entity, description, to_be_updated=None,
+                           verbosity=INFO, force=False):
+    """
+    Checks whether `entity` has the description that is passed.
+
+    If this is the case this function ends. Otherwise the entity is assigned
+    a new description. The list to_be_updated is supplied, the entity is added to
+    the list in order to indicate, that the entity `entity` should be updated.
+    Otherwise it is directly updated
+    """
+
+    if entity.description == description:
+        return
+
+    entity.description = description
+
+    if to_be_updated is None:
+        if force:
+            entity.update(unique=False)
+        else:
+            guard.safe_update(entity, unique=False)
+    else:
+        to_be_updated.append(entity)
+
+
 def assure_has_parent(entity, parent, to_be_updated=None, verbosity=INFO,
                       force=False):
     """
diff --git a/src/caosadvancedtools/utils.py b/src/caosadvancedtools/utils.py
index ef902b9a..843c14ad 100644
--- a/src/caosadvancedtools/utils.py
+++ b/src/caosadvancedtools/utils.py
@@ -80,7 +80,7 @@ def find_file_included_by(glob):
     Returns a container.
     """
 
-    query_string = "FIND file which is stored at '{}'".format(glob)
+    query_string = "FIND file which is stored at {}".format(glob)
 
     try:
         return db.execute_query(query_string)
-- 
GitLab