Beispiel #1
0
 def test_get_queryset(self):
     j = JQueryDataTableMixin()
     j.model = mock.Mock()
     q = j.get_queryset()
     self.assertEqual(
         q._extract_mock_name(),
         "mock.objects.all()"
     )
Beispiel #2
0
 def test_apply_filter_without_search_value(self, mocked_parse):
     q = mock.Mock()
     j = JQueryDataTableMixin()
     j.request = mock.Mock()
     self.assertEqual(
         j.apply_filter(q),
         q
     )
Beispiel #3
0
 def test_get_table_data_with_object_list(
     self,
     mocked_get_queryset,
     mocked_queryset_count,
     mocked_apply_filter,
     mocked_order,
     mocked_paginate_objects,
     mocked_get_row,
     mocked_get_row_identifier,
     mocked_get_row_href
 ):
     j = JQueryDataTableMixin()
     mocked_queryset = mock.Mock()
     mocked_apply_filter.return_value = mocked_queryset
     mocked_get_queryset.return_value = mocked_apply_filter
     mocked_get_queryset.return_value = mocked_queryset
     mocked_queryset_count.return_value = 1
     mocked_apply_filter.return_value = mocked_queryset
     mocked_order.return_value = mocked_queryset
     paginator_object = mock.Mock()
     paginator_object.count = 2
     page_object = mock.Mock()
     page_object.object_list = [1, 2, 3]
     mocked_paginate_objects.return_value = paginator_object, page_object
     mocked_get_row.return_value = {}
     mocked_get_row_identifier.return_value = 3
     mocked_get_row_href.return_value = ""
     j.request = mock.Mock()
     j.request.GET.get = mock.Mock()
     j.request.GET.get.return_value = 4
     result = j.get_table_data()
     expected = {
         "draw": 4,
         "recordsTotal": 1,
         "recordsFiltered": 2,
         "data": [
             {"DT_RowData": {
                 "pk": 3,
                 "href": ""
             }},
             {"DT_RowData": {
                 "pk": 3,
                 "href": ""
             }},
             {"DT_RowData": {
                 "pk": 3,
                 "href": ""
             }}
         ]
     }
     self.assertEqual(
         DeepDiff(expected, result),
         {}
     )
Beispiel #4
0
 def test_get_rows(self):
     j = JQueryDataTableMixin()
     j.columns = ["some_column"]
     o = mock.Mock()
     o.some_column = "some_column_value"
     row = j.get_row(o)
     self.assertEqual(
         row,
         {
             'some_column': 'some_column_value'
         }
     )
Beispiel #5
0
 def test_apply_filter_without_searchable_fields(self, mocked_parse):
     mocked_parse.return_value = {
         "search": {
             "value": "some value"
         }
     }
     q = mock.Mock()
     j = JQueryDataTableMixin()
     j.request = mock.Mock()
     new_queryset = j.apply_filter(q)
     self.assertEqual(
         j.apply_filter(q),
         q
     )
Beispiel #6
0
 def test_apply_filter_with_searchable_fields(self, mocked_parse, mocked_get_inputs):
     mocked_get_inputs.return_value = mock.Mock()
     mocked_parse.return_value = {
         "search": {
             "value": "some value"
         }
     }
     q = mock.Mock()
     j = JQueryDataTableMixin()
     j.searchable_fields = ["some field"]
     j.request = mock.Mock()
     new_queryset = j.apply_filter(q)
     self.assertEqual(
         new_queryset._extract_mock_name(),
         "mock.annotate().filter()"
     )
Beispiel #7
0
    def test(self):
        with mock.patch("accountancy.views.JQueryDataTableMixin.order_by") as mocked_order_by, \
                mock.patch("accountancy.views.get_value") as mocked_get_value, \
                mock.patch("accountancy.views.sort_multiple") as mocked_sort_multiple:

            mocked_order_by.return_value = ["total", "-ref"]
            mocked_get_value.return_value = "a"
            j = JQueryDataTableMixin()
            j.order_objects([mock.Mock()])
            args, kwargs = mocked_sort_multiple.call_args_list[0]
            self.assertEqual(
                args[1][1],
                False
            )
            self.assertEqual(
                args[2][1],
                True
            )
Beispiel #8
0
 def test_one_asc_another_desc(self):
     d = {
         'draw': '3',
         'columns': {
             0: {'data': '', 'name': '', 'searchable': 'false', 'orderable': 'false', 'search': {'value': '', 'regex': 'false'}},
             1: {'data': 'supplier__name', 'name': '', 'searchable': 'true', 'orderable': 'true', 'search': {'value': '', 'regex': 'false'}},
             2: {'data': 'ref', 'name': '', 'searchable': 'true', 'orderable': 'true', 'search': {'value': '', 'regex': 'false'}},
             3: {'data': 'period', 'name': '', 'searchable': 'true', 'orderable': 'true', 'search': {'value': '', 'regex': 'false'}},
             4: {'data': 'date', 'name': '', 'searchable': 'true', 'orderable': 'true', 'search': {'value': '', 'regex': 'false'}},
             5: {'data': 'due_date', 'name': '', 'searchable': 'true', 'orderable': 'true', 'search': {'value': '', 'regex': 'false'}},
             6: {'data': 'total', 'name': '', 'searchable': 'true', 'orderable': 'true', 'search': {'value': '', 'regex': 'false'}},
             7: {'data': 'paid', 'name': '', 'searchable': 'true', 'orderable': 'true', 'search': {'value': '', 'regex': 'false'}},
             8: {'data': 'due', 'name': '', 'searchable': 'true', 'orderable': 'true', 'search': {'value': '', 'regex': 'false'}}
         },
         'order': {
             0: {'column': '1', 'dir': 'desc'},
             1: {'column': '2', 'dir': 'asc'}
         },
         'start': '0',
         'length': '10',
         'search': {
             'value': '',
             'regex': 'false'
         },
         'supplier': '',
         'reference': '',
         'total': '',
         'period': '',
         'search_within': 'any',
         'start_date': '',
         'end_date': '',
         'use_adv_search': 'False'
     }
     mock_self = mock.Mock()
     mock_self.request.GET.urlencode.return_value = ""
     with mock.patch("accountancy.views.parser.parse") as mocked_parse:
         mocked_parse.return_value = d
         ordering = JQueryDataTableMixin.order_by(mock_self)
         self.assertEqual(
             len(ordering),
             2
         )
         self.assertEqual(
             ordering[0],
             "-supplier__name"
         )
         self.assertEqual(
             ordering[1],
             "ref"
         )
Beispiel #9
0
    def test_paginate_objects_page_1(self):
        objs = []
        for i in range(50):
            o = {"i": i}
            objs.append(o)

        mock_self = mock.Mock()
        mock_self.request.GET = {"start": 0, "length": 25}
        paginator_obj, page_obj = JQueryDataTableMixin.paginate_objects(
            mock_self, objs)
        self.assertEqual(
            paginator_obj.object_list,
            objs
        )
        self.assertEqual(
            paginator_obj.per_page,
            25
        )
        self.assertEqual(
            paginator_obj.count,
            50
        )
        self.assertEqual(
            paginator_obj.num_pages,
            2
        )
        self.assertEqual(
            page_obj.object_list,
            objs[:25]
        )
        self.assertEqual(
            page_obj.number,
            1
        )
        self.assertEqual(
            page_obj.has_other_pages(),
            True
        )