Пример #1
0
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.")
Пример #2
0
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)
Пример #3
0
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
Пример #4
0
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}
Пример #5
0
 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
Пример #6
0
def test_load_state_returns_default_state():
    assert load_state(None) == DEFAULT_STATE