def test_fetch_count_ignores_missing_start_and_end_time(self):
        widget = TermCountChartWidget()

        with patch('hid.widgets.term_count_chart.term_itemcount') as itemcount:
            widget._fetch_counts('tax', 3, None, None, 'Others')
            itemcount_kwargs = itemcount.call_args[1]

        self.assertNotIn('start_time', itemcount_kwargs)
        self.assertNotIn('end_time', itemcount_kwargs)
    def test_fetch_count_uses_start_and_end_time(self):
        widget = TermCountChartWidget()
        t1 = datetime.now()
        t2 = t1 + timedelta(days=4)
        with patch('hid.widgets.term_count_chart.term_itemcount') as itemcount:
            widget._fetch_counts('tax', 3, t1, t2, 'Others')
            itemcount_kwargs = itemcount.call_args[1]

        self.assertEqual(t1, itemcount_kwargs['start_time'])
        self.assertEqual(t2, itemcount_kwargs['end_time'])
    def test_fetch_counts_orders_by_long_name(self):
        widget = TermCountChartWidget()

        with patch('hid.widgets.term_count_chart.term_itemcount') as itemcount:
            itemcount.return_value = [
                {
                    'name': 'aaa-name',
                    'long_name': 'zzz-long-name',
                    'count': 0
                },
                {
                    'name': 'zzz-name',
                    'long_name': 'aaa-long-name',
                    'count': 1000
                },
            ]
            counts = widget._fetch_counts('tax', 0, None, None, 'Others')

        self.assertEqual(
            counts.items(),
            [('aaa-long-name', 1000), ('zzz-long-name', 0)]
        )
    def test_fetch_counts_gets_n_larger_and_aggregates_others_items(self):
        widget = TermCountChartWidget()

        with patch('hid.widgets.term_count_chart.term_itemcount') as itemcount:
            itemcount.return_value = [
                {
                    'name': 'name one',
                    'long_name': 'long-name one',
                    'count': 1
                },
                {
                    'name': 'name two',
                    'long_name': 'long-name two',
                    'count': 10
                },
                {
                    'name': 'name three',
                    'long_name': 'long-name three',
                    'count': 20
                },
                {
                    'name': 'name four',
                    'long_name': 'long-name four',
                    'count': 30
                },

            ]
            counts = widget._fetch_counts('tax', 3, None, None, 'Others')

        self.assertEqual(
            counts.items(),
            [
                ('long-name four', 30), ('long-name three', 20),
                ('Others', 11)
            ]
        )