Beispiel #1
0
    def test_send_mails(self):
        maillist = self._create_maillist()
        subscriber, _ = Subscriber.objects.get_or_create(
            maillist=maillist,
            email='*****@*****.**'
        )
        subscriber.subscription_date -= timedelta(10)
        subscriber.save()

        other_subscriber, _ = Subscriber.objects.get_or_create(
            maillist=maillist,
            email='*****@*****.**'
        )
        other_subscriber.subscription_date -= timedelta(10)
        other_subscriber.save()

        third_subscriber, _ = Subscriber.objects.get_or_create(
            maillist=maillist,
            email='*****@*****.**'
        )
        third_subscriber.subscription_date -= timedelta(10)
        third_subscriber.save()

        old_mail = self._create_mail(maillist, days=-20) # 0
        not_old_mail = self._create_mail(maillist, days=-5) # 1
        new_mail = self._create_mail(maillist, days=5) # 2

        sent_mail = self._create_mail(maillist, days=-5) # 3
        EmailLog.objects.create(mail=sent_mail, subscriber=subscriber, success=True)
        EmailLog.objects.create(mail=sent_mail, subscriber=other_subscriber, success=True)
        EmailLog.objects.create(mail=sent_mail, subscriber=third_subscriber, success=True)

        not_sent_mail = self._create_mail(maillist, days=-4) # 4
        EmailLog.objects.create(mail=not_sent_mail, subscriber=subscriber, success=False)
        EmailLog.objects.create(mail=not_sent_mail, subscriber=other_subscriber, success=True)


        send_mails()

        for log in EmailLog.objects.all():
            print log.mail.subject, log.subscriber.email, log.success

        self.assertEquals(len(mail.outbox), 5)
        self.assertEquals(EmailLog.objects.count(), 9)

        for subscriber in (subscriber, other_subscriber, third_subscriber):
            self.assertEquals(EmailLog.objects.filter(subscriber=subscriber, mail=not_old_mail).count(), 1)
            self.assertEquals(EmailLog.objects.filter(subscriber=subscriber, mail=sent_mail).count(), 1)
            self.assertEquals(EmailLog.objects.filter(subscriber=subscriber, mail=not_sent_mail).count(), 1)
 def handle_noargs(self, **options):
     send_mails()
 def execute(self):
     send_mails()