#19 Tests
* adds workflow testing for geometries on new creation of intervention and compensation *
This commit is contained in:
		
							parent
							
								
									ce7033943f
								
							
						
					
					
						commit
						ea14a01f89
					
				@ -37,6 +37,63 @@ class CompensationWorkflowTestCase(BaseWorkflowTestCase):
 | 
				
			|||||||
        # Create a fresh dummy (non-valid) compensation before each test
 | 
					        # Create a fresh dummy (non-valid) compensation before each test
 | 
				
			||||||
        self.compensation = self.create_dummy_compensation()
 | 
					        self.compensation = self.create_dummy_compensation()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_new(self):
 | 
				
			||||||
 | 
					        """ Test the creation of a compensation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Returns:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        # Prepare url and form data to be posted
 | 
				
			||||||
 | 
					        new_url = reverse("compensation:new")
 | 
				
			||||||
 | 
					        test_id = self.create_dummy_string()
 | 
				
			||||||
 | 
					        test_title = self.create_dummy_string()
 | 
				
			||||||
 | 
					        test_geom = self.create_dummy_geometry()
 | 
				
			||||||
 | 
					        post_data = {
 | 
				
			||||||
 | 
					            "identifier": test_id,
 | 
				
			||||||
 | 
					            "title": test_title,
 | 
				
			||||||
 | 
					            "geom": test_geom.geojson,
 | 
				
			||||||
 | 
					            "intervention": self.intervention.id,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Preserve the current number of intervention's compensations
 | 
				
			||||||
 | 
					        num_compensations = self.intervention.compensations.count()
 | 
				
			||||||
 | 
					        self.client_user.post(new_url, post_data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.intervention.refresh_from_db()
 | 
				
			||||||
 | 
					        self.assertEqual(num_compensations + 1, self.intervention.compensations.count())
 | 
				
			||||||
 | 
					        new_compensation = self.intervention.compensations.get(identifier=test_id)
 | 
				
			||||||
 | 
					        self.assertEqual(new_compensation.identifier, test_id)
 | 
				
			||||||
 | 
					        self.assertEqual(new_compensation.title, test_title)
 | 
				
			||||||
 | 
					        self.assert_equal_geometries(new_compensation.geometry.geom, test_geom)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_new_from_intervention(self):
 | 
				
			||||||
 | 
					        """ Test the creation of a compensation from a given intervention
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Returns:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        # Prepare url and form data to be posted
 | 
				
			||||||
 | 
					        new_url = reverse("compensation:new", args=(self.intervention.id,))
 | 
				
			||||||
 | 
					        test_id = self.create_dummy_string()
 | 
				
			||||||
 | 
					        test_title = self.create_dummy_string()
 | 
				
			||||||
 | 
					        test_geom = self.create_dummy_geometry()
 | 
				
			||||||
 | 
					        post_data = {
 | 
				
			||||||
 | 
					            "identifier": test_id,
 | 
				
			||||||
 | 
					            "title": test_title,
 | 
				
			||||||
 | 
					            "geom": test_geom.geojson,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Preserve the current number of intervention's compensations
 | 
				
			||||||
 | 
					        num_compensations = self.intervention.compensations.count()
 | 
				
			||||||
 | 
					        self.client_user.post(new_url, post_data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.intervention.refresh_from_db()
 | 
				
			||||||
 | 
					        self.assertEqual(num_compensations + 1, self.intervention.compensations.count())
 | 
				
			||||||
 | 
					        new_compensation = self.intervention.compensations.get(identifier=test_id)
 | 
				
			||||||
 | 
					        self.assertEqual(new_compensation.identifier, test_id)
 | 
				
			||||||
 | 
					        self.assertEqual(new_compensation.title, test_title)
 | 
				
			||||||
 | 
					        self.assert_equal_geometries(new_compensation.geometry.geom, test_geom)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_checkability(self):
 | 
					    def test_checkability(self):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        This tests if the checkability of the compensation (which is defined by the linked intervention's checked
 | 
					        This tests if the checkability of the compensation (which is defined by the linked intervention's checked
 | 
				
			||||||
 | 
				
			|||||||
@ -356,7 +356,7 @@ class EditInterventionForm(NewInterventionForm):
 | 
				
			|||||||
            self.instance.save()
 | 
					            self.instance.save()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Uncheck and unrecord intervention due to changed data
 | 
					            # Uncheck and unrecord intervention due to changed data
 | 
				
			||||||
            self.instance.set_unchecked(user)
 | 
					            self.instance.set_unchecked()
 | 
				
			||||||
            self.instance.set_unrecorded(user)
 | 
					            self.instance.set_unrecorded(user)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return self.instance
 | 
					        return self.instance
 | 
				
			||||||
 | 
				
			|||||||
@ -39,7 +39,9 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        # Define the intervention identifier for easier handling on the next lines
 | 
					        # Define the intervention identifier for easier handling on the next lines
 | 
				
			||||||
        test_id = "Test_IDENTIFIER"
 | 
					        test_id = self.create_dummy_string()
 | 
				
			||||||
 | 
					        test_title = self.create_dummy_string()
 | 
				
			||||||
 | 
					        test_geom = self.create_dummy_geometry()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        new_url = reverse("intervention:new", args=())
 | 
					        new_url = reverse("intervention:new", args=())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -52,8 +54,8 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
 | 
				
			|||||||
        # User creates a new intervention with bare minimum content, using the proper url and post data
 | 
					        # User creates a new intervention with bare minimum content, using the proper url and post data
 | 
				
			||||||
        post_data = {
 | 
					        post_data = {
 | 
				
			||||||
            "identifier": test_id,
 | 
					            "identifier": test_id,
 | 
				
			||||||
            "title": "Test_TITLE",
 | 
					            "title": test_title,
 | 
				
			||||||
            "geometry": "",
 | 
					            "geom": test_geom.geojson,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        response = self.client_user.post(
 | 
					        response = self.client_user.post(
 | 
				
			||||||
            new_url,
 | 
					            new_url,
 | 
				
			||||||
@ -66,6 +68,8 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
 | 
				
			|||||||
                identifier=test_id
 | 
					                identifier=test_id
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            self.assertEqual(obj.identifier, test_id)
 | 
					            self.assertEqual(obj.identifier, test_id)
 | 
				
			||||||
 | 
					            self.assertEqual(obj.title, test_title)
 | 
				
			||||||
 | 
					            self.assert_equal_geometries(obj.geometry.geom, test_geom)
 | 
				
			||||||
        except ObjectDoesNotExist:
 | 
					        except ObjectDoesNotExist:
 | 
				
			||||||
            # Fail if there is no such object
 | 
					            # Fail if there is no such object
 | 
				
			||||||
            self.fail()
 | 
					            self.fail()
 | 
				
			||||||
@ -210,7 +214,7 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
 | 
				
			|||||||
        # Create form data to be sent to the url
 | 
					        # Create form data to be sent to the url
 | 
				
			||||||
        test_amount = 10.00
 | 
					        test_amount = 10.00
 | 
				
			||||||
        test_due = "2021-01-01"
 | 
					        test_due = "2021-01-01"
 | 
				
			||||||
        test_comment = "test_comment"
 | 
					        test_comment = self.create_dummy_string()
 | 
				
			||||||
        post_data = {
 | 
					        post_data = {
 | 
				
			||||||
            "amount": test_amount,
 | 
					            "amount": test_amount,
 | 
				
			||||||
            "due": test_due,
 | 
					            "due": test_due,
 | 
				
			||||||
 | 
				
			|||||||
@ -19,6 +19,7 @@ from intervention.models import LegalData, ResponsibilityData, Intervention
 | 
				
			|||||||
from konova.management.commands.setup_data import GROUPS_DATA
 | 
					from konova.management.commands.setup_data import GROUPS_DATA
 | 
				
			||||||
from konova.models import Geometry
 | 
					from konova.models import Geometry
 | 
				
			||||||
from konova.settings import DEFAULT_GROUP
 | 
					from konova.settings import DEFAULT_GROUP
 | 
				
			||||||
 | 
					from konova.utils.generators import generate_random_string
 | 
				
			||||||
from user.models import UserActionLogEntry, UserAction
 | 
					from user.models import UserActionLogEntry, UserAction
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -79,6 +80,15 @@ class BaseTestCase(TestCase):
 | 
				
			|||||||
            )
 | 
					            )
 | 
				
			||||||
        cls.groups = Group.objects.all()
 | 
					        cls.groups = Group.objects.all()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def create_dummy_string(prefix: str = ""):
 | 
				
			||||||
 | 
					        """ Create
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Returns:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        return f"{prefix}{generate_random_string(3, True)}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def create_dummy_intervention(cls):
 | 
					    def create_dummy_intervention(cls):
 | 
				
			||||||
        """ Creates an intervention which can be used for tests
 | 
					        """ Creates an intervention which can be used for tests
 | 
				
			||||||
@ -213,7 +223,10 @@ class BaseTestCase(TestCase):
 | 
				
			|||||||
        Returns:
 | 
					        Returns:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        return MultiPolygon(Polygon.from_bbox([-4.526367, 18.354526, -1.801758, 20.591652]))
 | 
					        polygon = Polygon.from_bbox((7.157593, 49.882247, 7.816772, 50.266521))
 | 
				
			||||||
 | 
					        polygon.srid = 4326
 | 
				
			||||||
 | 
					        polygon = polygon.transform(3857, clone=True)
 | 
				
			||||||
 | 
					        return MultiPolygon(polygon, srid=3857)  # 3857 is the default srid used for MultiPolygonField in the form
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def fill_out_intervention(cls, intervention: Intervention) -> Intervention:
 | 
					    def fill_out_intervention(cls, intervention: Intervention) -> Intervention:
 | 
				
			||||||
@ -258,6 +271,27 @@ class BaseTestCase(TestCase):
 | 
				
			|||||||
        compensation.geometry.save()
 | 
					        compensation.geometry.save()
 | 
				
			||||||
        return compensation
 | 
					        return compensation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def assert_equal_geometries(self, geom1: MultiPolygon, geom2: MultiPolygon):
 | 
				
			||||||
 | 
					        """ Assert for geometries to be equal
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Transforms the geometries to matching srids before checking
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Args:
 | 
				
			||||||
 | 
					            geom1 (MultiPolygon): A geometry
 | 
				
			||||||
 | 
					            geom2 (MultiPolygon): A geometry
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Returns:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        if geom1.srid != geom2.srid:
 | 
				
			||||||
 | 
					            # Due to prior possible transformation of any of these geometries, we need to make sure there exists a
 | 
				
			||||||
 | 
					            # transformation from one coordinate system into the other, which is valid
 | 
				
			||||||
 | 
					            geom1_t = geom1.transform(geom2.srid, clone=True)
 | 
				
			||||||
 | 
					            geom2_t = geom2.transform(geom1.srid, clone=True)
 | 
				
			||||||
 | 
					            self.assertTrue(geom1_t.equals(geom2) or geom2_t.equals(geom1))
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            self.assertTrue(geom1.equals(geom2))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BaseViewTestCase(BaseTestCase):
 | 
					class BaseViewTestCase(BaseTestCase):
 | 
				
			||||||
    """ Wraps basic test functionality, reusable for every specialized ViewTestCase
 | 
					    """ Wraps basic test functionality, reusable for every specialized ViewTestCase
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user