changeset 10:8a523e580854

Added a test for the core weighmail function.
author Brian Neal <bgneal@gmail.com>
date Sun, 20 May 2012 13:05:12 -0500
parents a3ef032b6878
children 330b4c283a09
files weighmail/core.py weighmail/tests/test_core.py
diffstat 2 files changed, 117 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/weighmail/core.py	Sun May 20 12:13:33 2012 -0500
+++ b/weighmail/core.py	Sun May 20 13:05:12 2012 -0500
@@ -39,10 +39,11 @@
 
     msgs = imap.search(criteria)
 
-    if observer is not None:
-        observer.labeling(label.name, len(msgs))
+    if len(msgs) > 0:
+        if observer is not None:
+            observer.labeling(label.name, len(msgs))
 
-    imap.add_gmail_labels(msgs, [label.name])
+        imap.add_gmail_labels(msgs, [label.name])
 
-    if observer is not None:
-        observer.done_labeling(label.name, len(msgs))
+        if observer is not None:
+            observer.done_labeling(label.name, len(msgs))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/weighmail/tests/test_core.py	Sun May 20 13:05:12 2012 -0500
@@ -0,0 +1,111 @@
+import unittest
+import collections
+
+from weighmail.core import weighmail
+from weighmail.utils import Label
+from weighmail.observers import BaseObserver
+
+
+class FakeClient(object):
+    folder = None
+    logout_called = False
+    search_cnt = 0
+    add_label_cnt = 0
+
+    def __init__(self, labels, results, test_client):
+        self.labels = labels
+        self.results = results
+        self.test_client = test_client
+
+        self.searches = {}
+        for label in labels:
+            c = ''
+            if label.min is not None:
+                c = 'LARGER %d' % label.min
+            if label.max is not None:
+                if c:
+                    c += ' '
+                c += 'SMALLER %d' % label.max
+
+            self.searches[c] = label
+
+    def select_folder(self, folder):
+        self.folder = folder
+
+    def search(self, criteria):
+        self.search_cnt += 1
+        self.test_client.assertTrue(criteria in self.searches)
+        return self.results[self.searches[criteria].name]
+
+    def add_gmail_labels(self, msgs, labels):
+        self.add_label_cnt += 1
+        self.test_client.assertTrue(len(msgs) > 0)
+        self.test_client.assertTrue(len(labels) == 1)
+        self.test_client.assertTrue(labels[0] in self.results)
+        self.test_client.assertTrue(msgs == self.results[labels[0]])
+
+    def logout(self):
+        self.logout_called = True
+
+
+class TestObserver(BaseObserver):
+    searches = collections.Counter()
+    labels = []
+    done_labels = []
+    done = False
+
+    def searching(self, label):
+        self.searches[label] += 1
+
+    def labeling(self, label, count):
+        self.labels.append((label, count))
+
+    def done_labeling(self, label, count):
+        self.done_labels.append((label, count))
+
+    def done(self):
+        self.done = True
+
+
+MB = 1024 * 1024
+LABELS = [
+    Label(name='big', min=2 * MB, max=5 * MB),
+    Label(name='bigger', min=5 * MB, max=10 * MB),
+    Label(name='huge', min=10 * MB, max=None)
+]
+
+RESULTS = {
+    'big': [42, 1893, 2004],
+    'bigger': [],
+    'huge': [100, 200, 300, 400]
+}
+
+
+class WeighmailTestCase(unittest.TestCase):
+
+    def test_simple(self):
+        folder = '[Gmail]/All Mail'
+        imap = FakeClient(LABELS, RESULTS, self)
+        observer = TestObserver()
+
+        weighmail(imap, folder, LABELS, observer)
+
+        self.assertEqual(folder, imap.folder)
+        self.assertEqual(3, imap.search_cnt)
+        self.assertEqual(2, imap.add_label_cnt)
+        self.assertFalse(imap.logout_called)
+        
+        self.assertEqual(observer.searches['big'], 1)
+        self.assertEqual(observer.searches['bigger'], 1)
+        self.assertEqual(observer.searches['huge'], 1)
+        self.assertEqual(len(observer.searches), 3)
+
+        self.assertEqual(len(observer.labels), 2)
+        self.assertTrue(('big', 3) in observer.labels)
+        self.assertTrue(('huge', 4) in observer.labels)
+
+        self.assertEqual(len(observer.done_labels), 2)
+        self.assertTrue(('big', 3) in observer.done_labels)
+        self.assertTrue(('huge', 4) in observer.done_labels)
+
+        self.assertTrue(observer.done)