|
| 1 | +from unittest import TestCase |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import pandas as pd |
| 4 | +from niaarm.visualize import two_key_plot |
| 5 | + |
| 6 | +class Rule: |
| 7 | + def __init__(self, antecedent, consequent, support, confidence): |
| 8 | + self.antecedent = antecedent |
| 9 | + self.consequent = consequent |
| 10 | + self.support = support |
| 11 | + self.confidence = confidence |
| 12 | + |
| 13 | + def __repr__(self): |
| 14 | + return f"Rule({self.antecedent} -> {self.consequent})" |
| 15 | + |
| 16 | +class TestTwoKeyPlot(TestCase): |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def setUpClass(cls): |
| 20 | + cls.rule1 = Rule(antecedent=["A", "B"], consequent=["C"], support=0.3, confidence=0.8) |
| 21 | + cls.rule2 = Rule(antecedent=["D"], consequent=["E", "F"], support=0.5, confidence=0.7) |
| 22 | + cls.rule3 = Rule(antecedent=["G", "H"], consequent=["I"], support=0.2, confidence=0.9) |
| 23 | + |
| 24 | + cls.rules = [cls.rule1, cls.rule2, cls.rule3] # Ensure rules are available to all tests |
| 25 | + |
| 26 | + def test_two_key_plot(self): |
| 27 | + metrics = ("support", "confidence") |
| 28 | + |
| 29 | + plot = two_key_plot(self.rules, metrics, interactive=False) |
| 30 | + |
| 31 | + # Verify that the return type is Matplotlib's pyplot |
| 32 | + self.assertIs(plot, plt) |
| 33 | + |
| 34 | + # Ensure a figure is created |
| 35 | + self.assertTrue(plt.gcf().axes, "No axes found in the generated plot.") |
| 36 | + |
| 37 | + def test_invalid_metrics(self): |
| 38 | + with self.assertRaises(ValueError): |
| 39 | + two_key_plot(self.rules, ("support",), interactive=False) |
| 40 | + |
| 41 | + def test_interactive_plot(self): |
| 42 | + metrics = ("support", "confidence") |
| 43 | + fig = two_key_plot(self.rules, metrics, interactive=True) |
| 44 | + |
| 45 | + # Verify that a Plotly figure is returned |
| 46 | + self.assertEqual(fig.__class__.__name__, "Figure", "Expected a Plotly figure but got a different type.") |
0 commit comments