def test_preview_url(self): """ Assert preview url behavior for LearningResources. """ learning_resource = LearningResource.objects.first() expected_jump_to_id_url = ( "https://www.sandbox.edx.org/courses/" "test-org/infinity/Febtober/jump_to_id/url_name1" ) self.assertEqual( expected_jump_to_id_url, get_preview_url(learning_resource) ) resource_dict = self.get_learning_resource( self.repo.slug, learning_resource.id) self.assertEqual( expected_jump_to_id_url, resource_dict['preview_url']) learning_resource.url_name = None self.assertEqual( "https://www.sandbox.edx.org/courses/" "test-org/infinity/Febtober/courseware", get_preview_url(learning_resource) )
def assert_result_equal(self, result, resource): """Helper method to assert result == resource.""" self.assertEqual( { 'course': resource.course.course_number, 'description': resource.description, 'description_path': resource.description_path, 'id': resource.id, 'preview_url': get_preview_url(resource), 'resource_type': resource.learning_resource_type.name, 'run': resource.course.run, 'title': resource.title, 'xa_avg_grade': resource.xa_avg_grade, 'xa_nr_attempts': resource.xa_nr_attempts, 'xa_nr_views': resource.xa_nr_views, }, result )
def test_preview_url(self): """ Test get_preview_url function. It returns different results depending upon whether property url_name is None. """ base_url = "{0}courses/org/".format(settings.LORE_PREVIEW_BASE_URL) resource = LearningResource() kwargs = {"resource": resource, "org": "org", "course_number": "babelfish", "run": "gazelle"} tests = ( (None, "{0}babelfish/gazelle/courseware".format(base_url)), ("WNYX", "{0}babelfish/gazelle/jump_to_id/WNYX".format(base_url)), ) for url_name, wanted in tests: resource.url_name = url_name url = get_preview_url(**kwargs) self.assertEqual(url, wanted)
def test_parent_preview_link(self): """ Test that if url_name is blank we import the parent's url_name when viewing the preview link. """ xml = """ <course org="DevOps" course="0.001" url_name="2015_Summer" semester="2015_Summer"> <chapter> <sequential> <vertical> <html></html> </vertical> </sequential> </chapter> </course> """ repo = create_repo("html_repo", "...", self.user.id) xml = etree.fromstring(xml) bundle = XBundle( keep_urls=True, keep_studio_urls=True, preserve_url_name=True ) bundle.set_course(xml) import_course(bundle, repo.id, self.user.id, "") html_resources = LearningResource.objects.filter( learning_resource_type__name="html" ) self.assertEqual(html_resources.count(), 1) html_resource = html_resources.first() self.assertEqual( get_preview_url(html_resource), "{base}courses/{org}/{course}/{run}/jump_to_id/{url_path}".format( base=settings.LORE_PREVIEW_BASE_URL, org=html_resource.course.org, course=html_resource.course.course_number, run=html_resource.course.run, url_path="2015_Summer" ) )
def assert_result_equal(self, result, resource): """Helper method to assert result == resource.""" # remove the score from the result because # it is meaningless for the tests del result['score'] self.assertEqual( { 'course': resource.course.course_number, 'description': resource.description, 'description_path': resource.description_path, 'id': resource.id, 'preview_url': get_preview_url(resource), 'resource_type': resource.learning_resource_type.name, 'run': resource.course.run, 'title': resource.title, 'xa_avg_grade': resource.xa_avg_grade, 'xa_nr_attempts': resource.xa_nr_attempts, 'xa_nr_views': resource.xa_nr_views, }, result)
def assert_result_equal(self, result, resource): """Helper method to assert result == resource.""" # remove the score from the result because # it is meaningless for the tests del result['score'] self.assertEqual( { 'course': resource.course.course_number, 'description': resource.description, 'description_path': resource.description_path, 'id': resource.id, 'preview_url': get_preview_url(resource), 'resource_type': resource.learning_resource_type.name, 'run': resource.course.run, 'title': resource.title, 'xa_avg_grade': resource.xa_avg_grade, 'xa_nr_attempts': resource.xa_nr_attempts, 'xa_nr_views': resource.xa_nr_views, }, result )
def test_preview_url(self): """ Test get_preview_url function. It returns different results depending upon whether property url_name is None. """ base_url = "{0}courses/org/".format(settings.LORE_PREVIEW_BASE_URL) resource = LearningResource() kwargs = { 'resource': resource, 'org': "org", 'course_number': 'babelfish', 'run': 'gazelle', } tests = ((None, '{0}babelfish/gazelle/courseware'.format(base_url)), ("WNYX", '{0}babelfish/gazelle/jump_to_id/WNYX'.format(base_url))) for url_name, wanted in tests: resource.url_name = url_name url = get_preview_url(**kwargs) self.assertEqual(url, wanted)
def resource_to_dict(resource, term_info): """ Retrieve important values from a LearningResource to index. This dict corresponds to the mapping created in Elasticsearch. The titlesort bits, with the "0" and "1" prefixes, were copied from the prepare_titlesort function in search/search_indexes.py. It was there to make blank titles sort to the bottom instead of the top. Args: resource (LearningResource): Item to convert to dict. term_info (dict): Vocabulary terms assigned to resource. Returns: rec (dict): Dictionary representation of the LearningResource. """ rec = { "title": resource.title, # The zero is for sorting blank items to the bottom. See below. "titlesort": "0{0}".format(resource.title.strip()), "id": resource.id, "_id": resource.id, # The ID used by Elasticsearch. "resource_type": resource.learning_resource_type.name, "description": resource.description, "description_path": resource.description_path, "content_xml": resource.content_xml, "content_stripped": strip_xml(resource.content_xml), "xa_nr_views": resource.xa_nr_views, "xa_nr_attempts": resource.xa_nr_attempts, "xa_avg_grade": resource.xa_avg_grade, "xa_histogram_grade": resource.xa_histogram_grade, } course = get_course_metadata(resource.course_id) rec["preview_url"] = get_preview_url( resource, org=course["org"], course_number=course["course_number"], run=course["run"], ) rec["run"] = course["run"] rec["course"] = course["course_number"] rec["repository"] = course["repo_slug"] # Index term info. Since these fields all use the "not_analyzed" # index, they must all be exact matches. for vocab_id, term_ids in term_info.items(): rec[make_vocab_key(vocab_id)] = term_ids # If the title is empty, sort it to the bottom. See above. if rec["titlesort"] == "0": rec["titlesort"] = "1" # Keys that may have unicode issues. text_keys = ( 'title', 'titlesort', 'resource_type', 'description', 'content_xml', 'content_stripped', 'description_path', ) for key in text_keys: try: # Thanks to unicode_literals above, this works in # Python 2 and Python 3. Avoid trying to decode a string # if it's already unicode. if not isinstance(rec[key], type("")): rec[key] = rec[key].decode('utf-8') except AttributeError: pass # Python 3 return rec