Source

trac.plugins.traccron / tests / utils.py

Full commit
# -*- coding: utf-8 -*-
import sys
import time

from trac.ticket.model import Ticket


def has_log_message(caplog, expected_messages, strict=True):
    log_status = dict((msg, False) for msg in expected_messages)
    for log in caplog.records():
        for msg in expected_messages:
            if log.message.startswith(msg):
                log_status[msg] = True
                break
        else:
            assert False, 'unknown message: "%s"' % log.message

    if strict:
        for msg, is_logged in log_status.items():
            assert is_logged, 'unlogged message: "%s"' % msg
    return True


def compat_attrgetter(*args):
    from operator import attrgetter
    if sys.version_info[:2] == (2, 4):
        return lambda obj: tuple(attrgetter(attr)(obj) for attr in args)
    else:  # version >= 2.5
        return attrgetter(*args)


def create_ticket(env, owner='user1', when=None):
    t = Ticket(env)
    t['summary'] = 'test'
    t['description'] = 'ticket for test'
    t['owner'] = owner
    t.insert(when=when)
    assert t.exists
    t['status'] = 'new'
    assert t.save_changes(when=when)
    return t

def create_tickets(env, num, owner='user1', when=None):
    return [create_ticket(env, owner, when) for _ in range(num)]

def create_orphaned_ticket(env, reporter='user1', when=None):
    t = Ticket(env)
    t['summary'] = 'orphaned test'
    t['description'] = 'orphaned ticket for test'
    t['reporter'] = reporter
    t.insert(when=when)
    assert t.exists
    db = env.get_db_cnx()
    cursor = db.cursor()
    cursor.execute("update ticket set status='new' where id=%s", (t.id,))
    return t