'Verify that certificates are regenerated for all eligible students enrolled in a course whose generated
certificate status lies in the list \'statuses_to_regenerate\' given in task_input. but the \'unavailable\'
status is not touched if it is not in the \'statuses_to_regenerate\' list.'

def test_certificate_regeneration_with_existing_unavailable_status(self):

    default_grade = '-1'
    students = self._create_students(10)
    for student in students[:2]:
        GeneratedCertificateFactory.create(user=student, course_id=self.course.id, status=CertificateStatuses.downloadable, mode='honor', grade=default_grade)
    for student in students[2:5]:
        GeneratedCertificateFactory.create(user=student, course_id=self.course.id, status=CertificateStatuses.error, mode='honor', grade=default_grade)
    for student in students[5:7]:
        GeneratedCertificateFactory.create(user=student, course_id=self.course.id, status=CertificateStatuses.unavailable, mode='honor', grade=default_grade)
    for student in students[7:]:
        GeneratedCertificateFactory.create(user=student, course_id=self.course.id, status=CertificateStatuses.generating, mode='honor', grade=default_grade)
    for student in students[:]:
        CertificateWhitelistFactory.create(user=student, course_id=self.course.id, whitelist=True)
    task_input = {'statuses_to_regenerate': [CertificateStatuses.downloadable, CertificateStatuses.error, CertificateStatuses.generating]}
    expected_results = {'action_name': 'certificates    generated', 'total': 10, 'attempted': 8, 'succeeded': 8, 'failed': 0, 'skipped': 2}
    self.assertCertificatesGenerated(task_input, expected_results)
    generated_certificates = GeneratedCertificate.eligible_certificates.filter(user__in=students, course_id=self.course.id, mode='honor')
    certificate_statuses = [generated_certificate.status for generated_certificate in generated_certificates]
    certificate_grades = [generated_certificate.grade for generated_certificate in generated_certificates]
    self.assertEqual(certificate_statuses.count(CertificateStatuses.generating), 8)
    self.assertEqual(certificate_statuses.count(CertificateStatuses.unavailable), 2)
    self.assertEqual(certificate_grades.count('0.0'), 8)
    self.assertEqual(certificate_grades.count(default_grade), 2)
    unavailable_certificates = [cert for cert in generated_certificates if ((cert.status == CertificateStatuses.unavailable) and (cert.grade == default_grade))]
    self.assertEquals(len(unavailable_certificates), 2)
