Unit test analysis

* adds unit test for creating report
* fixes bug where new (>2018) eco accounts have not been fetched correctly from the  db
* adds enhancements in the frontend
* improves test data setup
pull/347/head
mpeltriaux 1 year ago
parent 345b266422
commit d9cf9669f0

@ -9,4 +9,8 @@ Created on: 19.10.21
# Defines the date of the legal publishing of the LKompVzVo
from django.utils import timezone
LKOMPVZVO_PUBLISH_DATE = timezone.make_aware(timezone.datetime.fromisoformat("2018-06-16")).date()
LKOMPVZVO_PUBLISH_DATE = timezone.make_aware(
timezone.datetime.fromisoformat(
"2018-06-16"
)
).date()

@ -31,6 +31,6 @@
{% include 'analysis/reports/includes/intervention/card_intervention.html' %}
{% include 'analysis/reports/includes/compensation/card_compensation.html' %}
{% include 'analysis/reports/includes/eco_account/card_eco_account.html' %}
{% include 'analysis/reports/includes/old_data/card_old_interventions.html' %}
{% include 'analysis/reports/includes/old_data/card_old_data.html' %}
</div>
{% endblock %}

@ -10,6 +10,7 @@
{% fa5_icon 'leaf' %}
{% trans 'Compensations' %}
</h5>
<span>{% trans 'Binding date after' %} 16.06.2018</span>
</div>
</div>
</div>

@ -10,6 +10,7 @@
{% fa5_icon 'tree' %}
{% trans 'Eco-Accounts' %}
</h5>
<span>{% trans 'Binding date after' %} 16.06.2018</span>
</div>
</div>
</div>

@ -9,6 +9,7 @@
{% fa5_icon 'pencil-ruler' %}
{% trans 'Interventions' %}
</h5>
<span>{% trans 'Binding date after' %} 16.06.2018</span>
</div>
</div>
</div>

@ -1,3 +0,0 @@
from django.test import TestCase
# Create your tests here.

@ -0,0 +1,7 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 15.08.23
"""

@ -0,0 +1,7 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 15.08.23
"""

@ -0,0 +1,47 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 15.08.23
"""
from datetime import timedelta
from django.urls import reverse
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from analysis.forms import TimespanReportForm
from konova.tests.test_views import BaseTestCase
class TimeSpanReportFormTestCase(BaseTestCase):
def setUp(self) -> None:
super().setUp()
eiv = self.create_dummy_intervention()
def test_init(self):
form = TimespanReportForm()
self.assertEqual(form.form_title, str(_("Generate report")))
self.assertEqual(form.form_caption, str(_("Select a timespan and the desired conservation office") ))
self.assertEqual(form.action_url, reverse("analysis:reports"))
self.assertFalse(form.show_cancel_btn)
self.assertEqual(form.action_btn_label, str(_("Continue")))
def test_save(self):
date_from = now().date() - timedelta(days=365)
date_to = now().date()
office = self.get_conservation_office_code()
data = {
"date_from": date_from,
"date_to": date_to,
"conservation_office": office,
}
form = TimespanReportForm(data)
self.assertTrue(form.is_valid(), msg=f"{form.errors}")
detail_report_url = form.save()
self.assertEqual(
detail_report_url,
reverse("analysis:report-detail", args=(office.id,)) + f"?df={date_from}&dt={date_to}"
)

@ -0,0 +1,94 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 17.08.23
"""
from datetime import timedelta
from django.utils.timezone import now
from analysis.settings import LKOMPVZVO_PUBLISH_DATE
from analysis.utils.report import TimespanReport
from konova.sub_settings.django_settings import DEFAULT_DATE_FORMAT
from konova.tests.test_views import BaseTestCase
class TimeSpanReportTestCase(BaseTestCase):
def setUp(self) -> None:
super().setUp()
today = now().date()
old_date = LKOMPVZVO_PUBLISH_DATE - timedelta(days=1)
self.conservation_office = self.get_conservation_office_code()
self.eiv_old = self.create_dummy_intervention()
self.kom_old = self.create_dummy_compensation(interv=self.eiv_old)
self.assertNotEqual(self.compensation.intervention, self.kom_old.intervention)
self.eiv = self.compensation.intervention
self.oek_old = self.create_dummy_eco_account()
self.eiv_old.responsible.conservation_office = self.conservation_office
self.eiv_old.legal.binding_date = old_date
self.eiv_old.legal.registration_date = old_date
self.eiv.responsible.conservation_office = self.conservation_office
self.eiv.legal.binding_date = today
self.eiv.legal.registration_date = today
self.eco_account.responsible.conservation_office = self.conservation_office
self.eco_account.legal.registration_date = today
self.eco_account.legal.binding_date = today
self.oek_old.responsible.conservation_office = self.conservation_office
self.oek_old.legal.registration_date = old_date
self.oek_old.legal.binding_date = old_date
self.eiv.legal.save()
self.eiv.responsible.save()
self.eiv_old.legal.save()
self.eiv_old.responsible.save()
self.eco_account.legal.save()
self.eco_account.responsible.save()
self.oek_old.legal.save()
self.oek_old.responsible.save()
def test_init(self):
date_from = now().date() - timedelta(days=365)
date_to = now().date()
report = TimespanReport(self.conservation_office.id, date_from, date_to)
self.assertEqual(report.office_id, self.conservation_office.id)
self.assertEqual(report.date_from, date_from)
self.assertEqual(report.date_to, date_to)
self.assertIsNotNone(report.intervention_report)
self.assertIsNotNone(report.compensation_report)
self.assertIsNotNone(report.eco_account_report)
self.assertIsNotNone(report.old_data_report)
self.assertEqual(report.excel_map["date_from"], date_from.strftime(DEFAULT_DATE_FORMAT))
self.assertEqual(report.excel_map["date_to"], date_to.strftime(DEFAULT_DATE_FORMAT))
self.assertEqual(report.old_data_report.queryset_intervention_count, 1)
self.assertEqual(report.old_data_report.queryset_intervention_recorded_count, 0)
self.assertEqual(report.old_data_report.queryset_comps_count, 1)
self.assertEqual(report.old_data_report.queryset_acc_count, 1)
self.assertEqual(report.old_data_report.queryset_acc_recorded_count, 0)
self.assertEqual(report.intervention_report.queryset_count, 1)
self.assertEqual(report.intervention_report.queryset_checked_count, 0)
self.assertEqual(report.intervention_report.queryset_recorded_count, 0)
self.assertEqual(report.compensation_report.queryset_count, 1)
self.assertEqual(report.compensation_report.queryset_checked_count, 0)
self.assertEqual(report.compensation_report.queryset_recorded_count, 0)
self.assertEqual(report.eco_account_report.queryset_count, 1)
self.assertEqual(report.eco_account_report.queryset_recorded_count, 0)
self.assertEqual(report.eco_account_report.queryset_deductions_count, 1)
self.assertEqual(report.eco_account_report.queryset_deductions_recorded_count, 0)

