Ejemplo n.º 1
0
    def test_create(self):
        """Test the creating of the Document on the file system"""
        create_1_xml = """<?xml version="1.0" encoding="UTF-8"?>
<test/>"""
        d = structures.Document(root=NamedElement)
        d.set_base('create1.xml')
        d.create()
        try:
            f = open("create1.xml")
            data = f.read()
            f.close()
            self.assertTrue(data == create_1_xml, "create Test")
        except IOError:
            self.fail("create Test failed to create file")
Ejemplo n.º 2
0
 def test_id(self):
     """Test the built-in handling of a document's ID space."""
     doc = structures.Document()
     e1 = structures.Element(doc)
     e2 = structures.Element(doc)
     e1.id = e2.id = 'test'
     doc.register_element(e1)
     try:
         doc.register_element(e2)
         self.fail("Failed to spot ID clash")
     except structures.XMLIDClashError:
         pass
     e2.id = 'test2'
     doc.register_element(e2)
     self.assertTrue(
         doc.get_element_by_id('test') is e1, "Element look-up failed")
     new_id = doc.get_unique_id('test')
     self.assertFalse(new_id == 'test' or new_id == 'test2')
Ejemplo n.º 3
0
    def test_update(self):
        """Test the updating of the MXLDocument on the file system"""
        update_1_xml = """<?xml version="1.0" encoding="UTF-8"?>
<test>
\t<test/>
</test>"""
        d = structures.Document(root=NamedElement)
        d.set_base('update1.xml')
        try:
            d.update()
            self.fail("update Document failed to spot missing file")
        except structures.XMLMissingResourceError:
            pass
        d.create()
        d.root.add_child(NamedElement)
        d.update()
        try:
            f = open("update1.xml")
            data = f.read()
            f.close()
            self.assertTrue(data == update_1_xml, "update Test")
        except IOError:
            self.fail("update Test failed to update file")
Ejemplo n.º 4
0
 def test_resolve_base(self):
     """Test the use of resolve_uri and resolve_base"""
     os.chdir(TEST_DATA_DIR)
     parent = structures.Element(None)
     self.assertTrue(parent.resolve_base() is None, "No default base")
     child = structures.Element(parent)
     self.assertTrue(child.resolve_base() is None, "No xml:base by default")
     parent.set_base('file:///index.xml')
     self.assertTrue(child.resolve_base() == 'file:///index.xml',
                     "No xml:base inheritance")
     # Tests with a document follow....
     furl = str(uri.URI.from_path(os.path.abspath('base.xml')))
     href = uri.URI.from_path(os.path.abspath('link.xml'))
     href_path = href.abs_path
     href = str(href)
     alt_ref = 'file:///hello/link.xml'
     d = structures.Document(base_uri='base.xml')
     self.assertTrue(d.get_base() == furl,
                     "Base not resolved relative to w.d. by constructor")
     d.read()
     tag = d.root
     self.assertTrue(tag.resolve_base() == furl,
                     "Root element resolves from document")
     self.assertTrue(
         str(tag.resolve_uri("link.xml")) == href, "Root element HREF")
     self.assertTrue(
         str(tag.relative_uri(href)) == 'link.xml', "Root element relative")
     # self.assertTrue(
     #   tag.relative_uri(alt_ref)=='/hello/link.xml',
     #   'Root element full path relative: %s'%tag.relative_uri(alt_ref))
     child_tag = tag._children[0]
     self.assertTrue(
         child_tag.resolve_base() == "file:///hello/base.xml",
         "xml:base overrides in child_tag (%s)" % child_tag.resolve_base())
     self.assertTrue(
         str(child_tag.resolve_uri("link.xml")) == alt_ref,
         "child element HREF")
     self.assertTrue(
         str(child_tag.relative_uri(href)) == '..' + href_path,
         "child element relative resulting in full path: %s" %
         child_tag.relative_uri(href))
     self.assertTrue(
         str(child_tag.relative_uri(alt_ref)) == 'link.xml',
         'child element relative')
     # We require this next test to ensure that an href to the
     # current document comes up blank.  Although this was a major
     # source of bugs in browsers (<img src=''> causing infinite
     # loading loops) these are largely fixed now and obfuscating by
     # using a non-empty relative link to ourselves is likely to
     # start the whole thing going again.
     self.assertTrue(
         str(child_tag.relative_uri(child_tag.resolve_base())) == '',
         'child element relative avoiding empty URI(%s)' %
         child_tag.relative_uri(child_tag.resolve_base()))
     grandchild_tag = child_tag._children[0]
     self.assertTrue(
         grandchild_tag.resolve_base() == "file:///hello/base.xml",
         "xml:base inherited")
     self.assertTrue(
         str(grandchild_tag.resolve_uri("link.xml")) == alt_ref,
         "grandchild element HREF inherited")
     self.assertTrue(
         str(grandchild_tag.relative_uri(href)) == '..' + href_path,
         "grandchild element relative inherited: %s" %
         grandchild_tag.relative_uri(href))
     self.assertTrue(
         str(grandchild_tag.relative_uri(alt_ref)) == 'link.xml',
         'grandchild element relative inherited')
