From 7e1644d5c199cb23aae2afcbc5febfed1b934f72 Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Mon, 9 Oct 2023 22:43:27 +0200 Subject: [PATCH] =?UTF-8?q?Parci=C3=A1ln=C3=AD=20fix=20testu=20d=C4=9Bl?= =?UTF-8?q?=C3=A1n=C3=AD=20org=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- personalni/tests.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/personalni/tests.py b/personalni/tests.py index 633ff1d4..6f00cd1e 100644 --- a/personalni/tests.py +++ b/personalni/tests.py @@ -1,6 +1,8 @@ -from django.test import TestCase +from django.test import TestCase, RequestFactory from django.contrib.auth.models import User, Group +from django.contrib.admin.sites import AdminSite +from personalni.admin import OsobaAdmin # Tohle bude peklo, až jednou ty modely fakt rozstřelíme… Možná vyrobit various.all_models, které půjdou importovat jako m? :-) import seminar.models as m @@ -9,6 +11,15 @@ logger = logging.getLogger(__name__) class DelaniOrguTest(TestCase): def setUp(self): + # Admin musí mít instanci + # Ref: https://www.argpar.se/posts/programming/testing-django-admin/ + adm_site = AdminSite() + self.admin = OsobaAdmin(m.Osoba, adm_site) + + from django.contrib.messages.storage.cookie import CookieStorage + self.request = RequestFactory().get('/admin') + self.request._messages = CookieStorage(self.request) + self.org_group = Group.objects.get(name='org') novy_user = User.objects.create(username='osoba') @@ -31,9 +42,8 @@ class DelaniOrguTest(TestCase): breakpoint # Pak orga uděláme… - from personalni.admin import OsobaAdmin qs = m.Osoba.objects.filter(id=self.nova_osoba.id) - OsobaAdmin.udelej_orgem(None, None, qs) + self.admin.udelej_orgem(self.request, qs) # A pak už to org má být. logger.info(f'Nová osoba je staff: {self.nova_osoba.user.is_staff}') @@ -45,8 +55,7 @@ class DelaniOrguTest(TestCase): self.assertIsNotNone(novy_org.organizuje_od) def test_pridani_stareho_orga(self): - from personalni.admin import OsobaAdmin - OsobaAdmin.udelej_orgem(None, None, m.Osoba.objects.filter(id=self.stary_org.osoba.id)) # Ugly + self.admin.udelej_orgem(self.request, m.Osoba.objects.filter(id=self.stary_org.osoba.id)) # Ugly # Když to spadne, tak jsem se to dozvěděl, takže už nepotřebuju nic kontrolovat. # Jestli to funguje správně má řešit jiný test.