File: test_note.py

package info (click to toggle)
iotas 0.12.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,440 kB
  • sloc: python: 14,665; xml: 725; javascript: 45; makefile: 2
file content (331 lines) | stat: -rw-r--r-- 11,658 bytes parent folder | download
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
import time
import unittest
import warnings

from iotas.note import Note

warnings.filterwarnings("ignore", "version")


class Test(unittest.TestCase):

    def test_duplicate(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        note.excerpt = note.title
        note.favourite = False
        note.category = "category"

        dupe = note.duplicate()
        self.assertIsNotNone(dupe)
        self.assertEqual(dupe.content, "content")
        self.assertEqual(dupe.title, "Test Note Title")
        self.assertEqual(dupe.excerpt, "Test Note Title")
        self.assertEqual(dupe.favourite, False)
        self.assertEqual(dupe.category, "category")

    def test_flag_changed(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        note.saving = False
        self.assertFalse(note.dirty)
        note.flag_changed(update_last_modified=False)
        self.assertTrue(note.dirty)
        self.assertEqual(note.last_modified, 0)
        self.assertFalse(note.dirty_while_saving)

        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        note.saving = False
        self.assertFalse(note.dirty)
        note.flag_changed(update_last_modified=True)
        self.assertTrue(note.dirty)
        self.assertTrue(note.last_modified > 0)
        self.assertFalse(note.dirty_while_saving)

        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        note.saving = True
        note.flag_changed(update_last_modified=False)
        self.assertTrue(note.dirty)
        self.assertEqual(note.last_modified, 0)
        self.assertTrue(note.dirty_while_saving)

    def test_regenerate_excerpt(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        self.assertEqual(note.excerpt, "")
        self.assertTrue(note.regenerate_excerpt())
        self.assertEqual(note.excerpt, "content")

        self.assertFalse(note.regenerate_excerpt())
        self.assertEqual(note.excerpt, "content")

        note.content = ""
        self.assertTrue(note.regenerate_excerpt())
        self.assertEqual(note.excerpt, "")

        note.content = "# updated content"
        self.assertTrue(note.regenerate_excerpt())
        self.assertEqual(note.excerpt, "updated content")

        note.content = "## Test Note Title\npost-title"
        self.assertTrue(note.regenerate_excerpt())
        self.assertEqual(note.excerpt, "post-title")

        note.content = "- one\n+ two\n* three"
        self.assertTrue(note.regenerate_excerpt())
        self.assertEqual(note.excerpt, "• one   • two   • three")

        note.content = "- [ ] one\n- [x] two"
        self.assertTrue(note.regenerate_excerpt())
        self.assertEqual(note.excerpt, "☐ one   ☑ two")

        note.content = """long long long long long long long long long long long long long long
        long long long long long long long long long long long long long long long long long long
        long long long long long long long long long long long long long long long long long long
        long long long long long long long long long long long long long long long long long long
        long long long long long long long long long long long long long long long long long long
        long long long long long long long long long long long long long long long long long long
        """
        self.assertTrue(note.regenerate_excerpt())
        self.assertEqual(len(note.excerpt), note.EXCERPT_LENGTH)

    def test_repopulate_meta_from_sync_update(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        note.title_is_top_line = False

        modified = int(time.time())
        meta = {
            "title": "New Title",
            "category": "category",
            "modified": modified,
            "etag": "etag",
            "id": "remoteid",
        }
        note.repopulate_meta_from_sync_update(meta)
        self.assertEqual(note.title, "New Title")
        self.assertEqual(note.category, "category")
        self.assertEqual(note.last_modified, modified)
        self.assertEqual(note.etag, "etag")
        self.assertEqual(note.remote_id, "remoteid")

        note.title = "Reset Title"
        note.title_is_top_line = True
        note.repopulate_meta_from_sync_update(meta)
        self.assertEqual(note.title, "Reset Title")
        self.assertEqual(note.server_sanitised_title, "New Title")

    def test_update_from_remote_sync(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"

        modified = int(time.time())
        remote_update = {
            "title": "New Title",
            "content": "new content",
            "category": "category",
            "modified": modified,
            "etag": "etag",
            "readonly": True,
            "favorite": True,
        }

        note.update_from_remote_sync(remote_update)
        self.assertEqual(note.title, "New Title")
        self.assertEqual(note.content, "new content")
        self.assertEqual(note.excerpt, "new content")
        self.assertEqual(note.category, "category")
        self.assertEqual(note.last_modified, modified)
        self.assertEqual(note.etag, "etag")
        self.assertTrue(note.favourite)
        self.assertTrue(note.read_only)

        remote_update["readonly"] = False
        remote_update["favorite"] = False
        note.update_from_remote_sync(remote_update)
        self.assertFalse(note.favourite)
        self.assertFalse(note.read_only)

    def test_update_title_from_top_line(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        note.title_is_top_line = True
        note.update_title_from_top_line()
        self.assertEqual(note.title, "content")

        note.content = "content"
        note.title = "Test Note Title"
        note.title_is_top_line = False
        note.update_title_from_top_line()
        self.assertEqual(note.title, "Test Note Title")

    def test_identical_excepting_sync_meta(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        note.category = "category"
        note.favourite = True
        note.locally_deleted = False
        note.remote_id = 7
        note.last_modified = int(time.time())
        note.etag = "etag"
        note.dirty = True

        second = Note(new_note=True)
        second.content = "content"
        second.title = "Test Note Title"
        second.category = "category"
        second.favourite = True
        second.locally_deleted = False
        second.remote_id = 7
        second.last_modified = 0
        second.etag = "otheretag"
        second.dirty = False

        self.assertTrue(note.identical_excepting_sync_meta(second))
        second.content = "changed"
        self.assertFalse(note.identical_excepting_sync_meta(second))

    def test_identical_to(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        note.category = "category"
        note.favourite = True
        note.locally_deleted = False
        note.remote_id = 7
        note.last_modified = int(time.time())
        note.etag = "etag"
        note.dirty = True

        second = Note(new_note=True)
        second.content = "content"
        second.title = "Test Note Title"
        second.category = "category"
        second.favourite = True
        second.locally_deleted = False
        second.remote_id = 7
        second.last_modified = note.last_modified
        second.etag = "etag"
        second.dirty = True

        self.assertTrue(note.identical_to(second))
        second.dirty = False
        self.assertFalse(note.identical_to(second))

    def test_etag_is_recent(self) -> None:
        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"

        self.assertFalse(note.etag_is_recent(""))
        note.etag = "etag"

        self.assertTrue(note.etag_is_recent("etag"))
        self.assertFalse(note.etag_is_recent("missing"))
        self.assertFalse(note.etag_is_recent(""))

        note.etag = "second"
        self.assertTrue(note.etag_is_recent("second"))
        self.assertTrue(note.etag_is_recent("etag"))

        note = Note(new_note=True)
        note.content = "content"
        note.title = "Test Note Title"
        note.etag = ""

        self.assertFalse(note.etag_is_recent(""))

    def test_from_backup(self) -> None:
        modified = int(time.time())
        details = {
            "Title": "New Title",
            "Category": "category",
            "Favourite": True,
            "LastModified": modified,
            "LocallyDeleted": False,
            "Dirty": False,
            "ETag": "etag",
            "RemoteId": 7,
        }
        note = Note.from_backup(details, "content", include_sync_attributes=False)
        self.assertIsNotNone(note)
        assert note  # mypy
        self.assertEqual(note.title, "New Title")
        self.assertEqual(note.content, "content")
        self.assertEqual(note.excerpt, "content")
        self.assertEqual(note.category, "category")
        self.assertEqual(note.last_modified, modified)
        self.assertEqual(note.favourite, True)
        self.assertEqual(note.read_only, False)

        note = Note.from_backup(details, "content", include_sync_attributes=True)
        self.assertIsNotNone(note)
        assert note  # mypy
        self.assertEqual(note.etag, "etag")
        self.assertEqual(note.remote_id, 7)

    def test_from_nextcloud(self) -> None:
        modified = int(time.time())
        remote = {
            "title": "New Title",
            "content": "new content",
            "category": "category",
            "modified": modified,
            "etag": "etag",
            "readonly": True,
            "favorite": True,
            "id": 7,
        }
        note = Note.from_nextcloud(remote)

        self.assertIsNotNone(note)
        self.assertEqual(note.title, "New Title")
        self.assertEqual(note.content, "new content")
        self.assertEqual(note.excerpt, "new content")
        self.assertEqual(note.category, "category")
        self.assertEqual(note.last_modified, modified)
        self.assertEqual(note.etag, "etag")
        self.assertEqual(note.read_only, True)
        self.assertEqual(note.favourite, True)
        self.assertEqual(note.remote_id, 7)

    def test_new_and_empty(self) -> None:
        note = Note(new_note=True)
        note.content = ""
        self.assertTrue(note.new_and_empty)

        note.content = "content"
        self.assertFalse(note.new_and_empty)

        note.content = ""
        note.id = 7
        self.assertFalse(note.new_and_empty)

    def test_has_id(self) -> None:
        note = Note(new_note=True)
        self.assertFalse(note.has_id)
        note.id = 13
        self.assertTrue(note.has_id)

    def test_has_remote_id(self) -> None:
        note = Note(new_note=True)
        self.assertFalse(note.has_remote_id)
        note.remote_id = 13
        self.assertTrue(note.has_remote_id)

    def test_content_loaded(self) -> None:
        note = Note(new_note=True)
        self.assertFalse(note.content_loaded)
        note.content = ""
        self.assertTrue(note.content_loaded)