Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
pblottiere committed Jul 31, 2017
1 parent 7c85b20 commit 33247cc
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -171,6 +171,7 @@ ADD_PYTHON_TEST(PyQgsVersionCompare test_versioncompare.py)
ADD_PYTHON_TEST(PyQgsDBManagerGpkg test_db_manager_gpkg.py)
ADD_PYTHON_TEST(PyQgsFileDownloader test_qgsfiledownloader.py)
ADD_PYTHON_TEST(PyQgsSettings test_qgssettings.py)
ADD_PYTHON_TEST(PyQgsZipUtils test_qgsziputils.py)

IF (NOT WIN32)
ADD_PYTHON_TEST(PyQgsLogger test_qgslogger.py)
Expand Down
127 changes: 127 additions & 0 deletions tests/src/python/test_qgsziputils.py
@@ -0,0 +1,127 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for zip functions.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Paul Blottiere'
__date__ = '06/7/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
from qgis.core import QgsZipUtils
from qgis.testing import unittest
from utilities import unitTestDataPath
from qgis.PyQt.QtCore import QTemporaryFile, QTemporaryDir


def tmpPath():
f = QTemporaryFile()
f.open()
f.close()
os.remove(f.fileName())

return f.fileName()


class TestQgsZip(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.zipDir = os.path.join(unitTestDataPath(), "zip")

def test_zip_ok(self):
f0 = os.path.join(unitTestDataPath(), 'multipoint.shp')
f1 = os.path.join(unitTestDataPath(), 'lines.shp')
f2 = os.path.join(unitTestDataPath(), 'joins.qgs')

rc = QgsZipUtils.zip(tmpPath(), [f0, f1, f2])
self.assertTrue(rc)

def test_zip_file_yet_exist(self):
zip = QTemporaryFile()
zip.open()
zip.close()
os.remove(zip.fileName())

f0 = os.path.join(unitTestDataPath(), 'multipoint.shp')
f1 = os.path.join(unitTestDataPath(), 'lines.shp')
f2 = os.path.join(unitTestDataPath(), 'joins.qgs')

rc = QgsZipUtils.zip(zip.fileName(), [f0, f1, f2])
self.assertTrue(rc)

rc = QgsZipUtils.zip(zip.fileName(), [f0, f1, f2])
self.assertFalse(rc)

def test_zip_file_empty(self):
f0 = os.path.join(unitTestDataPath(), 'multipoint.shp')
f1 = os.path.join(unitTestDataPath(), 'lines.shp')
f2 = os.path.join(unitTestDataPath(), 'joins.qgs')

rc = QgsZipUtils.zip("", [f0, f1, f2])
self.assertFalse(rc)

def test_zip_input_file_not_exist(self):
f0 = os.path.join(unitTestDataPath(), 'multipoint.shp')
f1 = os.path.join(unitTestDataPath(), 'fake.shp')
f2 = os.path.join(unitTestDataPath(), 'joins.qgs')

rc = QgsZipUtils.zip(tmpPath(), [f0, f1, f2])
self.assertFalse(rc)

def test_unzip_ok(self):
outDir = QTemporaryDir()

zip = os.path.join(self.zipDir, 'testzip.zip')
rc, files = QgsZipUtils.unzip(zip, outDir.path())

self.assertTrue(rc)
self.assertEqual(len(files), 11)

def test_unzip_file_not_exist(self):
outDir = QTemporaryDir()

zip = os.path.join(self.zipDir, 'fake.zip')
rc, files = QgsZipUtils.unzip(zip, outDir.path())

self.assertFalse(rc)

def test_unzip_file_empty(self):
outDir = QTemporaryDir()
rc, files = QgsZipUtils.unzip("", outDir.path())
self.assertFalse(rc)

def test_unzip_dir_not_exist(self):
zip = os.path.join(self.zipDir, 'testzip.zip')
rc, files = QgsZipUtils.unzip(zip, '/tmp/fake')
self.assertFalse(rc)

def test_unzip_dir_empty(self):
zip = os.path.join(self.zipDir, 'testzip.zip')
rc, files = QgsZipUtils.unzip(zip, '')
self.assertFalse(rc)

def test_zip_unzip_ok(self):
zip = tmpPath()

f0 = os.path.join(unitTestDataPath(), 'multipoint.shp')
f1 = os.path.join(unitTestDataPath(), 'lines.shp')
f2 = os.path.join(unitTestDataPath(), 'joins.qgs')

rc = QgsZipUtils.zip(zip, [f0, f1, f2])
self.assertTrue(rc)

outDir = QTemporaryDir()
rc, files = QgsZipUtils.unzip(zip, outDir.path())

self.assertTrue(rc)
self.assertEqual(len(files), 3)


if __name__ == '__main__':
unittest.main()

0 comments on commit 33247cc

Please sign in to comment.