You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
2.8 KiB
Python

import unittest
import medical_data_visualizer
import matplotlib as mpl
# the test case
class CatPlotTestCase(unittest.TestCase):
def setUp(self):
self.fig = medical_data_visualizer.draw_cat_plot()
self.ax = self.fig.axes[0]
def test_line_plot_labels(self):
actual = self.ax.get_xlabel()
expected = "variable"
self.assertEqual(actual, expected, "Expected line plot xlabel to be 'variable'")
actual = self.ax.get_ylabel()
expected = "total"
self.assertEqual(actual, expected, "Expected line plot ylabel to be 'total'")
actual = []
for label in self.ax.get_xaxis().get_majorticklabels():
actual.append(label.get_text())
expected = ['active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke']
self.assertEqual(actual, expected, "Expected bar plot secondary x labels to be 'active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke'")
def test_bar_plot_number_of_bars(self):
actual = len([rect for rect in self.ax.get_children() if isinstance(rect, mpl.patches.Rectangle)])
expected = 13
self.assertEqual(actual, expected, "Expected a different number of bars chart.")
class HeatMapTestCase(unittest.TestCase):
def setUp(self):
self.fig = medical_data_visualizer.draw_heat_map()
self.ax = self.fig.axes[0]
def test_heat_map_labels(self):
actual = []
for label in self.ax.get_xticklabels():
actual.append(label.get_text())
expected = ['id', 'age', 'sex', 'height', 'weight', 'ap_hi', 'ap_lo', 'cholesterol', 'gluc', 'smoke', 'alco', 'active', 'cardio', 'overweight']
self.assertEqual(actual, expected, "Expected heat map labels to be 'id', 'age', 'sex', 'height', 'weight', 'ap_hi', 'ap_lo', 'cholesterol', 'gluc', 'smoke', 'alco', 'active', 'cardio', 'overweight'.")
def test_heat_map_values(self):
actual = [text.get_text() for text in self.ax.get_default_bbox_extra_artists() if isinstance(text, mpl.text.Text)]
print(actual)
expected = ['0.0', '0.0', '-0.0', '0.0', '-0.1', '0.5', '0.0', '0.1', '0.1', '0.3', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.1', '0.0', '0.2', '0.1', '0.0', '0.1', '-0.0', '-0.1', '0.1', '0.0', '0.2', '0.0', '0.1', '-0.0', '-0.0', '0.1', '0.0', '0.1', '0.4', '-0.0', '-0.0', '0.3', '0.2', '0.1', '-0.0', '0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.2', '0.1', '0.1', '0.0', '0.0', '0.0', '0.0', '0.3', '0.0', '-0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.0', '0.0', '-0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '-0.0', '0.2', '0.1', '0.3', '0.2', '0.1', '-0.0', '-0.0', '-0.0', '-0.0', '0.1', '-0.1', '-0.1', '0.7', '0.0', '0.2', '0.1', '0.1', '-0.0', '0.0', '-0.0', '0.1']
self.assertEqual(actual, expected, "Expected different values in heat map.")
if __name__ == "__main__":
unittest.main()