def test_update_state(app, fake_states): latest = State.latest() State.update_state("set_point_enabled", not latest.set_point_enabled) new_state = State.latest() assert new_state.set_point_enabled != latest.set_point_enabled assert latest assert latest.id != new_state.id
def test_set_pt_up(client, app, fake_states): with app.app_context(): before = State.latest() response = client.post("/setpt-up") after = State.latest() assert response.status_code == 200 assert response.get_json() is not None assert after.set_point == before.set_point + 0.5 assert response.get_json()["set_point"] == before.set_point + 0.5
def fake_states(app): with app.app_context(): states = [ State(time=start_time, set_point=72, set_point_enabled=True), State(time=start_time + timedelta(minutes=1), set_point=72, set_point_enabled=True), ] for state in states: db.session.add(state) db.session.commit() yield states
def test_resample_not_enough_data(app, fake_samples, fake_states): samples_ts = Sample.timeseries(fake_samples[-10:]) states_ts = State.timeseries(fake_states[-2:]) res_samples, res_states = jointerpolate([samples_ts, states_ts], max_points=10) assert 8 < len(samples_ts) < 12 # There is not enough data to make a reasonable interpolation for states, so we mostly care that this # didn't crash and returned something assert states_ts is not None
def fake_states(app): """590 minute time range, with states every 10min with +/-4min jitter""" states_coarse = [ State(set_point=random.randrange(40, 80), time=START_TIME + timedelta(hours=i)) for i in range(10) ] states_fine = list( itertools.chain.from_iterable([[ State( set_point=samp.set_point + random.random(), time=samp.time + timedelta(minutes=10 * i + random.randrange(-4, 4)), ) for i in range(0, 6) ] for samp in states_coarse])) with app.app_context(): for samp in states_fine: db.session.add(samp) db.session.commit() yield states_fine
def test_set_point(mock_mpl115, mock_relay, app, fake_states): State.update_state("set_point", 72) State.update_state("set_point_enabled", True) State.update_state("heat_on", False) mock_mpl115.read.return_value = (60, 10) cli._poll_once() mock_mpl115.read.assert_called_once() mock_relay.on.assert_called_once() after = State.latest() assert after.heat_on
def test_resample_samples_states(app, fake_samples, fake_states): samples_ts = Sample.dataframe(fake_samples) states_ts = State.dataframe(fake_states) res_samples, res_states = jointerpolate([samples_ts, states_ts], max_points=50) assert 40 < len(res_samples) < 55 assert 40 < len(res_states) < 55 # assert interpolate_multiple yields same results as interpolate_samples_states resampled_samples, resampled_states = jointerpolate( [samples_ts, states_ts], max_points=50) assert 40 < len(resampled_samples) < 55 assert 40 < len(resampled_states) < 55
def test_adjust_temp(mock_buttons, app, fake_states): cli._register_buttons() mock_buttons.register_on_off.assert_called_once() mock_buttons.register_temp_up.assert_called_once() mock_buttons.register_temp_down.assert_called_once() # Call the up_callback and assert that set point is enabled, and raised by cli.TEMP_INCREMENT from previous up_callback, _ = mock_buttons.register_temp_up.call_args up_callback = up_callback[0] State.update_state("set_point_enabled", False) State.update_state("set_point", 72) before = State.latest() up_callback() cli.loop.run_until_complete(asyncio.sleep(0.1)) after = State.latest() assert after.set_point == before.set_point + cli.TEMP_INCREMENT assert after.set_point_enabled
def test_thread_safe(mock_buttons, app, fake_states): """Test thread safety. The callbacks in the GPIO run in their own thread, so they will not have app context. We can't use the GPIO library for off-device tests, so to simulate its behavior, we create a fake thread outside of app context to call the button callbacks. Assert that the callback provided by cli can safely be called from a thread outside of app context.""" cli._register_buttons() up_callback, _ = mock_buttons.register_temp_up.call_args up_callback = up_callback[0] button_thread = threading.Thread(target=up_callback) State.update_state("set_point_enabled", False) State.update_state("set_point", 72) before = State.latest() button_thread.start() cli.loop.run_until_complete(asyncio.sleep(0.1)) button_thread.join() after = State.latest() assert after.set_point == before.set_point + cli.TEMP_INCREMENT assert after.set_point_enabled
def test_get_states(app, fake_states): latest = State.latest() assert latest assert latest.id == fake_states[-1].id
def test_plot_nonempty(client, fake_samples, fake_states): params = _plot_temps_states(Sample.dataframe(fake_samples), State.dataframe(fake_states)) assert len(params["temp_values"]) > 0 assert len(params["set_points_heaton"]) + len( params["set_points_heatoff"]) > 0