def test_59_event_cancellation(tmpdir):
    """
    VEN, EiEvent Service, oadrDistributeEvent Payload
    If the VTN sends an oadrEvent with the eventStatus set to cancelled and
    has an eventID that the VEN is aware of then the VEN should cancel the
    existing event and delete it from its list of known events.
    """
    test_event = AdrEvent(
        id="FooEvent",
        start=datetime.utcnow() + timedelta(seconds=60),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=1.0)],
        status=AdrEventStatus.PENDING, mod_number=1
    )

    event_handler = event.EventHandler("VEN_ID", db_path=TEST_DB_ADDR % tmpdir)
    event_controller = controller.EventController(event_handler)

    event_handler.handle_payload(generate_payload([test_event]))

    active_event = event_handler.get_active_events()[0]
    assert active_event == test_event.to_obj()

    with freeze_time():
        test_event.status = AdrEventStatus.CANCELLED
        test_event.mod_number += 1
        test_event.end = datetime.utcnow()

        event_handler.handle_payload(generate_payload([test_event]))

        active_event = event_handler.get_active_events()[0]
        assert active_event == test_event.to_obj()

    signal_level, evt_id, remove_events = event_controller._calculate_current_event_status([test_event.to_obj()])
    assert (signal_level, evt_id, remove_events) == (0, None, ["FooEvent"])
def test_57_modified_event(tmpdir):
    """
    VEN/VTN, EiEvent Service, oadrDistributeEvent Payload
    If the VTN sends an oadrEvent with an eventID that the VEN is already
    aware of, but with a higher modification number then the VEN should
    replace the previous event with the new one In its list of known events.
    """
    test_event = AdrEvent(
        id="FooEvent",
        start=datetime.utcnow() + timedelta(seconds=60),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=1.0)],
        status=AdrEventStatus.PENDING
    )
    event_handler = event.EventHandler("VEN_ID", db_path=TEST_DB_ADDR % tmpdir)

    event_handler.handle_payload(generate_payload([test_event]))

    active_event = event_handler.get_active_events()[0]
    expected_event = test_event.to_obj()
    assert active_event == expected_event

    test_event.mod_number = 1
    test_event.status = AdrEventStatus.ACTIVE

    event_handler.handle_payload(generate_payload([test_event]))

    active_event = event_handler.get_active_events()[0]
    expected_event = test_event.to_obj()
    assert active_event == expected_event
Exemplo n.º 3
0
def test_implied_cancellation(tmpdir):
    event1 = AdrEvent(
        id="FooEvent1",
        start=datetime.utcnow()-timedelta(seconds=60),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=1.0)],
        status=AdrEventStatus.ACTIVE,
    )
    event2 = AdrEvent(
        id="FooEvent2",
        start=datetime.utcnow()-timedelta(seconds=50),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=2.0)],
        status=AdrEventStatus.ACTIVE,
    )

    event_handler = event.EventHandler("VEN_ID", db_path=TEST_DB_ADDR % tmpdir)

    event_handler.handle_payload(generate_payload([event1]))

    active_events = event_handler.get_active_events()

    assert [event1.to_obj()] == active_events

    with freeze_time():
        event_handler.handle_payload(generate_payload([event2]))
        active_events = event_handler.get_active_events()

        cancelled_evt = event1.to_obj()
        cancelled_evt.cancel()
        assert [cancelled_evt, event2.to_obj()] == active_events
def test_58_modified_event_error(tmpdir):
    """
    VEN, EiEvent Service, oadrDistributeEvent Payload
    If the VTN sends an oadrEvent with an eventID that the VEN is already
    aware of, but which has a lower modification number than one in which the
    VEN is already aware then this is an ERROR and the VEN should respond
    with the appropriate error code. Note that this is true regardless of the
    event state including cancelled.
    """
    test_event1 = AdrEvent(
        id="FooEvent",
        start=datetime.utcnow() + timedelta(seconds=60),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=1.0)],
        status=AdrEventStatus.PENDING, mod_number=5
    )
    test_event2 = AdrEvent(
        id="FooEvent",
        start=datetime.utcnow() + timedelta(seconds=60),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=1.0)],
        status=AdrEventStatus.PENDING, mod_number=3
    )

    event_handler = event.EventHandler("VEN_ID", db_path=TEST_DB_ADDR % tmpdir)

    event_handler.handle_payload(generate_payload([test_event1]))

    active_event = event_handler.get_active_events()[0]
    expected_event = test_event1.to_obj()
    assert active_event == expected_event

    event_handler.handle_payload(generate_payload([test_event2]))

    active_event = event_handler.get_active_events()[0]
    assert active_event == expected_event
