Exemplo n.º 1
0
    def test_export_and_keep_urls(self):
        """
        Test the changes to url_name after export_to_directory and import.
        """
        # Note url_name_orig in chapter.
        input_xml = input_data.URL_NAME_ORIG_IN_CHAPTER2
        bundle = XBundle(keep_urls=True, force_studio_format=True)
        bundle.load(file_from_string(input_xml))

        # str(bundle) doesn't change input xml, but export_to_directory will.
        self.assertEqual(clean_xml(input_xml), clean_xml(str(bundle)))

        old_current_dir = os.getcwd()
        tempdir = mkdtemp()
        try:
            os.chdir(tempdir)
            bundle.export_to_directory()

            bundle2 = XBundle(keep_urls=True, force_studio_format=True)
            bundle2.import_from_directory()

            expected = expected_data.KEEP_URLS_FORCE_STUDIO_FORMAT

            self.assertEqual(clean_xml(expected), clean_xml(str(bundle2)))
        finally:
            os.chdir(old_current_dir)
            rmtree(tempdir)
Exemplo n.º 2
0
    def test_export_import(self):
        """
        Test export then import.
        """
        bundle = XBundle()
        cxmls = input_data.COURSE

        pxmls = input_data.POLICIES

        bundle.set_course(etree.XML(cxmls))
        bundle.add_policies(etree.XML(pxmls))
        bundle.add_about_file("overview.html", "hello overview")

        xbin = str(bundle)

        tdir = mkdtemp()
        try:
            bundle.export_to_directory(tdir)

            # Test round- trip.
            xb2 = XBundle()
            xb2.import_from_directory(os.path.join(tdir, 'mitx.01'))

            xbreloaded = str(xb2)

            self.assertEqual(clean_xml(xbin), clean_xml(xbreloaded))
        finally:
            rmtree(tdir)
Exemplo n.º 3
0
    def test_add_descriptors(self):
        """
        Test add_descriptors.
        """
        # Note url_name_orig in chapter.
        input_xml = input_data.URL_NAME_ORIG_IN_CHAPTER1
        bundle = XBundle(keep_urls=True)
        bundle.load(file_from_string(input_xml))

        # str(bundle) doesn't change input xml, but export_to_directory will.
        self.assertEqual(clean_xml(input_xml), clean_xml(str(bundle)))

        old_current_dir = os.getcwd()
        tempdir = mkdtemp()
        try:
            os.chdir(tempdir)
            bundle.export_to_directory()

            bundle2 = XBundle(keep_urls=True)
            bundle2.import_from_directory()

            expected = expected_data.URL_NAME_ORIG

            self.assertEqual(clean_xml(expected), clean_xml(str(bundle2)))
        finally:
            os.chdir(old_current_dir)
            rmtree(tempdir)
Exemplo n.º 4
0
    def test_export_and_keep_urls(self):
        """
        Test the changes to url_name after export_to_directory and import.
        """
        # Note url_name_orig in chapter.
        input_xml = input_data.URL_NAME_ORIG_IN_CHAPTER2
        bundle = XBundle(keep_urls=True, force_studio_format=True)
        bundle.load(file_from_string(input_xml))

        # str(bundle) doesn't change input xml, but export_to_directory will.
        self.assertEqual(clean_xml(input_xml), clean_xml(str(bundle)))

        old_current_dir = os.getcwd()
        tempdir = mkdtemp()
        try:
            os.chdir(tempdir)
            bundle.export_to_directory()

            bundle2 = XBundle(keep_urls=True, force_studio_format=True)
            bundle2.import_from_directory()

            expected = expected_data.KEEP_URLS_FORCE_STUDIO_FORMAT

            self.assertEqual(clean_xml(expected), clean_xml(str(bundle2)))
        finally:
            os.chdir(old_current_dir)
            rmtree(tempdir)
