mpeltriaux
7430a239a5
* adds unit test for new/edit intervention forms * improves code base for generating new identifiers
124 lines
4.8 KiB
Python
124 lines
4.8 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: ksp-servicestelle@sgdnord.rlp.de
|
|
Created on: 24.08.23
|
|
|
|
"""
|
|
import json
|
|
|
|
from django.urls import reverse
|
|
from django.utils.timezone import now
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from intervention.forms.intervention import NewInterventionForm, EditInterventionForm
|
|
from konova.forms import SimpleGeomForm
|
|
from konova.tests.test_views import BaseTestCase
|
|
from konova.utils.generators import generate_random_string
|
|
from user.models import UserAction
|
|
|
|
|
|
class NewInterventionFormTestCase(BaseTestCase):
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
|
|
def test_init(self):
|
|
form = NewInterventionForm()
|
|
self.assertEqual(form.form_title, str(_("New intervention")))
|
|
self.assertEqual(form.action_url, reverse("intervention:new"))
|
|
self.assertEqual(form.cancel_redirect, reverse("intervention:index"))
|
|
|
|
initial_identifier = form.fields["identifier"].initial
|
|
self.assertIsNotNone(initial_identifier)
|
|
self.assertIn("EIV-", initial_identifier)
|
|
|
|
def test_is_valid(self):
|
|
data = {
|
|
"identifier": generate_random_string(length=15, use_letters_uc=True),
|
|
"title": generate_random_string(length=15, use_letters_uc=True),
|
|
}
|
|
form = NewInterventionForm({})
|
|
self.assertFalse(form.is_valid())
|
|
form = NewInterventionForm(data)
|
|
self.assertTrue(form.is_valid(), msg=form.errors)
|
|
|
|
def test_save(self):
|
|
data = {
|
|
"identifier": generate_random_string(length=15, use_letters_uc=True),
|
|
"title": generate_random_string(length=15, use_letters_uc=True),
|
|
}
|
|
test_geom = self.create_dummy_geometry()
|
|
geom_form_data = self.create_geojson(
|
|
test_geom
|
|
)
|
|
geom_form_data = json.loads(geom_form_data)
|
|
geom_form_data = {
|
|
"geom": json.dumps(geom_form_data)
|
|
}
|
|
geom_form = SimpleGeomForm(geom_form_data)
|
|
|
|
form = NewInterventionForm(data)
|
|
self.assertTrue(form.is_valid())
|
|
self.assertTrue(geom_form.is_valid())
|
|
obj = form.save(self.superuser, geom_form)
|
|
|
|
self.assertEqual(obj.identifier, data["identifier"])
|
|
self.assertEqual(obj.title, data["title"])
|
|
self.assertIsNotNone(obj.legal)
|
|
self.assertIsNotNone(obj.responsible)
|
|
self.assertIsNotNone(obj.responsible.handler)
|
|
self.assertEqual(obj.created.action, UserAction.CREATED)
|
|
self.assertEqual(obj.created.user, self.superuser)
|
|
self.assertEqual(obj.created, obj.log.first())
|
|
self.assertEqual(obj.created, obj.modified)
|
|
|
|
self.assertIn(self.superuser, obj.shared_users)
|
|
self.assertTrue(test_geom.equals_exact(obj.geometry.geom, 0.000001))
|
|
|
|
|
|
class EditInterventionFormTestCase(NewInterventionFormTestCase):
|
|
|
|
def test_init(self):
|
|
today = now().date()
|
|
data = {
|
|
"identifier": self.intervention.identifier,
|
|
"title": generate_random_string(length=5, use_letters_lc=True),
|
|
"comment": generate_random_string(length=5, use_letters_lc=True),
|
|
"registration_date": today,
|
|
"binding_date": today,
|
|
"registration_file_number": generate_random_string(length=5, use_numbers=True),
|
|
"conservation_file_number": generate_random_string(length=5, use_numbers=True),
|
|
}
|
|
test_geom = self.create_dummy_geometry()
|
|
geom_form_data = self.create_geojson(
|
|
test_geom
|
|
)
|
|
geom_form_data = json.loads(geom_form_data)
|
|
geom_form_data = {
|
|
"geom": json.dumps(geom_form_data)
|
|
}
|
|
|
|
geom_form = SimpleGeomForm(geom_form_data)
|
|
form = EditInterventionForm(data, instance=self.intervention)
|
|
self.assertTrue(geom_form.is_valid())
|
|
self.assertTrue(form.is_valid())
|
|
|
|
obj = form.save(self.superuser, geom_form)
|
|
|
|
last_log = obj.log.first()
|
|
self.assertEqual(last_log.user, self.superuser)
|
|
self.assertEqual(last_log.action, UserAction.EDITED)
|
|
self.assertEqual(last_log, obj.modified)
|
|
self.assertEqual(obj.identifier, self.intervention.identifier)
|
|
self.assertIsNotNone(obj.legal)
|
|
self.assertIsNotNone(obj.responsible)
|
|
self.assertIsNotNone(obj.responsible.handler)
|
|
self.assertEqual(obj.title, data["title"])
|
|
self.assertEqual(obj.comment, data["comment"])
|
|
self.assertTrue(test_geom.equals_exact(obj.geometry.geom, 0.000001))
|
|
|
|
self.assertEqual(obj.legal.binding_date, today)
|
|
self.assertEqual(obj.legal.registration_date, today)
|
|
self.assertEqual(obj.responsible.registration_file_number, data["registration_file_number"])
|
|
self.assertEqual(obj.responsible.conservation_file_number, data["conservation_file_number"])
|