def test_reindex_missing_idents(self):
        search_engine = mock()
        when(search_engine).search('tag:inbox', all_mails=True).thenReturn(['ident1', 'ident2'])

        listener = MailboxIndexerListener('INBOX', self.mail_store, search_engine)
        when(self.mail_store).get_mailbox_mail_ids('INBOX').thenReturn({'ident1', 'ident2', 'missing_ident'})
        self.mail_store.used_arguments = []
        self.mail_store.get_mails = lambda x: self.mail_store.used_arguments.append(x)
        listener.newMessages(10, 5)

        verify(self.mail_store, times=1).get_mails('INBOX')
        self.assertIn({'missing_ident'}, self.mail_store.used_arguments)
    def test_reindex_missing_idents(self):
        mail = mock()
        search_engine = mock()
        when(search_engine).search('tag:inbox', all_mails=True).thenReturn(['ident1', 'ident2'])

        listener = MailboxIndexerListener('INBOX', self.mail_store, search_engine)
        when(self.mail_store).get_mailbox_mail_ids('INBOX').thenReturn({'ident1', 'ident2', 'missing_ident'})
        when(self.mail_store).get_mails({'missing_ident'}, include_body=True).thenReturn([mail])
        listener.newMessages(10, 5)

        verify(self.mail_store, times=1).get_mails({'missing_ident'}, include_body=True)
        verify(search_engine).index_mails([mail])
    def test_reindex_missing_idents(self):
        search_engine = mock()
        when(search_engine).search('tag:inbox', all_mails=True).thenReturn(['ident1', 'ident2'])

        MailboxIndexerListener.SEARCH_ENGINE = search_engine

        listener = MailboxIndexerListener('INBOX', self.querier)
        when(self.querier).idents_by_mailbox('INBOX').thenReturn({'ident1', 'ident2', 'missing_ident'})
        self.querier.used_arguments = []
        self.querier.mails = lambda x: self.querier.used_arguments.append(x)
        listener.newMessages(10, 5)

        verify(self.querier, times=1).idents_by_mailbox('INBOX')
        self.assertIn({'missing_ident'}, self.querier.used_arguments)
    def test_reindex_missing_idents(self):
        search_engine = mock()
        when(search_engine).search('tag:inbox', all_mails=True).thenReturn(['ident1', 'ident2'])

        MailboxIndexerListener.SEARCH_ENGINE = search_engine

        listener = MailboxIndexerListener('INBOX', self.querier)
        when(self.querier).idents_by_mailbox('INBOX').thenReturn({'ident1', 'ident2', 'missing_ident'})
        self.querier.used_arguments = []
        self.querier.mails = lambda x: self.querier.used_arguments.append(x)
        listener.newMessages(10, 5)

        verify(self.querier, times=1).idents_by_mailbox('INBOX')
        self.assertIn({'missing_ident'}, self.querier.used_arguments)
    def test_catches_exceptions_to_not_break_other_listeners(self):
        when(logger).error(ANY()).thenReturn(None)
        listener = MailboxIndexerListener('INBOX', self.mail_store)

        yield listener.newMessages(1, 1)

        verify(logger).error(ANY())
    def test_reindex_missing_idents(self):
        mail = mock()
        search_engine = mock()
        when(search_engine).search('tag:inbox', all_mails=True).thenReturn(
            ['ident1', 'ident2'])

        listener = MailboxIndexerListener('INBOX', self.mail_store,
                                          search_engine)
        when(self.mail_store).get_mailbox_mail_ids('INBOX').thenReturn(
            {'ident1', 'ident2', 'missing_ident'})
        when(self.mail_store).get_mails({'missing_ident'},
                                        include_body=True).thenReturn([mail])
        listener.newMessages(10, 5)

        verify(self.mail_store, times=1).get_mails({'missing_ident'},
                                                   include_body=True)
        verify(search_engine).index_mails([mail])