Add simple test for trac backend.
authorJelmer Vernooij <jelmer@samba.org>
Fri, 20 Jun 2008 14:57:01 +0000 (16:57 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 20 Jun 2008 14:57:01 +0000 (16:57 +0200)
remote/tests/test_trac.py [new file with mode: 0644]
remote/trac.py

diff --git a/remote/tests/test_trac.py b/remote/tests/test_trac.py
new file mode 100644 (file)
index 0000000..57391d1
--- /dev/null
@@ -0,0 +1,53 @@
+# vim:set encoding=utf-8:
+###############################################################################
+# Copyright:
+#   © 2008 Jelmer Vernooij <jelmer@samba.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+# 3. The names of its contributors may not be used to endorse or promote
+#    products derived from this software without specific prior written
+#    permission.
+# 
+# THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
+# EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+###############################################################################
+
+from cStringIO import StringIO
+from remote.trac import TracTicket
+from remote import *
+from unittest import TestCase
+
+
+class TestTracTicket(TestCase):
+    def parse(self, text):
+        return TracTicket(text)
+
+    def test_tabformat(self):
+        ticket = self.parse("""id\tsummary\treporter\towner\tdescription\ttype\tstatus\tpriority\tmilestone\tcomponent\tversion\tresolution\tkeywords\tcc
+42\tWe should not foo\tanonymous\tsomebody\tThis is a long description\\r\\nAnd it includes whitespace stuff.\tdefect\tclosed\tnormal\t3.0.2\tcore\t3.0\tfixed\t\t""")
+        self.assertEquals(ticket.id, 42)
+        self.assertEquals(ticket.summary, "We should not foo")
+        self.assertEquals(ticket.milestone, "3.0.2")
+        self.assertEquals(ticket.version, "3.0")
+        self.assertEquals(ticket.reporter, "anonymous")
+        self.assertEquals(ticket.owner, "somebody")
+        self.assertEquals(ticket.description, "This is a long description\r\n" + 
+                                              "And it includes whitespace stuff.")
+        self.assertEquals(ticket.status, "closed")
+        self.assertEquals(ticket.priority, "normal")
+
index 21b74bb993502f2f462da57256ad260a894a1710..f8afe6d2a0c0c30954b6686fa8a05aaf72a59065 100644 (file)
@@ -37,20 +37,26 @@ from __init__ import *
 
 any = re.compile(r'.*')
 
-class TracData:
-    def __init__(self, uri, id):
-        self.id = id or failwith(uri, "Trac: no id")
 
-        page = wget(uri + '?format=tab')
+class TracTicket:
+    def __init__(self, page):
         lines = page.split('\n');
 
-        if len(lines) is 2:
+        if len(lines) == 2:
             keys = lines[0].split('\t')
             vals = lines[1].split('\t')
             dct  = dict((keys[i], vals[i]) for i in range(0, len(keys)))
 
             self.status     = dct['status']
             self.resolution = dct['resolution']
+            self.id         = int(dct['id'])
+            self.reporter   = dct['reporter']
+            self.owner      = dct['owner']
+            self.summary    = dct['summary']
+            self.milestone  = dct['milestone']
+            self.version    = dct['version']
+            self.priority   = dct['priority']
+            self.description = dct['description'].replace("\\r\\n", "\r\n")
         else:
             soup = BeautifulSoup(page)
 
@@ -66,12 +72,6 @@ class TracData:
                 except:
                     failwith(uri, "Probably error page")
 
-        if not self.status:
-            failwith(uri, "Trac: no status")
-
-        if self.resolution == "duplicate":
-            raise DupeExn(uri)
-
     def _process_status(self, text):
         words = text.split()
         assert len(words) in (1, 2)
@@ -80,6 +80,25 @@ class TracData:
         else:
             return [x.strip('()') for x in words]
 
+
+class TracData:
+    def __init__(self, uri, id):
+        self.id = id or failwith(uri, "Trac: no id")
+
+        page = wget(uri + '?format=tab')
+
+        ticket = TracTicket(page)
+
+        self.status = ticket.status
+        self.resolution = ticket.resolution
+
+        if not ticket.status:
+            failwith(uri, "Trac: no status")
+
+        if ticket.resolution == "duplicate":
+            raise DupeExn(uri)
+
+
 class RemoteTrac(RemoteBts):
     def __init__(self, cnf):
         bugre  =  r"^%(uri)s/ticket/([0-9]+)$"