Ejemplo n.º 5
0
 def test_output_v2(self):
     if vobject is None:
         logging.warn(
             "QTI v1 to v2 migration tests skipped: vobject required")
         return
     self.cp.manifest.root.set_id('outputv2')
     dpath = os.path.join(self.dataPath, 'input')
     flist = []
     for f in os.listdir(dpath):
         if self.cp.IgnoreFile(f):
             continue
         stem, ext = os.path.splitext(f)
         if ext.lower() == '.xml':
             flist.append(f)
     flist.sort()
     for f in flist:
         doc = qtiv1.QTIDocument(
             base_uri=str(uri.URI.from_path(os.path.join(dpath, f))))
         doc.read()
         doc.migrate_to_v2(self.cp)
     # Having migrated everything in the input folder, we now check
     # our CP against the output
     cp2 = imscp.ContentPackage(os.path.join(self.dataPath, 'outputv2'))
     # To do....
     # Compare the manifests
     # Compare each file
     flist1 = list(dict_keys(self.cp.fileTable))
     flist1.sort()
     flist2 = list(dict_keys(cp2.fileTable))
     flist2.sort()
     if flist1 != flist2:
         diagnosis = []
         for f in flist1:
             if f not in flist2:
                 diagnosis.append("Extra file found: %s" % f)
         for f in flist2:
             if f not in flist1:
                 diagnosis.append("Missing file: %s" % f)
         self.fail("File lists:\n  %s" % '\n  '.join(diagnosis))
     logging.debug(str(self.cp.manifest))
     logging.debug(str(cp2.manifest))
     output = self.cp.manifest.diff_string(cp2.manifest)
     self.assertTrue(self.cp.manifest.root == cp2.manifest.root,
                     "Manifests differ:\n%s" % output)
     for r in cp2.manifest.root.Resources.Resource:
         # Check the entry-point of each resource
         f = r.GetEntryPoint()
         if f:
             fpath = f.PackagePath(cp2)
             qti_doc = qtiv2.QTIDocument(
                 base_uri=str(
                     uri.URI.from_virtual_path(self.cp.dPath.join(fpath))))
             qti_doc.read()
             # logging.debug(str(qti_doc))
             qti_doc2 = qtiv2.QTIDocument(
                 base_uri=str(
                     uri.URI.from_virtual_path(cp2.dPath.join(fpath))))
             qti_doc2.read()
             # logging.debug(str(qti_doc2))
             output = qti_doc.diff_string(qti_doc2)
             result = (qti_doc.root == qti_doc2.root)
             if not result and output is None:
                 # This should not happen
                 self.print_pretty_weird(qti_doc.root, qti_doc2.root)
             self.assertTrue(
                 qti_doc.root == qti_doc2.root,
                 "QTI Files differ at %s (actual output shown first)\n%s" %
                 (fpath, output))
         for f in r.File:
             if f.href is None or f.href.is_absolute():
                 continue
             fpath = f.PackagePath(cp2)
             fabs_path = self.cp.dPath.join(fpath)
             fabs_path2 = cp2.dPath.join(fpath)
             base_uri = str(uri.URI.from_virtual_path(fabs_path))
             base_uri2 = str(
                 uri.URI.from_virtual_path(fabs_path2))
             if fabs_path.splitext()[1].lower() == '.xml':
                 # Two xml files, compare with simple XMLElement
                 doc = xml.Document(base_uri=base_uri)
                 doc.read()
                 doc2 = xml.Document(base_uri=base_uri2)
                 doc2.read()
                 output = doc.diff_string(doc2)
                 result = (doc.root == doc2.root)
                 if not result and output is None:
                     # This should not happen
                     self.print_pretty_weird(doc.root, doc2.root)
                 self.assertTrue(
                     doc.root == doc2.root,
                     "XML Files differ at %s "
                     "(actual output shown first)\n%s" % (fpath, output))
             else:
                 # Binary compare the two files.
                 f = fabs_path.open('rb')
                 f2 = fabs_path2.open('rb')
                 while True:
                     fdata = f.read(1024)
                     fdata2 = f2.read(1024)
                     self.assertTrue(fdata == fdata2,
                                     "Binary files don't match: %s" % fpath)
                     if not fdata:
                         break