diff options
author | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-08-05 17:14:58 +0200 |
---|---|---|
committer | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-08-11 17:00:32 +0200 |
commit | cc84a89c5c4b3d48b0ef8767c40fa34ef542bccf (patch) | |
tree | fb7fe5ef283bb60f1bbe4df4e36a78a87216fc71 /service | |
parent | 3dfec58b96876514a41abf916f280694f30bb8cd (diff) |
implemented add, delete and update of mails in searchable mail store.
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/adapter/mailstore/searchable_mailstore.py | 38 | ||||
-rw-r--r-- | service/test/unit/adapter/mailstore/test_searchable_mailstore.py | 31 |
2 files changed, 65 insertions, 4 deletions
diff --git a/service/pixelated/adapter/mailstore/searchable_mailstore.py b/service/pixelated/adapter/mailstore/searchable_mailstore.py index 1db34fd9..efb611fb 100644 --- a/service/pixelated/adapter/mailstore/searchable_mailstore.py +++ b/service/pixelated/adapter/mailstore/searchable_mailstore.py @@ -14,17 +14,51 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. from twisted.internet import defer -from pixelated.adapter.mailstore.mailstore import MailStore +from types import FunctionType +from pixelated.adapter.mailstore import MailStore -class SearchableMailStore(MailStore): +class SearchableMailStore(object): # implementes MailStore def __init__(self, delegate, search_engine): self._delegate = delegate self._search_engine = search_engine + @classmethod + def _create_delegator(cls, method_name): + def delegator(self, *args, **kw): + return getattr(self._delegate, method_name)(*args, **kw) + + setattr(cls, method_name, delegator) + @defer.inlineCallbacks def add_mail(self, mailbox_name, mail): stored_mail = yield self._delegate.add_mail(mailbox_name, mail) self._search_engine.index_mail(stored_mail) defer.returnValue(stored_mail) + + @defer.inlineCallbacks + def delete_mail(self, mail_id): + yield self._delegate.delete_mail(mail_id) + self._search_engine.remove_from_index(mail_id) + + @defer.inlineCallbacks + def update_mail(self, mail): + yield self._delegate.update_mail(mail) + self._search_engine.index_mail(mail) + + def __getattr__(self, name): + """ + Acts like method missing. If a method of MailStore is not implemented in this class, + a delegate method is created. + + :param name: attribute name + :return: method or attribute + """ + methods = ([key for key, value in MailStore.__dict__.items() if type(value) == FunctionType]) + + if name in methods: + SearchableMailStore._create_delegator(name) + return super(SearchableMailStore, self).__getattribute__(name) + else: + raise NotImplemented('No attribute %s' % name) diff --git a/service/test/unit/adapter/mailstore/test_searchable_mailstore.py b/service/test/unit/adapter/mailstore/test_searchable_mailstore.py index f486a8e2..ef3190e2 100644 --- a/service/test/unit/adapter/mailstore/test_searchable_mailstore.py +++ b/service/test/unit/adapter/mailstore/test_searchable_mailstore.py @@ -19,6 +19,7 @@ from mockito import verify, mock, when import pkg_resources from twisted.internet import defer from twisted.trial.unittest import TestCase +from pixelated.adapter.mailstore import MailStore from pixelated.adapter.mailstore.leap_mailstore import LeapMail from pixelated.adapter.mailstore.searchable_mailstore import SearchableMailStore from pixelated.adapter.search import SearchEngine @@ -32,7 +33,7 @@ class TestLeapMail(TestCase): def setUp(self): super(TestLeapMail, self).setUp() self.search_index = mock(mocked_obj=SearchEngine) - self.delegate_mail_store = mock() + self.delegate_mail_store = mock(mocked_obj=MailStore) self.store = SearchableMailStore(self.delegate_mail_store, self.search_index) @defer.inlineCallbacks @@ -56,10 +57,36 @@ class TestLeapMail(TestCase): self.assertEqual(leap_mail, result) + @defer.inlineCallbacks + def test_delete_mail_delegates_to_mail_store_and_updates_index(self): + when(self.delegate_mail_store).delete_mail('mail id').thenReturn(defer.succeed(None)) + when(self.search_index).remove_from_index('mail id').thenReturn(defer.succeed(None)) + + yield self.store.delete_mail('mail id') + + verify(self.delegate_mail_store).delete_mail('mail id') + verify(self.search_index).remove_from_index('mail id') + + @defer.inlineCallbacks + def test_update_mail_delegates_to_mail_store_and_updates_index(self): + leap_mail = LeapMail('id', ANY_MAILBOX) + + yield self.store.update_mail(leap_mail) + + verify(self.delegate_mail_store).update_mail(leap_mail) + verify(self.search_index).index_mail(leap_mail) + + @defer.inlineCallbacks + def test_other_methods_are_delegated(self): + mail = LeapMail('mail id', ANY_MAILBOX) + when(self.delegate_mail_store).get_mail('mail id').thenReturn(defer.succeed(mail), defer.succeed(mail)) + result = yield self.store.get_mail('mail id') + + self.assertEqual(mail, result) + def _load_mail_from_file(self, mail_file): mailset_dir = pkg_resources.resource_filename('test.unit.fixtures', 'mailset') mail_file = os.path.join(mailset_dir, 'new', mail_file) with open(mail_file) as f: mail = Parser().parse(f) return mail - |