class AdvancedReportTest(TestCase):
    def setUp(self):
        User.objects.create_user("test2", "*****@*****.**", "foobar")
        self.report = AdvancedReport()
        self.report.models = (User, )

    def test_sorting(self):
        self.assertQuerysetEqual(
            User.objects.order_by('pk'),
            self.report.get_sorted_queryset('__unicode__'),
            transform=lambda x: x)
        self.assertQuerysetEqual(User.objects.order_by('pk'),
                                 self.report.get_sorted_queryset('__str__'),
                                 transform=lambda x: x)
        self.assertQuerysetEqual(User.objects.order_by('first_name'),
                                 self.report.get_sorted_queryset('first_name'),
                                 transform=lambda x: x)
        self.assertQuerysetEqual(
            User.objects.all(),
            self.report.get_sorted_queryset('field_that_does_not_exist'),
            transform=lambda x: x,
            ordered=False)
        self.assertQuerysetEqual(
            User.objects.all(),
            self.report.get_sorted_queryset('field_that__does_not_exist'),
            transform=lambda x: x,
            ordered=False)

    def test_enriched_queryset_order(self):
        eqs = EnrichedQueryset(User.objects.all(), self.report)
        self.assertListEqual(['pk'], eqs.queryset.query.order_by)
        eqs = EnrichedQueryset(User.objects.all().order_by('first_name'),
                               self.report)
        self.assertListEqual(['first_name', 'pk'], eqs.queryset.query.order_by)
        eqs = EnrichedQueryset(['a', 'b', 'c'], self.report)
        self.assertListEqual(['a', 'b', 'c'], eqs.queryset)

    def test_multiple_actions(self):
        report = TestReport1()
        report.handle_multiple_actions('multiple1', [1, 2, 3])
        for item in report.items:
            self.assertEqual(item.a, 5)

        report = TestReport1()
        request = RequestFactory().post(
            '/', data={'multiple2_multiple-testfield': '7'})
        setattr(request, '_messages', mock.MagicMock())

        ids = [1, 2]

        _, count = report.handle_multiple_actions('multiple2', ids, request)

        self.assertEqual(count, len(ids))
        for i in ids:
            self.assertEqual(report.get_item_for_id(i).a, '7')
class AdvancedReportTest(TestCase):
    def setUp(self):
        User.objects.create_user("test2", "*****@*****.**", "foobar")
        self.report = AdvancedReport()
        self.report.models = (User,)

    def test_sorting(self):
        self.assertQuerysetEqual(User.objects.order_by('pk'), self.report.get_sorted_queryset('__unicode__'), transform=lambda x:x)
        self.assertQuerysetEqual(User.objects.order_by('pk'), self.report.get_sorted_queryset('__str__'), transform=lambda x:x)
        self.assertQuerysetEqual(User.objects.order_by('first_name'), self.report.get_sorted_queryset('first_name'), transform=lambda x:x)
        self.assertQuerysetEqual(User.objects.all(), self.report.get_sorted_queryset('field_that_does_not_exist'), transform=lambda x:x, ordered=False)
        self.assertQuerysetEqual(User.objects.all(), self.report.get_sorted_queryset('field_that__does_not_exist'), transform=lambda x:x, ordered=False)

    def test_enriched_queryset_order(self):
        eqs = EnrichedQueryset(User.objects.all(), self.report)
        self.assertListEqual(['pk'], eqs.queryset.query.order_by)
        eqs = EnrichedQueryset(User.objects.all().order_by('first_name'), self.report)
        self.assertListEqual(['first_name', 'pk'], eqs.queryset.query.order_by)
        eqs = EnrichedQueryset(['a','b','c'], self.report)
        self.assertListEqual(['a','b','c'], eqs.queryset)

    def test_multiple_actions(self):
        report = TestReport1()
        report.handle_multiple_actions('multiple1', [1, 2, 3])
        for item in report.items:
            self.assertEqual(item.a, 5)

        report = TestReport1()
        request = RequestFactory().post('/', data={'multiple2_multiple-testfield': '7'})
        setattr(request, '_messages', mock.MagicMock())

        ids = [1, 2]

        _, count = report.handle_multiple_actions('multiple2', ids, request)

        self.assertEqual(count, len(ids))
        for i in ids:
            self.assertEqual(report.get_item_for_id(i).a, '7')
 def setUp(self):
     User.objects.create_user("test2", "*****@*****.**", "foobar")
     self.report = AdvancedReport()
     self.report.models = (User,)
 def setUp(self):
     self.report = AdvancedReport()
