Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple image comparison extension types in one test? #687

Closed
drjasonharrison-vp-eio opened this issue Feb 2, 2023 · 3 comments
Closed
Labels

Comments

@drjasonharrison-vp-eio
Copy link

I'm starting with the example test_custom_image_extension ( https://github.com/tophat/syrupy/blob/main/tests/examples/test_custom_image_extension.py ) and wondering how to extend this to multiple image formats. For example JPEG and PNG.

from syrupy.extensions.single_file import SingleFileSnapshotExtension

class JPEGImageExtension(SingleFileSnapshotExtension):
    @property
    def _file_extension(self) -> str:
        return "jpg"

class PNGImageExtension(SingleFileSnapshotExtension):
    @property
    def _file_extension(self) -> str:
        return "png"

@pytest.fixture
def snapshot_jpeg(snapshot):
    return snapshot.use_extension(JPEGImageExtension)

@pytest.fixture
def snapshot_png(snapshot):
    return snapshot.use_extension(PNGImageExtension)

and then pass both fixtures into the test:

def create_image(format):
   if format == 0:
        return "JPEG_IMAGE_DATA", "jpeg_image.jpg"
   
    return "PNG_IMAGE_DATA", "png_image.png"

def test_image_format(snapshot, snapshot_jpeg, snapshot_png):
    image_data, filename = create_image(0)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_jpeg(name=filename)

    image_data, filename = create_image(1)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_png(name=filename)

I'm not sure if a snapshot extension can take additional parameters, like the file extension (.jpg, .png) and select the inherited class of SingleFileSnapshotExtension. This would allow us to do something like:

def test_image_format(snapshot, snapshot_image):
    image_data, filename = create_image(0)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_image(name=filename, image_type=filename_extension)

    image_data, filename = create_image(1)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_image(name=filename, image_type=filename_extension)
@noahnu
Copy link
Collaborator

noahnu commented Feb 2, 2023

@pytest.fixture
def snapshot_image(snapshot):
    def _function(image_type):
        if image_type == "jpeg":
            return snapshot.use_extension(JPEGImageExtension)
        if image_type == "png":
            return snapshot.use_extension(PNGImageExtension)
        raise ValueError("Bad image_type!")
    return _function


def test_image_format(snapshot, snapshot_image):
    image_data, filename = create_image(0)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_image(image_type=filename_extension)(name=filename)

    image_data, filename = create_image(1)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_image(image_type=filename_extension)(name=filename)

Something like this should work. There might be a way to wrap the snapshot assertion further to have it automatically curry the name parameter.

In general, as of this time, Syrupy does not support passing arbitrary parameters to the Snapshot extension class. It's a welcome feature request though. I was actually thinking about it today, since it opens up interesting possibilities for supporting multiple versions of an extension (like multiple serializer versions for the default amber extension).

@noahnu noahnu added feature request New feature or request question and removed question labels Feb 2, 2023
@drjasonharrison
Copy link

Thanks!

@noahnu
Copy link
Collaborator

noahnu commented Feb 6, 2023

Tracking the "how to pass arbitrary data to a custom extension" as a distinct feature request: #700

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants