[testing] testing the error messages for plotting

This commit is contained in:
mzwiessele 2015-11-30 10:49:49 +00:00
parent 065ab725ff
commit 3c618c1525
2 changed files with 16 additions and 9 deletions

View file

@ -1,11 +1,9 @@
# Copyright (c) 2014, GPy authors (see AUTHORS.txt).
# Licensed under the BSD 3-clause license (see LICENSE.txt)
from ConfigParser import NoOptionError
current_lib = [None]
supported_libraries = ['matplotlib', 'plotly', 'none']
error_suggestion = "Please make sure you specify your plotting library in your configuration file.\n\n[plotting]\nlibrary = <library>\n\nCurrently supported libraries: {}".format(", ".join(supported_libraries))
error_suggestion = "Please make sure you specify your plotting library in your configuration file (<User>/.config/GPy/user.cfg).\n\n[plotting]\nlibrary = <library>\n\nCurrently supported libraries: {}".format(", ".join(supported_libraries))
def change_plotting_library(lib):
try:
@ -15,7 +13,7 @@ def change_plotting_library(lib):
# This is hooking the library in
# for the usage in GPy:
if lib not in supported_libraries:
raise NoOptionError("Warning: Plotting library {} not recognized, currently supported libraries are: \n {}".format(lib, ", ".join(supported_libraries)))
raise ValueError("Warning: Plotting library {} not recognized, currently supported libraries are: \n {}".format(lib, ", ".join(supported_libraries)))
if lib == 'matplotlib':
import matplotlib
from .matplot_dep.plot_definitions import MatplotlibPlots
@ -31,15 +29,15 @@ def change_plotting_library(lib):
except (ImportError, NameError):
config.set('plotting', 'library', 'none')
import warnings
warnings.warn(ImportWarning("{} not available, install newest version of {} for plotting".format(lib, lib)))
warnings.warn(ImportWarning("You spevified {} in your configuration, but is not available. Install newest version of {} for plotting".format(lib, lib)))
from ConfigParser import NoOptionError
from ..util.config import config
try:
lib = config.get('plotting', 'library')
change_plotting_library(lib)
except NoOptionError:
print("No plotting library was specified. \n{}".format(error_suggestion))
print("No plotting library was specified in config file. \n{}".format(error_suggestion))
def plotting_library():
if current_lib[0] is None:

View file

@ -28,6 +28,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#===============================================================================
import matplotlib
from unittest.case import TestCase
matplotlib.use('agg')
import numpy as np
@ -35,13 +36,21 @@ import GPy, os
from nose import SkipTest
from ..util.config import config
from ..plotting import change_plotting_library
from ..plotting import change_plotting_library, plotting_library
class ConfigTest(TestCase):
def tearDown(self):
change_plotting_library('matplotlib')
def test_change_plotting(self):
self.assertRaises(ValueError, change_plotting_library, 'not+in9names')
change_plotting_library('none')
self.assertRaises(RuntimeError, plotting_library)
change_plotting_library('matplotlib')
if config.get('plotting', 'library') != 'matplotlib':
raise SkipTest("Matplotlib not installed, not testing plots")
try:
from matplotlib import cbook, pyplot as plt
from matplotlib.testing.compare import compare_images