コード例 #1
0
ファイル: test_node.py プロジェクト: sqofnyc/altair_saver
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]
コード例 #2
0
ファイル: test_node.py プロジェクト: sqofnyc/altair_saver
def test_enabled(monkeypatch: MonkeyPatch, enabled: bool) -> None:
    def exec_path(name: str) -> str:
        if enabled:
            return name
        else:
            raise _node.ExecutableNotFound(name)

    monkeypatch.setattr(_node, "exec_path", exec_path)
    assert NodeSaver.enabled() is enabled
コード例 #3
0
ファイル: test_node.py プロジェクト: sqofnyc/altair_saver
def test_stderr_suppression(
    interactive_spec: JSONDict,
    suppress_warnings: bool,
    capsys: SysCapture,
) -> None:
    message = "WARN Can not resolve event source: window"

    # Window resolve warnings are suppressed by default.
    if suppress_warnings:
        saver = NodeSaver(interactive_spec)
    else:
        saver = NodeSaver(interactive_spec, stderr_filter=None)

    saver.save(fmt="png")
    captured = capsys.readouterr()

    if suppress_warnings:
        assert message not in captured.err
    else:
        assert message in captured.err
コード例 #4
0
from altair_saver import NodeSaver

spec = {"data": {"values": []}, "mark": "point"}
out = NodeSaver(spec).mimebundle('png')
assert "image/png" in out
コード例 #5
0
ファイル: test_node.py プロジェクト: sqofnyc/altair_saver
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)