Source

trac.plugins.traccron / tests / test_sleeping_ticket_reminder_task.py

# -*- coding: utf-8 -*-
import logging
from datetime import datetime, timedelta

import pytest
from utils import create_orphaned_ticket, create_ticket, create_tickets
from utils import has_log_message

from trac.util.datefmt import utc


def pytest_funcarg__sleeping_ticket_reminder_task(request, component):
    sleeping_ticket_reminder_task = component['sleeping_ticket_reminder_task']
    return sleeping_ticket_reminder_task


def test_sleeping_ticket_reminder_task_getId(sleeping_ticket_reminder_task):
    assert 'sleeping_ticket' == sleeping_ticket_reminder_task.getId()


def test_sleeping_ticket_reminder_task_wake_up(sleeping_ticket_reminder_task,
                                               caplog):
    env = sleeping_ticket_reminder_task.env
    caplog.setLevel(logging.DEBUG, logger=env.log.name)

    # create tickets
    when = datetime.now(utc) - timedelta(days=5)
    t1 = create_tickets(env, 3, owner='sleeper1', when=when)
    o1 = create_orphaned_ticket(env, when=when)

    sleeping_ticket_reminder_task.wake_up()
    expected_messages = [
        'action controllers for ticket workflow',
        'warning sleeping ticket',
        'warning ticket',
    ]
    assert has_log_message(caplog, expected_messages)


@pytest.mark.parametrize('args', [
    (2,),
    (7, 'test'),
])
def test_sleeping_ticket_reminder_task_wake_up_args(
        sleeping_ticket_reminder_task, caplog, args):
    env = sleeping_ticket_reminder_task.env
    caplog.setLevel(logging.DEBUG, logger=env.log.name)

    # create tickets
    ticket_subtract_days = 5
    when = datetime.now(utc) - timedelta(days=ticket_subtract_days)
    t1 = create_tickets(env, 3, owner='sleeper1', when=when)
    o1 = create_orphaned_ticket(env, when=when)

    sleeping_ticket_reminder_task.wake_up(*args)
    expected_messages = ['action controllers for ticket workflow']
    if args[0] <= ticket_subtract_days:
        expected_messages.extend([
            'warning sleeping ticket',
            'warning ticket',
        ])
    assert has_log_message(caplog, expected_messages)