|
| 1 | +import pytest |
| 2 | +from langchain_core.documents import Document |
| 3 | + |
| 4 | +from langchain_community.graph_vectorstores.links import METADATA_LINKS_KEY, Link |
| 5 | +from langchain_community.graph_vectorstores.visualize import render_graphviz |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.requires("graphviz") |
| 9 | +def test_visualize_simple_graph() -> None: |
| 10 | + doc1 = Document( |
| 11 | + id="a", |
| 12 | + page_content="some content", |
| 13 | + metadata={ |
| 14 | + METADATA_LINKS_KEY: [ |
| 15 | + Link.incoming("href", "a"), |
| 16 | + Link.bidir("kw", "foo"), |
| 17 | + ] |
| 18 | + }, |
| 19 | + ) |
| 20 | + doc2 = Document( |
| 21 | + id="b", |
| 22 | + page_content="<some\n more content>", |
| 23 | + metadata={ |
| 24 | + METADATA_LINKS_KEY: [ |
| 25 | + Link.incoming("href", "b"), |
| 26 | + Link.outgoing("href", "a"), |
| 27 | + Link.bidir("kw", "foo"), |
| 28 | + Link.bidir("kw", "bar"), |
| 29 | + ] |
| 30 | + }, |
| 31 | + ) |
| 32 | + |
| 33 | + assert render_graphviz([doc1, doc2]).source == ( |
| 34 | + "digraph {\n" |
| 35 | + "\trankdir=LR\n" |
| 36 | + "\tnode [style=filled]\n" |
| 37 | + '\ta [label="a\nsome content" shape=note tooltip="some content"]\n' |
| 38 | + '\ttag_0 [label="href:a"]\n' |
| 39 | + "\ta -> tag_0 [dir=back]\n" |
| 40 | + '\ttag_1 [label="kw:foo"]\n' |
| 41 | + "\ta -> tag_1 [dir=both]\n" |
| 42 | + '\tb [label="b\n<some\n more content>" ' |
| 43 | + 'shape=note tooltip="<some\n more content>"]\n' |
| 44 | + '\ttag_2 [label="href:b"]\n' |
| 45 | + "\tb -> tag_2 [dir=back]\n" |
| 46 | + "\tb -> tag_0 [dir=forward]\n" |
| 47 | + "\tb -> tag_1 [dir=both]\n" |
| 48 | + '\ttag_3 [label="kw:bar"]\n' |
| 49 | + "\tb -> tag_3 [dir=both]\n" |
| 50 | + "}\n" |
| 51 | + ) |
| 52 | + |
| 53 | + assert render_graphviz([doc1, doc2], engine="fdp").engine == "fdp" |
| 54 | + |
| 55 | + assert render_graphviz([doc1, doc2], node_colors={"a": "gold"}).source == ( |
| 56 | + "digraph {\n" |
| 57 | + "\trankdir=LR\n" |
| 58 | + "\tnode [style=filled]\n" |
| 59 | + '\ta [label="a\nsome content" fillcolor=gold ' |
| 60 | + 'shape=note tooltip="some content"]\n' |
| 61 | + '\ttag_0 [label="href:a"]\n' |
| 62 | + "\ta -> tag_0 [dir=back]\n" |
| 63 | + '\ttag_1 [label="kw:foo"]\n' |
| 64 | + "\ta -> tag_1 [dir=both]\n" |
| 65 | + '\tb [label="b\n<some\n more content>" ' |
| 66 | + 'shape=note tooltip="<some\n more content>"]\n' |
| 67 | + '\ttag_2 [label="href:b"]\n' |
| 68 | + "\tb -> tag_2 [dir=back]\n" |
| 69 | + "\tb -> tag_0 [dir=forward]\n" |
| 70 | + "\tb -> tag_1 [dir=both]\n" |
| 71 | + '\ttag_3 [label="kw:bar"]\n' |
| 72 | + "\tb -> tag_3 [dir=both]\n" |
| 73 | + "}\n" |
| 74 | + ) |
| 75 | + |
| 76 | + assert render_graphviz( |
| 77 | + [doc1, doc2], node_color="gold", node_colors={"a": None} |
| 78 | + ).source == ( |
| 79 | + "digraph {\n" |
| 80 | + "\trankdir=LR\n" |
| 81 | + "\tnode [style=filled]\n" |
| 82 | + '\ta [label="a\nsome content" shape=note tooltip="some content"]\n' |
| 83 | + '\ttag_0 [label="href:a"]\n' |
| 84 | + "\ta -> tag_0 [dir=back]\n" |
| 85 | + '\ttag_1 [label="kw:foo"]\n' |
| 86 | + "\ta -> tag_1 [dir=both]\n" |
| 87 | + '\tb [label="b\n<some\n more content>" fillcolor=gold ' |
| 88 | + 'shape=note tooltip="<some\n more content>"]\n' |
| 89 | + '\ttag_2 [label="href:b"]\n' |
| 90 | + "\tb -> tag_2 [dir=back]\n" |
| 91 | + "\tb -> tag_0 [dir=forward]\n" |
| 92 | + "\tb -> tag_1 [dir=both]\n" |
| 93 | + '\ttag_3 [label="kw:bar"]\n' |
| 94 | + "\tb -> tag_3 [dir=both]\n" |
| 95 | + "}\n" |
| 96 | + ) |
| 97 | + |
| 98 | + assert render_graphviz([doc1, doc2], skip_tags=[("kw", "foo")]).source == ( |
| 99 | + "digraph {\n" |
| 100 | + "\trankdir=LR\n" |
| 101 | + "\tnode [style=filled]\n" |
| 102 | + '\ta [label="a\nsome content" shape=note tooltip="some content"]\n' |
| 103 | + '\ttag_0 [label="href:a"]\n' |
| 104 | + "\ta -> tag_0 [dir=back]\n" |
| 105 | + '\tb [label="b\n<some\n more content>" ' |
| 106 | + 'shape=note tooltip="<some\n more content>"]\n' |
| 107 | + '\ttag_1 [label="href:b"]\n' |
| 108 | + "\tb -> tag_1 [dir=back]\n" |
| 109 | + "\tb -> tag_0 [dir=forward]\n" |
| 110 | + '\ttag_2 [label="kw:bar"]\n' |
| 111 | + "\tb -> tag_2 [dir=both]\n" |
| 112 | + "}\n" |
| 113 | + ) |
0 commit comments