From 3d3606a0b37952d4a7e70b508c9fcfed3593f466 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Nov 20 2016 17:34:39 +0000
Subject: [PATCH 1/4] Sorting import error for unit test


Signed-off-by: Clement Verna <cverna@tutanota.com>

---

diff --git a/pagure_importer/__init__.py b/pagure_importer/__init__.py
index 8b13789..f43bc2d 100644
--- a/pagure_importer/__init__.py
+++ b/pagure_importer/__init__.py
@@ -1 +1,2 @@
-
+__all__ = ['app']
+from . import app
diff --git a/pagure_importer/app.py b/pagure_importer/app.py
index 054c422..1c2b507 100644
--- a/pagure_importer/app.py
+++ b/pagure_importer/app.py
@@ -13,10 +13,10 @@ __all__ = [
     'app',
 ]
 
-import pagure_importer.commands.fedorahosted
-import pagure_importer.commands.github
-import pagure_importer.commands.clone
-import pagure_importer.commands.push
+from .commands import fedorahosted
+from .commands import github
+from .commands import clone
+from .commands import push
 
 if __name__ == '__main__':
     app()

From 109131ff4cc8fc980784624eed823d936f6fb410 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Nov 20 2016 17:34:39 +0000
Subject: [PATCH 2/4] First let's test how we import trac comments


Signed-off-by: Clement Verna <cverna@tutanota.com>

---

