Skip to content

Commit

Permalink
Add unit-test for a Fault Report Log
Browse files Browse the repository at this point in the history
  • Loading branch information
SukiCZ committed Jan 20, 2025
1 parent bdcc170 commit 2a3fa25
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
2 changes: 2 additions & 0 deletions svjis/articles/tests/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .preferences import PreferencesFactory
from .company import CompanyFactory
from .advert_type import AdvertTypeFactory
from .fault_report import FaultReportFactory

__all__ = [
"ContentTypeFactory",
Expand All @@ -18,4 +19,5 @@
"PreferencesFactory",
"CompanyFactory",
"AdvertTypeFactory",
"FaultReportFactory",
]
17 changes: 17 additions & 0 deletions svjis/articles/tests/factories/fault_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import factory

from .user import UserFactory
from ...models import FaultReport


class FaultReportFactory(factory.django.DjangoModelFactory):
subject = factory.Faker('sentence', nb_words=3)
slug = factory.Faker('slug')
description = factory.Faker('text')
created_date = factory.Faker('date_time')
created_by_user = factory.SubFactory(UserFactory)
assigned_to_user = factory.SubFactory(UserFactory)
closed = factory.Faker('boolean')

class Meta:
model = FaultReport
54 changes: 51 additions & 3 deletions svjis/articles/tests/test_faults.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,70 @@
from django.test import TestCase
from django.urls import reverse

from .testdata import UserDataMixin
from .factories.fault_report import FaultReportFactory
from .testdata import UserDataMixin, PreferencesDataMixin


class FaultsTest(UserDataMixin, TestCase):
class FaultsTest(UserDataMixin, PreferencesDataMixin, TestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.fault = FaultReportFactory(created_by_user=cls.u_jiri, closed=False)

cls.create_url = reverse('faults_fault_create_save')
cls.detail_url = reverse('fault', kwargs={'slug': cls.fault.slug})
cls.update_url = reverse('faults_fault_update')

def create_fault_and_get_created_by(self, username, password, fault_form):
logged_in = self.client.login(username=username, password=password)
self.assertTrue(logged_in)
response = self.client.post(
reverse('faults_fault_create_save'),
self.create_url,
fault_form,
follow=True,
)
self.assertEqual(response.status_code, 200)
return response.context['obj']

def test_retrieve(self):
logged_in = self.client.login(username='jiri', password=self.u_jiri_password)
self.assertTrue(logged_in)
response = self.client.get(self.detail_url)
self.assertEqual(response.status_code, 200)
fault = response.context['obj']
self.assertEqual(fault, self.fault)
self.assertEqual(fault.logs.count(), 0)

def test_close_fault_retrieve_logs(self):
logged_in = self.client.login(username='jiri', password=self.u_jiri_password)
self.assertTrue(logged_in)
response = self.client.post(
self.update_url,
{
'assigned_to_user': self.u_jarda.pk,
'closed': True,
'pk': self.fault.pk,
'subject': self.fault.subject,
'description': self.fault.description,
'created_by_user': self.fault.created_by_user.pk,
},
follow=True,
)
self.assertEqual(response.status_code, 200)

self.fault.refresh_from_db()

self.assertEqual(self.fault.closed, True)
self.assertEqual(self.fault.logs.count(), 2)
assigned, closed = self.fault.logs.all()
self.assertEqual(assigned.type, assigned.TYPE_ASSIGNED)
self.assertEqual(assigned.resolver, self.u_jarda)
self.assertEqual(assigned.user, self.u_jiri)
self.assertEqual(closed.type, closed.TYPE_CLOSED)
self.assertEqual(closed.resolver, self.u_jarda)
self.assertEqual(closed.user, self.u_jiri)

# Created by user
def test_owner_created_by_user(self):
fault = self.create_fault_and_get_created_by(
Expand Down
3 changes: 3 additions & 0 deletions svjis/articles/tests/testdata/preferences_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ def setUpTestData(cls):
cls.p_fault_assigned = PreferencesFactory(
key="mail.template.fault.assigned", value="Uživatel {} vám přiřadil tiket {}: <br><br><br>{}"
)
cls.p_fault_closed = PreferencesFactory(
key="mail.template.fault.closed", value="Uživatel {} uzavřel tiket {}: <br><br><br>{}"
)

0 comments on commit 2a3fa25

Please sign in to comment.