commit fbcdbf7456d90c400ec18e86b706b57da7e4347e Author: Damian Johnson atagar@torproject.org Date: Sat Jun 25 13:56:03 2016 -0700
Test graphing labels
Kinda sad we need helpers just to generate the graph bounds but there's enough to them they indeed need their own helpers. Adding test coverage. --- nyx/panel/graph.py | 16 +++++++--------- test/panel/graph.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 9 deletions(-)
diff --git a/nyx/panel/graph.py b/nyx/panel/graph.py index 68c136a..70b7426 100644 --- a/nyx/panel/graph.py +++ b/nyx/panel/graph.py @@ -660,21 +660,19 @@ def _x_axis_labels(interval, subgraph_columns):
interval_sec = INTERVAL_SECONDS[interval] interval_spacing = 10 if subgraph_columns >= WIDE_LABELING_GRAPH_COL else 5 - units_label, decimal_precision = None, 0 + previous_units, decimal_precision = None, 0
for i in range((subgraph_columns - 4) / interval_spacing): x = (i + 1) * interval_spacing time_label = str_tools.time_label(x * interval_sec, decimal_precision)
- if not units_label: - units_label = time_label[-1] - elif units_label != time_label[-1]: - # upped scale so also up precision of future measurements - units_label = time_label[-1] - decimal_precision += 1 + if not previous_units: + previous_units = time_label[-1] + elif previous_units != time_label[-1]: + previous_units = time_label[-1] + decimal_precision = 1 # raised precision for future measurements else: - # if constrained on space then strips labeling since already provided - time_label = time_label[:-1] + time_label = time_label[:-1] # strip units since already provided
x_axis_labels[x] = time_label
diff --git a/test/panel/graph.py b/test/panel/graph.py index 26fd7da..c8635e1 100644 --- a/test/panel/graph.py +++ b/test/panel/graph.py @@ -31,6 +31,56 @@ Accounting (awake) Time to reset: 01:02
class TestGraph(unittest.TestCase): + def test_x_axis_labels(self): + test_inputs = { + 0: {}, + 7: {}, + 10: {5: '25s'}, + 15: {5: '25s', 10: '50'}, + 20: {5: '25s', 10: '50', 15: '1m'}, + 25: {5: '25s', 10: '50', 15: '1m', 20: '1.6'}, + 45: {5: '25s', 10: '50', 15: '1m', 20: '1.6', 25: '2.0', 30: '2.5', 35: '2.9', 40: '3.3'}, + 80: {10: '50s', 20: '1m', 30: '2.5', 40: '3.3', 50: '4.1', 60: '5.0', 70: '5.8'}, # spaced more since wide + } + + for width, expected in test_inputs.items(): + self.assertEqual(expected, nyx.panel.graph._x_axis_labels(nyx.panel.graph.Interval.FIVE_SECONDS, width)) + + test_inputs = { + nyx.panel.graph.Interval.EACH_SECOND: { + 10: '10s', 20: '20', 30: '30', 40: '40', 50: '50', 60: '1m', 70: '1.1' + }, nyx.panel.graph.Interval.FIVE_SECONDS: { + 10: '50s', 20: '1m', 30: '2.5', 40: '3.3', 50: '4.1', 60: '5.0', 70: '5.8' + }, nyx.panel.graph.Interval.THIRTY_SECONDS: { + 10: '5m', 20: '10', 30: '15', 40: '20', 50: '25', 60: '30', 70: '35' + }, nyx.panel.graph.Interval.MINUTELY: { + 10: '10m', 20: '20', 30: '30', 40: '40', 50: '50', 60: '1h', 70: '1.1' + }, nyx.panel.graph.Interval.FIFTEEN_MINUTE: { + 10: '2h', 20: '5', 30: '7', 40: '10', 50: '12', 60: '15', 70: '17' + }, nyx.panel.graph.Interval.THIRTY_MINUTE: { + 10: '5h', 20: '10', 30: '15', 40: '20', 50: '1d', 60: '1.2', 70: '1.4' + }, nyx.panel.graph.Interval.HOURLY: { + 10: '10h', 20: '20', 30: '1d', 40: '1.6', 50: '2.0', 60: '2.5', 70: '2.9' + }, nyx.panel.graph.Interval.DAILY: { + 10: '10d', 20: '20', 30: '30', 40: '40', 50: '50', 60: '60', 70: '70' + }, + } + + for interval, expected in test_inputs.items(): + self.assertEqual(expected, nyx.panel.graph._x_axis_labels(interval, 80)) + + def test_y_axis_labels(self): + data = nyx.panel.graph.ConnectionStats() + + # check with both even and odd height since that determines an offset in the middle + + self.assertEqual({2: '10', 4: '7', 6: '5', 9: '2', 11: '0'}, nyx.panel.graph._y_axis_labels(12, data.primary, 0, 10)) + self.assertEqual({2: '10', 4: '6', 6: '3', 8: '0'}, nyx.panel.graph._y_axis_labels(9, data.primary, 0, 10)) + + # check where the min and max are the same + + self.assertEqual({2: '0', 11: '0'}, nyx.panel.graph._y_axis_labels(12, data.primary, 0, 0)) + @require_curses @patch('nyx.panel.graph.tor_controller') def test_draw_subgraph_blank(self, tor_controller_mock):