Skip to content

Commit

Permalink
Added tests for line info widget events
Browse files Browse the repository at this point in the history
  • Loading branch information
jaladh-singhal committed Aug 25, 2020
1 parent 5a40f09 commit 7a6406e
Showing 1 changed file with 189 additions and 5 deletions.
194 changes: 189 additions & 5 deletions tardis/widgets/tests/test_line_info.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import pytest
import pandas as pd
import numpy as np
from plotly.callbacks import Points, BoxSelector
from tardis.widgets.line_info import LineInfoWidget
from tardis.util.base import species_string_to_tuple


@pytest.fixture(scope="class")
def line_info_widget(simulation_verysimple):
line_info_widget = LineInfoWidget.from_simulation(simulation_verysimple)
# To attach event listeners to component widgets of line_info_widget
_ = line_info_widget.display()
return line_info_widget


Expand Down Expand Up @@ -48,6 +48,10 @@ def test_get_species_interactions(

@pytest.fixture
def allowed_species(self, line_info_widget, wavelength_range, filter_mode):
"""
For different combinations of wavelength_range and filter_mode
parameters, it calls get_species_interactions on line_info_widget
"""
# Find species present within the selected wavelength range
species_interactions_df = line_info_widget.get_species_interactions(
wavelength_range, filter_mode
Expand All @@ -67,16 +71,15 @@ def test_get_last_line_counts(
Since this method depends on get_species_interactions() so we need to
make sure that we select only allowed species i.e. present within the
wavelength range selected by the get_species_interactions()
wavelength range selected by the get_species_interactions(), which is
being done here by allowed_species fixture.
"""
if allowed_species is None:
last_line_counts_df = line_info_widget.get_last_line_counts(
None, filter_mode, group_mode
)

# Dataframe contains all falsy values (proxy for empty)
assert last_line_counts_df.all(axis=None) == False

return

for selected_species in allowed_species:
Expand Down Expand Up @@ -114,3 +117,184 @@ def test_get_last_line_counts(
# Test shape of the dataframe
assert last_line_counts_df.shape == (expected_df_length, 1)


class TestLineInfoWidgetEvents:
"""
Test changes in table widgets data by triggering all possible events.
This will make sure that all event listeners are working properly and
updating data in tables accurately. The following four methods are to
trigger each event (interaction) which is possible in LineInfoWidget.
"""

@pytest.fixture(
scope="class",
params=[
[2500, 3500], # Wavelength range with plenty of line interactions
[16200, 16300], # Wavelength range with no line interactions
None, # No selection of wavelength range
],
)
def liw_with_selection(self, simulation_verysimple, request):
"""
Makes different wavelength range selection on figure (specified by
params) after creating a LineInfoWidget object.
"""
liw = LineInfoWidget.from_simulation(simulation_verysimple)
# To attach event listeners to component widgets of line_info_widget
_ = liw.display()

selection_range = request.param

# Since we cannot programatically make a Box selection on spectrum
# so we have to directly call its event listener by passing
# selected wavelength range in a BoxSelector object
if selection_range:
liw._spectrum_selection_handler(
trace=liw.figure_widget.data[0],
points=Points(),
selector=BoxSelector(
xrange=selection_range,
yrange=[
-1.8e39,
1.8e40,
], # Not very relevant, approx height of box
),
)

return liw, selection_range

def test_selection_on_plot(self, liw_with_selection):
"""
Test if selection on spectrum plot, updates correct data in both
the tables and total packets label.
"""
# Since wavelength range selection is already made by liw_with_selection
# fixture, we don't need to trigger selection event here again

line_info_widget, selected_wavelength_range = liw_with_selection

expected_species_interactions = line_info_widget.get_species_interactions(
wavelength_range=selected_wavelength_range,
filter_mode=line_info_widget.FILTER_MODES[
line_info_widget.filter_mode_buttons.index
],
)

pd.testing.assert_frame_equal(
expected_species_interactions,
line_info_widget.species_interactions_table.df,
)

expected_last_line_counts = line_info_widget.get_last_line_counts(
selected_species=expected_species_interactions.index[0],
filter_mode=line_info_widget.FILTER_MODES[
line_info_widget.filter_mode_buttons.index
],
group_mode=line_info_widget.GROUP_MODES[
line_info_widget.group_mode_dropdown.index
],
)

pd.testing.assert_frame_equal(
expected_last_line_counts,
line_info_widget.last_line_counts_table.df,
)

if selected_wavelength_range in [None, [16200, 16300]]:
expected_total_packets = 0
else:
expected_total_packets = expected_last_line_counts.iloc[:, 0].sum()
assert expected_total_packets == int(
line_info_widget.total_packets_label.widget.children[1].value
)

@pytest.mark.parametrize("selected_filter_mode_idx", [0, 1])
def test_filter_mode_toggle(
self, liw_with_selection, selected_filter_mode_idx,
):
"""
Test if toggling filter_mode_buttons updates correct data in both
the tables and total packets label.
"""
line_info_widget, selected_wavelength_range = liw_with_selection

# Toggle the filter_mode_buttons
line_info_widget.filter_mode_buttons.index = selected_filter_mode_idx

expected_species_interactions = line_info_widget.get_species_interactions(
wavelength_range=selected_wavelength_range,
filter_mode=line_info_widget.FILTER_MODES[selected_filter_mode_idx],
)

pd.testing.assert_frame_equal(
expected_species_interactions,
line_info_widget.species_interactions_table.df,
)

expected_last_line_counts = line_info_widget.get_last_line_counts(
selected_species=expected_species_interactions.index[0],
filter_mode=line_info_widget.FILTER_MODES[selected_filter_mode_idx],
group_mode=line_info_widget.GROUP_MODES[
line_info_widget.group_mode_dropdown.index
],
)

pd.testing.assert_frame_equal(
expected_last_line_counts,
line_info_widget.last_line_counts_table.df,
)

if selected_wavelength_range in [None, [16200, 16300]]:
expected_total_packets = 0
else:
expected_total_packets = expected_last_line_counts.iloc[:, 0].sum()
assert expected_total_packets == int(
line_info_widget.total_packets_label.widget.children[1].value
)

def test_selection_on_species_intrctn_table(self, liw_with_selection):
"""
Test if selection on each row in species_interaction_table updates
correct data in last_line_counts_table and total packets label.
"""
line_info_widget, _ = liw_with_selection

for (
selected_species
) in line_info_widget.species_interactions_table.df.index:
# Select row in species_interactions_table
line_info_widget.species_interactions_table.change_selection(
[selected_species]
)

if bool(selected_species) == False:
# When selected_species is a falsy value due to empty
# species_interactions_table, use it as None in get_last_line_counts()
selected_species = None

expected_last_line_counts = line_info_widget.get_last_line_counts(
selected_species=selected_species,
filter_mode=line_info_widget.FILTER_MODES[
line_info_widget.filter_mode_buttons.index
],
group_mode=line_info_widget.GROUP_MODES[
line_info_widget.group_mode_dropdown.index
],
)

pd.testing.assert_frame_equal(
expected_last_line_counts,
line_info_widget.last_line_counts_table.df,
)

if selected_species is None:
expected_total_packets = 0
else:
expected_total_packets = expected_last_line_counts.iloc[
:, 0
].sum()
assert expected_total_packets == int(
line_info_widget.total_packets_label.widget.children[1].value
)

0 comments on commit 7a6406e

Please sign in to comment.