|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +from nodestream.model import DesiredIngestion, Node |
| 4 | + |
| 5 | +from nodestream_plugin_semantic.model import Content, hash |
| 6 | + |
| 7 | + |
| 8 | +def test_content_from_text(): |
| 9 | + content_text = "test content" |
| 10 | + content = Content.from_text(content_text) |
| 11 | + assert content.content == content_text |
| 12 | + assert content.id == hash(content_text) |
| 13 | + assert content.parent is None |
| 14 | + |
| 15 | + |
| 16 | +def test_content_add_metadata(): |
| 17 | + content = Content.from_text("test content") |
| 18 | + content.add_metadata("key", "value") |
| 19 | + assert content.metadata == {"key": "value"} |
| 20 | + |
| 21 | + |
| 22 | +def test_content_split_on_delimiter(): |
| 23 | + content_text = "line1\nline2\nline3" |
| 24 | + content = Content.from_text(content_text) |
| 25 | + lines = list(content.split_on_delimiter("\n")) |
| 26 | + assert len(lines) == 3 |
| 27 | + assert lines[0].content == "line1" |
| 28 | + assert lines[1].content == "line2" |
| 29 | + assert lines[2].content == "line3" |
| 30 | + assert all(line.parent == content for line in lines) |
| 31 | + |
| 32 | + |
| 33 | +def test_content_assign_embedding(): |
| 34 | + content = Content.from_text("test content") |
| 35 | + embedding = [0.1, 0.2, 0.3] |
| 36 | + content.assign_embedding(embedding) |
| 37 | + assert content.embedding == embedding |
| 38 | + |
| 39 | + |
| 40 | +def test_content_apply_to_node(): |
| 41 | + content = Content.from_text("test content") |
| 42 | + node = Mock(spec=Node) |
| 43 | + content.apply_to_node("test_type", node) |
| 44 | + node.type = "test_type" |
| 45 | + node.key_values.set_property.assert_called_with("id", content.id) |
| 46 | + node.properties.set_property.assert_any_call("content", content.content) |
| 47 | + |
| 48 | + |
| 49 | +def test_content_make_ingestible(): |
| 50 | + parent_content = Content.from_text("parent content") |
| 51 | + child_content = Content.from_text("child content", parent=parent_content) |
| 52 | + ingest = child_content.make_ingestible("test_type", "test_relationship") |
| 53 | + |
| 54 | + assert isinstance(ingest, DesiredIngestion) |
| 55 | + assert ingest.source.type == "test_type" |
| 56 | + ingest.source.key_values == {"id": child_content.id} |
| 57 | + ingest.source.properties == {"content": child_content.content} |
| 58 | + |
| 59 | + assert len(ingest.relationships) == 1 |
| 60 | + relationship = ingest.relationships[0] |
| 61 | + assert relationship.relationship.type == "test_relationship" |
| 62 | + assert relationship.outbound is False |
| 63 | + assert relationship.to_node.type == "test_type" |
| 64 | + relationship.to_node.key_values == {"id": parent_content.id} |
| 65 | + relationship.to_node.properties == {"content": parent_content.content} |
0 commit comments