-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_persistence.py
496 lines (395 loc) · 16 KB
/
test_persistence.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env python3
"""Unit test for note.Persistence."""
# Copyright (c) 2022 Falk Werner
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import tempfile
from shutil import rmtree
import os
import pytest
import yaml
from note import Persistence
class FileSystem:
"""Filesystem helper."""
def __init__(self):
self.root_dir = tempfile.mkdtemp(prefix="notepy-test-")
def __del__(self):
rmtree(self.root_dir)
def mkdir(self, directory):
"""Creates a directory."""
full_path = os.path.join(self.root_dir, directory)
os.mkdir(full_path)
def write_file(self, filename, contents):
"""Writes a text file and returns it's full qualified path."""
full_path = os.path.join(self.root_dir, filename)
with open(full_path, "wb") as text_file:
text_file.write(contents.encode('utf-8'))
return full_path
def read_file(self, filename):
"""Returns the contents of a text file."""
full_path = os.path.join(self.root_dir, filename)
with open(full_path, "rb") as text_file:
contents = text_file.read().decode('utf-8')
return contents
def exists(self, filename):
"""Returns true if the file exists."""
full_path = os.path.join(self.root_dir, filename)
return os.path.exists(full_path)
def is_file(self, filename):
"""Returns true, if filename is a file."""
full_path = os.path.join(self.root_dir, filename)
return os.path.isfile(full_path)
def is_dir(self, filename):
"""Returns true, if filename is a directory."""
full_path = os.path.join(self.root_dir, filename)
return os.path.isdir(full_path)
# pylint: disable-next=too-many-arguments
def write_configfile(self,
filename=".notepy.yml",
persistence_version=3,
base_path="base",
geometry="800x600",
font_size=20,
screenshot_command="true",
theme="arc"):
"""Writes a note.py config file."""
config = yaml.dump({
"persistence_version": persistence_version,
"base_path": os.path.join(self.root_dir, base_path),
"geometry": geometry,
"font_size": font_size,
"screenshot_command": screenshot_command,
"theme": theme
})
return self.write_file(filename, config)
fs: FileSystem = None
@pytest.fixture(autouse=True)
def manage_filesystem():
"""Manages a temporary file system for each test.
A fresh file system is created before test.
The file system is cleaned up after each test.
"""
# pylint: disable-next=global-statement
global fs
fs = FileSystem()
yield
fs = None
def check_note(persistence, title, contents, note_tags):
"""Checks if a note is present with correct contents and tags."""
assert title in persistence.list_notes()
tags = persistence.list_tags()
for tag in note_tags:
assert tag in tags
assert persistence.read_note(title) == contents
tags = persistence.list_tags()
assert len(tags) == len(note_tags)
for tag in note_tags:
assert tag in tags
def test_empty_notes():
"""Test basic directory structure on empty notes.
When notes directory does not exist, the basic directory
structure should be created.
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- style.css : style
```
"""
config_file = fs.write_configfile(geometry="320x240",
font_size=42, theme="my-theme")
persistence = Persistence(config_file)
# check root directory
items = os.listdir(fs.root_dir)
assert len(items) == 2
assert fs.is_file(".notepy.yml")
assert fs.is_dir("base")
# check notes directory
notes_path = os.path.join(fs.root_dir, "base")
items = os.listdir(notes_path)
assert len(items) == 1
assert fs.is_file(os.path.join("base","style.css"))
# check persistence
assert len(persistence.list_notes()) == 0
assert len(persistence.list_tags()) == 0
assert persistence.read_note("non-existent") == ""
assert len(persistence.read_tags("non-existent")) == 0
assert persistence.note_path("non-existent") == os.path.join(notes_path, "non-existent")
assert persistence.geometry() == "320x240"
assert persistence.font_size() == 42
assert persistence.theme() == "my-theme"
assert persistence.css() != ""
def test_read_existing():
"""Checks reading existing notes.
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- style.css : style
+-- simple : simple note directory
| +-- README.md : simple note contents
+-- tagged : tagged note directory
| +-- README.md : tagged note contents
| +-- tags.txt : tagged note tags
+-- complex : complex note directory
+-- README.md : complex note contents
+-- tags.txt : complex note tags
+-- pic.png : complex note attachment
```
"""
config_file = fs.write_configfile()
fs.mkdir("base")
custom_style = "custom_ style"
fs.write_file(os.path.join("base", "style.css"), custom_style)
simple_note_path = os.path.join("base","simple")
fs.mkdir(simple_note_path)
simple_note_contents = "# Simple Note"
fs.write_file(os.path.join(simple_note_path, "README.md"), simple_note_contents)
tagged_note_path = os.path.join("base","tagged")
fs.mkdir(tagged_note_path)
tagged_note_contents = "# Tagged Note"
fs.write_file(os.path.join(tagged_note_path, "README.md"), tagged_note_contents)
fs.write_file(os.path.join(tagged_note_path, "tags.txt"), "info")
complex_note_path = os.path.join("base","complex")
fs.mkdir(complex_note_path)
complex_note_contents = "# Complex Note"
fs.write_file(os.path.join(complex_note_path, "README.md"), complex_note_contents)
fs.write_file(os.path.join(complex_note_path, "tags.txt"), "info\ncomplex")
fs.write_file(os.path.join(complex_note_path, "pic.png"), "some attachment")
persistence = Persistence(config_file)
# check custom css
assert persistence.css() == custom_style
# check notes
notes = persistence.list_notes()
assert len(notes) == 3
assert "simple" in notes
assert "tagged" in notes
assert "complex" in notes
# check overall tags
tags = persistence.list_tags()
assert len(tags) == 2
assert "info" in tags
assert "complex" in tags
# check simple note
assert persistence.read_note("simple") == simple_note_contents
assert len(persistence.read_tags("simple")) == 0
# check tagged note
assert persistence.read_note("tagged") == tagged_note_contents
tags = persistence.read_tags("tagged")
assert len(tags) == 1
assert "info" in tags
# check comple note
assert persistence.read_note("complex") == complex_note_contents
tags = persistence.read_tags("complex")
assert len(tags) == 2
assert "info" in tags
assert "complex" in tags
def test_write_geometry():
"""Checks if geometry is written to config file."""
initial_geometry = "320x240"
config_file = fs.write_configfile(geometry=initial_geometry)
persistence = Persistence(config_file)
assert persistence.geometry() == initial_geometry
new_geometry = "640x480"
assert persistence.geometry(new_geometry) == new_geometry
new_persistence = Persistence(config_file)
assert new_geometry == new_persistence.geometry()
def test_create_note():
"""Checks if a new note can be created."""
config_file = fs.write_configfile()
persistence = Persistence(config_file)
title = "new_note"
contents = "# New Note\n\nThis is a new note."
persistence.write_note(title, contents)
persistence.write_tags(title, ["new", "note"])
check_note(persistence, title, contents, ["new", "note"])
# Check also against fresh persistence to make
# sure everything is written to file system.
check_note(Persistence(config_file), title, contents, ["new", "note"])
def test_create_note_special_char():
"""Checks if a new note can be created with some special characters."""
config_file = fs.write_configfile()
persistence = Persistence(config_file)
title = "new_note /!<>:\ \\ | ?*%"
contents = "# New Note\n\nThis is a new note."
persistence.write_note(title, contents)
persistence.write_tags(title, ["new", "note"])
check_note(persistence, title, contents, ["new", "note"])
# Check also against fresh persistence to make
# sure everything is written to file system.
check_note(Persistence(config_file), title, contents, ["new", "note"])
def test_modify_existing_note():
"""Checks if a note can be moified."""
config_file = fs.write_configfile()
initial_persistence = Persistence(config_file)
title = "some note"
old_contents = "old contents"
initial_persistence.write_note(title, old_contents)
initial_persistence.write_tags(title, ["old", "note"])
persistence = Persistence(config_file)
assert persistence.read_note(title) == old_contents
new_contents = "new contents"
persistence.write_note(title, new_contents)
persistence.write_tags(title, ["new"])
check_note(persistence, title, new_contents, ["new"])
# Check also against fresh persistence to make
# sure everything is written to file system.
new_persistence = Persistence(config_file)
check_note(new_persistence, title, new_contents, ["new"])
assert len(new_persistence.list_tags()) == 1
def test_rename_note():
"""Checks if a note can be renamed."""
config_file = fs.write_configfile()
initial_persistence = Persistence(config_file)
old_title = "old-note"
contents = "some contents"
initial_persistence.write_note(old_title, contents)
initial_persistence.write_tags(old_title, ["some", "note"])
persistence = Persistence(config_file)
check_note(persistence, old_title, contents, ["some", "note"])
new_title = "new-note"
persistence.rename_note(old_title, new_title)
check_note(persistence, new_title, contents, ["some", "note"])
assert len(persistence.list_notes()) == 1
# Check also against fresh persistence to make
# sure everything is written to file system.
new_persistence = Persistence(config_file)
check_note(new_persistence, new_title, contents, ["some", "note"])
assert len(new_persistence.list_notes()) == 1
def test_remove_note():
"""Check if a note can be removed."""
config_file = fs.write_configfile()
initial_persistence = Persistence(config_file)
title = "some-note"
contents = "some contents"
initial_persistence.write_note(title, contents)
initial_persistence.write_tags(title, ["some", "note"])
persistence = Persistence(config_file)
check_note(persistence, title, contents, ["some", "note"])
persistence.remove_note(title)
assert len(persistence.list_notes()) == 0
assert len(persistence.list_tags()) == 0
assert len(persistence.read_tags(title)) == 0
# Check also against fresh persistence to make
# sure everything is written to file system.
new_persistence = Persistence(config_file)
assert len(new_persistence.list_notes()) == 0
assert len(new_persistence.list_tags()) == 0
assert len(new_persistence.read_tags(title)) == 0
def test_migrate_from_v1():
"""Checks migration from persistence v1.
In persistence v1, the contents of a note where stored in
a file called note.md. This file should be renamed to
README.md.
V1 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- notes : notes directory
+-- old-note : direcotry of the old note
+-- note.md : contents of the old note
```
V3 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- old-note : direcotry of the old note
+-- README.md : contents of the old note
```
"""
config_file = fs.write_configfile(persistence_version=1)
fs.mkdir("base")
fs.mkdir(os.path.join("base","notes"))
note_path = os.path.join("base","notes","old-note")
fs.mkdir(note_path)
contents = "Contents of old note"
fs.write_file(os.path.join(note_path, "note.md"), contents)
persistence = Persistence(config_file)
assert fs.is_file(os.path.join("base", "old-note", "README.md"))
assert not fs.is_dir(note_path)
notes = persistence.list_notes()
assert len(notes) == 1
assert len(persistence.list_tags()) == 0
assert "old-note" in notes
assert persistence.read_note("old-note") == contents
assert len(persistence.read_tags("old-note")) == 0
def test_migrate_from_v2():
"""Checks migration from persistence v1.
In persistence v2, a subdirectory "notes" is used
to store each note.
V2 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- notes : notes directory
+-- old-note : direcotry of the old note
+-- README.md : contents of the old note
```
V3 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- old-note : direcotry of the old note
+-- README.md : contents of the old note
```
"""
config_file = fs.write_configfile(persistence_version=2)
fs.mkdir("base")
fs.mkdir(os.path.join("base","notes"))
note_path = os.path.join("base","notes","old-note")
fs.mkdir(note_path)
contents = "Contents of old note"
fs.write_file(os.path.join(note_path, "README.md"), contents)
persistence = Persistence(config_file)
assert fs.is_file(os.path.join("base", "old-note", "README.md"))
assert not fs.is_dir(note_path)
notes = persistence.list_notes()
assert len(notes) == 1
assert len(persistence.list_tags()) == 0
assert "old-note" in notes
assert persistence.read_note("old-note") == contents
assert len(persistence.read_tags("old-note")) == 0
def test_migrate_from_v2_with_note_names_notes():
"""Checks migration from persistence v1.
In persistence v2, a subdirectory "notes" is used
to store each note.
V2 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- notes : notes directory
+-- notes : direcotry of the old note
+-- README.md : contents of the old note
```
V3 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- old-note : direcotry of the old note
+-- README.md : contents of the old note
```
"""
config_file = fs.write_configfile(persistence_version=2)
fs.mkdir("base")
fs.mkdir(os.path.join("base","notes"))
note_path = os.path.join("base","notes","notes")
fs.mkdir(note_path)
contents = "Contents of old note"
fs.write_file(os.path.join(note_path, "README.md"), contents)
persistence = Persistence(config_file)
assert fs.is_file(os.path.join("base", "notes", "README.md"))
assert not fs.is_dir(note_path)
notes = persistence.list_notes()
assert len(notes) == 1
assert len(persistence.list_tags()) == 0
assert "notes" in notes
assert persistence.read_note("notes") == contents
assert len(persistence.read_tags("notes")) == 0