[tor-commits] [nyx/master] Test _draw_accounting_stats()

atagar at torproject.org atagar at torproject.org
Sun Jun 26 00:48:01 UTC 2016


commit 28f7b5967d1c99073af70176621340cc442569c2
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Apr 20 09:04:18 2016 -0700

    Test _draw_accounting_stats()
    
    Test the graph panel method that renders accounting stats. This also changes
    the method to display user friendly units (ex. '5 MB') rather than the raw byte
    count.
---
 nyx/panel/graph.py  | 42 ++++++++++++++++++++----------------------
 test/panel/graph.py | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 22 deletions(-)

diff --git a/nyx/panel/graph.py b/nyx/panel/graph.py
index 132a7d6..1b2a067 100644
--- a/nyx/panel/graph.py
+++ b/nyx/panel/graph.py
@@ -33,7 +33,7 @@ GraphStat = enum.Enum(('BANDWIDTH', 'bandwidth'), ('CONNECTIONS', 'connections')
 Interval = enum.Enum(('EACH_SECOND', 'each second'), ('FIVE_SECONDS', '5 seconds'), ('THIRTY_SECONDS', '30 seconds'), ('MINUTELY', 'minutely'), ('FIFTEEN_MINUTE', '15 minute'), ('THIRTY_MINUTE', '30 minute'), ('HOURLY', 'hourly'), ('DAILY', 'daily'))
 Bounds = enum.Enum(('GLOBAL_MAX', 'global_max'), ('LOCAL_MAX', 'local_max'), ('TIGHT', 'tight'))
 
-DrawAttributes = collections.namedtuple('DrawAttributes', ('stat', 'subgraph_height', 'subgraph_width', 'interval', 'bounds_type', 'accounting', 'right_to_left'))
+DrawAttributes = collections.namedtuple('DrawAttributes', ('stat', 'subgraph_height', 'subgraph_width', 'interval', 'bounds_type', 'right_to_left'))
 
 INTERVAL_SECONDS = {
   Interval.EACH_SECOND: 1,
@@ -552,7 +552,6 @@ class GraphPanel(nyx.panel.Panel):
       subgraph_width = min(subwindow.width / 2, CONFIG['features.graph.max_width']),
       interval = self.update_interval,
       bounds_type = self.bounds_type,
-      accounting = accounting_stats,
       right_to_left = CONFIG['features.graph.right_to_left'],
     )
 
@@ -565,8 +564,8 @@ class GraphPanel(nyx.panel.Panel):
       if subwindow.width <= COLLAPSE_WIDTH:
         self._draw_bandwidth_stats(subwindow, attr, subwindow.width)
 
-      if attr.accounting:
-        self._draw_accounting_stats(subwindow, attr)
+      if accounting_stats:
+        _draw_accounting_stats(subwindow, DEFAULT_CONTENT_HEIGHT + attr.subgraph_height - 2, accounting_stats)
 
   def _draw_subgraph(self, subwindow, attr, data, x, color):
     # Concering our subgraph colums, the y-axis label can be at most six
@@ -700,24 +699,6 @@ class GraphPanel(nyx.panel.Panel):
     subwindow.addstr(1, labeling_line, primary_footer, PRIMARY_COLOR)
     subwindow.addstr(attr.subgraph_width + 1, labeling_line, secondary_footer, SECONDARY_COLOR)
 
-  def _draw_accounting_stats(self, subwindow, attr):
-    y = DEFAULT_CONTENT_HEIGHT + attr.subgraph_height - 2
-
-    if tor_controller().is_alive():
-      hibernate_color = CONFIG['attr.hibernate_color'].get(attr.accounting.status, RED)
-
-      x = subwindow.addstr(0, y, 'Accounting (', BOLD)
-      x = subwindow.addstr(x, y, attr.accounting.status, BOLD, hibernate_color)
-      x = subwindow.addstr(x, y, ')', BOLD)
-
-      subwindow.addstr(35, y, 'Time to reset: %s' % str_tools.short_time_label(attr.accounting.time_until_reset))
-
-      subwindow.addstr(2, y + 1, '%s / %s' % (attr.accounting.read_bytes, attr.accounting.read_limit), PRIMARY_COLOR)
-      subwindow.addstr(37, y + 1, '%s / %s' % (attr.accounting.written_bytes, attr.accounting.write_limit), SECONDARY_COLOR)
-    else:
-      subwindow.addstr(0, y, 'Accounting:', BOLD)
-      subwindow.addstr(12, y, 'Connection Closed...')
-
   def _update_accounting(self, event):
     if not CONFIG['features.graph.bw.accounting.show']:
       self._accounting_stats = None
@@ -744,6 +725,23 @@ class GraphPanel(nyx.panel.Panel):
         self.redraw(True)
 
 
+def _draw_accounting_stats(subwindow, y, accounting):
+  if tor_controller().is_alive():
+    hibernate_color = CONFIG['attr.hibernate_color'].get(accounting.status, RED)
+
+    x = subwindow.addstr(0, y, 'Accounting (', BOLD)
+    x = subwindow.addstr(x, y, accounting.status, BOLD, hibernate_color)
+    x = subwindow.addstr(x, y, ')', BOLD)
+
+    subwindow.addstr(35, y, 'Time to reset: %s' % str_tools.short_time_label(accounting.time_until_reset))
+
+    subwindow.addstr(2, y + 1, '%s / %s' % (str_tools.size_label(accounting.read_bytes), str_tools.size_label(accounting.read_limit)), PRIMARY_COLOR)
+    subwindow.addstr(37, y + 1, '%s / %s' % (str_tools.size_label(accounting.written_bytes), str_tools.size_label(accounting.write_limit)), SECONDARY_COLOR)
+  else:
+    subwindow.addstr(0, y, 'Accounting:', BOLD)
+    subwindow.addstr(12, y, 'Connection Closed...')
+
+
 def _size_label(byte_count, decimal = 1):
   """
   Alias for str_tools.size_label() that accounts for if the user prefers bits
diff --git a/test/panel/graph.py b/test/panel/graph.py
new file mode 100644
index 0000000..7ffc48e
--- /dev/null
+++ b/test/panel/graph.py
@@ -0,0 +1,45 @@
+"""
+Unit tests for nyx.panel.graph.
+"""
+
+import datetime
+import unittest
+
+import stem.control
+
+import nyx.panel.graph
+import test
+
+from test import require_curses
+from mock import patch
+
+EXPECTED_ACCOUNTING = """
+Accounting (awake)                 Time to reset: 01:02
+  4 KB / 105 KB                      2 KB / 9 KB
+""".strip()
+
+
+class TestGraph(unittest.TestCase):
+  @require_curses
+  @patch('nyx.panel.graph.tor_controller')
+  def test_draw_accounting_stats(self, tor_controller_mock):
+    tor_controller_mock().is_alive.return_value = True
+
+    stat = stem.control.AccountingStats(
+      1410723598.276578,
+      'awake',
+      datetime.datetime(2014, 9, 14, 19, 41),
+      62,
+      4837, 102944, 107781,
+      2050, 7440, 9490,
+    )
+
+    rendered = test.render(nyx.panel.graph._draw_accounting_stats, 0, stat)
+    self.assertEqual(EXPECTED_ACCOUNTING, rendered.content)
+
+  @require_curses
+  @patch('nyx.panel.graph.tor_controller')
+  def test_draw_accounting_stats_disconnected(self, tor_controller_mock):
+    tor_controller_mock().is_alive.return_value = False
+    rendered = test.render(nyx.panel.graph._draw_accounting_stats, 0, None)
+    self.assertEqual('Accounting: Connection Closed...', rendered.content)





More information about the tor-commits mailing list