<commit_before>from django.core import mail
from django.test import TestCase
from feder.monitorings.factory import factory_monitoring
from feder.cases.factory import factory_case
from feder.institutions.factory import factory_institution
from feder.cases.models import Case
from ..models import Letter

try:
    from django.contrib.auth import get_user_model
    User = get_user_model()
except ImportError:
    from django.contrib.auth.models import User


class ModelTestCase(TestCase):
    def setUp(self):
        self.user = User.objects.create(username="rob")
        self.l_u = Letter.objects.create(author_user=self.user,
                                         case=factory_case(self.user),
                                         title="Wniosek",
                                         body="Prosze przeslac informacje",
                                         email="<EMAIL>")

        self.l_i = Letter.objects.create(author_institution=factory_institution(self.user),
                                         case=factory_case(self.user),
                                         title="Odpowiedz",
                                         body="W zalaczeniu.",
                                         email="<EMAIL>")

    def test_is_incoming(self):
        self.assertTrue(self.l_i.is_incoming)
        self.assertFalse(self.l_u.is_incoming)

    def test_is_outgoing(self):
        self.assertTrue(self.l_u.is_outgoing)
        self.assertFalse(self.l_i.is_outgoing)

    def test_author(self):
        self.assertEqual(self.l_u.author, self.l_u.author_user)
        self.assertEqual(self.l_i.author, self.l_i.author_institution)

    def test_author_setter(self):
        u = User.objects.create(username="smith")
        self.l_u.author = u
        self.assertEqual(self.l_u.author_user, u)

        i = factory_institution(u)
        self.l_i.author = i
        self.assertEqual(self.l_i.author_institution, i)

    def test_queryset_is_incoming(self):
        self.assertEqual(Letter.objects.is_outgoing().get(), self.l_u)

    def test_queryset_is_outgoing(self):
        self.assertEqual(Letter.objects.is_incoming().get(), self.l_i)

    def test_send(self):
        self.l_u.send()
        self.assertEqual(len(mail.outbox), 1)
        self.assertIn(self.l_u.email, mail.outbox[0].to)


class NewLetterTestCase(TestCase):
    def setUp(self):
        self.user = User.objects.create(username="tom")

    def test_send_new(self):
        Letter.send_new_case(user=self.user,
                             monitoring=factory_monitoring(self.user),
                             institution=factory_institution(self.user),
                             text="Przeslac informacje szybko")
        self.assertEqual(Case.objects.count(), 1)
        self.assertEqual(Letter.objects.count(), 1)
        self.assertEqual(len(mail.outbox), 1)<commit_msg>Improve test coverage in models letters.models<commit_after>from django.core import mail
from django.test import TestCase
from feder.monitorings.factory import factory_monitoring
from feder.cases.factory import factory_case
from feder.institutions.factory import factory_institution
from feder.cases.models import Case
from ..models import Letter

try:
    from django.contrib.auth import get_user_model
    User = get_user_model()
except ImportError:
    from django.contrib.auth.models import User


class ModelTestCase(TestCase):
    def setUp(self):
        self.user = User.objects.create(username="rob")
        self.l_u = Letter.objects.create(author_user=self.user,
                                         case=factory_case(self.user),
                                         title="Wniosek",
                                         body="Prosze przeslac informacje",
                                         email="<EMAIL>")

        self.l_i = Letter.objects.create(author_institution=factory_institution(self.user),
                                         case=factory_case(self.user),
                                         title="Odpowiedz",
                                         body="W zalaczeniu.",
                                         email="<EMAIL>")

    def test_is_incoming(self):
        self.assertTrue(self.l_i.is_incoming)
        self.assertFalse(self.l_u.is_incoming)

    def test_is_outgoing(self):
        self.assertTrue(self.l_u.is_outgoing)
        self.assertFalse(self.l_i.is_outgoing)

    def test_author(self):
        self.assertEqual(self.l_u.author, self.l_u.author_user)
        self.assertEqual(self.l_i.author, self.l_i.author_institution)

    def test_author_setter(self):
        u = User.objects.create(username="smith")
        self.l_u.author = u
        self.assertEqual(self.l_u.author_user, u)

        i = factory_institution(u)
        self.l_i.author = i
        self.assertEqual(self.l_i.author_institution, i)

        with self.assertRaises(ValueError):
            self.l_i.author = self.l_i

    def test_queryset_is_incoming(self):
        self.assertEqual(Letter.objects.is_outgoing().get(), self.l_u)

    def test_queryset_is_outgoing(self):
        self.assertEqual(Letter.objects.is_incoming().get(), self.l_i)

    def test_send(self):
        self.l_u.send()
        self.assertEqual(len(mail.outbox), 1)
        self.assertIn(self.l_u.email, mail.outbox[0].to)

    def test_attachment_count(self):
        self.assertFalse(hasattr(Letter.objects.all()[0], 'attachment_count'))


class NewLetterTestCase(TestCase):
    def setUp(self):
        self.user = User.objects.create(username="tom")

    def test_send_new(self):
        Letter.send_new_case(user=self.user,
                             monitoring=factory_monitoring(self.user),
                             institution=factory_institution(self.user),
                             text="Przeslac informacje szybko")
        self.assertEqual(Case.objects.count(), 1)
        self.assertEqual(Letter.objects.count(), 1)
        self.assertEqual(len(mail.outbox), 1)
{'fraction_non_alphanumeric': 0.10073627844712182, 'fraction_numerical': 0.0018406961178045514, 'mean_word_length': 2.170822281167109, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5505998', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1690, 'n_words': 262}