[Solved] How can i write a test for a custom Validator?

My Goal, check the dimension of an uploaded Image in a custom validator for the image field in the LeadImageBehavior.

The validator and the registration:

from plone.app.contenttypes.behaviors.leadimage import ILeadImageBehavior
from z3c.form import validator

class ImageSizeValidator(validator.FileUploadValidator):
    """z3c.form validator class for international phone numbers"""

    def validate(self, value):
        """Validate international phone number on input"""
        super(ImageSizeValidator, self).validate(value)
        # here, check the dimension
        ....


# Set conditions for which fields the validator class applies
validator.WidgetValidatorDiscriminators(
    ImageSizeValidator, field=ILeadImageBehavior["image"]
)

The test:

class TestImageSizeValidatorFunctionalTest(unittest.TestCase):
    layer = MY_ADDON_FUNCTIONAL_TESTING

    def setUp(self):
        # ..do some setup stuff
        # ... create a news with an image

    def test_validator(self):
        from plone.dexterity.utils import iterSchemata
        from z3c.form.interfaces import IValidator
        from zope.schema import getFieldsInOrder

        imagefield = None
        for schema in iterSchemata(self.portal.news):
            print(f"{schema}")
            for name, field in getFieldsInOrder(schema):
                if name == "image":
                    imagefield = field

        import pdb; pdb.set_trace() 
        
        # this check doesn't work
        self.assertTrue(IValidator.providedBy(imagefield))

My Question, what is the right way for testing the adapted Validator? Has anyone a hint for me or an example?

Update, i found a solution to test the validator:

class TestImageSizeValidatorIntegrationTest(unittest.TestCase):
    layer = MY_ADDON_INTEGRATION_TESTING

    def setUp(self):
        self.portal = self.layer["portal"]
        self.request = self.layer["request"]
        self.portal_url = self.portal.absolute_url()

    def test_validator(self):
        from importlib.resources import files
        from plone.app.contenttypes.behaviors.leadimage import ILeadImageBehavior
        from plone.namedfile import NamedBlobImage
        from my.addon.validators import ImageSizeValidator
        from zope.schema.interfaces import WrongType

        source = files("my.addon.tests.testcontent").joinpath("im-1600x900.png")
        with source.open(mode="rb") as f:
            file = NamedBlobImage()
            file.contentType = "image/png"
            file.data = f.read()

        _validator = ImageSizeValidator(
            None, None, None, ILeadImageBehavior["image"], None
        )

        with self.assertRaises(WrongType):
            _validator.validate("this is a string")

        self.assertIsNone(_validator.validate(file))

z3c.form doctests might also help: z3c.form/validator.rst at master · zopefoundation/z3c.form · GitHub

Thanks, i have read this before. I'm not sure what is the right test for the adaption. Perhaps, i should implement a custom Interface for my validator and test the adapter via:

zope.component.queryMultiAdapter(
     (None, None, None, ILeadImage['image'], None),
    IMyValidator)

The Zope Magic is a litle bit obscure :wink: