-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_sync_tips.py
110 lines (83 loc) · 2.38 KB
/
test_sync_tips.py
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
import os
import pathlib
import pytest
from freezegun import freeze_time
from sync_tips import Tip, parse_tip_file, get_latest_tips
note1_content = """# Some title
Some description
```
>>> start_index, end_index = [
... i for i, line in enumerate(lines)
... if line.strip() == "~~~"
... ]
```
#tag
"""
note2_content = """# Another tip
Multiline
description
and extra newline
```
>>> print("hello")
```
#tag1 #tag2
"""
expected_tip1 = Tip(
title="Some title",
description="Some description",
code='>>> start_index, end_index = [\n... i for i, line in enumerate(lines)\n... if line.strip() == "~~~"\n... ]',
)
expected_tip2 = Tip(
title="Another tip",
description="Multiline\ndescription\nand extra newline",
code='>>> print("hello")',
)
files = """20221130102028.md
20221130102010.md
20221201102028.md
20221201134231.md"""
@pytest.fixture
def note1(tmp_path):
note_path = tmp_path / "note1"
note_path.write_text(note1_content)
return note_path
@pytest.fixture
def note2(tmp_path):
note_path = tmp_path / "note2"
note_path.write_text(note2_content)
return note_path
@pytest.fixture
def note_files(tmpdir_factory):
# casting because LocalPath does not have touch()
notes_dir = pathlib.Path(tmpdir_factory.mktemp("notes"))
for file in files.split():
(notes_dir / file).touch()
assert len(os.listdir(notes_dir)) == 4
return notes_dir
@pytest.mark.parametrize(
"file, expected",
[
("note1", expected_tip1),
("note2", expected_tip2),
],
)
def test_parsing_note_files(request, file, expected):
note_file = request.getfixturevalue(file)
assert parse_tip_file(note_file) == expected
@freeze_time("2022-12-1")
def test_all_new_tips(note_files):
tips = get_latest_tips(tips_dir=note_files)
# 20221130 and 20221201 tips
expected = ['20221130102010', '20221130102028', '20221201102028', '20221201134231']
assert sorted(p.stem for p in tips) == expected
@freeze_time("2022-12-2")
def test_two_new_tips(note_files):
tips = get_latest_tips(tips_dir=note_files)
# only 20221201 tips
expected = ["20221201102028", "20221201134231"]
assert sorted(p.stem for p in tips) == expected
@freeze_time("2022-12-3")
def test_no_new_tips(note_files):
tips = get_latest_tips(tips_dir=note_files)
# no tips, they are all outdated
assert [p.stem for p in tips] == []