]> khamidou — GitHub mirrors - kite.git/commitdiff
better unit tests
authorkarim hamidou <khamidou@gmail.com>
Sun, 27 Oct 2013 18:01:43 +0000 (19:01 +0100)
committerkarim hamidou <khamidou@gmail.com>
Sun, 27 Oct 2013 18:01:43 +0000 (19:01 +0100)
src/back/tests/__init__.py [new file with mode: 0644]
src/back/tests/test_maildir.py

diff --git a/src/back/tests/__init__.py b/src/back/tests/__init__.py
new file mode 100644 (file)
index 0000000..acd1d09
--- /dev/null
@@ -0,0 +1 @@
+# used to debug interactively unit tests
index f9003841b221f6ceef88561210c75966d55a1061..1a549b43c7b83e64ed80a2b140a842681f17e318 100644 (file)
@@ -1,6 +1,38 @@
 import kite.maildir
 import unittest
+import types
 
-class TestMaildirFunction(unittest.TestCase):
-    def test_choice(self):
-        self.assertTrue(True)
+mockdata = {"from": "gerard", "subject": "test", "contents": "empty", "to": "gerard"}
+
+class EmptyObject(object):
+    """Empty class used by mocks. Required because python doesn't let us add attributes
+    to an empty object()"""
+    pass
+
+class MockMailItem(object):
+    def __init__(self):
+        self.fp = EmptyObject()
+        self.fp.read = types.MethodType(lambda self: mockdata["contents"], self.fp)
+
+    def getheaders(self, header):
+        hd = header.lower()
+        if hd in mockdata:
+            return (mockdata[hd], "Nulltuple")
+    
+class MockMailDir(object):
+    def iteritems(self):
+        mi = MockMailItem()
+        return [("randomId", mi)]
+
+
+class TestMaildir(unittest.TestCase):
+    def test_get_mails(self):
+        md = MockMailDir()
+        parsedMaildir = kite.maildir.get_mails(md)
+        self.assertEqual(len(parsedMaildir), 1)
+
+        firstEmail = parsedMaildir[0]
+        self.assertEqual(firstEmail["id"], 0)
+
+        del firstEmail["id"] 
+        self.assertEqual(firstEmail, mockdata)