test: add unit tests for image_array_to_data_uri#5657
Conversation
emilykl
left a comment
There was a problem hiding this comment.
@eeshsaxena Thanks for the PR!
Can you move this test file to tests/test_plotly_utils/test_data_utils.py so that it lives with the rest of the _plotly_utils tests?
Could you also update the first 3 tests to verify the contents of the whole string, in addition to the prefix? This could be done by comparing against a hard-coded string, or decoding the string and extracting the values and comparing them to the input. I'd also be okay with removing those tests entirely for now; up to you. But leaving them in gives a false impression of coverage.
Otherwise LGTM.
Address review: - Move the file to tests/test_plotly_utils/ so it lives with the rest of the _plotly_utils tests. - The greyscale/RGB/RGBA tests only asserted the data URI prefix, which gave a false impression of coverage. Decode the base64 payload with the vendored pypng Reader and compare the pixels against the input array, and assert the PNG colour mode (greyscale/alpha) matches the input shape. Decoding via the vendored Reader keeps the tests Pillow-free.
|
Thanks @emilykl - both points addressed:
I decoded with the vendored 7 tests pass and |
emilykl
left a comment
There was a problem hiding this comment.
Thanks @eeshsaxena for the quick follow-up and for the tests. Looks good to me.
Description
_plotly_utils.data_utils.image_array_to_data_uriconverts a numpy uint8image array into a base64 PNG/JPG data URI. It had no direct test coverage.
This adds
tests/test_optional/test_utils/test_data_utils.pycovering:ValueErroron out-of-range compression (-1and10)ValueErroron an invalid image shapeValueErrorwhen requestingjpgwith a non-PIL backendThe tests use the
pypngbackend so they do not require Pillow. No sourcechanges.
Verification
pytest tests/test_optional/test_utils/test_data_utils.py- 7 passed.ruff check/ruff format --checkclean.