'Test saving and restoring an event.'

def test_saving_event(self):

    event_type = 'EVENT_TEST'
    event_data = {'test_attr': 5, 'test_attr_10': 'nice'}
    events = []
    @callback
    def event_listener(event):
        'Record    events    from    eventbus.'
        if (event.event_type == event_type):
            events.append(event)
    self.hass.bus.listen(MATCH_ALL, event_listener)
    self.hass.bus.fire(event_type, event_data)
    self.hass.block_till_done()
    assert (len(events) == 1)
    event = events[0]
    self.hass.data[DATA_INSTANCE].block_till_done()
    with session_scope(hass=self.hass) as session:
        db_events = list(session.query(Events).filter_by(event_type=event_type))
        assert (len(db_events) == 1)
        db_event = db_events[0].to_native()
    assert (event.event_type == db_event.event_type)
    assert (event.data == db_event.data)
    assert (event.origin == db_event.origin)
    assert (event.time_fired.replace(microsecond=0) == db_event.time_fired.replace(microsecond=0))
