[tor-commits] [nyx/master] Unit test count popup

atagar at torproject.org atagar at torproject.org
Sun Apr 3 00:38:54 UTC 2016


commit 5f08fc9eacb1ab5c40b7c9d9c2d1aed2cfbfba42
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Apr 2 17:31:37 2016 -0700

    Unit test count popup
    
    Finish unit testing the popups we've finished overhauling. I'll be slowly
    expanding test coverage as we switch more things over to the new curses draw()
    pattern.
---
 nyx/popups.py    |  5 +++--
 test/__init__.py |  6 ++++--
 test/popups.py   | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/nyx/popups.py b/nyx/popups.py
index e65be70..4a51e0c 100644
--- a/nyx/popups.py
+++ b/nyx/popups.py
@@ -150,13 +150,14 @@ def show_about():
     nyx.curses.key_input()
 
 
-def show_counts(title, counts):
+def show_counts(title, counts, fill_char = ' '):
   """
   Provides a dialog with bar graphs and percentages for the given set of
   counts. Pressing any key closes the dialog.
 
   :param str title: dialog title
   :param dict counts: mapping of labels to their value
+  :param str fill_char: character to use for rendering the bar graph
   """
 
   def _render_no_stats(subwindow):
@@ -183,7 +184,7 @@ def show_counts(title, counts):
       x = subwindow.addstr(2, y + 1, label, GREEN, BOLD)
 
       for j in range(graph_width * v / value_total):
-        subwindow.addstr(x + j + 1, y + 1, ' ', RED, HIGHLIGHT)
+        subwindow.addstr(x + j + 1, y + 1, fill_char, RED, HIGHLIGHT)
 
     subwindow.addstr(2, subwindow.height - 2, 'Press any key...')
 
diff --git a/test/__init__.py b/test/__init__.py
index 44a53b0..0884866 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -27,7 +27,7 @@ SHOW_RENDERED_CONTENT = None
 RenderResult = collections.namedtuple('RenderResult', ['content', 'return_value', 'runtime'])
 
 
-def render(func):
+def render(func, *args, **kwargs):
   """
   Runs the given curses function, providing content that's rendered on the
   screen.
@@ -41,8 +41,10 @@ def render(func):
 
   def draw_func():
     nyx.curses.disable_acs()
+    nyx.curses.CURSES_SCREEN.erase()
+
     start_time = time.time()
-    attr['return_value'] = func()
+    attr['return_value'] = func(*args, **kwargs)
     attr['runtime'] = time.time() - start_time
     attr['content'] = nyx.curses.screenshot()
 
diff --git a/test/popups.py b/test/popups.py
index fde87f2..b82c9a2 100644
--- a/test/popups.py
+++ b/test/popups.py
@@ -34,6 +34,24 @@ About:-------------------------------------------------------------------------+
 +------------------------------------------------------------------------------+
 """.strip()
 
+EXPECTED_EMPTY_COUNTS = """
+Client Locales---------------------------------------+
+| Usage stats aren't available yet, press any key... |
++----------------------------------------------------+
+""".strip()
+
+EXPECTED_COUNTS = """
+Client Locales-----------------------------------------------------------------+
+| de  41 (43%) ***************************                                     |
+| ru  32 (33%) *********************                                           |
+| ca  11 (11%) *******                                                         |
+| us   6 (6 %) ****                                                            |
+| fr   5 (5 %) ***                                                             |
+|                                                                              |
+| Press any key...                                                             |
++------------------------------------------------------------------------------+
+""".strip()
+
 
 class TestPopups(unittest.TestCase):
   @patch('nyx.controller.get_controller')
@@ -77,3 +95,25 @@ class TestPopups(unittest.TestCase):
 
     rendered = test.render(nyx.popups.show_about)
     self.assertEqual(EXPECTED_ABOUT_POPUP, rendered.content)
+
+  @patch('nyx.controller.get_controller')
+  def test_counts_when_empty(self, get_controller_mock):
+    get_controller_mock().header_panel().get_height.return_value = 0
+
+    rendered = test.render(nyx.popups.show_counts, 'Client Locales', {})
+    self.assertEqual(EXPECTED_EMPTY_COUNTS, rendered.content)
+
+  @patch('nyx.controller.get_controller')
+  def test_counts(self, get_controller_mock):
+    get_controller_mock().header_panel().get_height.return_value = 0
+
+    clients = {
+      'fr': 5,
+      'us': 6,
+      'ca': 11,
+      'ru': 32,
+      'de': 41,
+    }
+
+    rendered = test.render(nyx.popups.show_counts, 'Client Locales', clients, fill_char = '*')
+    self.assertEqual(EXPECTED_COUNTS, rendered.content)



More information about the tor-commits mailing list