예제 #1
0
async def test_available_port_ref(num_mock_port1):
    port_expr = MockPortRef(num_mock_port1)
    expr = various.AvailableFunction([port_expr])
    assert await expr.eval() == 1

    num_mock_port1.set_last_read_value(16)
    assert await expr.eval() == 1

    port_expr = MockPortRef(None, port_id='some-id')
    expr = various.AvailableFunction([port_expr])
    assert await expr.eval() == 0
예제 #2
0
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
예제 #3
0
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
예제 #4
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
예제 #5
0
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()
예제 #6
0
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