Exemplo n.º 5
0
    def test_add_descriptors(self):
        """
        Test add_descriptors.
        """
        # Note url_name_orig in chapter.
        input_xml = input_data.URL_NAME_ORIG_IN_CHAPTER1
        bundle = XBundle(keep_urls=True)
        bundle.load(file_from_string(input_xml))

        # str(bundle) doesn't change input xml, but export_to_directory will.
        self.assertEqual(clean_xml(input_xml), clean_xml(str(bundle)))

        old_current_dir = os.getcwd()
        tempdir = mkdtemp()
        try:
            os.chdir(tempdir)
            bundle.export_to_directory()

            bundle2 = XBundle(keep_urls=True)
            bundle2.import_from_directory()

            expected = expected_data.URL_NAME_ORIG

            self.assertEqual(clean_xml(expected), clean_xml(str(bundle2)))
        finally:
            os.chdir(old_current_dir)
            rmtree(tempdir)
Exemplo n.º 6
0
    def test_export_import(self):
        """
        Test export then import.
        """
        bundle = XBundle()
        cxmls = input_data.COURSE

        pxmls = input_data.POLICIES

        bundle.set_course(etree.XML(cxmls))
        bundle.add_policies(etree.XML(pxmls))
        bundle.add_about_file("overview.html", "hello overview")

        xbin = str(bundle)

        tdir = mkdtemp()
        try:
            bundle.export_to_directory(tdir)

            # Test round- trip.
            xb2 = XBundle()
            xb2.import_from_directory(os.path.join(tdir, 'mitx.01'))

            xbreloaded = str(xb2)

            self.assertEqual(clean_xml(xbin), clean_xml(xbreloaded))
        finally:
            rmtree(tdir)
Exemplo n.º 7
0
    def test_fix_old_descriptor_name(self):
        """
        Test fix_old_descriptor_name.
        """
        bundle = XBundle()
        elem = etree.XML('<sequential name="abc" />')
        bundle.fix_old_descriptor_name(elem)

        expected = '<sequential display_name="abc" />'
        self.assertEqual(clean_xml(expected), clean_xml(etree.tostring(elem)))
Exemplo n.º 8
0
    def test_fix_old_descriptor_name(self):
        """
        Test fix_old_descriptor_name.
        """
        bundle = XBundle()
        elem = etree.XML('<sequential name="abc" />')
        bundle.fix_old_descriptor_name(elem)

        expected = '<sequential display_name="abc" />'
        self.assertEqual(clean_xml(expected), clean_xml(etree.tostring(elem)))
Exemplo n.º 9
0
    def test_import_url_name(self):
        """
        Test that we import url_name as url_name_orig.
        """
        bundle = XBundle(keep_urls=True, keep_studio_urls=True)
        bundle.import_from_directory(os.path.join('input_testdata', 'mitx.01'))

        bundle_string = str(bundle)

        expected = expected_data.KEEP_URLS
        self.assertEqual(clean_xml(expected), clean_xml(bundle_string))
Exemplo n.º 10
0
    def test_xml_header(self):
        """
        Test removal of xml header. The <?xml ... should not show up in the
        output and the XML should still be parsed correctly.
        """
        input_xml = input_data.EMPTY_XBUNDLE

        bundle = XBundle()
        bundle.load(file_from_string(input_xml))
        self.assertFalse(str(bundle).startswith("<?xml"))
        self.assertEqual(clean_xml(input_xml), clean_xml(str(bundle)))
Exemplo n.º 11
0
    def test_import_skip_hidden(self):
        """
        Test skip_hidden flag.
        """
        bundle = XBundle(skip_hidden=True)
        path = os.path.join('input_testdata', 'mitx.01')
        bundle.import_from_directory(path)

        expected = expected_data.SKIP_HIDDEN

        self.assertEqual(clean_xml(str(bundle)), clean_xml(expected))
Exemplo n.º 12
0
    def test_xml_header(self):
        """
        Test removal of xml header. The <?xml ... should not show up in the
        output and the XML should still be parsed correctly.
        """
        input_xml = input_data.EMPTY_XBUNDLE

        bundle = XBundle()
        bundle.load(file_from_string(input_xml))
        self.assertFalse(str(bundle).startswith("<?xml"))
        self.assertEqual(clean_xml(input_xml), clean_xml(str(bundle)))