diff --git a/tests/test_pgimport_importer_trac.py b/tests/test_pgimport_importer_trac.py
new file mode 100644
index 0000000..a3184f0
--- /dev/null
+++ b/tests/test_pgimport_importer_trac.py
@@ -0,0 +1,131 @@
+import unittest
+from pagure_importer.utils.importer_trac import TracImporter
+
+
+class PgimportImporterTrac (unittest.TestCase):
+
+    def __init__(self, method_name='runTest'):
+        unittest.TestCase.__init__(self, method_name)
+
+    def setUp(self):
+        self.trac = TracImporter(self, "https://foobar.bar", "foo", "bar")
+        self.somebody = {'name': 'somebody', 'fullname': 'somebody',
+                         'emails': ['some@body.com']}
+
+    def test_create_comments(self):
+
+        # Case 1 - Import 2 comments
+        trac_comments = [
+            [{u'__jsonclass__': [u'datetime', u'2016-11-08T20:30:13']}, u'foobar',
+             u'comment', u'1', u'Yeah, let\'s have some test.', 1],
+            [{u'__jsonclass__': [u'datetime', u'2016-11-08T20:32:20']}, u'foobar',
+             u'comment', u'2', u'Well this will improve the code base :).', 1]]
+
+        results = TracImporter.create_comments(self.trac, trac_comments)
+
+        ts_1 = TracImporter.to_timestamp(self.trac,
+                                         trac_comments[0][0]['__jsonclass__'][1])
+
+        self.assertEqual("Yeah, let\'s have some test.",
+                         results[ts_1].comment)
+        self.assertEqual(ts_1, results[ts_1].date_created)
+        self.assertEqual([], results[ts_1].attachment)
+        self.assertEqual(self.somebody, results[ts_1].user)
+
+        ts_2 = TracImporter.to_timestamp(self.trac,
+                                         trac_comments[1][0]['__jsonclass__'][1])
+
+        self.assertEqual("Well this will improve the code base :).",
+                         results[ts_2].comment)
+        self.assertEqual(ts_2, results[ts_2].date_created)
+        self.assertEqual([], results[ts_2].attachment)
+        self.assertEqual(self.somebody, results[ts_2].user)
+
+        # Case 2 - Import an empty comment, returns and empty dic.
+        trac_comments = [
+            [{u'__jsonclass__': [u'datetime', u'2016-11-08T20:30:13']}, u'foobar',
+             u'comment', u'1', u'', 1]]
+
+        results = TracImporter.create_comments(self.trac, trac_comments)
+        self.assertEqual({}, results)
+
+        # Case 3 - Import something which is not a comment, returns an empty dic.
+        trac_comments = [
+            [{u'__jsonclass__': [u'datetime', u'2014-04-09T16:02:20']}, u'foobar',
+             u'resolution', u'', u'wontfix', 1]]
+
+        results = TracImporter.create_comments(self.trac, trac_comments)
+        self.assertEqual({}, results)
+
+        # Case 4 - Import an attachment
+        trac_comments = [
+            [{u'__jsonclass__': [u'datetime', u'2014-10-31T12:32:18']}, u'foobar',
+             u'attachment', u'', u'mytest.png', 0]]
+
+        results = TracImporter.create_comments(self.trac, trac_comments)
+        ts_1 = TracImporter.to_timestamp(self.trac,
+                                         trac_comments[0][0]['__jsonclass__'][1])
+
+        self.assertEqual("attachment",
+                         results[ts_1].comment)
+        self.assertEqual(ts_1, results[ts_1].date_created)
+        self.assertEqual(['mytest.png'], results[ts_1].attachment)
+        self.assertEqual(self.somebody, results[ts_1].user)
+
+        # Case 5 - Import an Attachment then a Comment (same timestamp)
+        trac_comments = [
+            [{u'__jsonclass__': [u'datetime', u'2014-10-31T12:32:18']}, u'foobar',
+             u'attachment', u'', u'mytest.png', 0],
+            [{u'__jsonclass__': [u'datetime', u'2014-10-31T12:32:18']}, u'foobar',
+             u'comment', u'', u'test attachment', 0]]
+
+        results = TracImporter.create_comments(self.trac, trac_comments)
+        ts_1 = TracImporter.to_timestamp(self.trac,
+                                         trac_comments[0][0]['__jsonclass__'][1])
+
+        self.assertEqual("test attachment",
+                         results[ts_1].comment)
+        self.assertEqual(ts_1, results[ts_1].date_created)
+        self.assertEqual(['mytest.png'], results[ts_1].attachment)
+        self.assertEqual(self.somebody, results[ts_1].user)
+
+        # Case 6 - Import a Comment then an Attachment (same timestamp)
+        trac_comments = [
+            [{u'__jsonclass__': [u'datetime', u'2014-10-31T12:32:18']}, u'foobar',
+             u'comment', u'', u'test attachment', 0],
+            [{u'__jsonclass__': [u'datetime', u'2014-10-31T12:32:18']}, u'foobar',
+             u'attachment', u'', u'mytest.png', 0]]
+
+        results = TracImporter.create_comments(self.trac, trac_comments)
+        ts_1 = TracImporter.to_timestamp(self.trac,
+                                         trac_comments[0][0]['__jsonclass__'][1])
+
+        self.assertEqual("test attachment",
+                         results[ts_1].comment)
+        self.assertEqual(ts_1, results[ts_1].date_created)
+        self.assertEqual(['mytest.png'], results[ts_1].attachment)
+        self.assertEqual(self.somebody, results[ts_1].user)
+
+        # Case 7 - Import 2 Attachments then a Comment (same timestamp)
+        trac_comments = [
+            [{u'__jsonclass__': [u'datetime', u'2014-10-31T12:32:18']}, u'foobar',
+             u'attachment', u'', u'mytest.png', 0],
+            [{u'__jsonclass__': [u'datetime', u'2014-10-31T12:32:18']}, u'foobar',
+             u'attachment', u'', u'mysecondtest.png', 0],
+            [{u'__jsonclass__': [u'datetime', u'2014-10-31T12:32:18']}, u'foobar',
+             u'comment', u'', u'test 2 attachments', 0]]
+
+        results = TracImporter.create_comments(self.trac, trac_comments)
+        ts_1 = TracImporter.to_timestamp(self.trac,
+                                         trac_comments[0][0]['__jsonclass__'][1])
+
+        self.assertEqual("test 2 attachments",
+                         results[ts_1].comment)
+        self.assertEqual(ts_1, results[ts_1].date_created)
+        self.assertEqual(['mytest.png', 'mysecondtest.png'], results[ts_1].attachment)
+        self.assertEqual(self.somebody, results[ts_1].user)
+
+
+if __name__ == '__main__':
+    SUITE = unittest.TestLoader().loadTestsFromTestCase(PgimportImporterTrac)
+    unittest.TextTestRunner(verbosity=2).run(SUITE)

From d58aca76786c980d87354f46de111dbdf7eab863 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Nov 20 2016 17:34:39 +0000
Subject: [PATCH 3/4] Test and fix ticket status method


Signed-off-by: Clement Verna <cverna@tutanota.com>

---

diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index 2e69720..efa55d8 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -216,8 +216,10 @@ class TracImporter():
         if trac_ticket['status'] != 'closed':
             return ('Open', '')
         elif trac_ticket['resolution'] in ['invalid', 'wontfix',
-                                           'worksforme', 'duplicate']:
+                                           'worksforme']:
             return ('Closed', 'Invalid')
+        elif trac_ticket['resolution'] == 'duplicate':
+            return ('Closed', 'Duplicate')
         elif trac_ticket['resolution'] == 'insufficient_info':
             return ('Closed', 'Insufficient data')
         else:
diff --git a/tests/test_pgimport_importer_trac.py b/tests/test_pgimport_importer_trac.py
index a3184f0..91b495e 100644
--- a/tests/test_pgimport_importer_trac.py
+++ b/tests/test_pgimport_importer_trac.py
@@ -125,6 +125,45 @@ class PgimportImporterTrac (unittest.TestCase):
         self.assertEqual(['mytest.png', 'mysecondtest.png'], results[ts_1].attachment)
         self.assertEqual(self.somebody, results[ts_1].user)
 
+    def test_get_ticket_status(self):
+
+        trac_ticket = {}
+        trac_ticket['status'] = 'open'
+
+        results = TracImporter.get_ticket_status(self.trac, trac_ticket)
+        self.assertEqual(('Open', ''), results)
+
+        trac_ticket['status'] = 'closed'
+        trac_ticket['resolution'] = 'invalid'
+
+        results = TracImporter.get_ticket_status(self.trac, trac_ticket)
+        self.assertEqual(('Closed', 'Invalid'), results)
+
+        trac_ticket['resolution'] = 'wontfix'
+
+        results = TracImporter.get_ticket_status(self.trac, trac_ticket)
+        self.assertEqual(('Closed', 'Invalid'), results)
+
+        trac_ticket['resolution'] = 'worksforme'
+
+        results = TracImporter.get_ticket_status(self.trac, trac_ticket)
+        self.assertEqual(('Closed', 'Invalid'), results)
+
+        trac_ticket['resolution'] = 'duplicate'
+
+        results = TracImporter.get_ticket_status(self.trac, trac_ticket)
+        self.assertEqual(('Closed', 'Duplicate'), results)
+
+        trac_ticket['resolution'] = 'insufficient_info'
+
+        results = TracImporter.get_ticket_status(self.trac, trac_ticket)
+        self.assertEqual(('Closed', 'Insufficient data'), results)
+
+        trac_ticket['resolution'] = 'fixed'
+
+        results = TracImporter.get_ticket_status(self.trac, trac_ticket)
+        self.assertEqual(('Closed', 'Fixed'), results)
+
 
 if __name__ == '__main__':
     SUITE = unittest.TestLoader().loadTestsFromTestCase(PgimportImporterTrac)

From 646e4f5b8f653c1e5b2b6d9bc6b810870e098874 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Nov 20 2016 17:44:42 +0000
Subject: [PATCH 4/4] Work in Progress testing importer trac


Signed-off-by: Clement Verna <cverna@tutanota.com>

---

diff --git a/pagure_importer/app.py b/pagure_importer/app.py
index 1c2b507..054c422 100644
--- a/pagure_importer/app.py
+++ b/pagure_importer/app.py
@@ -13,10 +13,10 @@ __all__ = [
     'app',
 ]
 
-from .commands import fedorahosted
-from .commands import github
-from .commands import clone
-from .commands import push
+import pagure_importer.commands.fedorahosted
+import pagure_importer.commands.github
+import pagure_importer.commands.clone
+import pagure_importer.commands.push
 
 if __name__ == '__main__':
     app()
diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index efa55d8..f493596 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -28,7 +28,7 @@ class TracImporter():
         self.somebody = User(name='somebody', fullname='somebody',
                              emails=['some@body.com'])
         self.reqid = 0
-        self.custom_fields = self.get_custom_fields()
+        self.custom_fields = []
 
     def request(self, method, *args):
         ''' Common method for querying trac '''
@@ -78,7 +78,7 @@ class TracImporter():
     def import_issues(self, repo_name, repo_folder,
                       trac_query='max=0&order=id'):
         ''' Import issues from trac instance using jsonrpc API '''
-
+        self.custom_fields = self.get_custom_fields()
         newpath, new_repo = clone_repo(repo_name, repo_folder)
         tickets_id = self.request('ticket.query', trac_query)
 

