Skip to content

Commit

Permalink
#91 increase test coverage for PointBrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
akorosov committed Mar 9, 2018
1 parent 7345ce1 commit 3eee875
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
8 changes: 4 additions & 4 deletions nansat/pointbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import numpy as np

try:
if 'DISPLAY' not in os.environ:
import matplotlib; matplotlib.use('Agg')
import matplotlib
if 'DISPLAY' not in os.environ:
matplotlib.use('Agg')
import matplotlib.pyplot as plt
except ImportError:
MATPLOTLIB_IS_INSTALLED = False
Expand Down Expand Up @@ -63,11 +63,11 @@ class PointBrowser():
lines = None
coordinates = None

def __init__(self, data, fmt='x-k', **kwargs):
def __init__(self, data, fmt='x-k', force_interactive=True, **kwargs):
"""Open figure with imshow and colorbar"""
if not MATPLOTLIB_IS_INSTALLED:
raise ImportError(' Matplotlib is not installed ')
if not matplotlib.is_interactive():
if force_interactive and not matplotlib.is_interactive():
raise SystemError('''
Python is started with -pylab option, transect will not work.
Please restart python without -pylab.''')
Expand Down
27 changes: 13 additions & 14 deletions nansat/tests/test_pointbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,22 @@
from nansat.pointbrowser import PointBrowser

try:
if 'DISPLAY' not in os.environ:
import matplotlib; matplotlib.use('Agg')
import matplotlib
if 'DISPLAY' not in os.environ:
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.switch_backend('qt5agg')
except ImportError:
MATPLOTLIB_IS_INSTALLED = False
else:
MATPLOTLIB_IS_INSTALLED = True


class PointBrowserTest(unittest.TestCase):
@unittest.skipUnless(MATPLOTLIB_IS_INSTALLED and 'DISPLAY' in os.environ, 'Matplotlib is required')
def setUp(self):
plt.switch_backend('qt5agg')
plt.ion()
data = np.ndarray(shape=(4, 4), dtype=float, order='F')
self.point = PointBrowser(data)

def test_onclick(self):
point_browser = PointBrowser(np.zeros((4, 4)), force_interactive=False)
event = Event(xdata=0, ydata=0, key=None)
self.point.onclick(event)
t = self.point._convert_coordinates()[0]
point_browser.onclick(event)
t = point_browser._convert_coordinates()[0]
self.assertIsInstance(t, np.ndarray)
xPoints = t[0]
self.assertIsInstance(xPoints, np.ndarray)
Expand All @@ -48,20 +41,26 @@ def test_onclick(self):
self.assertEqual(yPoints[0], event.ydata, "y coordinates is set wrong")

def test_onclick_multilines(self):
point_browser = PointBrowser(np.zeros((4, 4)), force_interactive=False)
events = []
events.append(Event(xdata=0, ydata=0, key=None))
events.append(Event(xdata=1, ydata=0, key=None))
events.append(Event(xdata=2, ydata=2, key='AnyKeyButZorAltZ'))
events.append(Event(xdata=2, ydata=3, key=None))
for event in events:
self.point.onclick(event)
points = self.point._convert_coordinates()
point_browser.onclick(event)
points = point_browser._convert_coordinates()
self.assertEqual(len(points), 2, 'There should be two transects')
self.assertTrue(np.alltrue(points[0] == np.array([[0, 1], [0, 0]])),
't1 is not correct')
self.assertTrue(np.alltrue(points[1] == np.array([[2, 2], [2, 3]])),
't2 is not correct')

@unittest.skipIf('DISPLAY' in os.environ, 'Non-interactive mode is required')
def test_fail_non_interactive(self):
with self.assertRaises(SystemError):
point_browser = PointBrowser(np.zeros((4, 4)))

class Event:
def __init__(self, **kwds):
self.__dict__.update(kwds)
Expand Down

0 comments on commit 3eee875

Please sign in to comment.