Exemplo n.º 13
0
    def test_fix_old_course_section(self):
        """
        Test fix_old_course_section.
        """
        bundle = XBundle()
        bundle.import_from_directory(
            os.path.join("input_testdata", "sections"))

        # Section element should be removed.
        expected = expected_data.MISSING_SECTION
        self.assertEqual(clean_xml(expected), clean_xml(str(bundle)))
Exemplo n.º 14
0
    def test_import_skip_hidden(self):
        """
        Test skip_hidden flag.
        """
        bundle = XBundle(skip_hidden=True)
        path = os.path.join('input_testdata', 'mitx.01')
        bundle.import_from_directory(path)

        expected = expected_data.SKIP_HIDDEN

        self.assertEqual(clean_xml(str(bundle)), clean_xml(expected))
Exemplo n.º 15
0
    def test_import_url_name(self):
        """
        Test that we import url_name as url_name_orig.
        """
        bundle = XBundle(keep_urls=True, keep_studio_urls=True)
        bundle.import_from_directory(os.path.join('input_testdata', 'mitx.01'))

        bundle_string = str(bundle)

        expected = expected_data.KEEP_URLS
        self.assertEqual(clean_xml(expected), clean_xml(bundle_string))
Exemplo n.º 16
0
    def test_fix_old_course_section(self):
        """
        Test fix_old_course_section.
        """
        bundle = XBundle()
        bundle.import_from_directory(os.path.join("input_testdata",
                                                  "sections"))

        # Section element should be removed.
        expected = expected_data.MISSING_SECTION
        self.assertEqual(clean_xml(expected), clean_xml(str(bundle)))
Exemplo n.º 17
0
    def test_preserve_url_name(self):
        """
        Test that preserve_url_name imports as url_name and not url_name_orig.
        """
        bundle = XBundle(
            keep_urls=True, keep_studio_urls=True, preserve_url_name=True)
        bundle.import_from_directory('input_testdata/mitx.01')

        bundle_string = str(bundle)

        expected = expected_data.PRESERVE_URL_NAME
        self.assertEqual(clean_xml(expected), clean_xml(bundle_string))
Exemplo n.º 18
0
    def test_preserve_url_name(self):
        """
        Test that preserve_url_name imports as url_name and not url_name_orig.
        """
        bundle = XBundle(keep_urls=True,
                         keep_studio_urls=True,
                         preserve_url_name=True)
        bundle.import_from_directory('input_testdata/mitx.01')

        bundle_string = str(bundle)

        expected = expected_data.PRESERVE_URL_NAME
        self.assertEqual(clean_xml(expected), clean_xml(bundle_string))
Exemplo n.º 19
0
    def test_save(self):
        """
        Test save method.
        """

        input_xml = "<xbundle><metadata /><course /></xbundle>"
        bundle = XBundle()
        bundle.load(file_from_string(input_xml))
        self.assertEqual(clean_xml(str(bundle)), clean_xml(input_xml))

        curdir = os.getcwd()
        tempdir = mkdtemp()
        try:
            os.chdir(tempdir)
            bundle.save()

            with open(os.path.join(tempdir, "xbundle.xml")) as f:
                self.assertEqual(clean_xml(f.read()), clean_xml(input_xml))

            bundle.save(filename="other.xml")
            with open(os.path.join(tempdir, "other.xml")) as f:
                self.assertEqual(clean_xml(f.read()), clean_xml(input_xml))

            handle_path = os.path.join(tempdir, "third.xml")
            with open(handle_path, "w") as f:
                bundle.save(file_handle=f)
            with open(handle_path) as f:
                self.assertEqual(clean_xml(f.read()), clean_xml(input_xml))
        finally:
            os.chdir(curdir)
            rmtree(tempdir)
