Browse code

update plotter tests to test when plotly is not installed

Kelvin Loh authored on 06/12/2018 10:32:40
Showing 2 changed files
... ...
@@ -79,6 +79,17 @@ def set_backend(backend):
79 79
 def get_backend():
80 80
     return _p.backend
81 81
 
82
+
83
+def _check_incompatible_args_plotly(dpi, fig_size, ax):
84
+    assert(get_backend() == _p.Backends.plotly)
85
+    if(dpi or fig_size or ax):
86
+        raise RuntimeError(
87
+            "Plotly backend does not support setting 'dpi', 'fig_size' "
88
+            "or 'ax', either leave these parameters unspecified, or "
89
+            "select the matplotlib backend with"
90
+            "'kwant.plotter.select_backend(\"matplotlib\")'")
91
+
92
+
82 93
 def _sample_array(array, n_samples, rng=None):
83 94
     rng = _common.ensure_rng(rng)
84 95
     la = len(array)
... ...
@@ -1539,10 +1550,10 @@ def spectrum(syst, x, y=None, params=None, mask=None, file=None,
1539 1550
         return _spectrum_matplotlib(syst, x, y, params, mask, file,
1540 1551
                                     show, dpi, fig_size, ax)
1541 1552
     elif get_backend() == _p.Backends.plotly:
1542
-        if(dpi or fig_size or ax):
1543
-            raise RuntimeError('Incompatible arguments of dpi, fig_size, or '
1544
-                               'ax. Current plotting backend is plotly.')
1553
+        _check_incompatible_args_plotly(dpi, fig_size, ax)
1545 1554
         return _spectrum_plotly(syst, x, y, params, mask, file, show)
1555
+    else:
1556
+        raise RuntimeError("Backend not supported by spectrum().")
1546 1557
 
1547 1558
 
1548 1559
 def _generate_spectrum(syst, params, mask, x, y):
... ...
@@ -45,7 +45,7 @@ def test_matplotlib_backend_unset():
45 45
     assert matplotlib_backend_chosen is False
46 46
 
47 47
 
48
-def test_importable_without_matplotlib():
48
+def test_importable_without_backends():
49 49
     prefix, sep, suffix = _plotter.__file__.rpartition('.')
50 50
     if suffix in ['pyc', 'pyo']:
51 51
         suffix = 'py'
... ...
@@ -55,13 +55,16 @@ def test_importable_without_matplotlib():
55 55
         code = f.read()
56 56
     code = code.replace(b'from . import', b'from kwant import')
57 57
     code = code.replace(b'matplotlib', b'totalblimp')
58
+    code = code.replace(b'plotly', b'plylot')
58 59
 
59 60
     with warnings.catch_warnings(record=True) as w:
60 61
         warnings.simplefilter("always")
61 62
         exec(code)               # Trigger the warning.
62
-        assert len(w) == 1
63
+        assert len(w) == 2
63 64
         assert issubclass(w[0].category, RuntimeWarning)
64
-        assert "only iterator-providing functions" in str(w[0].message)
65
+        assert issubclass(w[1].category, RuntimeWarning)
66
+        assert "totalblimp is not available" in str(w[0].message)
67
+        assert "plylot is not available" in str(w[1].message)
65 68
 
66 69
 
67 70
 def syst_2d(W=3, r1=3, r2=8):