-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathtest_entity_parser.py
More file actions
269 lines (208 loc) · 8.45 KB
/
test_entity_parser.py
File metadata and controls
269 lines (208 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""Tests for entity markdown parsing."""
from datetime import datetime
from pathlib import Path
from textwrap import dedent
import pytest
from basic_memory.markdown.schemas import EntityMarkdown, EntityFrontmatter, Relation
from basic_memory.markdown.entity_parser import parse
@pytest.fixture
def valid_entity_content():
"""A complete, valid entity file with all features."""
return dedent("""
---
title: Auth Service
type: component
permalink: auth_service
created: 2024-12-21T14:00:00Z
modified: 2024-12-21T14:00:00Z
tags: authentication, security, core
---
Core authentication service that handles user authentication.
some [[Random Link]]
another [[Random Link with Title|Titled Link]]
## Observations
- [design] Stateless authentication #security #architecture (JWT based)
- [feature] Mobile client support #mobile #oauth (Required for App Store)
- [tech] Caching layer #performance (Redis implementation)
## Relations
- implements [[OAuth Implementation]] (Core auth flows)
- uses [[Redis Cache]] (Token caching)
- specified_by [[Auth API Spec]] (OpenAPI spec)
""")
@pytest.mark.asyncio
async def test_parse_complete_file(test_config, entity_parser, valid_entity_content):
"""Test parsing a complete entity file with all features."""
test_file = test_config.home / "test_entity.md"
test_file.write_text(valid_entity_content)
entity = await entity_parser.parse_file(test_file)
# Verify entity structure
assert isinstance(entity, EntityMarkdown)
assert isinstance(entity.frontmatter, EntityFrontmatter)
assert isinstance(entity.content, str)
# Check frontmatter
assert entity.frontmatter.title == "Auth Service"
assert entity.frontmatter.type == "component"
assert entity.frontmatter.permalink == "auth_service"
assert set(entity.frontmatter.tags) == {"authentication", "security", "core"}
# Check content
assert "Core authentication service that handles user authentication." in entity.content
# Check observations
assert len(entity.observations) == 3
obs = entity.observations[0]
assert obs.category == "design"
assert obs.content == "Stateless authentication #security #architecture"
assert set(obs.tags or []) == {"security", "architecture"}
assert obs.context == "JWT based"
# Check relations
assert len(entity.relations) == 5
assert (
Relation(type="implements", target="OAuth Implementation", context="Core auth flows")
in entity.relations
), "missing [[OAuth Implementation]]"
assert (
Relation(type="uses", target="Redis Cache", context="Token caching") in entity.relations
), "missing [[Redis Cache]]"
assert (
Relation(type="specified_by", target="Auth API Spec", context="OpenAPI spec")
in entity.relations
), "missing [[Auth API Spec]]"
# inline links in content
assert Relation(type="links to", target="Random Link", context=None) in entity.relations, (
"missing [[Random Link]]"
)
assert (
Relation(type="links to", target="Random Link with Title|Titled Link", context=None)
in entity.relations
), "missing [[Random Link with Title|Titled Link]]"
@pytest.mark.asyncio
async def test_parse_minimal_file(test_config, entity_parser):
"""Test parsing a minimal valid entity file."""
content = dedent("""
---
type: component
tags: []
---
# Minimal Entity
## Observations
- [note] Basic observation #test
## Relations
- references [[Other Entity]]
""")
test_file = test_config.home / "minimal.md"
test_file.write_text(content)
entity = await entity_parser.parse_file(test_file)
assert entity.frontmatter.type == "component"
assert entity.frontmatter.permalink is None
assert len(entity.observations) == 1
assert len(entity.relations) == 1
assert entity.created is not None
assert entity.modified is not None
@pytest.mark.asyncio
async def test_error_handling(test_config, entity_parser):
"""Test error handling."""
# Missing file
with pytest.raises(FileNotFoundError):
await entity_parser.parse_file(Path("nonexistent.md"))
# Invalid file encoding
test_file = test_config.home / "binary.md"
with open(test_file, "wb") as f:
f.write(b"\x80\x81") # Invalid UTF-8
with pytest.raises(UnicodeDecodeError):
await entity_parser.parse_file(test_file)
@pytest.mark.asyncio
async def test_parse_file_without_section_headers(test_config, entity_parser):
"""Test parsing a minimal valid entity file."""
content = dedent("""
---
type: component
permalink: minimal_entity
status: draft
tags: []
---
# Minimal Entity
some text
some [[Random Link]]
- [note] Basic observation #test
- references [[Other Entity]]
""")
test_file = test_config.home / "minimal.md"
test_file.write_text(content)
entity = await entity_parser.parse_file(test_file)
assert entity.frontmatter.type == "component"
assert entity.frontmatter.permalink == "minimal_entity"
assert "some text\nsome [[Random Link]]" in entity.content
assert len(entity.observations) == 1
assert entity.observations[0].category == "note"
assert entity.observations[0].content == "Basic observation #test"
assert entity.observations[0].tags == ["test"]
assert len(entity.relations) == 2
assert entity.relations[0].type == "links to"
assert entity.relations[0].target == "Random Link"
assert entity.relations[1].type == "references"
assert entity.relations[1].target == "Other Entity"
def test_parse_date_formats(entity_parser):
"""Test date parsing functionality."""
# Valid formats
assert entity_parser.parse_date("2024-01-15") is not None
assert entity_parser.parse_date("Jan 15, 2024") is not None
assert entity_parser.parse_date("2024-01-15 10:00 AM") is not None
assert entity_parser.parse_date(datetime.now()) is not None
# Invalid formats
assert entity_parser.parse_date(None) is None
assert entity_parser.parse_date(123) is None # Non-string/datetime
assert entity_parser.parse_date("not a date") is None # Unparseable string
assert entity_parser.parse_date("") is None # Empty string
# Test dateparser error handling
assert entity_parser.parse_date("25:00:00") is None # Invalid time
def test_parse_empty_content():
"""Test parsing empty or minimal content."""
result = parse("")
assert result.content == ""
assert len(result.observations) == 0
assert len(result.relations) == 0
result = parse("# Just a title")
assert result.content == "# Just a title"
assert len(result.observations) == 0
assert len(result.relations) == 0
@pytest.mark.asyncio
async def test_parse_file_with_absolute_path(test_config, entity_parser):
"""Test parsing a file with an absolute path."""
content = dedent("""
---
type: component
permalink: absolute_path_test
---
# Absolute Path Test
A file with an absolute path.
""")
# Create a test file in the project directory
test_file = test_config.home / "absolute_path_test.md"
test_file.write_text(content)
# Get the absolute path to the test file
absolute_path = test_file.resolve()
# Parse the file using the absolute path
entity = await entity_parser.parse_file(absolute_path)
# Verify the file was parsed correctly
assert entity.frontmatter.permalink == "absolute_path_test"
assert "Absolute Path Test" in entity.content
assert entity.created is not None
assert entity.modified is not None
# @pytest.mark.asyncio
# async def test_parse_file_invalid_yaml(test_config, entity_parser):
# """Test parsing file with invalid YAML frontmatter."""
# content = dedent("""
# ---
# invalid: [yaml: ]syntax]
# ---
#
# # Invalid YAML Frontmatter
# """)
#
# test_file = test_config.home / "invalid_yaml.md"
# test_file.write_text(content)
#
# # Should handle invalid YAML gracefully
# entity = await entity_parser.parse_file(test_file)
# assert entity.frontmatter.title == "invalid_yaml.md"
# assert entity.frontmatter.type == "note"
# assert entity.content.strip() == "# Invalid YAML Frontmatter"