Exemplo n.º 20
0
    def test_save(self):
        """
        Test save method.
        """

        input_xml = "<xbundle><metadata /><course /></xbundle>"
        bundle = XBundle()
        bundle.load(file_from_string(input_xml))
        self.assertEqual(clean_xml(str(bundle)), clean_xml(input_xml))

        curdir = os.getcwd()
        tempdir = mkdtemp()
        try:
            os.chdir(tempdir)
            bundle.save()

            with open(os.path.join(tempdir, "xbundle.xml")) as f:
                self.assertEqual(clean_xml(f.read()), clean_xml(input_xml))

            bundle.save(filename="other.xml")
            with open(os.path.join(tempdir, "other.xml")) as f:
                self.assertEqual(clean_xml(f.read()), clean_xml(input_xml))

            handle_path = os.path.join(tempdir, "third.xml")
            with open(handle_path, "w") as f:
                bundle.save(file_handle=f)
            with open(handle_path) as f:
                self.assertEqual(clean_xml(f.read()), clean_xml(input_xml))
        finally:
            os.chdir(curdir)
            rmtree(tempdir)
Exemplo n.º 21
0
    def test_unicode_in_html(self):
        """
        Test that unicode doesn't cause problems in overview file.
        """
        bundle = XBundle()
        bundle.import_from_directory(os.path.join("input_testdata", "mitx.01"))
        bundle.add_about_file("overview.html", "\u2e18 interrobang \u203d")

        expected = expected_data.ESCAPED_UNICODE
        self.assertEqual(clean_xml(str(bundle)), clean_xml(expected))

        # Reimport to start from a clean slate. This time use bytes.
        bundle = XBundle()
        bundle.import_from_directory(os.path.join("input_testdata", "mitx.01"))

        bundle.add_about_file("overview.html",
                              "\u2e18 interrobang \u203d".encode('utf-8'))
        self.assertEqual(clean_xml(str(bundle)), clean_xml(expected))
Exemplo n.º 22
0
    def test_unicode_in_html(self):
        """
        Test that unicode doesn't cause problems in overview file.
        """
        bundle = XBundle()
        bundle.import_from_directory(os.path.join("input_testdata", "mitx.01"))
        bundle.add_about_file("overview.html",
                              "\u2e18 interrobang \u203d")

        expected = expected_data.ESCAPED_UNICODE
        self.assertEqual(clean_xml(str(bundle)), clean_xml(expected))

        # Reimport to start from a clean slate. This time use bytes.
        bundle = XBundle()
        bundle.import_from_directory(os.path.join("input_testdata", "mitx.01"))

        bundle.add_about_file(
            "overview.html", "\u2e18 interrobang \u203d".encode('utf-8'))
        self.assertEqual(clean_xml(str(bundle)), clean_xml(expected))
Exemplo n.º 23
0
def _normalize_xml(dirname):
    """Removes whitespace from xml files in the given directory."""
    for dname, _, files in os.walk(dirname):
        for fname in files:
            fpath = os.path.join(dname, fname)
            if not fpath.endswith('.xml'):
                continue
            with open(fpath) as f:
                s = f.read()
                s = clean_xml(s)
            with open(fpath, 'w') as f:
                f.write(s)
Exemplo n.º 24
0
def _normalize_xml(dirname):
    """Removes whitespace from xml files in the given directory."""
    for dname, _, files in os.walk(dirname):
        for fname in files:
            fpath = os.path.join(dname, fname)
            if not fpath.endswith('.xml'):
                continue
            with open(fpath) as f:
                s = f.read()
                s = clean_xml(s)
            with open(fpath, 'w') as f:
                f.write(s)
