-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_index.py
More file actions
74 lines (53 loc) · 2.1 KB
/
Copy pathtest_index.py
File metadata and controls
74 lines (53 loc) · 2.1 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
import pytest
from index import create_index, group_notes_by_tag, INDEX_NAME, NOTES_DIR
EXPECTED_OUTPUT = """# Notes index
## Productivity
- [Pybites productivity tips](notes/20220817104440.md)
## Python
- [Enumerate](notes/20220817104441.md)
- [Pathlib](notes/20220817104442.md)
"""
@pytest.fixture
def notes_dir(tmp_path):
notes_dir = tmp_path / NOTES_DIR
notes_dir.mkdir()
return notes_dir
@pytest.fixture
def create_notes(notes_dir):
note_base = "2022081710444"
notes = (
"# Pybites productivity tips\n\nblabla\n\n#productivity #Python",
"# enumerate\n\nfor i, line in enumerate(lines, start=1):\n\n#python",
"# pathlib\n\nyou can use home() and cwd() on a Path object\n\n#python",
)
for i, note in enumerate(notes):
file = notes_dir / f"{note_base}{i}.md"
with open(file, "w") as f:
f.write(note + "\n")
def test_create_index(create_notes, notes_dir, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
tag_index_tree = group_notes_by_tag(notes_dir)
create_index(tag_index_tree)
with open(INDEX_NAME) as f:
content = f.read().strip()
# stripping off header
actual = content.splitlines()[5:]
expected = EXPECTED_OUTPUT.splitlines()[2:]
assert actual == expected
def test_tags_inside_code_blocks_are_ignored(notes_dir):
note_with_code = notes_dir / "20220817104443.md"
note_with_code.write_text(
"# C include example\n\n```c\n#include <stdio.h>\n```\n\n#clang\n"
)
tag_index_tree = group_notes_by_tag(notes_dir)
assert "Include" not in tag_index_tree
assert "Clang" in tag_index_tree
def test_note_with_multiple_tags_uses_first_tag_only(notes_dir):
"""Notes should only be indexed under their first tag to avoid duplicates."""
note = notes_dir / "20220817104444.md"
note.write_text("# Multi-tag note\n\nContent here\n\n#first #second #third\n")
tag_index_tree = group_notes_by_tag(notes_dir)
assert "First" in tag_index_tree
assert "Second" not in tag_index_tree
assert "Third" not in tag_index_tree
assert len(tag_index_tree["First"]) == 1