Repository: ResearchObject/ro-crate-py
Pull request #23 (2020-10-05 15:26:59)

Test read update
Expands `test_read.py` to test more entities and fixes some issues encountered along the way.


Patch:
diff --git a/rocrate/model/dataset.py b/rocrate/model/dataset.py
index afdce7d..091dc38 100644
--- a/rocrate/model/dataset.py
+++ b/rocrate/model/dataset.py
@@ -40,6 +40,10 @@ class Dataset(DataEntity):
         }
         return val
 
+    # SHOULD end with /
+    def format_id(self, identifier):
+        return identifier.rstrip("/") + "/"
+
     # name contentSize dateModified encodingFormat identifier sameAs
     @property
     def datePublished(self):
diff --git a/rocrate/rocrate.py b/rocrate/rocrate.py
index c750b90..68455aa 100644
--- a/rocrate/rocrate.py
+++ b/rocrate/rocrate.py
@@ -130,7 +130,7 @@ class ROCrate():
                 identifier = entity.pop('@id', None)
                 if os.path.exists(file_path):
                     # referencing a file path relative to crate-root
-                    instance = File(self, file_path, identifier, entity)
+                    instance = File(self, file_path, identifier, properties=entity)
                 else:
                     # check if it is a valid absolute URI
                     try:


Test patch:
diff --git a/test/conftest.py b/test/conftest.py
index 714455a..9927ac2 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -7,26 +7,28 @@ import pytest
 
 THIS_DIR = pathlib.Path(__file__).absolute().parent
 TEST_DATA_NAME = 'test-data'
-METADATA_FILE_NAME = 'ro-crate-metadata.jsonld'
-WORKFLOW_TYPES = {"File", "SoftwareSourceCode", "Workflow"}
 
 
 class Helpers:
 
-    @staticmethod
-    def read_json_entities(crate_base_path):
-        metadata_path = pathlib.Path(crate_base_path) / METADATA_FILE_NAME
+    CONTEXT = "https://w3id.org/ro/crate/1.0/context"
+    METADATA_FILE_NAME = 'ro-crate-metadata.jsonld'
+    WORKFLOW_TYPES = {"File", "SoftwareSourceCode", "Workflow"}
+
+    @classmethod
+    def read_json_entities(cls, crate_base_path):
+        metadata_path = pathlib.Path(crate_base_path) / cls.METADATA_FILE_NAME
         with open(metadata_path, "rt") as f:
             json_data = json.load(f)
         return {_["@id"]: _ for _ in json_data["@graph"]}
 
-    @staticmethod
-    def check_wf_crate(json_entities, wf_file_name):
+    @classmethod
+    def check_wf_crate(cls, json_entities, wf_file_name):
         assert json_entities["./"]["mainEntity"]["@id"] == wf_file_name
         assert wf_file_name in json_entities
         wf_entity = json_entities[wf_file_name]
         assert isinstance(wf_entity["@type"], list)
-        assert set(wf_entity["@type"]) == WORKFLOW_TYPES
+        assert set(wf_entity["@type"]) == cls.WORKFLOW_TYPES
         assert "programmingLanguage" in wf_entity
 
 
diff --git a/test/test_read.py b/test/test_read.py
index 74d6fc1..618d23f 100644
--- a/test/test_read.py
+++ b/test/test_read.py
@@ -1,25 +1,80 @@
 from rocrate.rocrate import ROCrate
 
+_URL = ('https://raw.githubusercontent.com/ResearchObject/ro-crate-py/master/'
+        'test/test-data/sample_file.txt')
 
-def test_crate_dir_loading(test_data_dir, tmpdir):
+
+def test_crate_dir_loading(test_data_dir, tmpdir, helpers):
     # load crate from directory
     crate_dir = test_data_dir / 'read_crate'
     crate = ROCrate(crate_dir, load_preview=True)
 
     # check loaded entities and properties
+    root = crate.dereference('./')
+    assert crate.root_dataset is root
+    root_prop = root.properties()
+    assert root_prop['@id'] == root.id
+    assert root_prop['@id'] == './'
+    assert root_prop['@type'] == 'Dataset'
+
+    metadata = crate.dereference(helpers.METADATA_FILE_NAME)
+    assert crate.metadata is metadata
+    md_prop = metadata.properties()
+    assert md_prop['@id'] == metadata.id
+    assert md_prop['@id'] == helpers.METADATA_FILE_NAME
+    assert md_prop['@type'] == 'CreativeWork'
+    assert md_prop['about'] == {'@id': './'}
+    assert md_prop['conformsTo'] == {'@id': 'https://w3id.org/ro/crate/1.0'}
+    assert metadata.root is root
+
+    preview = crate.dereference('ro-crate-preview.html')
+    assert preview == crate.preview
+    preview_prop = preview.properties()
+    assert preview_prop['@id'] == 'ro-crate-preview.html'
+    assert preview_prop['@id'] == preview.id
+    assert preview_prop['@type'] == 'CreativeWork'
+    assert preview_prop['about'] == {'@id': './'}
+
     main_wf = crate.dereference('test_galaxy_wf.ga')
     wf_prop = main_wf.properties()
     assert wf_prop['@id'] == 'test_galaxy_wf.ga'
+    assert wf_prop['@id'] == main_wf.id
+    assert set(wf_prop['@type']) == helpers.WORKFLOW_TYPES
+    assert wf_prop['programmingLanguage'] == {'@id': 'https://galaxyproject.org'}
+    assert wf_prop['subjectOf'] == {'@id': 'abstract_wf.cwl'}
+
+    abs_wf = crate.dereference('abstract_wf.cwl')
+    abs_wf_prop = abs_wf.properties()
+    assert abs_wf_prop['@id'] == 'abstract_wf.cwl'
+    assert abs_wf_prop['@id'] == abs_wf.id
+    assert set(abs_wf_prop['@type']) == helpers.WORKFLOW_TYPES
 
     wf_author = crate.dereference('#joe')
     author_prop = wf_author.properties()
+    assert author_prop['@id'] == '#joe'
+    assert author_prop['@id'] == wf_author.id
     assert author_prop['@type'] == 'Person'
     assert author_prop['name'] == 'Joe Bloggs'
 
+    test_file = crate.dereference('test_file_galaxy.txt')
+    test_file_prop = test_file.properties()
+    assert test_file_prop['@id'] == 'test_file_galaxy.txt'
+    assert test_file_prop['@id'] == test_file.id
+
+    remote_file = crate.dereference(_URL)
+    remote_file_prop = remote_file.properties()
+    assert remote_file_prop['@id'] == _URL
+    assert remote_file_prop['@id'] == remote_file.id
+
+    dataset = crate.dereference('examples/')
+    dataset_prop = dataset.properties()
+    assert dataset_prop['@id'] == 'examples/'
+    assert dataset_prop['@id'] == dataset.id
+
     # write the crate in a different directory
     out_path = tmpdir / 'crate_read_out'
     out_path.mkdir()
     crate.write_crate(out_path)
 
-    metadata_path = out_path / 'ro-crate-metadata.jsonld'
+    metadata_path = out_path / helpers.METADATA_FILE_NAME
     assert metadata_path.exists()