def test_navigator_api(self): path = "file.nc" variable = "air_temperature" initial_time = "2019-01-01 00:00:00" valid_time = "2019-01-01 12:00:00" pressure = 750. navigator = db.Navigator() controls = db.Controls(navigator) store = redux.Store(db.reducer, middlewares=[controls]) actions = [ db.set_value("pattern", path), db.set_value("variable", variable), db.set_value("initial_time", initial_time), db.set_value("valid_time", valid_time) ] for action in actions: store.dispatch(action) result = store.state expect = { "pattern": path, "variable": variable, "variables": [variable], "initial_time": initial_time, "initial_times": [initial_time], "valid_time": valid_time, "valid_times": [valid_time], "pressures": [pressure], } self.assertEqual(expect, result)
def test_set_valid_time_sets_pressures(self): path = "file.nc" variable = "air_temperature" initial_time = "2019-01-01 00:00:00" valid_time = "2019-01-01 12:00:00" pressure = 750. index = 0 self.database.insert_file_name(path, initial_time) self.database.insert_time(path, variable, valid_time, index) self.database.insert_pressure(path, variable, pressure, index) store = redux.Store(db.reducer, middlewares=[self.controls]) actions = [ db.set_value("pattern", path), db.set_value("variable", variable), db.set_value("initial_time", initial_time), db.set_value("valid_time", valid_time) ] for action in actions: store.dispatch(action) result = store.state expect = { "pattern": path, "variable": variable, "variables": [variable], "initial_time": initial_time, "initial_times": [initial_time], "valid_time": valid_time, "valid_times": [valid_time], "pressures": [pressure], } self.assertEqual(expect, result)
def test_set_variable_given_initial_time_changes_times_and_pressure(self): path = "some.nc" initial_time = "2019-01-01 00:00:00" self.database.insert_file_name(path, initial_time) index = 0 for variable, valid_time, pressure in [ ("air_temperature", "2019-01-01 01:00:00", 1000.), ("olr", "2019-01-01 01:03:00", 10.) ]: self.database.insert_time(path, variable, valid_time, index) self.database.insert_pressure(path, variable, pressure, index) store = redux.Store(db.reducer, middlewares=[self.controls]) actions = [ db.set_value("pattern", path), db.set_value("variable", "air_temperature"), db.set_value("initial_time", initial_time), db.set_value("variable", "olr") ] for action in actions: store.dispatch(action) result = store.state expect = { "pattern": path, "variable": "olr", "variables": ["air_temperature", "olr"], "initial_time": initial_time, "initial_times": [initial_time], "valid_times": ["2019-01-01 01:03:00"], "pressures": [0.0] } self.assertEqual(expect, result)
def test_middleware_given_set_name_emits_set_numbers(): store = redux.Store(colors.reducer) action = colors.set_palette_name("Blues") result = list(colors.palettes(store, action)) assert result == [ colors.set_palette_numbers(colors.palette_numbers("Blues")), colors.set_palette_name("Blues"), ]
def test_middleware_given_fixed_allows_source_limit_actions(): log = Log() store = redux.Store(colors.reducer, middlewares=[colors.palettes, log]) actions = [colors.set_source_limits(0, 100)] for action in actions: store.dispatch(action) assert log.actions == actions assert store.state == {"colorbar": {"low": 0, "high": 100}}
def test_middleware_given_set_name_emits_set_numbers(): log = Log() store = redux.Store(colors.reducer, middlewares=[colors.palettes, log]) store.dispatch(colors.set_palette_name("Blues")) assert log.actions == [ colors.set_palette_numbers(colors.palette_numbers("Blues")), colors.set_palette_name("Blues") ]
def test_middleware_given_fixed_swallows_source_limit_actions(): log = Log() store = redux.Store(colors.reducer, middlewares=[colors.palettes, log]) actions = [colors.set_fixed(True), colors.set_source_limits(0, 100)] for action in actions: store.dispatch(action) assert log.actions == [colors.set_fixed(True)] assert store.state == {"colorbar": {"fixed": True}}
def test_middleware_converts_next_value_to_set_value(self): store = redux.Store(forest.db.control.reducer, initial_state={ "k": 2, "ks": [1, 2, 3] }) action = forest.db.control.next_value("k", "ks") result = list(forest.db.control.next_previous(store, action)) self.assertEqual(result, [forest.db.control.set_value("k", 3)])
def test_middleware_given_incomplete_state_emits_set_colorbar(): store = redux.Store( colors.reducer, initial_state={"colorbar": {"low": -1}} ) action = {"kind": "ANY"} result = list(colors.palettes(store, action)) settings = {**colors.defaults(), **{"low": -1}} expect = [action, colors.set_colorbar(settings)] assert expect == result
def test_middleware_on_save_given_add(): mode = "add" index = 42 initial_state = {"layers": {"mode": {"state": mode}, "index": {index: {}}}} store = redux.Store(layers.reducer, initial_state=initial_state) action = layers.on_save({}) expect = layers.save_layer(index + 1, {}) result = list(layers.middleware(store, action)) assert expect == result[0]
def test_middleware_next_pressure_given_pressures_none(): store = redux.Store(db.reducer, middlewares=[ db.InverseCoordinate("pressure"), db.next_previous, ]) action = forest.db.control.next_value("pressure", "pressures") store.dispatch(action) assert store.state == {}
def test_storage_middleware_given_on_save_copies_colorbar(): store = redux.Store( presets.reducer, initial_state={"colorbar": {"K": "V"}} ) storage = presets.Storage() action = presets.on_save("label") middleware = presets.Middleware(storage) list(middleware(store, action)) store.state["colorbar"]["K"] = "T" assert storage.load("label") == {"K": "V"}
def test_support_old_style_state(self): """Not all components are ready to accept dict() states""" listener = unittest.mock.Mock() store = redux.Store(forest.db.control.reducer) old_states = (rx.Stream().listen_to(store).map( lambda x: forest.db.control.State(**x))) old_states.add_subscriber(listener) store.dispatch(forest.db.control.set_value("pressure", 1000)) expect = forest.db.control.State(pressure=1000) listener.assert_called_once_with(expect)
def test_middleware_given_inconsistent_number(): store = redux.Store(colors.reducer) store.dispatch(colors.set_palette_number(1000)) action = colors.set_palette_name("Viridis") result = list(colors.palettes(store, action)) assert result == [ colors.set_palette_numbers(colors.palette_numbers("Viridis")), colors.set_palette_number(256), colors.set_palette_name("Viridis"), ]
def test_type_system_middleware(): times = np.array([dt.datetime(2019, 1, 1), dt.datetime(2019, 1, 2)], dtype="datetime64[s]") converter = db.Converter({"valid_times": db.stamps}) store = redux.Store(db.reducer, middlewares=[converter]) store.dispatch(db.set_value("valid_times", times)) result = store.state expect = {"valid_times": ["2019-01-01 00:00:00", "2019-01-02 00:00:00"]} assert expect == result
def test_middleware_next_pressure_given_pressures_returns_first_element(): pressure = 950 store = redux.Store(db.reducer, initial_state={"pressures": [pressure]}, middlewares=[db.next_previous]) action = forest.db.control.next_value("pressure", "pressures") store.dispatch(action) result = store.state expect = {"pressure": pressure, "pressures": [pressure]} assert expect == result
def test_next_given_none_selects_latest_time(self): action = db.next_value("initial_time", "initial_times") state = dict(initial_times=self.initial_times) store = redux.Store(db.reducer, initial_state=state, middlewares=[db.next_previous]) store.dispatch(action) result = store.state expect = dict(initial_time="2019-01-04 00:00:00", initial_times=self.initial_times) self.assertEqual(expect, result)
def test_middleware_converts_next_value_to_set_value(self): log = db.Log() state = {"k": 2, "ks": [1, 2, 3]} store = redux.Store(db.reducer, initial_state=state, middlewares=[db.next_previous, log]) store.dispatch(db.next_value("k", "ks")) result = store.state expect = {"k": 3, "ks": [1, 2, 3]} self.assertEqual(expect, result) self.assertEqual(log.actions, [db.set_value("k", 3)])
def test_render_called_once_with_two_identical_settings(): store = redux.Store(colors.reducer) controls = colors.ColorPalette() controls.render = unittest.mock.Mock() controls.connect(store) for action in [ colors.set_palette_name("Accent"), colors.set_palette_name("Accent"), ]: store.dispatch(action) controls.render.assert_called_once()
def test_render_called_once_with_non_relevant_settings(): """Render should only happen when relevant state changes""" store = redux.Store(redux.combine_reducers(db.reducer, colors.reducer)) controls = colors.ColorPalette() controls.render = unittest.mock.Mock() controls.connect(store) for action in [ colors.set_palette_name("Accent"), db.set_value("variable", "air_temperature") ]: store.dispatch(action) controls.render.assert_called_once()
def test_pressure_middleware_reverses_pressure_coordinate(self): pressures = [1000, 850, 500] state = {"pressure": 850, "pressures": pressures} store = redux.Store( db.reducer, initial_state=state, middlewares=[db.InverseCoordinate("pressure"), db.next_previous]) action = db.next_value("pressure", "pressures") store.dispatch(action) result = store.state expect = {"pressure": 500, "pressures": pressures} self.assertEqual(expect, result)
def test_middleware_next_pressure_given_current_pressure(): pressure = 950 pressures = [1000, 950, 800] store = redux.Store( db.reducer, initial_state={ "pressure": pressure, "pressures": pressures }, middlewares=[db.InverseCoordinate("pressure"), db.next_previous]) action = forest.db.control.next_value("pressure", "pressures") store.dispatch(action) assert store.state["pressure"] == 800
def test_next_pressure_given_pressures_returns_first_element(self): pressure = 950 store = redux.Store( db.reducer, initial_state={"pressures": [pressure]}, middlewares=[db.next_previous, db.Controls(self.database)]) view = db.ControlView() view.subscribe(store.dispatch) view.on_next('pressure', 'pressures')() result = store.state expect = {"pressure": pressure, "pressures": [pressure]} self.assertEqual(expect, result)
def test_previous_given_none_selects_earliest_time(self): action = db.previous_value("initial_time", "initial_times") state = {"initial_times": self.initial_times} store = redux.Store(db.reducer, initial_state=state, middlewares=[db.next_previous]) store.dispatch(action) result = store.state expect = { "initial_time": "2019-01-01 00:00:00", "initial_times": self.initial_times } self.assertEqual(expect, result)
def test_next_pressure_given_pressures_none(self): store = redux.Store(db.reducer, middlewares=[ db.InverseCoordinate("pressure"), db.next_previous, db.Controls(self.database) ]) view = db.ControlView() view.subscribe(store.dispatch) view.on_next('pressure', 'pressures')() result = store.state expect = {} self.assertEqual(expect, result)
def test_middleware_given_inconsistent_number(): log = Log() store = redux.Store(colors.reducer, middlewares=[colors.palettes, log]) actions = [ colors.set_palette_number(1000), colors.set_palette_name("Viridis") ] for action in actions: store.dispatch(action) assert len(log.actions) == 4 assert log.actions == [ colors.set_palette_number(1000), colors.set_palette_numbers(colors.palette_numbers("Viridis")), colors.set_palette_number(256), colors.set_palette_name("Viridis") ]
def test_middleware_on_save_given_edit(): mode = "edit" index = 5 initial_state = { "layers": { "mode": { "state": mode, "index": index }, "index": { 0: {} } } } store = redux.Store(layers.reducer, initial_state=initial_state) action = layers.on_save({}) expect = [layers.save_layer(index, {})] result = list(layers.middleware(store, action)) assert expect == result
def test_set_initial_time(self): initial = dt.datetime(2019, 1, 1) valid = dt.datetime(2019, 1, 1, 3) self.database.insert_file_name("file.nc", initial) self.database.insert_time("file.nc", "variable", valid, 0) action = db.set_value("initial_time", "2019-01-01 00:00:00") initial_state = {"pattern": "file.nc", "variable": "variable"} store = redux.Store(db.reducer, initial_state=initial_state, middlewares=[self.controls]) store.dispatch(action) result = store.state expect = { "pattern": "file.nc", "variable": "variable", "initial_time": str(initial), "valid_times": [str(valid)] } self.assertEqual(expect, result)
def test_reducer_next_given_time_moves_forward_in_time(self): initial_times = [ "2019-01-01 00:00:00", "2019-01-01 02:00:00", "2019-01-01 01:00:00" ] action = db.next_value("initial_time", "initial_times") state = { "initial_time": "2019-01-01 00:00:00", "initial_times": initial_times } store = redux.Store(db.reducer, initial_state=state, middlewares=[db.next_previous]) store.dispatch(action) result = store.state expect = { "initial_time": "2019-01-01 01:00:00", "initial_times": initial_times } self.assertEqual(expect, result)
def test_next_pressure_given_current_pressure(self): pressure = 950 pressures = [1000, 950, 800] store = redux.Store(db.reducer, initial_state={ "pressure": pressure, "pressures": pressures }, middlewares=[ db.InverseCoordinate("pressure"), db.next_previous, db.Controls(self.database) ]) view = db.ControlView() view.subscribe(store.dispatch) view.on_next('pressure', 'pressures')() result = store.state["pressure"] expect = 800 self.assertEqual(expect, result)