import pytest
from tests.annotations_tests.factories import (
    ETDRSGridAnnotationFactory,
    MeasurementAnnotationFactory,
    BooleanClassificationAnnotationFactory,
    IntegerClassificationAnnotationFactory,
    CoordinateListAnnotationFactory,
    PolygonAnnotationSetFactory,
    SinglePolygonAnnotationFactory,
    LandmarkAnnotationSetFactory,
    SingleLandmarkAnnotationFactory,
)
from tests.model_helpers import batch_test_factories
from django.db import IntegrityError


@pytest.mark.django_db
class TestAnnotationModels:
    # test functions are added dynamically to this class

    def test_default_model_str(self):
        etdrs = ETDRSGridAnnotationFactory()
        assert str(etdrs) == "<{} by {} on {} for {}>".format(
            etdrs.__class__.__name__,
            etdrs.grader.username,
            etdrs.created.strftime("%Y-%m-%d at %H:%M:%S"),
            etdrs.image,
        )

    def test_measurement_duplicate_not_allowed(self):
        measurement = MeasurementAnnotationFactory()
        try:
            measuremt_duplicate = MeasurementAnnotationFactory(
                image=measurement.image,
                grader=measurement.grader,
                created=measurement.created,
                start_voxel=measurement.start_voxel,
                end_voxel=measurement.end_voxel,
            )
            pytest.fail(
                "No integrity error when submitting duplicate measurement annotation"
            )
        except IntegrityError:
            pass


factories = {
    "etdrs": ETDRSGridAnnotationFactory,
    "measurement": MeasurementAnnotationFactory,
    "boolean": BooleanClassificationAnnotationFactory,
    "integer": IntegerClassificationAnnotationFactory,
    "coordinates": CoordinateListAnnotationFactory,
    "polygonset": PolygonAnnotationSetFactory,
    "singlepolygon": SinglePolygonAnnotationFactory,
    "landmarkset": LandmarkAnnotationSetFactory,
    "singlelandmark": SingleLandmarkAnnotationFactory,
}
batch_test_factories(factories, TestAnnotationModels)

@@ -7,16 +7,95 @@
  pytest%0A
+from django.db import IntegrityError%0Afrom guardian.shortcuts import get_perms%0A%0A
 from tes
@@ -529,150 +529,158 @@
 rom 
-django.db import IntegrityError%0A%0A%0A@pytest.mark.django_db%0Aclass TestAnnotationModels:%0A    # test functions are added dynamically to this class%0A
+tests.viewset_helpers import get_user_from_user_type%0Afrom config.settings import PERMISSION_TYPES%0A%0A%0A@pytest.mark.django_db%0Aclass TestAnnotationModels:
 %0A   
@@ -1595,16 +1595,1488 @@
  pass%0A%0A%0A
+@pytest.mark.parametrize(%0A    %22user_type%22,%0A    %5B%0A        %22normal_user%22,%0A        %22retina_grader_non_allowed%22,%0A        %22retina_grader%22,%0A        %22retina_admin%22,%0A    %5D,%0A)%0A@pytest.mark.django_db%0Aclass TestPermissions:%0A    def test_single_model_permissions(%0A        self, TwoRetinaPolygonAnnotationSets, user_type%0A    ):%0A        user = get_user_from_user_type(%0A            user_type, grader=TwoRetinaPolygonAnnotationSets.grader1%0A        )%0A        perms = get_perms(%0A            user,%0A            TwoRetinaPolygonAnnotationSets.polygonset1.singlepolygonannotation_set.first(),%0A        )%0A        for permission_type in PERMISSION_TYPES:%0A            if user_type == %22retina_grader_non_allowed%22:%0A                assert (%0A                    f%22%7Bpermission_type%7D_singlepolygonannotation%22 not in perms%0A                )%0A            else:%0A                assert f%22%7Bpermission_type%7D_singlepolygonannotation%22 in perms%0A%0A    def test_model_permissions(%0A        self, TwoRetinaPolygonAnnotationSets, user_type%0A    ):%0A        user = get_user_from_user_type(%0A            user_type, grader=TwoRetinaPolygonAnnotationSets.grader1%0A        )%0A        perms = get_perms(user, TwoRetinaPolygonAnnotationSets.polygonset1)%0A        for permission_type in PERMISSION_TYPES:%0A            if user_type == %22retina_grader_non_allowed%22:%0A                assert f%22%7Bpermission_type%7D_polygonannotationset%22 not in perms%0A            else:%0A                assert f%22%7Bpermission_type%7D_polygonannotationset%22 in perms%0A%0A%0A
 factorie

Add test for permissions