From e68aa9387a9ab621d5e42966e7b969f2e8efeb68 Mon Sep 17 00:00:00 2001
From: Alice Heaton <aliceh@aptivate.org>
Date: Wed, 15 Jul 2015 17:05:32 +0100
Subject: [PATCH] Update tests to match the new table widget functionality.

---
 .../website/hid/tests/table_widget_tests.py   | 59 +++++++++++++------
 1 file changed, 41 insertions(+), 18 deletions(-)

diff --git a/django/website/hid/tests/table_widget_tests.py b/django/website/hid/tests/table_widget_tests.py
index 89ff5c3e..0d91ab65 100644
--- a/django/website/hid/tests/table_widget_tests.py
+++ b/django/website/hid/tests/table_widget_tests.py
@@ -1,4 +1,7 @@
+from mock import patch
+
 from django.test import TestCase
+
 from hid.widgets.table import TableWidget
 
 
@@ -10,25 +13,45 @@ class TestTableWidget(TestCase):
         )
         self.assertEqual(context_data['title'], 'table title')
 
-    def test_context_data_includes_headers(self):
+    def test_get_context_data_invokes_api(self):
         widget = TableWidget()
-        context_data = widget.get_context_data(
-            headers=['header one', 'header two']
-        )
-        self.assertEqual(context_data['headers'], [
-            'header one', 'header two'
-        ])
+        with patch('hid.widgets.table.transport.items.list') as mock:
+            widget.get_context_data()
+            self.assertTrue(mock.called)
 
-    def test_context_data_includes_rows(self):
+    def test_get_context_data_invokes_api_with_filters(self):
         widget = TableWidget()
+        with patch('hid.widgets.table.transport.items.list') as mock:
+            widget.get_context_data(filters={'a': 'b'})
+            self.assertEquals(mock.call_args[1], {'a': 'b'})
 
-        context_data = widget.get_context_data(
-            rows=[
-                ['row one, col one', 'row one, col two'],
-                ['row two, col one', 'row two, col two']
-            ]
-        )
-        self.assertEqual(context_data['rows'], [
-            ['row one, col one', 'row one, col two'],
-            ['row two, col one', 'row two, col two']
-        ])
+    def test_get_context_data_limits_rows_as_per_settings(self):
+        widget = TableWidget()
+        with patch('hid.widgets.table.transport.items.list') as mock:
+            mock.return_value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+            with patch('hid.widgets.table.ItemTable') as mock_table:
+                widget.get_context_data(count=3)
+                processed_rows = mock_table.call_args[0][0]
+                self.assertEqual(len(processed_rows), 3)
+
+    def test_get_context_data_orders_rows_as_per_settings(self):
+        widget = TableWidget()
+        with patch('hid.widgets.table.transport.items.list') as mock:
+            mock.return_value = [{'a': 1}, {'a': 4}, {'a': 2}]
+            with patch('hid.widgets.table.ItemTable') as mock_table:
+                widget.get_context_data(order_by='a')
+                processed_rows = mock_table.call_args[0][0]
+                self.assertEqual(processed_rows, [
+                    {'a': 1}, {'a': 2}, {'a': 4}
+                ])
+
+    def test_get_context_data_orders_row_reverse_as_per_settings(self):
+        widget = TableWidget()
+        with patch('hid.widgets.table.transport.items.list') as mock:
+            mock.return_value = [{'a': 1}, {'a': 4}, {'a': 2}]
+            with patch('hid.widgets.table.ItemTable') as mock_table:
+                widget.get_context_data(order_by='-a')
+                processed_rows = mock_table.call_args[0][0]
+                self.assertEqual(processed_rows, [
+                    {'a': 4}, {'a': 2}, {'a': 1}
+                ])
-- 
GitLab