@ -413,6 +413,7 @@ class TimespanReport:
def __init__(self, id: str, date_from: str, date_to: str):
# First fetch all eco account for this office
self.queryset = EcoAccount.objects.filter(
legal__registration_date__gt=LKOMPVZVO_PUBLISH_DATE,
responsible__conservation_office__id=id,
deleted=None,
created__timestamp__date__gte=date_from,
@ -516,8 +517,8 @@ class TimespanReport:
legal__registration_date__lte=LKOMPVZVO_PUBLISH_DATE,
responsible__conservation_office__id=id,
deleted=None,
created__timestamp__gte=date_from,
created__timestamp__lte=date_to,
created__timestamp__date__gte=date_from,
created__timestamp__date__lte=date_to,
)
self.queryset_acc_recorded = self.queryset_acc.filter(
recorded__isnull=False,

@ -65,7 +65,7 @@ class BaseTestCase(TestCase):
self.compensation = self.create_dummy_compensation()
self.eco_account = self.create_dummy_eco_account()
self.ema = self.create_dummy_ema()
self.deduction = self.create_dummy_deduction()
self.deduction = self.create_dummy_deduction(acc=self.eco_account, interv=self.intervention)
self.create_dummy_states()
self.create_dummy_action()
self.codes = self.create_dummy_codes()
@ -157,14 +157,17 @@ class BaseTestCase(TestCase):
intervention.generate_access_token(make_unique=True)
return intervention
def create_dummy_compensation(self):
def create_dummy_compensation(self, interv: Intervention=None):
""" Creates a compensation which can be used for tests
Returns:
"""
if self.intervention is None:
self.intervention = self.create_dummy_intervention()
if not interv:
if self.intervention is None:
interv = self.create_dummy_intervention()
else:
interv = self.intervention
# Create dummy data
# Create log entry
action = UserActionLogEntry.get_created_action(self.superuser)
@ -173,7 +176,7 @@ class BaseTestCase(TestCase):
compensation = Compensation.objects.create(
identifier="TEST",
title="Test_title",
intervention=self.intervention,
intervention=interv,
created=action,
geometry=geometry,
comment="Test",
@ -196,9 +199,11 @@ class BaseTestCase(TestCase):
handler = self.handler
responsible_data.handler = handler
responsible_data.save()
identifier = EcoAccount().generate_new_identifier()
# Finally create main object, holding the other objects
eco_account = EcoAccount.objects.create(
identifier="TEST",
identifier=identifier,
title="Test_title",
deductable_surface=500,
legal=lega_data,
@ -234,10 +239,15 @@ class BaseTestCase(TestCase):
)
return ema
def create_dummy_deduction(self):
def create_dummy_deduction(self, acc: EcoAccount = None, interv: Intervention = None):
if not acc:
acc = self.create_dummy_eco_account()
if not interv:
interv = self.create_dummy_intervention()
return EcoAccountDeduction.objects.create(
account=self.create_dummy_eco_account(),
intervention=self.create_dummy_intervention(),
account=acc,
intervention=interv,
surface=100,
)
@ -270,12 +280,14 @@ class BaseTestCase(TestCase):
Returns:
"""
codes = KonovaCode.objects.bulk_create([
KonovaCode(id=1, is_selectable=True, is_archived=False, is_leaf=True, long_name="Test1"),
KonovaCode(id=2, is_selectable=True, is_archived=False, is_leaf=True, long_name="Test2"),
KonovaCode(id=3, is_selectable=True, is_archived=False, is_leaf=True, long_name="Test3"),
KonovaCode(id=4, is_selectable=True, is_archived=False, is_leaf=True, long_name="Test4"),
])
codes = KonovaCode.objects.all()
if codes.count() == 0:
codes = KonovaCode.objects.bulk_create([
KonovaCode(id=1, is_selectable=True, is_archived=False, is_leaf=True, long_name="Test1"),
KonovaCode(id=2, is_selectable=True, is_archived=False, is_leaf=True, long_name="Test2"),
KonovaCode(id=3, is_selectable=True, is_archived=False, is_leaf=True, long_name="Test3"),
KonovaCode(id=4, is_selectable=True, is_archived=False, is_leaf=True, long_name="Test4"),
])
return codes
def create_dummy_team(self):

Binary file not shown.

@ -43,7 +43,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-07-10 10:16+0200\n"
"POT-Creation-Date: 2023-08-17 10:09+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -96,15 +96,16 @@ msgstr "Verantwortliche Stelle"
msgid "Click for selection"
msgstr "Auswählen..."
#: analysis/forms.py:70
#: analysis/forms.py:70 analysis/tests/unit/test_forms.py:25
msgid "Generate report"
msgstr "Bericht generieren"
#: analysis/forms.py:71
#: analysis/forms.py:71 analysis/tests/unit/test_forms.py:26
msgid "Select a timespan and the desired conservation office"
msgstr "Wählen Sie die Zeitspanne und die gewünschte Eintragungsstelle"
#: analysis/forms.py:74 konova/forms/modals/base_form.py:30
#: analysis/forms.py:74 analysis/tests/unit/test_forms.py:29
#: konova/forms/modals/base_form.py:30
msgid "Continue"
msgstr "Weiter"
@ -231,6 +232,12 @@ msgstr "Andere Zulassungsbehörden"
msgid "Compensations"
msgstr "Kompensationen"
#: analysis/templates/analysis/reports/includes/compensation/card_compensation.html:13
#: analysis/templates/analysis/reports/includes/eco_account/card_eco_account.html:13
#: analysis/templates/analysis/reports/includes/intervention/card_intervention.html:12
msgid "Binding date after"
msgstr "Bestandskraft- bzw. Rechtskraftdatum nach"
#: analysis/templates/analysis/reports/includes/eco_account/card_eco_account.html:11
msgid "Eco-Accounts"
msgstr "Ökokonten"
@ -345,11 +352,11 @@ msgstr "Eingriff"
msgid "Eco-account"
msgstr "Ökokonto"
#: analysis/templates/analysis/reports/includes/old_data/card_old_interventions.html:11
#: analysis/templates/analysis/reports/includes/old_data/card_old_data.html:11
msgid "Old interventions"
msgstr "Altfälle"
#: analysis/templates/analysis/reports/includes/old_data/card_old_interventions.html:13
#: analysis/templates/analysis/reports/includes/old_data/card_old_data.html:13
msgid "Binding date before"
msgstr "Bestandskraft- bzw. Rechtskraftdatum vor"
@ -2291,7 +2298,7 @@ msgstr ""
"Dieses Datum ist unrealistisch. Geben Sie bitte das korrekte Datum ein "
"(>1950)."
#: konova/views/home.py:74 templates/navbars/navbar.html:16
#: konova/views/home.py:75 templates/navbars/navbar.html:16
msgid "Home"
msgstr "Home"
@ -2299,13 +2306,13 @@ msgstr "Home"
msgid "Log"
msgstr "Log"
#: konova/views/map_proxy.py:71
#: konova/views/map_proxy.py:70
msgid ""
"The external service is currently unavailable.<br>Please try again in a few "
"moments..."
msgstr ""
"Der externe Dienst ist zur Zeit nicht erreichbar.<br>Versuchen Sie es in ein paar "
"Sekunden nochmal."
"Der externe Dienst ist zur Zeit nicht erreichbar.<br>Versuchen Sie es in ein "
"paar Sekunden nochmal."
#: konova/views/record.py:30
msgid "{} unrecorded"

Loading…
Cancel
Save