def test_unicode(self): """Check that courses with unicode characters in filenames and in org/course/name import properly. Currently, this means: (a) Having files with unicode names does not prevent import; (b) if files are not loaded because of unicode filenames, there are appropriate exceptions/errors to that effect.""" print("Starting import") modulestore = XMLModuleStore(DATA_DIR, course_dirs=['test_unicode']) courses = modulestore.get_courses() self.assertEquals(len(courses), 1) course = courses[0] print("course errors:") # Expect to find an error/exception about characters in "®esources" expect = "InvalidKeyError" errors = [ (msg.encode("utf-8"), err.encode("utf-8")) for msg, err in modulestore.get_course_errors(course.id) ] self.assertTrue(any( expect in msg or expect in err for msg, err in errors )) chapters = course.get_children() self.assertEqual(len(chapters), 4)
def test_colon_in_url_name(self): """Ensure that colons in url_names convert to file paths properly""" print("Starting import") # Not using get_courses because we need the modulestore object too afterward modulestore = XMLModuleStore(DATA_DIR, source_dirs=['toy']) courses = modulestore.get_courses() self.assertEquals(len(courses), 1) course = courses[0] print("course errors:") for (msg, err) in modulestore.get_course_errors(course.id): print(msg) print(err) chapters = course.get_children() self.assertEquals(len(chapters), 5) ch2 = chapters[1] self.assertEquals(ch2.url_name, "secret:magic") print("Ch2 location: ", ch2.location) also_ch2 = modulestore.get_item(ch2.location) self.assertEquals(ch2, also_ch2) print("making sure html loaded") loc = course.id.make_usage_key('html', 'secret:toylab') html = modulestore.get_item(loc) self.assertEquals(html.display_name, "Toy lab")
def import_course(course_dir, verbose=True): course_dir = path(course_dir) data_dir = course_dir.dirname() course_dirs = [course_dir.basename()] # No default class--want to complain if it doesn't find plugins for any # module. modulestore = XMLModuleStore(data_dir, default_class=None, course_dirs=course_dirs) def str_of_err(tpl): (msg, exc_str) = tpl return '{msg}\n{exc}'.format(msg=msg, exc=exc_str) courses = modulestore.get_courses() n = len(courses) if n != 1: sys.stderr.write( 'ERROR: Expect exactly 1 course. Loaded {n}: {lst}\n'.format( n=n, lst=courses)) return None course = courses[0] errors = modulestore.get_course_errors(course.id) if len(errors) != 0: sys.stderr.write('ERRORs during import: {0}\n'.format('\n'.join( map(str_of_err, errors)))) return course
def test_colon_in_url_name(self): """Ensure that colons in url_names convert to file paths properly""" print("Starting import") # Not using get_courses because we need the modulestore object too afterward modulestore = XMLModuleStore(DATA_DIR, course_dirs=['toy']) courses = modulestore.get_courses() self.assertEquals(len(courses), 1) course = courses[0] print("course errors:") for (msg, err) in modulestore.get_course_errors(course.id): print(msg) print(err) chapters = course.get_children() self.assertEquals(len(chapters), 5) ch2 = chapters[1] self.assertEquals(ch2.url_name, "secret:magic") print("Ch2 location: ", ch2.location) also_ch2 = modulestore.get_item(ch2.location) self.assertEquals(ch2, also_ch2) print("making sure html loaded") loc = course.id.make_usage_key('html', 'secret:toylab') html = modulestore.get_item(loc) self.assertEquals(html.display_name, "Toy lab")
def test_unicode(self): """Check that courses with unicode characters in filenames and in org/course/name import properly. Currently, this means: (a) Having files with unicode names does not prevent import; (b) if files are not loaded because of unicode filenames, there are appropriate exceptions/errors to that effect.""" print("Starting import") modulestore = XMLModuleStore(DATA_DIR, source_dirs=['test_unicode']) courses = modulestore.get_courses() assert len(courses) == 1 course = courses[0] print("course errors:") # Expect to find an error/exception about characters in "®esources" expect = "InvalidKeyError" errors = [ (msg, err) for msg, err # lint-amnesty, pylint: disable=unnecessary-comprehension in modulestore.get_course_errors(course.id) ] assert any( ((expect in msg) or (expect in err)) for (msg, err) in errors) chapters = course.get_children() assert len(chapters) == 4
def import_course(course_dir, verbose=True): course_dir = path(course_dir) data_dir = course_dir.dirname() course_dirs = [course_dir.basename()] # No default class--want to complain if it doesn't find plugins for any # module. modulestore = XMLModuleStore(data_dir, default_class=None, course_dirs=course_dirs) def str_of_err(tpl): (msg, exc_str) = tpl return '{msg}\n{exc}'.format(msg=msg, exc=exc_str) courses = modulestore.get_courses() n = len(courses) if n != 1: sys.stderr.write('ERROR: Expect exactly 1 course. Loaded {n}: {lst}\n'.format( n=n, lst=courses)) return None course = courses[0] errors = modulestore.get_course_errors(course.id) if len(errors) != 0: sys.stderr.write('ERRORs during import: {0}\n'.format('\n'.join(map(str_of_err, errors)))) return course
def test_unicode(self): """Check that courses with unicode characters in filenames and in org/course/name import properly. Currently, this means: (a) Having files with unicode names does not prevent import; (b) if files are not loaded because of unicode filenames, there are appropriate exceptions/errors to that effect.""" print "Starting import" modulestore = XMLModuleStore(DATA_DIR, source_dirs=['test_unicode']) courses = modulestore.get_courses() self.assertEquals(len(courses), 1) course = courses[0] print "course errors:" # Expect to find an error/exception about characters in "®esources" expect = "InvalidKeyError" errors = [ (msg.encode("utf-8"), err.encode("utf-8")) for msg, err in modulestore.get_course_errors(course.id) ] self.assertTrue(any( expect in msg or expect in err for msg, err in errors )) chapters = course.get_children() self.assertEqual(len(chapters), 4)
def import_with_checks(course_dir): all_ok = True print(u'Attempting to load "{}"'.format(course_dir)) course_dir = path(course_dir) data_dir = course_dir.dirname() source_dirs = [course_dir.basename()] # No default class--want to complain if it doesn't find plugins for any # module. modulestore = XMLModuleStore( data_dir, default_class=None, source_dirs=source_dirs ) def str_of_err(tpl): (msg, exc_str) = tpl return '{msg}\n{exc}'.format(msg=msg, exc=exc_str) courses = modulestore.get_courses() n = len(courses) if n != 1: print(u'ERROR: Expect exactly 1 course. Loaded {n}: {lst}'.format(n=n, lst=courses)) return (False, None) course = courses[0] errors = modulestore.get_course_errors(course.id) if len(errors) != 0: all_ok = False print( '\n' + '========================================' + 'ERRORs during import:' + '\n'.join(map(str_of_err, errors)) + '========================================' + '\n' ) # print course validators = ( traverse_tree, ) print('========================================') print('Running validators...') for validate in validators: print(u'Running {}'.format(validate.__name__)) all_ok = validate(course) and all_ok if all_ok: print('Course passes all checks!') else: print('Course fails some checks. See above for errors.') return all_ok, course
def import_with_checks(course_dir): all_ok = True print('Attempting to load "{}"'.format(course_dir)) course_dir = path(course_dir) data_dir = course_dir.dirname() source_dirs = [course_dir.basename()] # No default class--want to complain if it doesn't find plugins for any # module. modulestore = XMLModuleStore( data_dir, default_class=None, source_dirs=source_dirs ) def str_of_err(tpl): (msg, exc_str) = tpl return '{msg}\n{exc}'.format(msg=msg, exc=exc_str) courses = modulestore.get_courses() n = len(courses) if n != 1: print('ERROR: Expect exactly 1 course. Loaded {n}: {lst}'.format(n=n, lst=courses)) return (False, None) course = courses[0] errors = modulestore.get_course_errors(course.id) if len(errors) != 0: all_ok = False print( '\n' + '========================================' + 'ERRORs during import:' + '\n'.join(map(str_of_err, errors)) + '========================================' + '\n' ) # print course validators = ( traverse_tree, ) print('========================================') print('Running validators...') for validate in validators: print('Running {}'.format(validate.__name__)) all_ok = validate(course) and all_ok if all_ok: print('Course passes all checks!') else: print('Course fails some checks. See above for errors.') return all_ok, course
def test_unicode_chars_in_xml_content(self): # edX/full/6.002_Spring_2012 has non-ASCII chars, and during # uniquification of names, would raise a UnicodeError. It no longer does. # Ensure that there really is a non-ASCII character in the course. with open(os.path.join(DATA_DIR, "toy/sequential/vertical_sequential.xml")) as xmlf: xml = xmlf.read() with self.assertRaises(UnicodeDecodeError): xml.decode('ascii') # Load the course, but don't make error modules. This will succeed, # but will record the errors. modulestore = XMLModuleStore(DATA_DIR, course_dirs=['toy'], load_error_modules=False) # Look up the errors during load. There should be none. errors = modulestore.get_course_errors(SlashSeparatedCourseKey("edX", "toy", "2012_Fall")) assert errors == []
def test_unicode_chars_in_xml_content(self): # edX/full/6.002_Spring_2012 has non-ASCII chars, and during # uniquification of names, would raise a UnicodeError. It no longer does. # Ensure that there really is a non-ASCII character in the course. with open(os.path.join(DATA_DIR, "toy/sequential/vertical_sequential.xml")) as xmlf: xml = xmlf.read() with self.assertRaises(UnicodeDecodeError): xml.decode('ascii') # Load the course, but don't make error modules. This will succeed, # but will record the errors. modulestore = XMLModuleStore(DATA_DIR, course_dirs=['toy'], load_error_modules=False) # Look up the errors during load. There should be none. errors = modulestore.get_course_errors(SlashSeparatedCourseKey("edX", "toy", "2012_Fall")) assert errors == []