Esempio n. 1
0
 def test__load_raises_on_missing_rels_item(self):
     """Part._load() raises on missing rels item"""
     # setup -----------------------
     path = absjoin(test_file_dir, "missing_rels_item.pptx")
     pkg = Package()
     # verify ----------------------
     with self.assertRaises(CorruptedPackageError):
         pkg.open(path)
Esempio n. 2
0
 def test_save_accepts_stream(self):
     """Package.save() can write to a file-like object"""
     # setup -----------------------
     pkg = Package().open(dir_pkg_path)
     stream = StringIO()
     # exercise --------------------
     pkg.save(stream)
     # verify ----------------------
     # can't use is_zipfile() directly on stream in Python 2.6
     stream.seek(0)
     with open(test_save_pptx_path, "wb") as f:
         f.write(stream.read())
     actual = is_zipfile(test_save_pptx_path)
     msg = "Package.save(stream) did not create zipfile"
     self.assertTrue(actual, msg)
Esempio n. 3
0
 def setUp(self):
     self.pkg = Package()
Esempio n. 4
0
class TestPackage(TestCase):
    """Test Package"""

    def setUp(self):
        self.pkg = Package()

    def tearDown(self):
        if os.path.isfile(test_save_pptx_path):
            os.remove(test_save_pptx_path)

    def test_marshal_returns_self(self):
        """Package.marshal() returns self-reference"""
        # setup -----------------------
        model_pkg = pptx.presentation._Package(test_pptx_path)
        # exercise --------------------
        retval = self.pkg.marshal(model_pkg)
        # verify ----------------------
        expected = self.pkg
        actual = retval
        msg = "expected '%s', got '%s'" % (expected, actual)
        self.assertEqual(expected, actual, msg)

    def test_open_returns_self(self):
        """Package.open() returns self-reference"""
        for file in (dir_pkg_path, zip_pkg_path, open(zip_pkg_path)):
            # exercise ----------------
            retval = self.pkg.open(file)
            # verify ------------------
            expected = self.pkg
            actual = retval
            msg = "expected '%s', got '%s'" % (expected, actual)
            self.assertEqual(expected, actual, msg)

    def test_open_part_count(self):
        """Package.open() produces expected part count"""
        # exercise --------------------
        self.pkg.open(zip_pkg_path)
        # verify ----------------------
        self.assertLength(self.pkg.parts, 22)

    def test_open_populates_target_part(self):
        """Part.open() populates Relationship.target"""
        # setup -----------------------
        cls = Part
        # exercise --------------------
        self.pkg.open(zip_pkg_path)
        # verify ----------------------
        for rel in self.pkg.relationships:
            obj = rel.target
            self.assertIsInstance(obj, cls)
        for part in self.pkg.parts:
            for rel in part.relationships:
                obj = rel.target
                self.assertIsInstance(obj, cls)

    # def test_blob_correct_length_after_load_binary_part(self):
    #     """Part.blob correct length after load binary part"""
    #     # setup -----------------------
    #     partname = '/docProps/thumbnail.jpeg'
    #     content_type = 'image/jpeg'
    #     self.part.load(self.fs, partname, content_type)
    #     # exercise --------------------
    #     blob = self.part.blob
    #     # verify ----------------------
    #     self.assertLength(blob, 8147)
    #
    # def test__rels_element_correct_xml(self):
    #     BOTH PKG AND PARTS, MAYBE USE TEST FILES
    #     """RelationshipsItem.element produces expected XML"""
    #     # setup -----------------------
    #     ns = 'http://schemas.openxmlformats.org/package/2006/relationships'
    #     exp_xml = '<Relationships xmlns="%s"/>' % ns
    #     # exercise --------------------
    #     elm = self.ri.element
    #     # verify ----------------------
    #     xml_out = etree.tostring(elm)
    #     expected = exp_xml
    #     actual = xml_out
    #     msg = "expected '%s'\n%sgot '%s'" % (expected, ' '*21, actual)
    #     self.assertEqual(expected, actual, msg)
    #
    # def test_typespec_correct_after_load(self):
    #     """Part.typespec is correct after load"""
    #     # setup -----------------------
    #     self.part.load(self.fs, self.partname, self.content_type)
    #     # exercise --------------------
    #     typespec = self.part.typespec
    #     test_cases =\
    #         ( (typespec.basename    , 'slideMaster')
    #         , (typespec.cardinality , 'multiple'   )
    #         , (typespec.has_rels    , 'always'     )
    #         , (typespec.format      , 'xml'        )
    #         )
    #     # verify ----------------------
    #     for actual, expected in test_cases:
    #         msg = "expected '%s', got '%s'" % (expected, actual)
    #         self.assertEqual(expected, actual, msg)

    def test_parts_property_empty_on_construction(self):
        """Package.parts property empty on construction"""
        # verify ----------------------
        self.assertIsSizedProperty(self.pkg, "parts", 0)

    def test_relationships_property_empty_on_construction(self):
        """Package.relationships property empty on construction"""
        # verify ----------------------
        self.assertIsSizedProperty(self.pkg, "relationships", 0)

    def test_relationships_correct_length_after_open(self):
        """Package.relationships correct length after open()"""
        # exercise --------------------
        self.pkg.open(zip_pkg_path)
        # verify ----------------------
        self.assertLength(self.pkg.relationships, 4)

    def test_relationships_discarded_before_open(self):
        """Package.relationships discards rels before second open()"""
        # setup -----------------------
        self.pkg.open(zip_pkg_path)
        # exercise --------------------
        self.pkg.open(dir_pkg_path)
        # verify ----------------------
        self.assertLength(self.pkg.relationships, 4)

    def test_save_accepts_stream(self):
        """Package.save() can write to a file-like object"""
        # setup -----------------------
        pkg = Package().open(dir_pkg_path)
        stream = StringIO()
        # exercise --------------------
        pkg.save(stream)
        # verify ----------------------
        # can't use is_zipfile() directly on stream in Python 2.6
        stream.seek(0)
        with open(test_save_pptx_path, "wb") as f:
            f.write(stream.read())
        actual = is_zipfile(test_save_pptx_path)
        msg = "Package.save(stream) did not create zipfile"
        self.assertTrue(actual, msg)

    def test_save_writes_pptx_zipfile(self):
        """Package.save(path) writes .pptx file"""
        # setup -----------------------
        self.pkg.open(dir_pkg_path)
        save_path = test_save_pptx_path
        # exercise --------------------
        self.pkg.save(save_path)
        # verify ----------------------
        actual = is_zipfile(save_path)
        msg = "no zipfile at %s" % (save_path)
        self.assertTrue(actual, msg)

    def test_save_member_count(self):
        """Package.save() produces expected zip member count"""
        # setup -----------------------
        self.pkg.open(dir_pkg_path)
        save_path = test_save_pptx_path
        # exercise --------------------
        self.pkg.save(save_path)
        # verify ----------------------
        zip = ZipFile(save_path)
        names = zip.namelist()
        zip.close()
        partnames = [name for name in names if not name.endswith("/")]
        self.assertLength(partnames, 38)