From 3c79a54ab332e15f31a4a57a4a9baabf4b62e26a Mon Sep 17 00:00:00 2001 From: Patrick Maia and Victor Shyba Date: Fri, 5 Sep 2014 22:45:55 +0000 Subject: #51 - persists new tags globally (in a local file) and shows on tag list --- service/test/adapter/pixelated_mail_test.py | 8 +-- service/test/adapter/pixelated_mailbox_test.py | 42 +++++++++----- service/test/adapter/test_helper.py | 6 +- service/test/adapter/test_tag.py | 76 ++++++++++++++++---------- 4 files changed, 80 insertions(+), 52 deletions(-) (limited to 'service/test') diff --git a/service/test/adapter/pixelated_mail_test.py b/service/test/adapter/pixelated_mail_test.py index 7de7c96a..557c3f6a 100644 --- a/service/test/adapter/pixelated_mail_test.py +++ b/service/test/adapter/pixelated_mail_test.py @@ -16,7 +16,6 @@ import unittest from pixelated.adapter.pixelated_mail import PixelatedMail -from pixelated.adapter.tag import Tag import test_helper @@ -65,10 +64,11 @@ class TestPixelatedMail(unittest.TestCase): self.assertEqual(mail.tags, ['sent']) self.assertEqual(mail.body, 'Este \xe9 o corpo') - def test_update_tags_return_a_set_for_current_tags(self): + def test_update_tags_return_a_set_for_added_tags_and_a_set_for_removed_ones(self): pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(extra_headers={'X-tags': ['custom_1', 'custom_2']})) - current_tags = pixelated_mail.update_tags(set([Tag('custom_1'), Tag('custom_3')])) - self.assertEquals(set([Tag('custom_3'), Tag('custom_1')]), current_tags) + added, removed = pixelated_mail.update_tags(set(['custom_1', 'custom_3'])) + self.assertEquals(set(['custom_3']), added) + self.assertEquals(set(['custom_2']), removed) def test_to_mime_multipart(self): mail = PixelatedMail.from_dict(self.mail_dict) diff --git a/service/test/adapter/pixelated_mailbox_test.py b/service/test/adapter/pixelated_mailbox_test.py index 1047eed4..2944275f 100644 --- a/service/test/adapter/pixelated_mailbox_test.py +++ b/service/test/adapter/pixelated_mailbox_test.py @@ -14,28 +14,42 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . import unittest +import os import test_helper from pixelated.adapter.tag import Tag +from pixelated.adapter.tag_index import TagIndex from pixelated.adapter.pixelated_mailbox import PixelatedMailbox class TestPixelatedMailbox(unittest.TestCase): - def test_retrieve_all_tags_from_mailbox(self): - leap_flags = ['\\Deleted', '\\Draft', '\\Recent', 'tag_custom', 'should_ignore_all_from_here', 'List'] - mailbox = PixelatedMailbox(test_helper.leap_mailbox(leap_flags=leap_flags)) - - self.assertEquals(set([Tag('trash'), Tag('inbox'), Tag('drafts'), Tag('custom')]), mailbox.all_tags()) + def setUp(self): + self.db_file_path = '/tmp/test_tags' - def test_new_tags_are_added_to_mailbox(self): - leap_flags = ['\\Deleted', 'tag_custom_one', 'tag_custom_two'] - leap_mailbox_mock = test_helper.leap_mailbox(leap_flags=leap_flags) - mailbox = PixelatedMailbox(leap_mailbox_mock) - tags = [Tag('custom_one'), Tag('custom_three')] - mailbox.update_tags(tags) + def tearDown(self): + os.remove(self.db_file_path + '.db') - expected = set(('\\Deleted', 'tag_custom_one', 'tag_custom_two', 'tag_custom_three')) - actual_args = set(leap_mailbox_mock.setFlags.call_args[0][0]) + def test_special_tags_always_exists(self): + mailbox = PixelatedMailbox(test_helper.leap_mailbox(), self.db_file_path) + self.assertEquals(mailbox.SPECIAL_TAGS, mailbox.all_tags()) - self.assertEquals(expected, actual_args) + def test_retrieve_all_tags_from_mailbox(self): + tag_index = TagIndex(self.db_file_path) + tag_index.set(Tag('one_tag')) + tag_index.set(Tag('two_tag')) + tag_index.close_db() + mailbox = PixelatedMailbox(test_helper.leap_mailbox(), self.db_file_path) + expected_tags = set([Tag('one_tag'), Tag('two_tag')]).union(mailbox.SPECIAL_TAGS) + self.assertEquals(expected_tags, mailbox.all_tags()) + + def test_notify_tags_updated_method_properly_changes_tags_state(self): + tag_index = TagIndex(self.db_file_path) + tag = Tag('one_tag') + tag.increment(12) + tag_index.set(tag) + tag_index.close_db() + mailbox = PixelatedMailbox(test_helper.leap_mailbox(), self.db_file_path) + self.assertEquals(0, mailbox.tag_index.get('inbox').total) + mailbox.notify_tags_updated(set(['inbox']), set(['one_tag']), 12) + self.assertEquals(1, mailbox.tag_index.get('inbox').total) diff --git a/service/test/adapter/test_helper.py b/service/test/adapter/test_helper.py index 756013ab..70673481 100644 --- a/service/test/adapter/test_helper.py +++ b/service/test/adapter/test_helper.py @@ -49,8 +49,6 @@ def leap_mail(uid=0, flags=LEAP_FLAGS, headers=DEFAULT_HEADERS, extra_headers={} hdoc=Mock(content={'headers': headers})) -def leap_mailbox(leap_flags=LEAP_FLAGS, extra_flags=[]): - flags = leap_flags + extra_flags - return Mock(getFlags=Mock(return_value=flags), - _get_mbox_doc=Mock(return_value=None), +def leap_mailbox(): + return Mock(_get_mbox_doc=Mock(return_value=None), messages=[leap_mail(uid=6)]) diff --git a/service/test/adapter/test_tag.py b/service/test/adapter/test_tag.py index b9b502d4..1f0478d3 100644 --- a/service/test/adapter/test_tag.py +++ b/service/test/adapter/test_tag.py @@ -20,42 +20,58 @@ from pixelated.adapter.tag import Tag class TestTag(unittest.TestCase): - def test_leap_recent_flag_is_translated_to_inbox_tag(self): - tag = Tag.from_flag('\\Recent') - self.assertEquals(Tag('inbox'), tag) + def test_from_dict_sets_all_tag_attributes(self): + tag_dict = {'name': 'a_tag', + 'default': False, + 'counts': {'total': 3, + 'read': 1, + 'starred': 1, + 'replied': 1}, + 'mails': set([1, 2, 3])} - def test_leap_deleted_flag_is_translated_to_trash_tag(self): - tag = Tag.from_flag('\\Deleted') - self.assertEquals(Tag('trash'), tag) + tag = Tag.from_dict(tag_dict) - def test_leap_draft_flag_is_translated_to_draft_tag(self): - tag = Tag.from_flag('\\Draft') - self.assertEquals(Tag('drafts'), tag) + self.assertEquals(tag_dict['name'], tag.name) + self.assertEquals(tag_dict['default'], tag.default) + self.assertEquals(tag_dict['counts']['total'], tag.total) + self.assertEquals(tag_dict['mails'], tag.mails) - def test_leap_flags_that_are_custom_tags_are_handled(self): - tag = Tag.from_flag('tag_work') - self.assertEquals(Tag('work'), tag) + def test_as_dict_puts_all_tag_attributes_in_the_returning_dict(self): + tag = Tag('some_tag', default=True) + tag.counts = {'total': 0, 'read': 0, 'starred': 0, 'replied': 0} + tag.mails = set([1, 2, 3]) - def test_custom_tags_containing_our_prefix_are_handled(self): - tag = Tag.from_flag('tag_tag_work_tag_') - self.assertEquals(Tag('tag_work_tag_'), tag) + tag_dict = tag.as_dict() - def test_bulk_conversion(self): - tags = Tag.from_flags(['\\Answered', '\\Seen', '\\Recent', 'tag_a_custom', 'List']) - self.assertEquals(set([Tag('inbox'), Tag('a_custom')]), tags) + self.assertEquals(tag.name, tag_dict['name']) + self.assertEquals(tag.default, tag_dict['default']) + self.assertEquals(tag.total, tag_dict['counts']['total']) + self.assertEquals(tag.mails, tag_dict['mails']) - def test_inbox_tag_is_translated_to_leap_recent_flag(self): - flag = Tag('inbox').to_flag() - self.assertEquals('\\Recent', flag) + def test_increments_total_count_and_adds_mails_id_to_mails(self): + tag = Tag('another') + tag.increment(12) - def test_trash_tag_is_translated_to_leap_deleted_flag(self): - flag = Tag('trash').to_flag() - self.assertEquals('\\Deleted', flag) + self.assertIn(12, tag.mails) + self.assertEquals(1, tag.total) - def test_drafts_tag_is_translated_to_leap_draft_flag(self): - flag = Tag('drafts').to_flag() - self.assertEquals('\\Draft', flag) + def test_decrement_does_nothing_if_mail_has_not_the_tag(self): + tag = Tag('tag') + tag.decrement(2000) - def test_custom_tag_has_prefix_when_translated_to_flag(self): - flag = Tag('work').to_flag() - self.assertEquals('tag_work', flag) + self.assertEquals(0, tag.total) + + def test_increment_does_nothing_if_mail_already_has_the_tag(self): + tag = Tag('tag') + tag.mails = set([12]) + tag.increment(12) + + self.assertEquals(1, tag.total) + + def test_decrements_total_count_and_removes_mails_id_from_mails(self): + tag = Tag('one_more') + tag.mails = set([12]) + tag.decrement(12) + + self.assertNotIn(12, tag.mails) + self.assertEquals(0, tag.total) -- cgit v1.2.3