diff options
Diffstat (limited to 'silx/test')
-rw-r--r-- | silx/test/__init__.py | 6 | ||||
-rw-r--r-- | silx/test/test_resources.py | 212 | ||||
-rw-r--r-- | silx/test/utils.py | 10 |
3 files changed, 202 insertions, 26 deletions
diff --git a/silx/test/__init__.py b/silx/test/__init__.py index 01ee8d1..0a85fe2 100644 --- a/silx/test/__init__.py +++ b/silx/test/__init__.py @@ -32,7 +32,7 @@ It will skip all tests from :mod:`silx.test.gui`. __authors__ = ["T. Vincent"] __license__ = "MIT" -__date__ = "20/04/2017" +__date__ = "22/06/2017" import logging @@ -53,6 +53,7 @@ def suite(): from ..gui import test as test_gui from ..utils import test as test_utils from ..opencl import test as test_ocl + from ..app import test as test_app test_suite = unittest.TestSuite() # test sx first cause qui tests load ipython module test_suite.addTest(test_sx.suite()) @@ -61,11 +62,12 @@ def suite(): test_suite.addTest(test_utils.suite()) test_suite.addTest(test_version.suite()) test_suite.addTest(test_resources.suite()) - test_suite.addTest(test_utils.suite()) test_suite.addTest(test_io.suite()) test_suite.addTest(test_math.suite()) test_suite.addTest(test_image.suite()) test_suite.addTest(test_ocl.suite()) + test_suite.addTest(test_app.suite()) + return test_suite diff --git a/silx/test/test_resources.py b/silx/test/test_resources.py index 9d3e277..eaf65c1 100644 --- a/silx/test/test_resources.py +++ b/silx/test/test_resources.py @@ -26,17 +26,52 @@ __authors__ = ["T. Vincent"] __license__ = "MIT" -__date__ = "20/04/2017" +__date__ = "06/09/2017" import os import unittest +import shutil +import tempfile +from silx.third_party import six import silx.resources from .utils import utilstest +import socket class TestResources(unittest.TestCase): + + @classmethod + def setUpClass(cls): + super(TestResources, cls).setUpClass() + + cls.tmpDirectory = tempfile.mkdtemp(prefix="resource_") + os.mkdir(os.path.join(cls.tmpDirectory, "gui")) + destination_dir = os.path.join(cls.tmpDirectory, "gui", "icons") + os.mkdir(destination_dir) + source = silx.resources.resource_filename("gui/icons/zoom-in.png") + destination = os.path.join(destination_dir, "foo.png") + shutil.copy(source, destination) + source = silx.resources.resource_filename("gui/icons/zoom-out.svg") + destination = os.path.join(destination_dir, "close.png") + shutil.copy(source, destination) + + @classmethod + def tearDownClass(cls): + super(TestResources, cls).tearDownClass() + shutil.rmtree(cls.tmpDirectory) + + def setUp(self): + # Store the original configuration + self._oldResources = dict(silx.resources._RESOURCE_DIRECTORIES) + unittest.TestCase.setUp(self) + + def tearDown(self): + unittest.TestCase.tearDown(self) + # Restiture the original configuration + silx.resources._RESOURCE_DIRECTORIES = self._oldResources + def test_resource_dir(self): """Get a resource directory""" icons_dirname = silx.resources.resource_filename('gui/icons/') @@ -52,44 +87,179 @@ class TestResources(unittest.TestCase): filename = silx.resources.resource_filename('non_existent_file.txt') self.assertFalse(os.path.exists(filename)) + def test_isdir(self): + self.assertTrue(silx.resources.is_dir('gui/icons')) + + def test_not_isdir(self): + self.assertFalse(silx.resources.is_dir('gui/icons/colormap.png')) + + def test_list_dir(self): + result = silx.resources.list_dir('gui/icons') + self.assertTrue(len(result) > 10) + + # With prefixed resources + + def test_resource_dir_with_prefix(self): + """Get a resource directory""" + icons_dirname = silx.resources.resource_filename('silx:gui/icons/') + self.assertTrue(os.path.isdir(icons_dirname)) + + def test_resource_file_with_prefix(self): + """Get a resource file name""" + filename = silx.resources.resource_filename('silx:gui/icons/colormap.png') + self.assertTrue(os.path.isfile(filename)) + + def test_resource_nonexistent_with_prefix(self): + """Get a non existent resource""" + filename = silx.resources.resource_filename('silx:non_existent_file.txt') + self.assertFalse(os.path.exists(filename)) + + def test_isdir_with_prefix(self): + self.assertTrue(silx.resources.is_dir('silx:gui/icons')) + + def test_not_isdir_with_prefix(self): + self.assertFalse(silx.resources.is_dir('silx:gui/icons/colormap.png')) + + def test_list_dir_with_prefix(self): + result = silx.resources.list_dir('silx:gui/icons') + self.assertTrue(len(result) > 10) + + # Test new repository + + def test_repository_not_exists(self): + """The resource from 'test' is available""" + self.assertRaises(ValueError, silx.resources.resource_filename, 'test:foo.png') + + def test_adding_test_directory(self): + """The resource from 'test' is available""" + silx.resources.register_resource_directory("test", "silx.test.resources", forced_path=self.tmpDirectory) + path = silx.resources.resource_filename('test:gui/icons/foo.png') + self.assertTrue(os.path.exists(path)) + + def test_adding_test_directory_no_override(self): + """The resource from 'silx' is still available""" + silx.resources.register_resource_directory("test", "silx.test.resources", forced_path=self.tmpDirectory) + filename1 = silx.resources.resource_filename('gui/icons/close.png') + filename2 = silx.resources.resource_filename('silx:gui/icons/close.png') + filename3 = silx.resources.resource_filename('test:gui/icons/close.png') + self.assertTrue(os.path.isfile(filename1)) + self.assertTrue(os.path.isfile(filename2)) + self.assertTrue(os.path.isfile(filename3)) + self.assertEqual(filename1, filename2) + self.assertNotEqual(filename1, filename3) + + def test_adding_test_directory_non_existing(self): + """A resource while not exists in test is not available anyway it exists + in silx""" + silx.resources.register_resource_directory("test", "silx.test.resources", forced_path=self.tmpDirectory) + resource_name = "gui/icons/colormap.png" + path = silx.resources.resource_filename('test:' + resource_name) + path2 = silx.resources.resource_filename('silx:' + resource_name) + self.assertFalse(os.path.exists(path)) + self.assertTrue(os.path.exists(path2)) + + +class TestResourcesWithoutPkgResources(TestResources): + + @classmethod + def setUpClass(cls): + super(TestResourcesWithoutPkgResources, cls).setUpClass() + cls._old = silx.resources.pkg_resources + silx.resources.pkg_resources = None + + @classmethod + def tearDownClass(cls): + silx.resources.pkg_resources = cls._old + del cls._old + super(TestResourcesWithoutPkgResources, cls).tearDownClass() + + +class TestResourcesWithCustomDirectory(TestResources): + + @classmethod + def setUpClass(cls): + super(TestResourcesWithCustomDirectory, cls).setUpClass() + cls._old = silx.resources._RESOURCES_DIR + base = os.path.dirname(silx.resources.__file__) + silx.resources._RESOURCES_DIR = base + + @classmethod + def tearDownClass(cls): + silx.resources._RESOURCES_DIR = cls._old + del cls._old + super(TestResourcesWithCustomDirectory, cls).tearDownClass() + + +def isSilxWebsiteAvailable(): + try: + six.moves.urllib.request.urlopen('http://www.silx.org', timeout=1) + return True + except six.moves.urllib.error.URLError: + return False + except socket.timeout: + # This exception is still received in Python 2.7 + return False + class TestExternalResources(unittest.TestCase): - "This is a test for the TestResources" + """This is a test for the ExternalResources""" + + @classmethod + def setUpClass(cls): + if not isSilxWebsiteAvailable(): + raise unittest.SkipTest("Network or silx website not available") + + def setUp(self): + self.utilstest = silx.resources.ExternalResources("toto", "http://www.silx.org/pub/silx/") + + def tearDown(self): + if self.utilstest.data_home: + shutil.rmtree(self.utilstest.data_home) + self.utilstest = None + def test_tempdir(self): "test the temporary directory creation" - myutilstest = silx.resources.ExternalResources("toto", "http://www.silx.org") - d = myutilstest.tempdir + d = self.utilstest.tempdir self.assertTrue(os.path.isdir(d)) - self.assertEqual(d, myutilstest.tempdir, 'tmpdir is stable') - myutilstest.clean_up() + self.assertEqual(d, self.utilstest.tempdir, 'tmpdir is stable') + self.utilstest.clean_up() self.assertFalse(os.path.isdir(d)) - e = myutilstest.tempdir + e = self.utilstest.tempdir self.assertTrue(os.path.isdir(e)) - self.assertEqual(e, myutilstest.tempdir, 'tmpdir is stable') + self.assertEqual(e, self.utilstest.tempdir, 'tmpdir is stable') self.assertNotEqual(d, e, "tempdir changed") - myutilstest.clean_up() + self.utilstest.clean_up() def test_download(self): "test the download from silx.org" - f = utilstest.getfile("lena.png") + f = self.utilstest.getfile("lena.png") self.assertTrue(os.path.exists(f)) - f = utilstest.getdir("source.tar.gz") - self.assertTrue(os.path.isfile(f)) - self.assertTrue(os.path.isdir(f[:-7])) + di = utilstest.getdir("source.tar.gz") + for fi in di: + self.assertTrue(os.path.exists(fi)) - def test_dowload_all(self): + def test_download_all(self): "test the download of all files from silx.org" - l = utilstest.download_all() - self.assertGreater(len(l), 1, "At least 2 items were downloaded") + filename = self.utilstest.getfile("lena.png") + directory = "source.tar.gz" + _filelist = self.utilstest.getdir(directory) + # download file and remove it to create a json mapping file + os.remove(filename) + directory_path = os.path.join(self.utilstest.data_home, "source") + shutil.rmtree(directory_path) + directory_path = os.path.join(self.utilstest.data_home, directory) + os.remove(directory_path) + filelist = self.utilstest.download_all() + self.assertGreater(len(filelist), 1, "At least 2 items were downloaded") def suite(): + loadTests = unittest.defaultTestLoader.loadTestsFromTestCase test_suite = unittest.TestSuite() - test_suite.addTest( - unittest.defaultTestLoader.loadTestsFromTestCase(TestResources)) - test_suite.addTest(TestExternalResources("test_tempdir")) - test_suite.addTest(TestExternalResources("test_download")) # order matters ! - test_suite.addTest(TestExternalResources("test_dowload_all")) + test_suite.addTest(loadTests(TestResources)) + test_suite.addTest(loadTests(TestResourcesWithoutPkgResources)) + test_suite.addTest(loadTests(TestResourcesWithCustomDirectory)) + test_suite.addTest(loadTests(TestExternalResources)) return test_suite diff --git a/silx/test/utils.py b/silx/test/utils.py index ec86a2a..44eb899 100644 --- a/silx/test/utils.py +++ b/silx/test/utils.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2016 European Synchrotron Radiation Facility +# Copyright (c) 2016-2017 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -34,7 +34,7 @@ __authors__ = ["T. Vincent"] __license__ = "MIT" -__date__ = "20/04/2017" +__date__ = "03/08/2017" import os @@ -48,7 +48,7 @@ import tempfile import unittest from ..resources import ExternalResources -logger = logging.getLogger(__name__) +_logger = logging.getLogger(__name__) utilstest = ExternalResources(project="silx", url_base="http://www.silx.org/pub/silx/", @@ -149,11 +149,15 @@ class TestLogging(logging.Handler): self.records = [] # Reset recorded LogRecords self.logger.addHandler(self) self.logger.propagate = False + # ensure no log message is ignored + self.entry_level = self.logger.level * 1 + self.logger.setLevel(logging.DEBUG) def __exit__(self, exc_type, exc_value, traceback): """Context (i.e., with) support""" self.logger.removeHandler(self) self.logger.propagate = True + self.logger.setLevel(self.entry_level) for level, expected_count in self.count_by_level.items(): if expected_count is None: |