class AdvancedReportTest(TestCase):
    def setUp(self):
        self.report = AdvancedReport()

    def test_tabbed_filter_fields(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                    'default': 'img/item.png',
                    'item2': 'img/item2.png'
                },
                'types': [
                    'item1', 'item2', 'item3', 'item4'
                ]
            }
        }

        self.assertEqual(['card'], [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual(
            [
                ('item1', 'img/item.png'),
                ('item2', 'img/item2.png'),
                ('item3', 'img/item.png'),
                ('item4', 'img/item.png'),
            ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_default_only(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                    'default': 'img/item2.png'
                },
                'types': [
                    'item1', 'item2', 'item3', 'item4'
                ]
            }
        }

        self.assertEqual(['card'], [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual(
            [
                ('item1', 'img/item2.png'),
                ('item2', 'img/item2.png'),
                ('item3', 'img/item2.png'),
                ('item4', 'img/item2.png'),
            ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_without_default_image(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                    'item2': 'img/item2.png'
                },
                'types': [
                    'item1', 'item2', 'item3', 'item4'
                ]
            }
        }

        self.assertEqual(['card'], [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual(
            [
                ('item1', None),
                ('item2', 'img/item2.png'),
                ('item3', None),
                ('item4', None),
            ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_without_image_values(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                },
                'types': [
                    'item1', 'item2', 'item3', 'item4'
                ]
            }
        }

        self.assertEqual(['card'], [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual(
            [
                ('item1', None),
                ('item2', None),
                ('item3', None),
                ('item4', None),
            ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_without_images(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'types': [
                    'item1', 'item2', 'item3', 'item4'
                ]
            }
        }

        self.assertEqual(['card'], [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual(
            [
                ('item1', None),
                ('item2', None),
                ('item3', None),
                ('item4', None),
            ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_without_types(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                },
            }
        }
        with self.assertRaises(Exception):
            self.report.get_tabbed_filter_links()

    def test_tabbed_filter_fields_multiple(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                    'default': 'img/item.png',
                    'item2': 'img/item2.png'
                },
                'types': [
                    'item1', 'item2', 'item3', 'item4'
                ]
            },
            'gender': {
                'images': {
                    'male': 'img/male.png',
                    'female': 'img/female.png'
                },
                'types': [
                    'male', 'female'
                ]
            }
        }

        self.assertEqual(['card', 'gender'], sorted([k for k, v in self.report.get_tabbed_filter_links()]))
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual(
            [
                ('item1', 'img/item.png'),
                ('item2', 'img/item2.png'),
                ('item3', 'img/item.png'),
                ('item4', 'img/item.png'),
            ], sorted([(k, v) for k, v in dict_iteritems[1]]))
        self.assertEqual(
            [
                ('female', 'img/female.png'),
                ('male', 'img/male.png'),
            ], sorted([(k, v) for k, v in dict_iteritems[0]]))
 def setUp(self):
     self.report = AdvancedReport()
class AdvancedReportTest(TestCase):
    def setUp(self):
        self.report = AdvancedReport()

    def test_tabbed_filter_fields(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                    'default': 'img/item.png',
                    'item2': 'img/item2.png'
                },
                'types': ['item1', 'item2', 'item3', 'item4']
            }
        }

        self.assertEqual(['card'],
                         [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual([
            ('item1', 'img/item.png'),
            ('item2', 'img/item2.png'),
            ('item3', 'img/item.png'),
            ('item4', 'img/item.png'),
        ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_default_only(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                    'default': 'img/item2.png'
                },
                'types': ['item1', 'item2', 'item3', 'item4']
            }
        }

        self.assertEqual(['card'],
                         [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual([
            ('item1', 'img/item2.png'),
            ('item2', 'img/item2.png'),
            ('item3', 'img/item2.png'),
            ('item4', 'img/item2.png'),
        ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_without_default_image(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                    'item2': 'img/item2.png'
                },
                'types': ['item1', 'item2', 'item3', 'item4']
            }
        }

        self.assertEqual(['card'],
                         [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual([
            ('item1', None),
            ('item2', 'img/item2.png'),
            ('item3', None),
            ('item4', None),
        ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_without_image_values(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {},
                'types': ['item1', 'item2', 'item3', 'item4']
            }
        }

        self.assertEqual(['card'],
                         [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual([
            ('item1', None),
            ('item2', None),
            ('item3', None),
            ('item4', None),
        ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_without_images(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'types': ['item1', 'item2', 'item3', 'item4']
            }
        }

        self.assertEqual(['card'],
                         [k for k, v in self.report.get_tabbed_filter_links()])
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual([
            ('item1', None),
            ('item2', None),
            ('item3', None),
            ('item4', None),
        ], sorted([(k, v) for k, v in dict_iteritems[0]]))

    def test_tabbed_filter_fields_without_types(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {},
            }
        }
        with self.assertRaises(Exception):
            self.report.get_tabbed_filter_links()

    def test_tabbed_filter_fields_multiple(self):
        self.report.tabbed_filter_fields = {
            'card': {
                'images': {
                    'default': 'img/item.png',
                    'item2': 'img/item2.png'
                },
                'types': ['item1', 'item2', 'item3', 'item4']
            },
            'gender': {
                'images': {
                    'male': 'img/male.png',
                    'female': 'img/female.png'
                },
                'types': ['male', 'female']
            }
        }

        self.assertEqual(
            ['card', 'gender'],
            sorted([k for k, v in self.report.get_tabbed_filter_links()]))
        dict_iteritems = [v for k, v in self.report.get_tabbed_filter_links()]
        self.assertEqual([
            ('item1', 'img/item.png'),
            ('item2', 'img/item2.png'),
            ('item3', 'img/item.png'),
            ('item4', 'img/item.png'),
        ], sorted([(k, v) for k, v in dict_iteritems[1]]))
        self.assertEqual([
            ('female', 'img/female.png'),
            ('male', 'img/male.png'),
        ], sorted([(k, v) for k, v in dict_iteritems[0]]))
 def setUp(self):
     User.objects.create_user("test2", "*****@*****.**", "foobar")
     self.report = AdvancedReport()
     self.report.models = (User, )