Commits

Tetsuya Morimoto committed 84a055f

added unit test for TicketRefsPlugin

Comments (0)

Files changed (3)

+# -*- coding: utf-8 -*-
+
+from trac.test import EnvironmentStub
+
+from ticketref.api import TicketRefsPlugin
+from ticketref.web_ui import TicketRefsTemplate
+
+def pytest_addoption(parser):
+    group = parser.getgroup("general")
+    group.addoption('--envscope',
+        action="store", dest="envscope", default="module",
+        type="choice", choices=["module", "function"],
+        help=("set environment scope, default: module."))
+
+def make_trac_environment_with_plugin():
+    env = EnvironmentStub(
+        enable=["ticketref.*", TicketRefsPlugin, TicketRefsTemplate])
+    TicketRefsPlugin(env).upgrade_environment(env.get_db_cnx())
+    tref = TicketRefsPlugin(env)
+    tmpl = TicketRefsTemplate(env)
+    return env, tref, tmpl
+
+def pytest_funcarg__env(request):
+    setup = make_trac_environment_with_plugin
+    scope = request.config.option.envscope
+    env, tref, tmpl = request.cached_setup(setup=setup, scope=scope)
+    return env
+
+def pytest_funcarg__tref(request):
+    setup = make_trac_environment_with_plugin
+    scope = request.config.option.envscope
+    env, tref, tmpl = request.cached_setup(setup=setup, scope=scope)
+    return tref
+
+def pytest_funcarg__tmpl(request):
+    setup = make_trac_environment_with_plugin
+    scope = request.config.option.envscope
+    env, tref, tmpl = request.cached_setup(setup=setup, scope=scope)
+    return tmpl

tests/test_api_ticketref_plugin.py

