Skip to content

Commit

Permalink
Merge pull request #74 from Novartis/73_reorder_filecrawl
Browse files Browse the repository at this point in the history
#73 moved new annotation link to front
  • Loading branch information
alokito authored Mar 2, 2024
2 parents 66cca86 + a00403c commit 1e02e0a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 19 deletions.
20 changes: 9 additions & 11 deletions cellxgene_gateway/filecrawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
# the specific language governing permissions and limitations under the License.

import os
import html
import urllib.parse

from cellxgene_gateway import env, flask_util
Expand All @@ -19,19 +19,17 @@ def render_annotations(item, item_source):
url = flask_util.view_url(
item_source.get_annotations_subpath(item), item_source.name
)
new_annotation = f"<a class='new' href='{url}'>new</a>"
new_annotation = [f"<a class='new' href='{url}'>new</a>"]

annotations = (
", ".join(
[
f"<a href='{CacheKey(item, item_source, a).view_url}/'>{a.name}</a>"
for a in item.annotations
]
)
+ ", "
[
f"<a href='{CacheKey(item, item_source, a).view_url}/'>{html.escape(a.name)}</a>"
for a in item.annotations
]
if item.annotations
else ""
else []
)
return " | annotations: " + annotations + new_annotation
return "| annotations: " + ", ".join(new_annotation + annotations)


def render_item(item, item_source):
Expand Down
66 changes: 58 additions & 8 deletions tests/test_filecrawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,76 @@
source = FileItemSource("/tmp")


def make_entry(subpath="somepath", annotations=None):
return FileItem(
subpath=subpath,
name="entry",
ext=".h5ad",
type=ItemType.h5ad,
annotations=annotations,
)


class TestRenderEntry(unittest.TestCase):
def test_GIVEN_path_both_slash_THEN_view_has_single_slash(self):
entry = FileItem(subpath="/somepath/", name="entry", type=ItemType.h5ad)
entry = make_entry(subpath="/somepath/")
rendered = render_item(entry, source)
self.assertIn("view/somepath/entry/'", rendered)
self.assertIn("view/somepath/entry.h5ad/'", rendered)

def test_GIVEN_path_starts_slash_THEN_view_has_single_slash(self):
entry = FileItem(subpath="/somepath", name="entry", type=ItemType.h5ad)
entry = make_entry(subpath="/somepath")
rendered = render_item(entry, source)
self.assertIn("view/somepath/entry/'", rendered)
self.assertIn("view/somepath/entry.h5ad/'", rendered)

def test_GIVEN_path_ends_slash_THEN_view_has_single_slash(self):
entry = FileItem(subpath="somepath/", name="entry", type=ItemType.h5ad)
entry = make_entry(subpath="somepath/")
rendered = render_item(entry, source)
self.assertIn("view/somepath/entry/'", rendered)
self.assertIn("view/somepath/entry.h5ad/'", rendered)

def test_GIVEN_path_no_slash_THEN_view_has_single_slash(self):
entry = FileItem(subpath="somepath", name="entry", type=ItemType.h5ad)
entry = make_entry(subpath="somepath")
rendered = render_item(entry, source)
self.assertIn("view/somepath/entry.h5ad/'", rendered)


class TestRenderAnnotation(unittest.TestCase):
def test_GIVEN_no_annotation_THEN_new_alone(self):
entry = make_entry(annotations=None)
rendered = render_item(entry, source)
self.assertIn(
"> | annotations: <a class='new' href='/source/Files:/tmp/view/somepath/entry_annotations'>new</a></li>",
rendered,
)

def test_GIVEN_annotation_THEN_new_before(self):
annotation = FileItem(
subpath="somepath/entry_annotations",
name="annot",
ext=".csv",
type=ItemType.annotation,
)
entry = make_entry(annotations=[annotation])
rendered = render_item(entry, source)
self.assertIn("view/somepath/entry/'", rendered)
self.assertIn(
"> | annotations: <a class='new' href='/source/Files:/tmp/view/somepath/entry_annotations'>new</a>,"
" <a href='/source/Files:/tmp/view/somepath/entry_annotations/annot.csv/'>annot</a></li>",
rendered,
)

def test_GIVEN_annotation_THEN_escaped(self):
annotation = FileItem(
subpath="somepath/entry_annotations",
name="hot&cold",
ext=".csv",
type=ItemType.annotation,
)
entry = make_entry(annotations=[annotation])
rendered = render_item(entry, source)
self.assertIn(
"> | annotations: <a class='new' href='/source/Files:/tmp/view/somepath/entry_annotations'>new</a>,"
" <a href='/source/Files:/tmp/view/somepath/entry_annotations/hot%26cold.csv/'>hot&amp;cold</a></li>",
rendered,
)


class TestRenderItemSource(unittest.TestCase):
Expand Down

0 comments on commit 1e02e0a

Please sign in to comment.