def test_56_new_event(tmpdir):
    """
    VEN, EiEvent Service, oadrDistributeEvent Payload
    If the VTN sends an oadrEvent with an eventID that the VEN is not aware
    then it should process the event and add it to its list of known events
    """
    test_event = AdrEvent(
        id="FooEvent",
        start=datetime.utcnow()+timedelta(seconds=60),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=1.0)],
        status=AdrEventStatus.PENDING
    )
    event_handler = event.EventHandler("VEN_ID", db_path=TEST_DB_ADDR % tmpdir)
    event_controller = controller.EventController(event_handler)

    event_handler.handle_payload(generate_payload([test_event]))

    signal_level, evt_id, remove_events = event_controller._calculate_current_event_status([test_event.to_obj()])
    assert (signal_level, evt_id, remove_events) == (0, None, [])

    active_event = event_handler.get_active_events()[0]
    expected_event = test_event.to_obj()
    assert active_event == expected_event

    with freeze_time(datetime.utcnow()+timedelta(seconds=70)):
        signal_level, evt_id, remove_events = event_controller._calculate_current_event_status([test_event.to_obj()])
        assert (signal_level, evt_id, remove_events) == (1.0, "FooEvent", [])
Exemplo n.º 6
0
def test_explicite_cancellation(tmpdir):
    test_event = AdrEvent(
        id="FooEvent",
        start=datetime.utcnow()-timedelta(seconds=60),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=2.0)],
        status=AdrEventStatus.ACTIVE,
    )

    event_handler = event.EventHandler("VEN_ID", db_path=TEST_DB_ADDR % tmpdir)

    event_handler.handle_payload(generate_payload([test_event]))

    active_events = event_handler.get_active_events()

    assert [test_event.to_obj()] == active_events

    with freeze_time():
        test_event.mod_number += 1
        test_event.status = AdrEventStatus.CANCELLED
        test_event.end = datetime.utcnow()

        event_handler.handle_payload(generate_payload([test_event]))
        active_events = event_handler.get_active_events()

        assert [test_event.to_obj()] == active_events

        # test second cancellation
        event_handler.handle_payload(generate_payload([test_event]))
        active_events = event_handler.get_active_events()

        assert [test_event.to_obj()] == active_events

    # test subsequent cancellation
    event_handler.handle_payload(generate_payload([test_event]))
    active_events = event_handler.get_active_events()

    assert [test_event.to_obj()] == active_events
def test_30_start_time_randomization(tmpdir):
    """
    VEN, EiEvent Service, oadrDistributeEvent Payload
    The VEN must randomize the dtstart time of the event if a value is present
    in the startafter element. Event completion times are determined by adding
    the event duration to the randomized dtstart time. Modifications to an event
    should maintain the same random offset, unless the startafter element itself
    is modified.
    """
    test_event = AdrEvent(
        id="FooEvent",
        start=datetime.utcnow() + timedelta(minutes=10),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=1.0)],
        status=AdrEventStatus.PENDING, start_after=timedelta(minutes=2)
    )
    event_handler = event.EventHandler("VEN_ID", db_path=TEST_DB_ADDR % tmpdir)

    event_handler.handle_payload(generate_payload([test_event]))

    active_event = event_handler.get_active_events()[0]
    expected_event = test_event.to_obj()
    assert active_event.start != expected_event.start
    assert (active_event.start - expected_event.start) < timedelta(minutes=2)
def test_6_test_event(tmpdir):
    """
    VEN, EiEvent Service, oadrDistributeEvent Payload
    The presence of any string except “false” in the oadrDisributeEvent
    testEvent element is treated as a trigger for a test event.
    """
    test_event = AdrEvent(
        id="FooEvent",
        start=datetime.utcnow()-timedelta(seconds=60),
        signals=[dict(index=0, duration=timedelta(minutes=10), level=1.0)],
        status=AdrEventStatus.ACTIVE, test_event=True
    )
    event_handler = event.EventHandler("VEN_ID", db_path=TEST_DB_ADDR % tmpdir)
    event_controller = controller.EventController(event_handler)

    event_handler.handle_payload(generate_payload([test_event]))

    signal_level, evt_id, remove_events = event_controller._calculate_current_event_status([test_event.to_obj()])
    assert (signal_level, evt_id, remove_events) == (0, None, [])

    active_event = event_handler.get_active_events()[0]
    expected_event = test_event.to_obj()
    assert active_event == expected_event