Repository: ResearchObject/ro-crate-py
Pull request #25 (2020-10-16 15:31:32)

Read preview
Adds a preview and related checks to the read crate test


Patch:
diff --git a/rocrate/model/preview.py b/rocrate/model/preview.py
index abf158c..247c733 100644
--- a/rocrate/model/preview.py
+++ b/rocrate/model/preview.py
@@ -81,14 +81,15 @@ class Preview(File):
             data_entities.append(entity._jsonld)
         out_html = src.render(crate=self.crate, context=context_entities, data=data_entities)
         return out_html
-    
-    # TODO:should take into account the case if a readed preview file. in this case there is a source of it:
-    # no need to generate it, just copy the html and any files present in ro-crate-preview_files/ (if this dir exists)
+
     def write(self, dest_base):
-        write_path = self.filepath(dest_base)
-        out_html = self.generate_html()
-        with open(write_path, 'w') as outfile:
-            outfile.write(out_html)
+        if self.source:
+            super().write(dest_base)
+        else:
+            write_path = self.filepath(dest_base)
+            out_html = self.generate_html()
+            with open(write_path, 'w') as outfile:
+                outfile.write(out_html)
 
     def write_zip(self, zip_out):
         write_path = self.filepath()
@@ -100,5 +101,3 @@ class Preview(File):
         tmpfile.close()
         zip_out.write(tmpfile_path, write_path)
         os.remove(tmpfile_path)
-        
-        
\ No newline at end of file


Test patch:
diff --git a/test/test-data/read_crate/ro-crate-preview.html b/test/test-data/read_crate/ro-crate-preview.html
new file mode 100644
index 0000000..ed657cd
--- /dev/null
+++ b/test/test-data/read_crate/ro-crate-preview.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>test_read_crate</title>
+</head>
+<body>
+  <p>Dummy preview</p>
+</body>
+</html>
diff --git a/test/test_read.py b/test/test_read.py
index 618d23f..3d26f3e 100644
--- a/test/test_read.py
+++ b/test/test_read.py
@@ -1,13 +1,16 @@
+import pytest
+
 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, helpers):
+@pytest.mark.parametrize("load_preview", [False, True])
+def test_crate_dir_loading(test_data_dir, tmpdir, helpers, load_preview):
     # load crate from directory
     crate_dir = test_data_dir / 'read_crate'
-    crate = ROCrate(crate_dir, load_preview=True)
+    crate = ROCrate(crate_dir, load_preview=load_preview)
 
     # check loaded entities and properties
     root = crate.dereference('./')
@@ -34,6 +37,10 @@ def test_crate_dir_loading(test_data_dir, tmpdir, helpers):
     assert preview_prop['@id'] == preview.id
     assert preview_prop['@type'] == 'CreativeWork'
     assert preview_prop['about'] == {'@id': './'}
+    if load_preview:
+        assert (crate_dir / 'ro-crate-preview.html').samefile(preview.source)
+    else:
+        assert not preview.source
 
     main_wf = crate.dereference('test_galaxy_wf.ga')
     wf_prop = main_wf.properties()
@@ -78,3 +85,11 @@ def test_crate_dir_loading(test_data_dir, tmpdir, helpers):
 
     metadata_path = out_path / helpers.METADATA_FILE_NAME
     assert metadata_path.exists()
+
+    if load_preview:
+        preview_out_path = out_path / 'ro-crate-preview.html'
+        with open(preview_out_path, "rb") as f:
+            preview_out_content = f.read()
+        with open(preview.source, "rb") as f:
+            preview_content = f.read()
+        assert preview_out_content == preview_content