我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用factory.Iterator()。
def test_program_page(browser, base_test_data, logged_in_student): """ Test viewing the program page """ courses = list(base_test_data.program.course_set.all()) + \ CourseFactory.create_batch(2, program=base_test_data.program) page = ProgramPageFactory.create(program=base_test_data.program, title="A Program Title") faculty = FacultyFactory.create_batch(3, program_page=page) info_links = InfoLinksFactory.create_batch(3, program_page=page) semester_dates = SemesterDateFactory.create_batch(3, program_page=page) program_courses = ProgramCourseFactory.create_batch( len(courses), program_page=page, course=Iterator(courses) ) browser.get("/a-program-title/") faculty_elements = browser.driver.find_elements_by_css_selector(".faculty-tile") assert len(faculty) == len(faculty_elements) info_elements = browser.driver.find_elements_by_css_selector(".program-contact-link") assert len(info_links) == len(info_elements) semester_elements = browser.driver.find_elements_by_css_selector(".semester-date") assert len(semester_dates) == len(semester_elements) program_course_elements = browser.driver.find_elements_by_css_selector(".program-course .title") assert len(program_courses) == len(program_course_elements)
def test_context(self): """ Assert context values for logged in user """ with mute_signals(post_save): profile = ProfileFactory.create() self.client.force_login(profile.user) # ProgramFaculty and ProgramCourse are asserted via ProgramPageSerializer below FacultyFactory.create_batch(3, program_page=self.program_page) courses = self.program_page.program.course_set.all() ProgramCourseFactory.create_batch( len(courses), program_page=self.program_page, course=Iterator(courses) ) ga_tracking_id = FuzzyText().fuzz() with self.settings( GA_TRACKING_ID=ga_tracking_id, ENVIRONMENT='environment', VERSION='version', ): response = self.client.get(self.program_page.url) assert response.context['authenticated'] is True assert response.context['username'] is None assert response.context['title'] == self.program_page.title assert response.context['is_public'] is True assert response.context['has_zendesk_widget'] is True assert response.context['is_staff'] is False self.assertContains(response, 'Share this page') js_settings = json.loads(response.context['js_settings_json']) assert js_settings == { 'gaTrackingID': ga_tracking_id, 'environment': 'environment', 'sentry_dsn': None, 'host': 'testserver', 'release_version': 'version', 'user': serialize_maybe_user(profile.user), 'program': ProgramPageSerializer(self.program_page).data, }
def test_filter_and_sorting(self, staff_client, program_data, fin_aid_status, sort_param): """ Tests ReviewFinancialAidView correctly applies sort and filter parameters, or uses defaults for those parameters """ FinancialAidFactory.create_batch( 10, tier_program=program_data.tier_programs["0k"], status=factory.Iterator([ ReviewFinancialAidView.default_status, FinancialAidStatus.AUTO_APPROVED ]) ) url = self.review_url(program_data.program.id, status=fin_aid_status, sort_param=sort_param) resp = staff_client.get(url) assert resp.status_code == status.HTTP_200_OK financial_aid_objects = resp.context_data["financial_aid_objects"] # View should filter FinancialAid objects by the provided status or a default status expected_status = fin_aid_status or ReviewFinancialAidView.default_status assert all(fin_aid.status == expected_status for fin_aid in financial_aid_objects) # View should sort by the given parameter, or sort by ascending last name by default should_reverse_sort = sort_param == '-last_name' assert ( list(financial_aid_objects) == sorted(financial_aid_objects, key=lambda f: f.user.last_name, reverse=should_reverse_sort) )
def test_view_with_search(self, staff_client, program_data): """ Tests that ReviewFinancialAidView returns the expected results with search """ fin_aid_status = FinancialAidStatus.AUTO_APPROVED profiles = ProfileFactory.create_batch( 4, first_name=factory.Iterator(['match_name', 'x', 'y', 'z']), last_name=factory.Iterator(['x', 'y', 'z', 'match_name']), ) FinancialAidFactory.create_batch( 4, tier_program=program_data.tier_programs["0k"], status=fin_aid_status, user=factory.Iterator([p.user for p in profiles]) ) name_query = 'match_name' url = self.review_url(program_data.program.id, status=fin_aid_status, search_param=name_query) resp = staff_client.get(url) assert resp.status_code == status.HTTP_200_OK financial_aid_objects = resp.context_data["financial_aid_objects"] # Two users should match the search term - one for first_name, one for last_name assert len(financial_aid_objects) == 2 assert all( fin_aid.user.profile.first_name == name_query or fin_aid.user.profile.last_name == name_query for fin_aid in financial_aid_objects )
def test_generate_course_certificates(): """ Test that generate_course_certificates_for_fa_students creates certificates for appropriate FinalGrades """ course = CourseFactory.create(program__financial_aid_availability=True) # Create two exam runs for course with different date_grades_available exam_run_grades_available = ExamRunFactory.create( course__program__financial_aid_availability=True, date_grades_available=now_in_utc() - timedelta(weeks=1)) course_with_exams = exam_run_grades_available.course exam_run_no_grades = ExamRunFactory.create( course=course_with_exams, date_grades_available=now_in_utc() + timedelta(weeks=1)) # Another non-fa course non_fa_course = CourseFactory.create(program__financial_aid_availability=False) # Create FinalGrade records with different courses and a mix of passed and failed outcomes passed_final_grades = FinalGradeFactory.create_batch(4, course_run__course=course, passed=True) passed_final_grades_with_exam = FinalGradeFactory.create_batch( 6, course_run__course=course_with_exams, passed=True ) FinalGradeFactory.create(course_run__course=non_fa_course, passed=True) FinalGradeFactory.create(course_run__course=course, passed=False) FinalGradeFactory.create(course_run__course=course, passed=True, status='pending') # Create ProctoredExamGrade records with a mix of passed and failed outcomes, and exam grade availability final_grades_with_passed_exam = passed_final_grades_with_exam[:2] final_grades_with_passed_exam_no_grades = passed_final_grades_with_exam[2:4] final_grades_with_failed_exam = passed_final_grades_with_exam[4:] ProctoredExamGradeFactory.create_batch( 2, user=factory.Iterator([final_grade.user for final_grade in final_grades_with_passed_exam]), course=course_with_exams, exam_run=exam_run_grades_available, passed=True, ) ProctoredExamGradeFactory.create_batch( 2, user=factory.Iterator([final_grade.user for final_grade in final_grades_with_passed_exam_no_grades]), course=course_with_exams, exam_run=exam_run_no_grades, passed=True, ) ProctoredExamGradeFactory.create_batch( 2, user=factory.Iterator([final_grade.user for final_grade in final_grades_with_failed_exam]), course=course_with_exams, passed=False, ) tasks.generate_course_certificates_for_fa_students.delay() # Make sure that certificates were created only for passed and 'complete' status FinalGrades that either # had no course exam, or had a passed ProctoredExamGrade. created_certificates = MicromastersCourseCertificate.objects.all() assert len(created_certificates) == 6 certificate_grade_ids = set([certificate.final_grade.id for certificate in created_certificates]) expected_certificate_final_grades = passed_final_grades + final_grades_with_passed_exam assert certificate_grade_ids == set([final_grade.id for final_grade in expected_certificate_final_grades])