Exemplo n.º 25
0
    def test_set_course(self):
        """
        Test functionality of set_course.
        """
        input_xml = input_data.EMPTY_COURSE

        bundle = XBundle(keep_urls=True)
        bundle.load(file_from_string(input_xml))

        # No org or semester is specified in XML above.
        self.assertEqual(bundle.course.get("org"), None)
        self.assertEqual(bundle.course.get("semester"), None)
        self.assertEqual(bundle.semester, "")

        # Note lack of org attribute and url_name for course element.
        course_str = input_data.NO_COURSE
        with self.assertRaises(Exception) as ex:
            bundle.set_course(etree.XML("<x>" + course_str + "</x>"))
        self.assertTrue(
            "set_course should be called with a <course> element"
            in ex.exception.args)

        with self.assertRaises(Exception) as ex:
            bundle.set_course(etree.XML("<course />"))
        self.assertTrue("No semester found." in ex.exception.args)

        bundle.set_course(etree.XML("<course url_name='x' />"))
        self.assertEqual(bundle.semester, "x")

        bundle.set_course(etree.XML(course_str))

        # MITx is not present in data, it is automatically set.
        self.assertEqual(bundle.course.get("org"), "MITx")
        self.assertEqual(bundle.course.get("semester"), "2013_Spring")
        self.assertEqual(bundle.semester, "2013_Spring")

        bundle_string = str(bundle)

        expected = expected_data.SET_COURSE
        self.assertEqual(clean_xml(bundle_string), clean_xml(expected))
Exemplo n.º 26
0
    def test_set_course(self):
        """
        Test functionality of set_course.
        """
        input_xml = input_data.EMPTY_COURSE

        bundle = XBundle(keep_urls=True)
        bundle.load(file_from_string(input_xml))

        # No org or semester is specified in XML above.
        self.assertEqual(bundle.course.get("org"), None)
        self.assertEqual(bundle.course.get("semester"), None)
        self.assertEqual(bundle.semester, "")

        # Note lack of org attribute and url_name for course element.
        course_str = input_data.NO_COURSE
        with self.assertRaises(Exception) as ex:
            bundle.set_course(etree.XML("<x>" + course_str + "</x>"))
        self.assertTrue("set_course should be called with a <course> element"
                        in ex.exception.args)

        with self.assertRaises(Exception) as ex:
            bundle.set_course(etree.XML("<course />"))
        self.assertTrue("No semester found." in ex.exception.args)

        bundle.set_course(etree.XML("<course url_name='x' />"))
        self.assertEqual(bundle.semester, "x")

        bundle.set_course(etree.XML(course_str))

        # MITx is not present in data, it is automatically set.
        self.assertEqual(bundle.course.get("org"), "MITx")
        self.assertEqual(bundle.course.get("semester"), "2013_Spring")
        self.assertEqual(bundle.semester, "2013_Spring")

        bundle_string = str(bundle)

        expected = expected_data.SET_COURSE
        self.assertEqual(clean_xml(bundle_string), clean_xml(expected))
Exemplo n.º 27
0
    def test_import_large(self):
        """
        Test import of a course slightly larger than mitx.01.
        """
        bundle = XBundle()
        path = os.path.join('input_testdata', 'content-devops-0001')
        bundle.import_from_directory(path)

        expected_path = os.path.join(
            'input_testdata', 'content-devops-0001.out.xml')
        with open(expected_path) as f:
            self.assertEqual(clean_xml(f.read()), clean_xml(str(bundle)))

        tempdir = mkdtemp()
        try:
            bundle.export_to_directory(tempdir, xml_only=True, newfmt=True)

            for _, _, files in os.walk(os.path.join(tempdir, "0.001")):
                for filename in files:
                    # We set xml_only=True so there shouldn't be anything else.
                    self.assertTrue(filename.endswith(".xml"))
        finally:
            rmtree(tempdir)
Exemplo n.º 28
0
    def test_import_large(self):
        """
        Test import of a course slightly larger than mitx.01.
        """
        bundle = XBundle()
        path = os.path.join('input_testdata', 'content-devops-0001')
        bundle.import_from_directory(path)

        expected_path = os.path.join('input_testdata',
                                     'content-devops-0001.out.xml')
        with open(expected_path) as f:
            self.assertEqual(clean_xml(f.read()), clean_xml(str(bundle)))

        tempdir = mkdtemp()
        try:
            bundle.export_to_directory(tempdir, xml_only=True, newfmt=True)

            for _, _, files in os.walk(os.path.join(tempdir, "0.001")):
                for filename in files:
                    # We set xml_only=True so there shouldn't be anything else.
                    self.assertTrue(filename.endswith(".xml"))
        finally:
            rmtree(tempdir)