+# -*- coding: utf-8 -*-
+
+import pytest
+from trac.ticket.model import Ticket
+
+from ticketref.model import TICKETREF
+
+from utils import (create_ticket, create_tickets,
+                   make_simple_ticketref, make_multi_ticketref)
+
+def test_tref_has_ticket_refs(env, tref):
+    ticket = create_ticket(env)
+    assert not tref.has_ticket_refs(ticket)
+    ticket[TICKETREF] = u"1"
+    assert tref.has_ticket_refs(ticket)
+
+@pytest.mark.parametrize(("value", "expected_msg"), [
+    (u"abc", "Input only numbers for ticket ID: abc"),
+])
+def test_tref_validate_ticket(env, tref, value, expected_msg):
+    ticket = create_ticket(env)
+    ticket[TICKETREF] = value
+    expected_field = env.config.get("ticket-custom", "ticketref.label")
+    for field, msg in tref.validate_ticket(None, ticket):
+        assert expected_field == field and expected_msg == msg
+        break
+    else:
+        assert False
+
+def test_tref_validate_ticket_with_own_id(env, tref):
+    ticket = create_ticket(env)
+    ticket[TICKETREF] = u"%s" % ticket.id
+    expected_field = env.config.get("ticket-custom", "ticketref.label")
+    expected_msg = "Ticket %s is this ticket ID, remove it." % ticket.id
+    for field, msg in tref.validate_ticket(None, ticket):
+        assert expected_field == field and expected_msg == msg
+        break
+    else:
+        assert False
+
+def test_tref_ticket_created_with_desc(env):
+    target = create_ticket(env)
+    ticket = Ticket(env)
+    ticket["summary"] = u"reference in description"
+    ticket["description"] = "refs #%s" % target.id
+    ticket.insert()  # called TicketRefsPlugin.ticket_created()
+    assert ticket.exists
+    target = Ticket(env, target.id)
+    assert target[TICKETREF] == u"%s" % ticket.id
+
+def test_tref_ticket_created_with_field(env):
+    target = create_ticket(env)
+    ticket = Ticket(env)
+    ticket["summary"] = u"has %s field" % TICKETREF
+    ticket[TICKETREF] = u"%s" % target.id
+    ticket.insert()  # called TicketRefsPlugin.ticket_created()
+    assert ticket.exists
+    target = Ticket(env, target.id)
+    assert target[TICKETREF] == u"%s" % ticket.id
+
+def test_tref_ticket_created_with_mixed(env):
+    t1, t2 = create_tickets(env, 2)
+    ticket = Ticket(env)
+    ticket["summary"] = u"has desc and %s field" % TICKETREF
+    ticket["description"] = "refs #%s" % t1.id
+    ticket[TICKETREF] = u"%s" % t2.id
+    ticket.insert()  # called TicketRefsPlugin.ticket_created()
+    assert ticket.exists
+    assert ticket[TICKETREF] == u"%s, %s" % (t1.id, t2.id)
+    t1, t2 = Ticket(env, t1.id), Ticket(env, t2.id)
+    assert t1[TICKETREF] == u"%s" % ticket.id
+    assert t2[TICKETREF] == u"%s" % ticket.id
+
+def test_tref_ticket_changed_with_comment(env):
+    t1, t2, t3 = create_tickets(env, 3)
+    # called TicketRefsPlugin.ticket_changed()
+    assert t1.save_changes(author="user1",
+                           comment="refs #%s, #%s" % (t2.id, t3.id))
+    assert t1[TICKETREF] == u"%s, %s" % (t2.id, t3.id)
+    t2, t3 = Ticket(env, t2.id), Ticket(env, t3.id)
+    assert t2[TICKETREF] == u"%s" % t1.id
+    assert t3[TICKETREF] == u"%s" % t1.id
+
+def test_tref_ticket_changed_with_field(env):
+    t1, t2, t3 = create_tickets(env, 3)
+    t1[TICKETREF] = u"%s, %s" % (t2.id, t3.id)
+    assert t1.save_changes()  # called TicketRefsPlugin.ticket_changed()
+    t2, t3 = Ticket(env, t2.id), Ticket(env, t3.id)
+    assert t2[TICKETREF] == u"%s" % t1.id
+    assert t3[TICKETREF] == u"%s" % t1.id
+
+def test_tref_ticket_changed_with_mixed(env):
+    t1, t2, t3 = create_tickets(env, 3)
+    t1[TICKETREF] = u"%s" % t3.id
+    # called TicketRefsPlugin.ticket_changed()
+    assert t1.save_changes(author="user1", comment="refs #%s" % t2.id)
+    assert t1[TICKETREF] == u"%s, %s" % (t2.id, t3.id)
+    t2, t3 = Ticket(env, t2.id), Ticket(env, t3.id)
+    assert t2[TICKETREF] == u"%s" % t1.id
+    assert t3[TICKETREF] == u"%s" % t1.id
+
+def test_tref_ticket_deleted(env):
+    t1, t2 = make_simple_ticketref(env)
+    assert t2[TICKETREF] == u"%s" % t1.id
+    t1.delete()  # called TicketRefsPlugin.ticket_deleted()
+    t2 = Ticket(env, t2.id)
+    assert t2[TICKETREF] is None
+# -*- coding: utf-8 -*-
+
+from trac.ticket.model import Ticket
+
+from ticketref.model import TICKETREF
+
+def create_ticket(env):
+    t = Ticket(env)
+    t["summary"] = "test"
+    t["description"] = "ticket for test"
+    t.insert()
+    assert t.exists
+    return t
+
+def create_tickets(env, num):
+    return [create_ticket(env) for _ in range(num)]
+
+def make_simple_ticketref(env):
+    t1 = create_ticket(env)
+    t2 = create_ticket(env)
+    t1[TICKETREF] = u"%s" % t2.id
+    assert t1.save_changes()
+    t2 = Ticket(env, t2.id)
+    assert t2[TICKETREF] == u"%s" % t1.id
+    return t1, t2
+
+def make_multi_ticketref(env):
+    t1, t2, t3 = create_tickets(env, 3)
+    t1[TICKETREF] = u"%s, %s" % (t2.id, t3.id)
+    assert t1.save_changes()
+    t2, t3 = Ticket(env, t2.id), Ticket(env, t3.id)
+    assert t2[TICKETREF] == u"%s" % t1.id
+    assert t3[TICKETREF] == u"%s" % t1.id
+    return t1, t2, t3