#19 Tests
* adds EMA tests * extends compensation tests * fixes bugs detected by testing * restructured tests for performance boost
This commit is contained in:
@@ -11,7 +11,7 @@ from django.contrib.auth.models import User, Group
|
||||
from django.test import TestCase, Client
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.models import Compensation
|
||||
from compensation.models import Compensation, CompensationState, CompensationAction
|
||||
from intervention.models import LegalData, ResponsibilityData, Intervention
|
||||
from konova.management.commands.setup_data import GROUPS_DATA
|
||||
from konova.models import Geometry
|
||||
@@ -30,33 +30,30 @@ class BaseTestCase(TestCase):
|
||||
superuser_pw = "root"
|
||||
user_pw = "root"
|
||||
|
||||
@abstractmethod
|
||||
def setUp(self) -> None:
|
||||
# To be implemented in the inheriting classes
|
||||
raise NotImplementedError
|
||||
|
||||
def create_users(self):
|
||||
@classmethod
|
||||
def create_users(cls):
|
||||
# Create superuser and regular user
|
||||
self.superuser = User.objects.create_superuser(
|
||||
cls.superuser = User.objects.create_superuser(
|
||||
username="root",
|
||||
email="root@root.com",
|
||||
password=self.superuser_pw,
|
||||
password=cls.superuser_pw,
|
||||
)
|
||||
self.user = User.objects.create_user(
|
||||
cls.user = User.objects.create_user(
|
||||
username="user1",
|
||||
email="user@root.com",
|
||||
password=self.user_pw
|
||||
password=cls.user_pw
|
||||
)
|
||||
self.users = User.objects.all()
|
||||
cls.users = User.objects.all()
|
||||
|
||||
def create_groups(self):
|
||||
@classmethod
|
||||
def create_groups(cls):
|
||||
# Create groups
|
||||
for group_data in GROUPS_DATA:
|
||||
name = group_data.get("name")
|
||||
Group.objects.get_or_create(
|
||||
name=name,
|
||||
)
|
||||
self.groups = Group.objects.all()
|
||||
cls.groups = Group.objects.all()
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
@@ -69,13 +66,16 @@ class BaseViewTestCase(BaseTestCase):
|
||||
login_url = None
|
||||
intervention = None
|
||||
compensation = None
|
||||
comp_state = None
|
||||
comp_action = None
|
||||
|
||||
def setUp(self) -> None:
|
||||
self.create_users()
|
||||
self.create_groups()
|
||||
self.create_dummy_intervention()
|
||||
self.create_dummy_compensation()
|
||||
self.login_url = reverse("simple-sso-login")
|
||||
@classmethod
|
||||
def setUpTestData(cls) -> None:
|
||||
cls.create_users()
|
||||
cls.create_groups()
|
||||
cls.create_dummy_intervention()
|
||||
cls.create_dummy_compensation()
|
||||
cls.login_url = reverse("simple-sso-login")
|
||||
|
||||
def assert_url_success(self, client: Client, urls: list):
|
||||
""" Assert for all given urls a direct 200 response
|
||||
@@ -122,7 +122,8 @@ class BaseViewTestCase(BaseTestCase):
|
||||
response = client.get(url)
|
||||
self.assertEqual(response.status_code, 302, msg=f"Failed for {url}")
|
||||
|
||||
def create_dummy_intervention(self):
|
||||
@classmethod
|
||||
def create_dummy_intervention(cls):
|
||||
""" Creates an intervention which can be used for tests
|
||||
|
||||
Returns:
|
||||
@@ -131,7 +132,7 @@ class BaseViewTestCase(BaseTestCase):
|
||||
# Create dummy data
|
||||
# Create log entry
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=self.superuser,
|
||||
user=cls.superuser,
|
||||
action=UserAction.CREATED,
|
||||
)
|
||||
# Create legal data object (without M2M laws first)
|
||||
@@ -140,7 +141,7 @@ class BaseViewTestCase(BaseTestCase):
|
||||
responsibility_data = ResponsibilityData.objects.create()
|
||||
geometry = Geometry.objects.create()
|
||||
# Finally create main object, holding the other objects
|
||||
self.intervention = Intervention.objects.create(
|
||||
cls.intervention = Intervention.objects.create(
|
||||
identifier="TEST",
|
||||
title="Test_title",
|
||||
responsible=responsibility_data,
|
||||
@@ -149,43 +150,70 @@ class BaseViewTestCase(BaseTestCase):
|
||||
geometry=geometry,
|
||||
comment="Test",
|
||||
)
|
||||
self.intervention.generate_access_token(make_unique=True)
|
||||
cls.intervention.generate_access_token(make_unique=True)
|
||||
|
||||
def create_dummy_compensation(self):
|
||||
@classmethod
|
||||
def create_dummy_compensation(cls):
|
||||
""" Creates an intervention which can be used for tests
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if self.intervention is None:
|
||||
self.create_dummy_intervention()
|
||||
if cls.intervention is None:
|
||||
cls.create_dummy_intervention()
|
||||
# Create dummy data
|
||||
# Create log entry
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=self.superuser,
|
||||
user=cls.superuser,
|
||||
action=UserAction.CREATED,
|
||||
)
|
||||
geometry = Geometry.objects.create()
|
||||
# Finally create main object, holding the other objects
|
||||
self.compensation = Compensation.objects.create(
|
||||
cls.compensation = Compensation.objects.create(
|
||||
identifier="TEST",
|
||||
title="Test_title",
|
||||
intervention=self.intervention,
|
||||
intervention=cls.intervention,
|
||||
created=action,
|
||||
geometry=geometry,
|
||||
comment="Test",
|
||||
)
|
||||
self.intervention.generate_access_token(make_unique=True)
|
||||
cls.intervention.generate_access_token(make_unique=True)
|
||||
|
||||
@classmethod
|
||||
def create_dummy_states(cls):
|
||||
""" Creates an intervention which can be used for tests
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
cls.comp_state = CompensationState.objects.create(
|
||||
surface=10.00,
|
||||
biotope_type=None,
|
||||
)
|
||||
return cls.comp_state
|
||||
|
||||
@classmethod
|
||||
def create_dummy_action(cls):
|
||||
""" Creates an intervention which can be used for tests
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
cls.comp_action = CompensationAction.objects.create(
|
||||
amount=10
|
||||
)
|
||||
return cls.comp_action
|
||||
|
||||
|
||||
class KonovaViewTestCase(BaseViewTestCase):
|
||||
""" Holds tests for all regular views, which are not app specific
|
||||
|
||||
"""
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
@classmethod
|
||||
def setUpTestData(cls) -> None:
|
||||
super().setUpTestData()
|
||||
|
||||
self.home_url = reverse("home")
|
||||
cls.home_url = reverse("home")
|
||||
|
||||
def test_views_logged_in_no_groups(self):
|
||||
""" Check correct status code for all requests
|
||||
@@ -221,17 +249,18 @@ class KonovaViewTestCase(BaseViewTestCase):
|
||||
|
||||
|
||||
class AutocompleteTestCase(BaseViewTestCase):
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.atcmplt_accs = reverse("accounts-autocomplete")
|
||||
self.atcmplt_interventions = reverse("interventions-autocomplete")
|
||||
self.atcmplt_code_comp_action = reverse("codes-compensation-action-autocomplete")
|
||||
self.atcmplt_code_comp_funding = reverse("codes-compensation-funding-autocomplete")
|
||||
self.atcmplt_code_comp_biotope = reverse("codes-biotope-autocomplete")
|
||||
self.atcmplt_code_comp_law = reverse("codes-law-autocomplete")
|
||||
self.atcmplt_code_comp_process = reverse("codes-process-type-autocomplete")
|
||||
self.atcmplt_code_comp_reg_off = reverse("codes-registration-office-autocomplete")
|
||||
self.atcmplt_code_comp_cons_off = reverse("codes-conservation-office-autocomplete")
|
||||
@classmethod
|
||||
def setUpTestData(cls) -> None:
|
||||
super().setUpTestData()
|
||||
cls.atcmplt_accs = reverse("accounts-autocomplete")
|
||||
cls.atcmplt_interventions = reverse("interventions-autocomplete")
|
||||
cls.atcmplt_code_comp_action = reverse("codes-compensation-action-autocomplete")
|
||||
cls.atcmplt_code_comp_funding = reverse("codes-compensation-funding-autocomplete")
|
||||
cls.atcmplt_code_comp_biotope = reverse("codes-biotope-autocomplete")
|
||||
cls.atcmplt_code_comp_law = reverse("codes-law-autocomplete")
|
||||
cls.atcmplt_code_comp_process = reverse("codes-process-type-autocomplete")
|
||||
cls.atcmplt_code_comp_reg_off = reverse("codes-registration-office-autocomplete")
|
||||
cls.atcmplt_code_comp_cons_off = reverse("codes-conservation-office-autocomplete")
|
||||
|
||||
def _test_views_anonymous_user(self):
|
||||
# ATTENTION: As of the current state of django-autocomplete-light, there is no way to check on authenticated
|
||||
|
||||
Reference in New Issue
Block a user