Skip to content

Commit

Permalink
improving tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishavlin committed Sep 1, 2023
1 parent 657b28b commit 48c54ac
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
19 changes: 16 additions & 3 deletions src/yt_napari/_ds_cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import os.path
from os import PathLike
from typing import Optional

import yt

from yt_napari import _special_loaders
Expand Down Expand Up @@ -42,11 +46,11 @@ def check_then_load(self, filename: str, cache_if_not_found: bool = True):
if self.exists(filename):
ytnapari_log.info(f"loading {filename} from cache.")
return self.get_ds(filename)
elif filename.startswith("_ytnapari") and hasattr(_special_loaders, filename):
elif callable_name := _check_for_special(filename):
# the filename is actually a function handle! get it, call it
# this allows yt-napari to to use all the yt fake datasets in
# this allows yt-napari to use all the yt fake datasets in
# testing without saving them to disk.
ds_callable = getattr(_special_loaders, filename)
ds_callable = getattr(_special_loaders, callable_name)
ds = ds_callable()
else:
ds = yt.load(filename)
Expand All @@ -57,3 +61,12 @@ def check_then_load(self, filename: str, cache_if_not_found: bool = True):


dataset_cache = DatasetCache()


def _check_for_special(filename: PathLike) -> Optional[str]:
# check if a "filename" is one of our short-circuiting special loaders
# and return the function name if it is valid.
basename = os.path.basename(filename)
if basename.startswith("_ytnapari") and hasattr(_special_loaders, basename):
return str(basename)
return None
25 changes: 25 additions & 0 deletions src/yt_napari/_tests/test_metadata_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from yt_napari._widget_matadata import LayersList, MetadataWidget


def test_widget_reader(make_napari_viewer):
viewer = make_napari_viewer()
r = MetadataWidget(napari_viewer=viewer)
r.metadata_input_container.filename.value = "_ytnapari_load_grid"
r.inspect_file()
r.inspect_file() # do it again to hit that clear statement

assert "stream" in r.field_lists.keys()

r.field_lists["stream"].expand()
r.field_lists["stream"].expand()

assert "domain_left_edge" in r.array_vals.keys()
r.array_vals["domain_left_edge"].update_units("km")
r.deleteLater()


def test_layer_list():
ll = LayersList("test_layer", range(0, 10), expand=False)
ll.expand()
assert ll.currently_expanded is True
ll.deleteLater()
16 changes: 6 additions & 10 deletions src/yt_napari/_widget_matadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ def inspect_file(self):
self.layout().removeWidget(list_widget)
list_widget.setParent(None)
self.widgets_to_clear = []

self.field_lists = {}
self.array_vals = {}
py_kwargs = {}

_gui_utilities.translator.get_pydantic_kwargs(
self.metadata_input_container, _data_model.MetadataModel, py_kwargs
)
Expand All @@ -62,6 +64,7 @@ def inspect_file(self):
for attr, val in meta_data_dict.items():
if isinstance(val, unyt_array):
newid = UnytArrayQWidget(attr, val)
self.array_vals[attr] = newid
else:
newid = QLabel(f"{attr}: {str(val)}")
self.widgets_to_clear.append(newid)
Expand All @@ -72,6 +75,7 @@ def inspect_file(self):
for ftype, fields in fields_by_type.items():
new_field_list = LayersList(ftype, fields, ilist < 3)
ilist += 1
self.field_lists[ftype] = new_field_list
self.widgets_to_clear.append(new_field_list)
self.layout().addWidget(new_field_list)

Expand All @@ -80,14 +84,6 @@ def inspect_file(self):
# https://stackoverflow.com/questions/11077793/is-there-a-standard-component-for-collapsible-panel-in-qt


class CustomQStandardItem(QStandardItem):
def __init__(self, icon, text):
super().__init__(icon, text)

def dropMimeData(self, data, action, row, column, parent):
pass


class LayersList(QWidget):
"""
LayerList class which acts as collapsable list.
Expand All @@ -113,7 +109,7 @@ def __init__(self, name, layers, expand=True):
self.main_layout.addWidget(self.layer_list)
self.expand_button.clicked.connect(self.expand)
self.setLayout(self.main_layout)
self.resized_size = 16.5 * len(layers)
self.resized_size = int(16 * len(layers))
if not expand:
self.expand()

Expand Down

0 comments on commit 48c54ac

Please sign in to comment.