Skip to content

Commit

Permalink
Merge pull request data-exp-lab#69 from chrishavlin/actually_fix_test…
Browse files Browse the repository at this point in the history
…_maybe

again fixing that windows test...
  • Loading branch information
chrishavlin authored Aug 2, 2023
2 parents 42a83bc + 7d317d7 commit 9f5ca55
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/yt_napari/_tests/test_widget_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from yt_napari._widget_reader import ReaderWidget, SelectionEntry
from yt_napari.viewer import Scene

# note: the cache is disabled for all the tests in this file due to flakiness
# in github CI. It may be that loading from a true file, rather than the
# yt_ugrid_ds_fn fixture would fix that...


def test_widget_reader_add_selections(make_napari_viewer, yt_ugrid_ds_fn):
viewer = make_napari_viewer()
Expand All @@ -28,16 +32,21 @@ def test_widget_reader_add_selections(make_napari_viewer, yt_ugrid_ds_fn):
assert sel.selection_type == "Slice"


def test_widget_reader(make_napari_viewer, yt_ugrid_ds_fn, caplog):
def _rebuild_data(final_shape, data):
# the yt file thats being loaded from the pytest fixture is a saved
# dataset created from an in-memory uniform grid, and the re-loaded
# dataset will not have the full functionality of a ds. so here, we
# inject a correctly shaped random array here. If we start using full
# test datasets from yt in testing, this should be changed.
return np.random.random(final_shape) * data.mean()

# make_napari_viewer is a pytest fixture. It takes any keyword arguments
# that napari.Viewer() takes. The fixture takes care of teardown, do **not**
# explicitly close it!
viewer = make_napari_viewer()

r = ReaderWidget(napari_viewer=viewer)
def test_widget_reader(make_napari_viewer, yt_ugrid_ds_fn):

viewer = make_napari_viewer()
r = ReaderWidget(napari_viewer=viewer)
r.ds_container.filename.value = yt_ugrid_ds_fn
r.ds_container.store_in_cache.value = False
r.add_new_button.click()
sel = list(r.active_selections.values())[0]
assert isinstance(sel, SelectionEntry)
Expand All @@ -47,24 +56,37 @@ def test_widget_reader(make_napari_viewer, yt_ugrid_ds_fn, caplog):
mgui_region.fields.field_name.value = "density"
mgui_region.resolution.value = (10, 10, 10)

def rebuild_data(final_shape, data):
# the yt file thats being loaded from the pytest fixture is a saved
# dataset created from an in-memory uniform grid, and the re-loaded
# dataset will not have the full functionality of a ds. so here, we
# inject a correctly shaped random array here. If we start using full
# test datasets from yt in testing, this should be changed.
return np.random.random(final_shape) * data.mean()
rebuild = partial(_rebuild_data, mgui_region.resolution.value)
r._post_load_function = rebuild
r.load_data()


def test_subsequent_load(make_napari_viewer, yt_ugrid_ds_fn):
viewer = make_napari_viewer()

rebuild = partial(rebuild_data, mgui_region.resolution.value)
r = ReaderWidget(napari_viewer=viewer)
r.ds_container.filename.value = yt_ugrid_ds_fn
r.ds_container.store_in_cache.value = False
r.add_new_button.click()

sel = list(r.active_selections.values())[0]
assert isinstance(sel, SelectionEntry)

mgui_region = sel.selection_container_raw
mgui_region.fields.field_type.value = "gas"
mgui_region.fields.field_name.value = "density"
mgui_region.resolution.value = (10, 10, 10)

rebuild = partial(_rebuild_data, mgui_region.resolution.value)
r._post_load_function = rebuild
r.load_data()

# alter parameters, load again
mgui_region.fields.field_name.value = "temperature"
mgui_region.left_edge.value.value = (0.4, 0.4, 0.4)
mgui_region.right_edge.value.value = (0.6, 0.6, 0.6)
r.load_data()
# should have read from cache, check the log:
assert yt_ugrid_ds_fn in caplog.text

# the viewer should now have two images
assert len(viewer.layers) == 2

Expand Down

0 comments on commit 9f5ca55

Please sign in to comment.