Beispiel #1
0
def test_timer_equals(mocker):
    task = Task(name="foo")
    ts = mocker.MagicMock(spec=datetime)
    timer1 = Timer(task=task, start=ts, stop=None)
    timer2 = Timer(task=task, start=ts, stop=None)

    assert timer1 == timer2
Beispiel #2
0
def test_timer_not_equals(mocker):
    task1 = Task(name="foo")
    task2 = Task(name="bar")

    ts1 = mocker.MagicMock(spec=datetime)
    ts2 = mocker.MagicMock(spec=datetime)
    timer1 = Timer(task=task1, start=ts1, stop=None)
    timer2 = Timer(task=task2, start=ts2, stop=None)

    assert timer1 != timer2
Beispiel #3
0
def test_validate_stop_in_future():

    start = datetime.now(timezone.utc) - timedelta(hours=1)
    stop = start + timedelta(hours=2)

    with pytest.raises(AssertionError):
        tt.timer._validate(Timer(start=start, stop=stop))
Beispiel #4
0
def test_update_timer_invalid_task_raises(session, task):
    session.add(task)

    session.add(Timer(task=task, start=datetime.now(timezone.utc)))

    with pytest.raises(ValidationError):
        tt.timer.update(1, task="invalid_task_name")
Beispiel #5
0
def test_validate(running):

    start = datetime.now(timezone.utc) - timedelta(hours=1)
    stop = start + timedelta(minutes=30)

    t = Timer(start=start, stop=None if running else stop)
    tt.timer._validate(t)
Beispiel #6
0
def test_active_running(session, task):
    session.add(task)

    session.add(Timer(task=task, start=datetime.now(timezone.utc)))

    cur = tt.timer.active()

    assert cur is not None
    assert cur.id == 1
Beispiel #7
0
def test_remove(session, task):
    session.add(task)

    session.add(Timer(task=task, start=datetime.now(timezone.utc)))

    assert session.query(Timer).count() == 1
    tt.timer.remove(1)

    assert session.query(Timer).count() == 0
Beispiel #8
0
def test_active_stopped(session, task):
    session.add(task)

    session.add(
        Timer(
            task=task, start=datetime.now(timezone.utc), stop=datetime.now(timezone.utc)
        )
    )

    assert tt.timer.active() is None
Beispiel #9
0
def test_update_stop_time_invalid_constraint(session, task, offset):

    session.add(task)

    now = datetime.now(timezone.utc)
    an_hour_ago = now - timedelta(hours=1)
    stop_time = now + offset

    session.add(Timer(task=task, start=an_hour_ago, stop=now))

    with pytest.raises(ValidationError):
        tt.timer.update(1, stop=stop_time)
Beispiel #10
0
def test_last(session, task):
    session.add(task)
    timers = [
        Timer(task=task, start=datetime.now(timezone.utc) + timedelta(hours=i))
        for i in range(-5, 0)
    ]
    session.add_all(timers)

    last = tt.timer.last()
    assert last is not None
    assert last["id"] == 5
    assert last["task"] == task.name
Beispiel #11
0
def test_stop_with_active(update, active, mocker, timer_service):

    timer = Timer(id=1,
                  task=Task(name="foo"),
                  start=mocker.MagicMock(spec=datetime))
    active.return_value = timer

    timestamp = mocker.MagicMock(spec=datetime)
    timer_service.stop(timestamp)

    assert active.called
    update.assert_called_once_with(1, stop=timestamp)
Beispiel #12
0
def test_update_timer_task(session):

    old_task = Task(name="old")
    new_task = Task(name="new")

    session.add_all([old_task, new_task])

    session.add(Timer(task=old_task, start=datetime.now(timezone.utc)))

    tt.timer.update(1, task=new_task.name)

    timer = session.query(Timer).get(1)
    assert timer.task.id == new_task.id
Beispiel #13
0
def test_update_invalid_start_raises(session, task, offset):

    session.add(task)

    now = datetime.now(timezone.utc)
    one_hour_ago = now - timedelta(hours=1)
    two_hours_ago = now - timedelta(hours=2)
    start_time = now + offset

    session.add(Timer(task=task, start=two_hours_ago, stop=one_hour_ago))

    with pytest.raises(ValidationError):
        tt.timer.update(1, start=start_time)
Beispiel #14
0
def test_update_time_start(session, task):

    session.add(task)

    now = datetime.now(timezone.utc)
    one_hour_ago = now - timedelta(hours=1)
    two_hours_ago = now - timedelta(hours=2)

    session.add(Timer(task=task, start=one_hour_ago))

    tt.timer.update(1, start=two_hours_ago)

    timer = session.query(Timer).get(1)
    assert timer.start == two_hours_ago
Beispiel #15
0
def test_timers(session, task):
    session.add(task)

    now = datetime.now(timezone.utc)
    duration = timedelta(hours=1)

    for hours in range(100, 0, -1):
        offset = timedelta(hours=hours)
        start = now - offset - duration
        stop = start + duration
        session.add(Timer(task=task, start=start, stop=stop))

    assert session.query(Timer).count() == 100

    timers_ = tt.timer.timers()
    assert len(list(timers_)) == 100
Beispiel #16
0
def test_remove_task_with_timers_raises(session):

    session.add(Task(name="foo"))
    session.flush()

    task = session.query(Task).get(1)

    session.add(Timer(task=task, start=datetime.now(timezone.utc)))
    session.flush()

    timer = session.query(Timer).get(1)

    # Ensure that there is a valid 2-way relationship
    assert timer in task.timers
    assert timer.task == task

    with pytest.raises(ValidationError):
        remove("foo")
Beispiel #17
0
def test_slice(session, task):

    session.add(task)

    now = datetime.now(timezone.utc)
    duration = timedelta(hours=1)

    for hours in range(100, 0, -1):
        offset = timedelta(hours=hours)
        start = now - offset - duration
        stop = start + duration
        session.add(Timer(task=task, start=start, stop=stop))

    start = now - timedelta(hours=24)

    slice_ = list(tt.timer.slice(start=start, end=now))
    assert len(slice_) == 23

    for s in slice_:
        assert s["start"] >= start
        assert s["start"] < now
Beispiel #18
0
def create(task, start):
    """Create a new timer for the given task.

    :param str: task: The name of an existing task.
    :param datetime.datetime start: The UTC starting time.
    :raises: ValidationError If the timer fails to validate.
    """

    with transaction() as session:
        try:
            task = session.query(Task).filter(Task.name == task).one()
        except NoResultFound:
            raise ValidationError("Invalid task %s" % task)

        for active in session.query(Timer).filter(Timer.stop.is_(None)).all():
            active.stop = start

        try:
            timer = Timer(task=task, start=start)
            _validate(timer)
            session.add(timer)
        except AssertionError as err:
            raise ValidationError(err)
Beispiel #19
0
def test_equals_different_types(mocker):
    task = Task(name="foo")
    ts = mocker.MagicMock(spec=datetime)
    timer = Timer(task=task, start=ts, stop=None)

    assert task != timer