<commit_before>from collections import namedtuple
from unittest.mock import MagicMock

from django.test import TestCase

from jarbas.core.models import Reimbursement
from jarbas.dashboard.admin import (
    ReceiptUrlWidget,
    ReimbursementModelAdmin,
    SubquotaWidget,
    SubuotaListfilter,
    SuspiciousWidget,
)


Request = namedtuple('Request', ('method',))
ReimbursementMock = namedtuple('Reimbursement', ('cnpj_cpf'))


class TestDashboardSite(TestCase):

    def setUp(self):
        self.requests = map(Request, ('GET', 'POST', 'PUT', 'PATCH', 'DELETE'))
        self.ma = ReimbursementModelAdmin(Reimbursement, 'dashboard')

    def test_has_add_permission(self):
        permissions = map(self.ma.has_add_permission, self.requests)
        self.assertNotIn(True, tuple(permissions))

    def test_has_change_permission(self):
        permissions = map(self.ma.has_change_permission, self.requests)
        expected = (True, False, False, False, False)
        self.assertEqual(expected, tuple(permissions))

    def test_has_delete_permission(self):
        permissions = map(self.ma.has_delete_permission, self.requests)
        self.assertNotIn(True, tuple(permissions))

    def test_format_document_with_cnpj(self):
        obj = ReimbursementMock('12345678901234')
        self.assertEqual('12.345.678/9012-34', self.ma._format_document(obj))

    def test_format_document_with_cpf(self):
        obj = ReimbursementMock('12345678901')
        self.assertEqual('123.456.789-01', self.ma._format_document(obj))

    def test_format_document_with_unknown(self):
        obj = ReimbursementMock('2345678')
        self.assertEqual('2345678', self.ma._format_document(obj))


class TestSubuotaListfilter(TestCase):

    def setUp(self):
        self.qs = MagicMock()
        self.list_filter = MagicMock()

    def test_queryset_without_subquota(self):
        self.list_filter.value.return_value = None
        SubuotaListfilter.queryset(self.list_filter, MagicMock(), self.qs)
        self.qs.filter.assert_not_called()

    def test_queryset_with_subquota(self):
        self.list_filter.value.return_value = 42
        SubuotaListfilter.queryset(self.list_filter, MagicMock(), self.qs)
        self.qs.filter.assert_called_once_with(subquota_id=42)


class TestCustomWidgets(TestCase):<commit_msg>Refactor tests for subquota list filter queryset method<commit_after>from collections import namedtuple
from unittest.mock import MagicMock, patch

from django.test import TestCase

from jarbas.core.models import Reimbursement
from jarbas.dashboard.admin import (
    ReceiptUrlWidget,
    ReimbursementModelAdmin,
    SubquotaWidget,
    SubquotaListFilter,
    SuspiciousWidget,
)


Request = namedtuple('Request', ('method',))
ReimbursementMock = namedtuple('Reimbursement', ('cnpj_cpf'))


class TestDashboardSite(TestCase):

    def setUp(self):
        self.requests = map(Request, ('GET', 'POST', 'PUT', 'PATCH', 'DELETE'))
        self.ma = ReimbursementModelAdmin(Reimbursement, 'dashboard')

    def test_has_add_permission(self):
        permissions = map(self.ma.has_add_permission, self.requests)
        self.assertNotIn(True, tuple(permissions))

    def test_has_change_permission(self):
        permissions = map(self.ma.has_change_permission, self.requests)
        expected = (True, False, False, False, False)
        self.assertEqual(expected, tuple(permissions))

    def test_has_delete_permission(self):
        permissions = map(self.ma.has_delete_permission, self.requests)
        self.assertNotIn(True, tuple(permissions))

    def test_format_document_with_cnpj(self):
        obj = ReimbursementMock('12345678901234')
        self.assertEqual('12.345.678/9012-34', self.ma._format_document(obj))

    def test_format_document_with_cpf(self):
        obj = ReimbursementMock('12345678901')
        self.assertEqual('123.456.789-01', self.ma._format_document(obj))

    def test_format_document_with_unknown(self):
        obj = ReimbursementMock('2345678')
        self.assertEqual('2345678', self.ma._format_document(obj))


class TestSubquotaListFilter(TestCase):

    def setUp(self):
        args = [MagicMock()] * 4  # SubquotaListFilter expectss 4 arguments
        self.list_filter = SubquotaListFilter(*args)
        self.qs = MagicMock()

    @patch.object(SubquotaListFilter, 'value')
    def test_queryset_without_subquota(self, value):
        value.return_value = None
        self.list_filter.queryset(MagicMock(), self.qs)
        self.qs.filter.assert_not_called()

    @patch.object(SubquotaListFilter, 'value')
    def test_queryset_with_subquota(self, value):
        value.return_value = 10
        self.list_filter.queryset(MagicMock(), self.qs)
        expected = dict(subquota_description='Telecommunication')
        self.qs.filter.assert_called_once_with(**expected)


class TestCustomWidgets(TestCase):
{'fraction_non_alphanumeric': 0.10666666666666667, 'fraction_numerical': 0.027897435897435898, 'mean_word_length': 4.127234490010515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '362311', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1501, 'n_words': 250}