Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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)))
Ejemplo n.º 8
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)))
Ejemplo n.º 9
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))
Ejemplo n.º 10
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))