def test_save_state_without_path_doesnt_throw(): mock_print = Mock() with patch("builtins.print", mock_print): state = load_state(None) save_state(state) mock_print.assert_called_once_with("No filepath to save state to.")
def test_state_invalid_key_raises_exception(): state = load_state(None) with pytest.raises(UnsupportedState) as ex: state["invalid_set"] = "fail" assert "Invalid key: invalid_set." == str(ex) state["invalid_get"] assert "Invalid key: invalid_get." == str(ex)
def test_load_state_returns_default_state_on_nonexisting_path(): path = "foobar" mock_file = Mock(side_effect=FileNotFoundError) with patch("builtins.open", mock_file): state = load_state(path) mock_file.assert_called_once() assert mock_file.call_args_list == call((path, "r")) assert state == DEFAULT_STATE
def test_load_state_returns_saved_state(): path = ".\save\state.json" json_state = '{"boil": 25, "pumpOn": true}' mock_file = mock_open(read_data=json_state) with patch("builtins.open", mock_file): state = load_state(path) mock_file.assert_called_once() assert mock_file.call_args_list == call((path, "r")) assert state == {"boil": 25, "pumpOn": True}
def __init__( self, state_path, boiler_pin, steam_pin, heater_pin, pump_pin, update_interval=1, ): self.state = load_state(state_path) self.pump = Pump(pump_pin) self.heater = Heater(boiler_pin, steam_pin, heater_pin) self.heater.tune(self.state) self.update_interval = update_interval
def test_load_state_returns_default_state(): assert load_state(None) == DEFAULT_STATE