async def test_history_newer_unlimited_past(freezer, mock_persist_driver, dummy_utc_datetime, dummy_timestamp, num_mock_port1): freezer.move_to(dummy_utc_datetime) mock_persist_driver.enable_history_support() port_expr = MockPortRef(num_mock_port1) ts_expr = MockExpression(dummy_timestamp - 7200) diff_expr = MockExpression(0) expr = various.HistoryFunction([port_expr, ts_expr, diff_expr]) num_mock_port1.set_last_read_value(-8) await history.save_sample(num_mock_port1, (dummy_timestamp - 8000) * 1000) num_mock_port1.set_last_read_value(0.01) assert await expr.eval() == 0.01 num_mock_port1.set_last_read_value(-4) await history.save_sample(num_mock_port1, (dummy_timestamp - 4000) * 1000) assert await expr.eval() == -4 num_mock_port1.set_last_read_value(-6) await history.save_sample(num_mock_port1, (dummy_timestamp - 6000) * 1000) diff_expr.set_value(3601) # Invalidates history expression internal cache assert await expr.eval() == -6
async def test_sample(freezer, dummy_local_datetime, literal_one_thousand): freezer.move_to(dummy_local_datetime) value_expr = MockExpression(3) expr = timeprocessing.SampleFunction([value_expr, literal_one_thousand]) assert await expr.eval() == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=100)) value_expr.set_value(16) assert await expr.eval() == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=500)) assert await expr.eval() == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=999)) assert await expr.eval() == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=1001)) assert await expr.eval() == 16 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=10000)) assert await expr.eval() == 16
async def test_history_older_past(freezer, mock_persist_driver, dummy_utc_datetime, dummy_timestamp, num_mock_port1): freezer.move_to(dummy_utc_datetime) mock_persist_driver.enable_history_support() port_expr = MockPortRef(num_mock_port1) ts_expr = MockExpression(dummy_timestamp - 3600) diff_expr = MockExpression(-3600) expr = various.HistoryFunction([port_expr, ts_expr, diff_expr]) num_mock_port1.set_last_read_value(-8) await history.save_sample(num_mock_port1, (dummy_timestamp - 8000) * 1000) num_mock_port1.set_last_read_value(-2) await history.save_sample(num_mock_port1, (dummy_timestamp - 2000) * 1000) num_mock_port1.set_last_read_value(0.01) with pytest.raises(PortValueUnavailable): await expr.eval() num_mock_port1.set_last_read_value(-6) await history.save_sample(num_mock_port1, (dummy_timestamp - 6000) * 1000) assert await expr.eval() == -6 num_mock_port1.set_last_read_value(-4) await history.save_sample(num_mock_port1, (dummy_timestamp - 4000) * 1000) diff_expr.set_value(-3601) # Invalidates history expression internal cache assert await expr.eval() == -4
async def test_delay(freezer, dummy_local_datetime, literal_one_thousand): freezer.move_to(dummy_local_datetime) value_expr = MockExpression(3) expr = timeprocessing.DelayFunction([value_expr, literal_one_thousand]) assert await expr.eval(context={}) == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=100)) assert await expr.eval(context={}) == 3 value_expr.set_value(16) freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=500)) assert await expr.eval(context={}) == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=1499)) assert await expr.eval(context={}) == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=1501)) assert await expr.eval(context={}) == 16 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=10000)) assert await expr.eval(context={}) == 16
async def test_held_different_value(freezer, dummy_local_datetime, literal_sixteen): freezer.move_to(dummy_local_datetime) value_expr = MockExpression(15) time_expr = MockExpression(200) expr = timeprocessing.HeldFunction( [value_expr, literal_sixteen, time_expr]) assert await expr.eval() == 0 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=501)) assert await expr.eval() == 0
async def test_held_fulfilled(freezer, dummy_local_datetime, literal_sixteen): freezer.move_to(dummy_local_datetime) value_expr = MockExpression(16) time_expr = MockExpression(200) expr = timeprocessing.HeldFunction( [value_expr, literal_sixteen, time_expr]) assert await expr.eval(context={}) == 0 time_expr.set_value(500) freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=499)) assert await expr.eval(context={}) == 0 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=501)) assert await expr.eval(context={}) == 1
async def test_available_func(num_mock_port1): port_expr = MockPortValue(num_mock_port1) acc_expr = MockExpression(13) func_expr = various.AccFunction([port_expr, acc_expr]) expr = various.AvailableFunction([func_expr]) assert await expr.eval(context={ 'port_values': { 'nid1': num_mock_port1.get_last_read_value() } }) == 0 num_mock_port1.set_last_read_value(16) assert await expr.eval(context={ 'port_values': { 'nid1': num_mock_port1.get_last_read_value() } }) == 1 port_expr = MockPortValue(None, port_id='some-id') func_expr = various.AccFunction([port_expr, acc_expr]) expr = various.AvailableFunction([func_expr]) assert await expr.eval(context={ 'port_values': { 'nid1': num_mock_port1.get_last_read_value() } }) == 0
async def test_history_older_future(freezer, mock_persist_driver, dummy_utc_datetime, dummy_timestamp, num_mock_port1): freezer.move_to(dummy_utc_datetime) mock_persist_driver.enable_history_support() port_expr = MockPortRef(num_mock_port1) ts_expr = MockExpression(dummy_timestamp + 7200) diff_expr = MockExpression(-3600) expr = various.HistoryFunction([port_expr, ts_expr, diff_expr]) num_mock_port1.set_last_read_value(-8) await history.save_sample(num_mock_port1, (dummy_timestamp - 8000) * 1000) num_mock_port1.set_last_read_value(0.01) assert await expr.eval() == 0.01
async def test_default(num_mock_port1): port_expr = MockPortValue(num_mock_port1) def_expr = MockExpression(13) expr = various.DefaultFunction([port_expr, def_expr]) assert await expr.eval() == 13 num_mock_port1.set_last_read_value(16) assert await expr.eval() == 16
async def test_history_newer_unlimited_future(freezer, mock_persist_driver, dummy_utc_datetime, dummy_timestamp, num_mock_port1): freezer.move_to(dummy_utc_datetime) mock_persist_driver.enable_history_support() port_expr = MockPortRef(num_mock_port1) ts_expr = MockExpression(dummy_timestamp + 7200) diff_expr = MockExpression(0) expr = various.HistoryFunction([port_expr, ts_expr, diff_expr]) num_mock_port1.set_last_read_value(-8) await history.save_sample(num_mock_port1, (dummy_timestamp - 8000) * 1000) num_mock_port1.set_last_read_value(0.01) with pytest.raises(PortValueUnavailable): await expr.eval()
async def test_history_newer_current(freezer, mock_persist_driver, dummy_utc_datetime, dummy_timestamp, num_mock_port1): freezer.move_to(dummy_utc_datetime) mock_persist_driver.enable_history_support() port_expr = MockPortRef(num_mock_port1) ts_expr = MockExpression(dummy_timestamp - 1800) diff_expr = MockExpression(3600) expr = various.HistoryFunction([port_expr, ts_expr, diff_expr]) num_mock_port1.set_last_read_value(-2) await history.save_sample(num_mock_port1, (dummy_timestamp - 2000) * 1000) num_mock_port1.set_last_read_value(-1) await history.save_sample(num_mock_port1, (dummy_timestamp - 1000) * 1000) num_mock_port1.set_last_read_value(0.01) assert await expr.eval(context={}) == -1
async def test_acc(): value_expr = MockExpression(16) acc_expr = MockExpression(13) expr = various.AccFunction([value_expr, acc_expr]) assert await expr.eval() == 13 acc_expr.set_value(-5) assert await expr.eval() == -5 value_expr.set_value(26) assert await expr.eval() == 5 value_expr.set_value(20) acc_expr.set_value(5) assert await expr.eval() == -1
async def test_integ(freezer, dummy_local_datetime): freezer.move_to(dummy_local_datetime) acc_expr = MockExpression(0) value_expr = MockExpression(0) time_expr = MockExpression(100) expr = timeprocessing.IntegFunction([value_expr, acc_expr, time_expr]) assert round(await expr.eval(), 1) == 0 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=200)) value_expr.set_value(10) assert round(await expr.eval(), 1) == 1 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=300)) with pytest.raises(EvalSkipped): await expr.eval() freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=400)) acc_expr.set_value(1) assert round(await expr.eval(), 1) == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=800)) value_expr.set_value(15) acc_expr.set_value(3) time_expr.set_value(200) assert round(await expr.eval(), 1) == 8 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=1000)) value_expr.set_value(-20) acc_expr.set_value(8) time_expr.set_value(100) assert round(await expr.eval(), 1) == 7.5 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=1200)) value_expr.set_value(0) acc_expr.set_value(7.5) assert round(await expr.eval(), 1) == 5.5
async def test_deriv(freezer, dummy_local_datetime): freezer.move_to(dummy_local_datetime) value_expr = MockExpression(0) time_expr = MockExpression(100) expr = timeprocessing.DerivFunction([value_expr, time_expr]) assert round(await expr.eval(), 1) == 0 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=200)) value_expr.set_value(1) assert round(await expr.eval(), 1) == 5 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=300)) with pytest.raises(EvalSkipped): await expr.eval() freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=400)) value_expr.set_value(2) assert round(await expr.eval(), 1) == 5 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=800)) value_expr.set_value(5) time_expr.set_value(200) assert round(await expr.eval(), 1) == 7.5 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=1000)) value_expr.set_value(1) time_expr.set_value(100) assert round(await expr.eval(), 1) == -20 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=1200)) assert round(await expr.eval(), 1) == 0
async def test_fmedian(freezer, dummy_local_datetime): freezer.move_to(dummy_local_datetime) value_expr = MockExpression(0) width_expr = MockExpression(4) time_expr = MockExpression(100) expr = timeprocessing.FMedianFunction([value_expr, width_expr, time_expr]) assert await expr.eval() == 0 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=101)) value_expr.set_value(8) assert await expr.eval() == 8 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=200)) value_expr.set_value(4) with pytest.raises(EvalSkipped): await expr.eval() freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=202)) assert await expr.eval() == 4 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=301)) value_expr.set_value(-2) with pytest.raises(EvalSkipped): await expr.eval() freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=303)) assert await expr.eval() == 4 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=402)) value_expr.set_value(6) with pytest.raises(EvalSkipped): await expr.eval() freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=504)) assert await expr.eval() == 6 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=603)) value_expr.set_value(11) width_expr.set_value(3) with pytest.raises(EvalSkipped): await expr.eval() freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=605)) assert await expr.eval() == 6 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=804)) value_expr.set_value(-8) time_expr.set_value(200) with pytest.raises(EvalSkipped): await expr.eval() freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=806)) assert await expr.eval() == 6
async def test_freeze(freezer, dummy_local_datetime): freezer.move_to(dummy_local_datetime) value_expr = MockExpression(1) time_expr = MockExpression(200) expr = timeprocessing.FreezeFunction([value_expr, time_expr]) assert await expr.eval() == 1 time_expr.set_value(50) freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=100)) value_expr.set_value(2) assert await expr.eval() == 1 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=199)) assert await expr.eval() == 1 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=201)) time_expr.set_value(200) assert await expr.eval() == 2 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=500)) value_expr.set_value(3) assert await expr.eval() == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=600)) value_expr.set_value(4) assert await expr.eval() == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=699)) assert await expr.eval() == 3 freezer.move_to(dummy_local_datetime + datetime.timedelta(milliseconds=701)) assert await expr.eval() == 4
async def test_hyst_fall(literal_three, literal_sixteen): value_expr = MockExpression(20) expr = various.HystFunction([value_expr, literal_three, literal_sixteen]) assert await expr.eval() == 1 value_expr.set_value(17) assert await expr.eval() == 1 value_expr.set_value(16) assert await expr.eval() == 1 value_expr.set_value(10) assert await expr.eval() == 1 value_expr.set_value(4) assert await expr.eval() == 1 value_expr.set_value(20) assert await expr.eval() == 1 value_expr.set_value(3) assert await expr.eval() == 1 value_expr.set_value(2) assert await expr.eval() == 0 value_expr.set_value(0) assert await expr.eval() == 0
async def test_lutli(freezer, literal_two, literal_three, literal_sixteen, literal_one_hundred, literal_two_hundreds, literal_one_thousand): value_expr = MockExpression(0) expr = various.LUTLIFunction([ value_expr, literal_three, literal_two_hundreds, literal_sixteen, literal_one_thousand, literal_two, literal_one_hundred ]) assert await expr.eval() == 100 value_expr.set_value(2) assert await expr.eval() == 100 value_expr.set_value(2.4) assert await expr.eval() == 140 value_expr.set_value(2.6) assert await expr.eval() == 160 value_expr.set_value(3) assert await expr.eval() == 200 value_expr.set_value(5) assert round(await expr.eval(), 2) == 323.08 value_expr.set_value(9.4) assert round(await expr.eval(), 2) == 593.85 value_expr.set_value(9.6) assert round(await expr.eval(), 2) == 606.15 value_expr.set_value(16) assert round(await expr.eval(), 2) == 1000 value_expr.set_value(100) assert round(await expr.eval(), 2) == 1000
async def test_lut(freezer, literal_two, literal_three, literal_sixteen, literal_one_hundred, literal_two_hundreds, literal_one_thousand): value_expr = MockExpression(0) expr = various.LUTFunction([ value_expr, literal_three, literal_two_hundreds, literal_sixteen, literal_one_thousand, literal_two, literal_one_hundred ]) assert await expr.eval() == 100 value_expr.set_value(2) assert await expr.eval() == 100 value_expr.set_value(2.4) assert await expr.eval() == 100 value_expr.set_value(2.6) assert await expr.eval() == 200 value_expr.set_value(3) assert await expr.eval() == 200 value_expr.set_value(5) assert await expr.eval() == 200 value_expr.set_value(9.4) assert await expr.eval() == 200 value_expr.set_value(9.6) assert await expr.eval() == 1000 value_expr.set_value(100) assert await expr.eval() == 1000