def test_remove_organization_course_missing_organization(self): """ Unit Test: test_remove_organization_course_missing_organization """ with self.assertNumQueries(1): api.remove_organization_course(self.test_organization, self.test_course_key) organizations = api.get_course_organizations(self.test_course_key) self.assertEqual(len(organizations), 0)
def test_add_organization_course_inactive_to_active(self): """ Unit Test: test_add_organization_course_inactive_to_active """ api.add_organization_course(self.test_organization, self.test_course_key) api.remove_organization_course(self.test_organization, self.test_course_key) with self.assertNumQueries(3): api.add_organization_course(self.test_organization, self.test_course_key)
def test_remove_organization_course_missing_course(self): """ Unit Test: test_remove_organization_course_missing_organization """ api.add_organization_course(self.test_organization, 'edX/DemoX/Demo_Course') organizations = api.get_course_organizations('edX/DemoX/Demo_Course') self.assertEqual(len(organizations), 1) with self.assertNumQueries(1): api.remove_organization_course(self.test_organization, self.test_course_key) organizations = api.get_course_organizations(self.test_course_key) self.assertEqual(len(organizations), 0)
def test_remove_organization_course(self): """ Unit Test: test_remove_organization_course """ api.add_organization_course(self.test_organization, self.test_course_key) organizations = api.get_course_organizations(self.test_course_key) self.assertEqual(len(organizations), 1) with self.assertNumQueries(3): api.remove_organization_course(self.test_organization, self.test_course_key) organizations = api.get_course_organizations(self.test_course_key) self.assertEqual(len(organizations), 0)
def test_remove_organization_course_missing_course(self): """ Unit Test: test_remove_organization_course_missing_organization """ api.add_organization_course( self.test_organization, 'edX/DemoX/Demo_Course' ) organizations = api.get_course_organizations('edX/DemoX/Demo_Course') self.assertEqual(len(organizations), 1) with self.assertNumQueries(1): api.remove_organization_course(self.test_organization, self.test_course_key) organizations = api.get_course_organizations(self.test_course_key) self.assertEqual(len(organizations), 0)
def test_remove_organization_course(self): """ Unit Test: test_remove_organization_course """ api.add_organization_course( self.test_organization, self.test_course_key ) organizations = api.get_course_organizations(self.test_course_key) self.assertEqual(len(organizations), 1) with self.assertNumQueries(3): api.remove_organization_course(self.test_organization, self.test_course_key) organizations = api.get_course_organizations(self.test_course_key) self.assertEqual(len(organizations), 0)
def test_add_organization_course_inactive_to_active(self): """ Unit Test: test_add_organization_course_inactive_to_active """ api.add_organization_course( self.test_organization, self.test_course_key ) api.remove_organization_course(self.test_organization, self.test_course_key) with self.assertNumQueries(3): api.add_organization_course( self.test_organization, self.test_course_key )
def test_add_several_organization_courses(self): """ Test that the query_count of bulk_add_organization_courses does not increase when given more organization-course linkages to add. """ org_a = api.add_organization(self.make_organization_data("org_a")) org_b = api.add_organization(self.make_organization_data("org_b")) org_c = api.add_organization(self.make_organization_data("org_c")) course_key_x = CourseKey.from_string("course-v1:x+x+x") course_key_y = CourseKey.from_string("course-v1:y+y+y") course_key_z = CourseKey.from_string("course-v1:z+z+z") # Add linkage A->X. api.add_organization_course(org_a, course_key_x) # Add linkage A->Y, and then remove (actually: deactivate). api.add_organization_course(org_a, course_key_y) api.remove_organization_course(org_a, course_key_y) # 1 query to load list of existing linkages, # 1 query to fetch organizations for existing linkages, # 1 query to ensure all existing linkages active, # 1 query to get organiations for new linkages, # 1 query to create new linkages. with self.assertNumQueries(5): api.bulk_add_organization_courses([ (org_a, course_key_x), # Already existing. (org_a, course_key_x), # Already existing. (org_a, course_key_y), # Reactivation. (org_a, course_key_z), # The rest are new. (org_b, course_key_x), (org_b, course_key_y), (org_b, course_key_z), (org_c, course_key_x), (org_c, course_key_y), (org_c, course_key_z), (org_c, course_key_z), # Redundant. (org_c, course_key_z), # Redundant. ]) assert len(api.get_organization_courses(org_a)) == 3 assert len(api.get_organization_courses(org_b)) == 3 assert len(api.get_organization_courses(org_c)) == 3
def test_edge_cases(self, mock_log_info): """ Test that bulk_add_organization_courses handles a few edge cases as expected. """ org_a = api.add_organization(self.make_organization_data("org_a")) org_b = api.add_organization(self.make_organization_data("org_b")) course_key_x = CourseKey.from_string("course-v1:x+x+x") course_key_y = CourseKey.from_string("course-v1:y+y+y") course_key_z = CourseKey.from_string("course-v1:z+z+z") # Add linkage A->X api.add_organization_course(org_a, course_key_x) # Add and then remove (under the hood: deactivate) linkage between A->Y. api.add_organization_course(org_a, course_key_y) api.remove_organization_course(org_a, course_key_y) # Add and then remove (under the hood: deactivate) linkage between A->Z. # This should NOT be reactivated, as we don't include it in the bulk_add call. api.add_organization_course(org_a, course_key_z) api.remove_organization_course(org_a, course_key_z) # 1 query to load list of existing linkages, # 1 query to fetch organizations for existing linkages, # 1 query to ensure all existing linkages active, # 1 query to get organiations for new linkages, # 1 query to create new linkages. with self.assertNumQueries(5): api.bulk_add_organization_courses([ # A->X: Existing linkage, should be a no-op. (org_a, course_key_x), # B->Y: Should create new linkage. (org_b, course_key_y), # A->Y: Is an inactive linkage; should be re-activated. (org_a, course_key_y), # B->Y: Is already in this list; shouldn't affect anything. (org_b, course_key_y), # B->Z: Adding with a stringified course id; should work as if we # used the course key object. (org_b, str(course_key_z)), # B->Z: Adding again with the course key object; should be a no-op. (org_b, course_key_z), ]) # Org A was linked to courses X and Y. # Org A also has an inactive link to course Z that we never re-activated. org_a_courses = api.get_organization_courses(org_a) assert {org_course["course_id"] for org_course in org_a_courses } == {"course-v1:x+x+x", "course-v1:y+y+y"} # Org B was linked to courses Y and Z. org_b_courses = api.get_organization_courses(org_b) assert {org_course["course_id"] for org_course in org_b_courses } == {"course-v1:y+y+y", "course-v1:z+z+z"} # Based on logging messages, make sure the expected breakdown of # created vs. reactivated vs. not touched # is true for the org-course linkages passed to `bulk_add_organization_courses`. logged_linkages_to_reactivate = mock_log_info.call_args_list[0][0][2] assert set(logged_linkages_to_reactivate) == { ("org_a", str(course_key_y)), } logged_linkages_to_create = mock_log_info.call_args_list[1][0][2] assert set(logged_linkages_to_create) == { ("org_b", str(course_key_y)), ("org_b", str(course_key_z)), }