From 7032f7b05deac0bbd2e868ffd3fe42dd1c97fb7d Mon Sep 17 00:00:00 2001 From: uditgulati Date: Tue, 5 Jan 2021 13:14:08 -0600 Subject: add server side class unit tests --- wqflask/tests/unit/wqflask/test_server_side.py | 31 ++++++++++++++++++++++++++ wqflask/wqflask/server_side.py | 16 ++++++------- 2 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 wqflask/tests/unit/wqflask/test_server_side.py diff --git a/wqflask/tests/unit/wqflask/test_server_side.py b/wqflask/tests/unit/wqflask/test_server_side.py new file mode 100644 index 00000000..4f91d8ca --- /dev/null +++ b/wqflask/tests/unit/wqflask/test_server_side.py @@ -0,0 +1,31 @@ +import unittest + +from wqflask.server_side import ServerSideTable + + +class TestServerSideTableTests(unittest.TestCase): + """ + Test the ServerSideTable class + + test table: + first, second, third + 'd', 4, 'zz' + 'b', 2, 'aa' + 'c', 1, 'ss' + """ + + def test_get_page(self): + rows_count = 3 + table_rows = [ + {'first': 'd', 'second': 4, 'third': 'zz'}, + {'first': 'b', 'second': 2, 'third': 'aa'}, + {'first': 'c', 'second': 1, 'third': 'ss'}, + ] + headers = ['first', 'second', 'third'] + request_args = {'sEcho': '1', 'iSortCol_0': '1', 'iSortingCols': '1', 'sSortDir_0': 'asc', 'iDisplayStart': '0', 'iDisplayLength': '3'} + + test_page = ServerSideTable(rows_count, table_rows, headers, request_args).get_page() + self.assertEqual(test_page['sEcho'], '1') + self.assertEqual(test_page['iTotalRecords'], 'nan') + self.assertEqual(test_page['iTotalDisplayRecords'], '3') + self.assertEqual(test_page['data'], [{'first': 'b', 'second': 2, 'third': 'aa'}, {'first': 'c', 'second': 1, 'third': 'ss'}, {'first': 'd', 'second': 4, 'third': 'zz'}]) diff --git a/wqflask/wqflask/server_side.py b/wqflask/wqflask/server_side.py index 824b00aa..5f764767 100644 --- a/wqflask/wqflask/server_side.py +++ b/wqflask/wqflask/server_side.py @@ -3,7 +3,7 @@ class ServerSideTable(object): - ''' + """ This class is used to do server-side processing on the DataTables table such as paginating, sorting, filtering(not implemented) etc. This takes the load off @@ -22,7 +22,7 @@ class ServerSideTable(object): Have a look at snp_browser_table() function in wqflask/wqflask/views.py for reference use. - ''' + """ def __init__(self, rows_count, table_rows, header_data_names, request_values): self.request_values = request_values @@ -36,12 +36,12 @@ class ServerSideTable(object): self.paginate_rows() def sort_rows(self): - ''' + """ Sorts the rows taking in to account the column (or columns) that the user has selected. - ''' + """ def is_reverse(str_direction): - ''' Maps the 'desc' and 'asc' words to True or False. ''' + """ Maps the 'desc' and 'asc' words to True or False. """ return True if str_direction == 'desc' else False if (self.request_values['iSortCol_0'] != "") and (int(self.request_values['iSortingCols']) > 0): @@ -54,12 +54,12 @@ class ServerSideTable(object): reverse=is_reverse(sort_direction)) def paginate_rows(self): - ''' + """ Selects a subset of the filtered and sorted data based on if the table has pagination, the current page and the size of each page. - ''' + """ def requires_pagination(): - ''' Check if the table is going to be paginated ''' + """ Check if the table is going to be paginated """ if self.request_values['iDisplayStart'] != "": if int(self.request_values['iDisplayLength']) != -1: return True -- cgit v1.2.3