Skip to content

Commit

Permalink
Merge pull request gecko984#15 from gecko984/test_plot
Browse files Browse the repository at this point in the history
rudimentary tests for supervenn()
  • Loading branch information
gecko984 authored Sep 6, 2020
2 parents 5a528f6 + 33818b3 commit 6405495
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
name='supervenn',
license='MIT',
description='supervenn is a tool for visualization of relations of many sets using matplotlib',
version='0.3.0',
version='0.3.1',
long_description='See https://github.com/gecko984/supervenn/blob/master/README.md',
url='https://github.com/gecko984/supervenn',
packages=setuptools.find_packages(),
Expand Down
69 changes: 69 additions & 0 deletions supervenn/tests/plots_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import unittest

from supervenn._plots import supervenn


def rectangle_present(ax, expected_bbox_bounds):
"""
Check whether Axes ax contains a matplotlib.patches.Rectangle with given bbox bounds
:param ax: matplolib Axes
:param expected_bbox_bounds: expected bbox bounds [min_x, min_y, max_x, max_y]
:return: True / False
"""
found = False
for child in ax.get_children():
if isinstance(child, Rectangle) and child.get_bbox().bounds == expected_bbox_bounds:
found = True
break
return found


class TestSupervenn(unittest.TestCase):

def test_no_sets(self):
sets = []
with self.assertRaises(ValueError):
supervenn(sets)
plt.close()

def test_empty_sets(self):
sets = [set(), set()]
with self.assertRaises(ValueError):
supervenn(sets)
plt.close()

def test_no_side_plots(self):
"""
Test that the supervenn() runs without side plots, produces only one axes, and if given a list of one set
produces a rectangle of correct dimensions.
"""
set_size = 3
sets = [set(range(set_size))]
supervenn_plot = supervenn(sets, side_plots=False)
self.assertEqual(list(supervenn_plot.axes), ['main'])
self.assertEqual(len(supervenn_plot.figure.axes), 1)
expected_rectangle_bounds = (0.0, 0.0, float(set_size), 1.0)
self.assertTrue(rectangle_present(supervenn_plot.axes['main'], expected_rectangle_bounds))
plt.close()

def test_with_side_plots(self):
"""
Test that supervenn() runs without side plots, produces four axes, and if given a list of one set
produces a rectangle of correct dimensions.
"""
set_size = 3
sets = [set(range(set_size))]
supervenn_plot = supervenn(sets, side_plots=True)
self.assertEqual(set(supervenn_plot.axes), {'main', 'right_side_plot', 'top_side_plot', 'unused'})
expected_rectangle_bounds = (0.0, 0.0, float(set_size), 1.0) # same for all the three axes actually!
for ax_name, ax in supervenn_plot.axes.items():
if ax_name == 'unused':
continue
self.assertTrue(rectangle_present(ax, expected_rectangle_bounds))
plt.close()


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

0 comments on commit 6405495

Please sign in to comment.