def test_node_mimebundle(name: str, data: Any, mode: str, fmt: str, vega_cli_options: Optional[List[str]]) -> None: saver = NodeSaver(data[mode], mode=mode, vega_cli_options=vega_cli_options) mimetype, out = saver.mimebundle(fmt).popitem() assert mimetype == fmt_to_mimetype(fmt) if fmt == "png": assert isinstance(out, bytes) im = Image.open(io.BytesIO(out)) assert im.format == "PNG" im_expected = Image.open(io.BytesIO(data[fmt])) assert abs(im.size[0] - im_expected.size[0]) < 5 assert abs(im.size[1] - im_expected.size[1]) < 5 elif fmt == "pdf": assert isinstance(out, bytes) pdf = PdfFileReader(io.BytesIO(out)) box = pdf.getPage(0).mediaBox pdf_expected = PdfFileReader(io.BytesIO(data[fmt])) box_expected = pdf_expected.getPage(0).mediaBox assert abs(box.getWidth() - box_expected.getWidth()) < 5 assert abs(box.getHeight() - box_expected.getHeight()) < 5 elif fmt == "svg": assert isinstance(out, str) im = SVGImage(out) im_expected = SVGImage(data[fmt]) assert abs(im.width - im_expected.width) < 5 assert abs(im.height - im_expected.height) < 5 else: assert out == data[fmt]
def test_node_mimebundle_fail(name: str, data: Any) -> None: fmt = "vega-lite" mode = "vega" saver = NodeSaver(data[mode], mode=mode) with pytest.raises(ValueError): saver.mimebundle(fmt)