def test_fetch_exercise_calculations(self): responses = BLSCHED.fetch_exercise_calculations() for response in responses: assert set(response.keys()) == set(( 'calculation_uuid', 'ecosystem_uuid', 'exercise_uuids', 'student_uuid' )) assert UUID_REGEX.match(response['calculation_uuid']) assert UUID_REGEX.match(response['ecosystem_uuid']) for exercise_uuid in response['exercise_uuids']: assert UUID_REGEX.match(exercise_uuid) assert UUID_REGEX.match(response['student_uuid'])
def test_fetch_course_metadatas(self): responses = BLAPI.fetch_course_metadatas( metadata_sequence_number_offset=42, max_num_metadatas=10 ) for response in responses: assert set(response.keys()) == set( ('uuid', 'metadata_sequence_number', 'initial_ecosystem_uuid') ) assert UUID_REGEX.match(response['uuid']) assert isinstance(response['metadata_sequence_number'], int) assert UUID_REGEX.match(response['initial_ecosystem_uuid'])
def test_fetch_clue_calculations(self): responses = BLSCHED.fetch_clue_calculations() for response in responses: assert set(response.keys()) == set(( 'ecosystem_uuid', 'responses', 'calculation_uuid', 'student_uuids', 'exercise_uuids' )) assert UUID_REGEX.match(response['ecosystem_uuid']) for rr in response['responses']: assert set(rr.keys()) == set(('response_uuid', 'trial_uuid', 'is_correct')) assert UUID_REGEX.match(rr['response_uuid']) assert UUID_REGEX.match(rr['trial_uuid']) assert type(rr['is_correct']) == bool assert UUID_REGEX.match(response['calculation_uuid']) for student_uuid in response['student_uuids']: assert UUID_REGEX.match(student_uuid) for exercise_uuid in response['exercise_uuids']: assert UUID_REGEX.match(exercise_uuid)
def test_fetch_ecosystem_matrix_updates(self): responses = BLSCHED.fetch_ecosystem_matrix_updates() for response in responses: assert set(response.keys()) == set(('ecosystem_uuid', 'calculation_uuid')) assert UUID_REGEX.match(response['ecosystem_uuid']) assert UUID_REGEX.match(response['calculation_uuid'])
def test_load_grouped_ecosystem_events(transaction): with transaction() as session: assert _load_grouped_ecosystem_events(session, []) == [] ecosystem_1 = Ecosystem(uuid=str(uuid4()), metadata_sequence_number=0, sequence_number=0) ecosystem_2 = Ecosystem(uuid=str(uuid4()), metadata_sequence_number=1, sequence_number=0) with transaction() as session: session.add(ecosystem_1) session.add(ecosystem_2) request_uuid_1 = str(uuid4()) request_uuid_2 = str(uuid4()) with patch('sparfa_server.tasks.loaders.uuid4', autospec=True) as uuid: uuid.side_effect = [request_uuid_1, request_uuid_2] chapter_uuid = str(uuid4()) page_1_uuid = str(uuid4()) page_1_exercise_uuids = [str(uuid4()) for i in range(4)] page_2_uuid = str(uuid4()) page_2_exercise_uuids = [str(uuid4()) for i in range(4)] all_exercise_uuids = page_1_exercise_uuids + page_2_exercise_uuids with patch('sparfa_server.tasks.loaders.BLAPI.fetch_ecosystem_events', autospec=True) as fetch_ecosystem_events: fetch_ecosystem_events.return_value = [{ 'request_uuid': request_uuid_1, 'ecosystem_uuid': ecosystem_1.uuid, 'events': [{ 'sequence_number': 0, 'event_uuid': uuid4(), 'event_type': 'create_ecosystem', 'event_data': { 'ecosystem_uuid': ecosystem_1.uuid, 'book': { 'contents': [{ 'container_parent_uuid': uuid4(), 'container_uuid': chapter_uuid, 'pools': [{ 'exercise_uuids': all_exercise_uuids }] }, { 'container_parent_uuid': chapter_uuid, 'container_uuid': page_1_uuid, 'pools': [{ 'exercise_uuids': page_1_exercise_uuids }, { 'exercise_uuids': page_1_exercise_uuids[:2] }, { 'exercise_uuids': page_1_exercise_uuids[2:] }] }, { 'container_parent_uuid': chapter_uuid, 'container_uuid': page_2_uuid, 'pools': [{ 'exercise_uuids': page_2_exercise_uuids }, { 'exercise_uuids': page_2_exercise_uuids[:2] }, { 'exercise_uuids': page_2_exercise_uuids[2:] }] }] } } }], 'is_gap': True, 'is_end': False }, { 'request_uuid': request_uuid_2, 'ecosystem_uuid': ecosystem_2.uuid, 'events': [], 'is_gap': False, 'is_end': False }] with transaction() as session: ecosystems = session.query(Ecosystem).all() assert _load_grouped_ecosystem_events( session, ecosystems) == [ecosystem_2.uuid] fetch_ecosystem_events.assert_called_once() args = fetch_ecosystem_events.call_args assert len(args) == 2 assert not args[1] assert len(args[0]) == 1 requests = args[0][0] for request in requests: assert request['sequence_number_offset'] == 0 assert request['event_types'] == ['create_ecosystem'] assert UUID_REGEX.match(request['request_uuid']) assert set(request['ecosystem_uuid'] for request in requests) == \ set(ecosystem.uuid for ecosystem in ecosystems) with transaction() as session: pages = session.query(Page).all() ecosystem_matrices = session.query(EcosystemMatrix).all() ecosystems = session.query(Ecosystem).all() assert len(pages) == 2 for page in pages: assert page.uuid in [page_1_uuid, page_2_uuid] assert page.ecosystem_uuid == ecosystem_1.uuid if page.uuid == page_1_uuid: assert set(page.exercise_uuids) == set(page_1_exercise_uuids) else: assert set(page.exercise_uuids) == set(page_2_exercise_uuids) assert len(ecosystem_matrices) == 1 ecosystem_matrix = ecosystem_matrices[0] assert ecosystem_matrix.ecosystem_uuid == ecosystem_1.uuid assert set(ecosystem_matrix.C_ids) == set((page_1_uuid, page_2_uuid)) assert set(ecosystem_matrix.Q_ids) == set(all_exercise_uuids) assert ecosystem_matrix.d_NQx1.shape == (ecosystem_matrix.NQ, 1) assert ecosystem_matrix.W_NCxNQ.shape == (ecosystem_matrix.NC, ecosystem_matrix.NQ) assert ecosystem_matrix.H_mask_NCxNQ.shape == (ecosystem_matrix.NC, ecosystem_matrix.NQ) assert len(ecosystems) == 2 for ecosystem in ecosystems: assert ecosystem.uuid in [ecosystem_1.uuid, ecosystem_2.uuid] assert ecosystem.sequence_number == (1 if ecosystem == ecosystem_1 else 0)
def test_load_grouped_course_events(transaction): with transaction() as session: assert _load_grouped_course_events(session, []) == [] course_1 = Course(uuid=str(uuid4()), metadata_sequence_number=0, sequence_number=1) course_2 = Course(uuid=str(uuid4()), metadata_sequence_number=1, sequence_number=2) ecosystem_matrix_1 = EcosystemMatrix(uuid=str(uuid4()), ecosystem_uuid=str(uuid4()), Q_ids=[], C_ids=[], d_data=[], W_data=[], W_row=[], W_col=[], H_mask_data=[], H_mask_row=[], H_mask_col=[]) assert not ecosystem_matrix_1.is_used_in_assignments with transaction() as session: session.add(course_1) session.add(course_2) session.add(ecosystem_matrix_1) request_uuid_1 = str(uuid4()) request_uuid_2 = str(uuid4()) with patch('sparfa_server.tasks.loaders.uuid4', autospec=True) as uuid: uuid.side_effect = [request_uuid_1, request_uuid_2] course_1_uuid = str(uuid4()) assignment_1_uuid = str(uuid4()) assignment_1_type = 'reading' assigned_book_container_uuids_1 = [str(uuid4())] goal_num_tutor_assigned_pes_1 = 3 goal_num_tutor_assigned_spes_1 = 3 assigned_exercises_1 = [str(uuid4())] created_at_1 = datetime.now() response_1_uuid = str(uuid4()) trial_1_uuid = str(uuid4()) student_1_uuid = str(uuid4()) exercise_uuid = str(uuid4()) responded_at_1 = datetime.now() calculation_1_uuid = str(uuid4()) response_2_uuid = str(uuid4()) responded_at_2 = datetime.now() response_3_uuid = str(uuid4()) ecosystem_2_uuid = str(uuid4()) trial_2_uuid = str(uuid4()) responded_at_3 = datetime.now() response_4_uuid = str(uuid4()) ecosystem_3_uuid = str(uuid4()) trial_3_uuid = str(uuid4()) student_2_uuid = str(uuid4()) responded_at_4 = datetime.now() response_5_uuid = str(uuid4()) trial_4_uuid = str(uuid4()) student_3_uuid = str(uuid4()) responded_at_5 = datetime.now() with patch('sparfa_server.tasks.loaders.BLAPI.fetch_course_events', autospec=True) as fetch_course_events: fetch_course_events.return_value = [{ 'request_uuid': request_uuid_1, 'course_uuid': course_1.uuid, 'events': [{ 'sequence_number': 1, 'event_uuid': uuid4(), 'event_type': 'create_update_assignment', 'event_data': { 'request_uuid': str(uuid4()), 'course_uuid': course_1_uuid, 'sequence_number': 1, 'assignment_uuid': assignment_1_uuid, 'is_deleted': False, 'ecosystem_uuid': ecosystem_matrix_1.ecosystem_uuid, 'student_uuid': student_1_uuid, 'assignment_type': assignment_1_type, 'exclusion_info': {}, 'pes': {}, 'spes': {}, 'assigned_book_container_uuids': assigned_book_container_uuids_1, 'goal_num_tutor_assigned_spes': goal_num_tutor_assigned_spes_1, 'spes_are_assigned': False, 'goal_num_tutor_assigned_pes': goal_num_tutor_assigned_pes_1, 'pes_are_assigned': False, 'assigned_exercises': assigned_exercises_1, 'created_at': created_at_1, 'updated_at': datetime.now() } }, { 'sequence_number': 2, 'event_uuid': uuid4(), 'event_type': 'record_response', 'event_data': { 'response_uuid': response_1_uuid, 'sequence_number': 2, 'course_uuid': course_1.uuid, 'ecosystem_uuid': ecosystem_matrix_1.ecosystem_uuid, 'trial_uuid': trial_1_uuid, 'student_uuid': student_1_uuid, 'exercise_uuid': exercise_uuid, 'is_correct': False, 'is_real_response': True, 'responded_at': responded_at_1 } }, { 'sequence_number': 3, 'event_uuid': uuid4(), 'event_type': 'create_update_assignment', 'event_data': { 'request_uuid': str(uuid4()), 'course_uuid': course_1_uuid, 'sequence_number': 3, 'assignment_uuid': assignment_1_uuid, 'is_deleted': False, 'ecosystem_uuid': ecosystem_matrix_1.ecosystem_uuid, 'student_uuid': student_1_uuid, 'assignment_type': assignment_1_type, 'exclusion_info': {}, 'pes': { 'calculation_uuid': calculation_1_uuid, 'ecosystem_matrix_uuid': ecosystem_matrix_1.uuid }, 'spes': {}, 'assigned_book_container_uuids': assigned_book_container_uuids_1, 'goal_num_tutor_assigned_spes': goal_num_tutor_assigned_spes_1, 'spes_are_assigned': False, 'goal_num_tutor_assigned_pes': goal_num_tutor_assigned_pes_1, 'pes_are_assigned': True, 'assigned_exercises': assigned_exercises_1, 'created_at': created_at_1, 'updated_at': datetime.now() } }, { 'sequence_number': 4, 'event_uuid': uuid4(), 'event_type': 'record_response', 'event_data': { 'response_uuid': response_2_uuid, 'sequence_number': 4, 'course_uuid': course_1.uuid, 'ecosystem_uuid': ecosystem_matrix_1.ecosystem_uuid, 'trial_uuid': trial_1_uuid, 'student_uuid': student_1_uuid, 'exercise_uuid': exercise_uuid, 'is_correct': True, 'is_real_response': True, 'responded_at': responded_at_2 } }, { 'sequence_number': 5, 'event_uuid': uuid4(), 'event_type': 'record_response', 'event_data': { 'response_uuid': response_3_uuid, 'sequence_number': 5, 'course_uuid': course_1.uuid, 'ecosystem_uuid': ecosystem_2_uuid, 'trial_uuid': trial_2_uuid, 'student_uuid': student_1_uuid, 'exercise_uuid': exercise_uuid, 'is_correct': False, 'is_real_response': True, 'responded_at': responded_at_3 } }, { 'sequence_number': 6, 'event_uuid': uuid4(), 'event_type': 'create_update_assignment', 'event_data': { 'request_uuid': str(uuid4()), 'course_uuid': course_1_uuid, 'sequence_number': 6, 'assignment_uuid': assignment_1_uuid, 'is_deleted': False, 'ecosystem_uuid': ecosystem_matrix_1.ecosystem_uuid, 'student_uuid': student_1_uuid, 'assignment_type': assignment_1_type, 'exclusion_info': {}, 'pes': { 'calculation_uuid': calculation_1_uuid, 'ecosystem_matrix_uuid': ecosystem_matrix_1.uuid }, 'spes': { 'calculation_uuid': str(uuid4()), 'ecosystem_matrix_uuid': ecosystem_matrix_1.uuid }, 'assigned_book_container_uuids': assigned_book_container_uuids_1, 'goal_num_tutor_assigned_spes': goal_num_tutor_assigned_spes_1, 'spes_are_assigned': True, 'goal_num_tutor_assigned_pes': goal_num_tutor_assigned_pes_1, 'pes_are_assigned': True, 'assigned_exercises': assigned_exercises_1, 'created_at': created_at_1, 'updated_at': datetime.now() } }], 'is_gap': False, 'is_end': False }, { 'request_uuid': request_uuid_2, 'course_uuid': course_2.uuid, 'events': [{ 'sequence_number': 2, 'event_uuid': uuid4(), 'event_type': 'record_response', 'event_data': { 'response_uuid': response_4_uuid, 'sequence_number': 2, 'course_uuid': course_2.uuid, 'ecosystem_uuid': ecosystem_3_uuid, 'trial_uuid': trial_3_uuid, 'student_uuid': student_2_uuid, 'exercise_uuid': exercise_uuid, 'is_correct': True, 'is_real_response': False, 'responded_at': responded_at_4 } }, { 'sequence_number': 3, 'event_uuid': uuid4(), 'event_type': 'record_response', 'event_data': { 'response_uuid': response_5_uuid, 'sequence_number': 3, 'course_uuid': course_2.uuid, 'ecosystem_uuid': ecosystem_3_uuid, 'trial_uuid': trial_4_uuid, 'student_uuid': student_3_uuid, 'exercise_uuid': exercise_uuid, 'is_correct': False, 'is_real_response': False, 'responded_at': responded_at_5 } }], 'is_gap': False, 'is_end': True }] with transaction() as session: courses = session.query(Course).all() assert _load_grouped_course_events(session, courses) == [course_1.uuid] fetch_course_events.assert_called_once() args = fetch_course_events.call_args assert len(args) == 2 assert not args[1] assert len(args[0]) == 1 requests = args[0][0] for request in requests: assert request['sequence_number_offset'] == ( 1 if request['course_uuid'] == course_1.uuid else 2) assert request['event_types'] == [ 'create_update_assignment', 'record_response' ] assert UUID_REGEX.match(request['request_uuid']) assert set(request['course_uuid'] for request in requests) == \ set(course.uuid for course in courses) with transaction() as session: ecosystem_matrices = session.query(EcosystemMatrix).all() responses = session.query(Response).all() courses = session.query(Course).all() assert len(ecosystem_matrices) == 1 ecosystem_matrix = ecosystem_matrices[0] assert ecosystem_matrix.uuid == ecosystem_matrix_1.uuid assert ecosystem_matrix.is_used_in_assignments assert len(responses) == 4 for response in responses: assert response.course_uuid in [course_1.uuid, course_2.uuid] if response.course_uuid == course_1.uuid: assert response.trial_uuid in [trial_1_uuid, trial_2_uuid] if response.trial_uuid == trial_1_uuid: assert response.uuid == response_2_uuid assert response.ecosystem_uuid == ecosystem_matrix_1.ecosystem_uuid assert response.is_correct assert response.responded_at == responded_at_2 else: assert response.uuid == response_3_uuid assert response.ecosystem_uuid == ecosystem_2_uuid assert not response.is_correct assert response.responded_at == responded_at_3 assert response.student_uuid == student_1_uuid assert response.is_real_response else: assert response.trial_uuid in [trial_3_uuid, trial_4_uuid] if response.trial_uuid == trial_3_uuid: assert response.uuid == response_4_uuid assert response.student_uuid == student_2_uuid assert response.is_correct assert response.responded_at == responded_at_4 else: assert response.uuid == response_5_uuid assert response.student_uuid == student_3_uuid assert not response.is_correct assert response.responded_at == responded_at_5 assert response.ecosystem_uuid == ecosystem_3_uuid assert not response.is_real_response assert response.exercise_uuid == exercise_uuid assert len(courses) == 2 for course in courses: assert course.uuid in [course_1.uuid, course_2.uuid] assert course.sequence_number == 7 if course == course_1 else 4