mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
fix markdown parsing
This commit is contained in:
@@ -1,103 +0,0 @@
|
||||
"""Tests for edge cases in entity parsing."""
|
||||
|
||||
from datetime import datetime
|
||||
from textwrap import dedent
|
||||
|
||||
import pytest
|
||||
|
||||
from basic_memory.markdown import ParseError
|
||||
from basic_memory.markdown.schemas import (
|
||||
Entity,
|
||||
EntityFrontmatter,
|
||||
EntityContent,
|
||||
)
|
||||
|
||||
|
||||
def test_entity_content_empty_lists():
|
||||
"""Test entity content with empty lists."""
|
||||
content = dedent("""
|
||||
# Empty Entity
|
||||
|
||||
## Observations
|
||||
|
||||
## Relations
|
||||
""")
|
||||
|
||||
entity_content = EntityContent.from_markdown(content)
|
||||
assert entity_content.observations == []
|
||||
assert entity_content.relations == []
|
||||
|
||||
|
||||
def test_entity_content_multiline_description():
|
||||
"""Test entity content with a multiline description."""
|
||||
content = dedent("""
|
||||
# Title
|
||||
First line
|
||||
Second line
|
||||
Third line
|
||||
|
||||
## Observations
|
||||
- [test] Test
|
||||
""")
|
||||
|
||||
entity = EntityContent.from_markdown(content)
|
||||
assert entity.title == "Title"
|
||||
# Each line should be as-is
|
||||
assert entity.description == "First line\nSecond line\nThird line"
|
||||
|
||||
|
||||
def test_entity_content_invalid_tokens():
|
||||
"""Test entity content with invalid markdown tokens."""
|
||||
entity = EntityContent.from_markdown("# Title\n## Section\n```invalid\n")
|
||||
assert entity.title == "Title"
|
||||
assert not entity.observations
|
||||
assert not entity.relations
|
||||
|
||||
|
||||
def test_frontmatter_parsing_errors():
|
||||
"""Test error handling in frontmatter parsing."""
|
||||
# Test various invalid formats
|
||||
with pytest.raises(ParseError):
|
||||
EntityFrontmatter.from_text("not yaml")
|
||||
|
||||
with pytest.raises(ParseError):
|
||||
EntityFrontmatter.from_text("missing:fields")
|
||||
|
||||
with pytest.raises(ParseError):
|
||||
EntityFrontmatter.from_text("type:test\nid:test\ncreated:invalid")
|
||||
|
||||
|
||||
def test_content_parsing_errors():
|
||||
"""Test error handling in content parsing."""
|
||||
# Test various markdown edge cases
|
||||
with pytest.raises(ParseError):
|
||||
EntityContent.from_markdown(None) # type: ignore
|
||||
|
||||
with pytest.raises(ParseError):
|
||||
EntityContent.from_markdown(object()) # type: ignore
|
||||
|
||||
content = dedent("""
|
||||
# Title
|
||||
## Invalid
|
||||
- [not a section] content
|
||||
""")
|
||||
|
||||
entity = EntityContent.from_markdown(content)
|
||||
assert entity.title == "Title"
|
||||
assert not entity.observations
|
||||
|
||||
|
||||
def test_invalid_entity_creation():
|
||||
"""Test error handling in full entity creation."""
|
||||
# Invalid frontmatter
|
||||
frontmatter = EntityFrontmatter(
|
||||
type="test", id="test", created=datetime.now(), modified=datetime.now(), tags=[]
|
||||
)
|
||||
|
||||
# Invalid content
|
||||
content = EntityContent(title="", description=None, observations=[], relations=[])
|
||||
|
||||
# Should still create valid entity
|
||||
entity = Entity(frontmatter=frontmatter, content=content)
|
||||
assert entity.frontmatter.type == "test"
|
||||
assert entity.content.title == ""
|
||||
@@ -1,103 +0,0 @@
|
||||
"""Tests for entity metadata parsing."""
|
||||
|
||||
from textwrap import dedent
|
||||
|
||||
import pytest
|
||||
|
||||
from basic_memory.markdown.parser import EntityParser
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_metadata():
|
||||
"""Test parsing basic metadata."""
|
||||
parser = EntityParser()
|
||||
metadata = {
|
||||
"owner": "team-auth",
|
||||
"priority": "high",
|
||||
"status": "active"
|
||||
}
|
||||
|
||||
result = await parser.parse_metadata(metadata)
|
||||
|
||||
assert result.metadata["owner"] == "team-auth"
|
||||
assert result.metadata["priority"] == "high"
|
||||
assert result.metadata["status"] == "active"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_metadata_empty():
|
||||
"""Test parsing empty metadata."""
|
||||
parser = EntityParser()
|
||||
result = await parser.parse_metadata(None)
|
||||
assert result.metadata == {}
|
||||
|
||||
# Should also handle empty dict
|
||||
result = await parser.parse_metadata({})
|
||||
assert result.metadata == {}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_metadata_whitespace():
|
||||
"""Test handling of various whitespace in metadata."""
|
||||
parser = EntityParser()
|
||||
metadata = {
|
||||
"owner": " team-auth ",
|
||||
"priority": " high ",
|
||||
"status": " active "
|
||||
}
|
||||
|
||||
result = await parser.parse_metadata(metadata)
|
||||
|
||||
assert result.metadata["owner"] == " team-auth " # Metadata preserves whitespace
|
||||
assert result.metadata["priority"] == " high "
|
||||
assert result.metadata["status"] == " active "
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_metadata_nested_objects():
|
||||
"""Test handling of nested metadata objects."""
|
||||
parser = EntityParser()
|
||||
metadata = {
|
||||
"owner": "team-auth",
|
||||
"config": {
|
||||
"level": "high",
|
||||
"tags": ["important", "urgent"],
|
||||
"settings": {
|
||||
"notifications": True,
|
||||
"visibility": "private"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = await parser.parse_metadata(metadata)
|
||||
|
||||
assert result.metadata["owner"] == "team-auth"
|
||||
assert result.metadata["config"]["level"] == "high"
|
||||
assert result.metadata["config"]["tags"] == ["important", "urgent"]
|
||||
assert result.metadata["config"]["settings"]["notifications"] is True
|
||||
assert result.metadata["config"]["settings"]["visibility"] == "private"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_metadata_mixed_types():
|
||||
"""Test handling of different value types in metadata."""
|
||||
parser = EntityParser()
|
||||
metadata = {
|
||||
"owner": "team-auth",
|
||||
"active": True,
|
||||
"priority": 1,
|
||||
"tags": ["tag1", "tag2"],
|
||||
"scores": {
|
||||
"a": 10,
|
||||
"b": 20
|
||||
}
|
||||
}
|
||||
|
||||
result = await parser.parse_metadata(metadata)
|
||||
|
||||
assert result.metadata["owner"] == "team-auth"
|
||||
assert result.metadata["active"] is True
|
||||
assert result.metadata["priority"] == 1
|
||||
assert result.metadata["tags"] == ["tag1", "tag2"]
|
||||
assert result.metadata["scores"]["a"] == 10
|
||||
assert result.metadata["scores"]["b"] == 20
|
||||
Reference in New Issue
Block a user