-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_strutils.py
More file actions
427 lines (355 loc) · 16 KB
/
test_strutils.py
File metadata and controls
427 lines (355 loc) · 16 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
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
from pathlib import Path
import tempfile
import os
import strutils
from unittest.mock import patch, MagicMock
from contextlib import contextmanager
def test_approx_eq():
assert strutils.approx_eq(1.0, 1.0)
assert strutils.approx_eq(1.0, 1.5, absdiff=0.6)
assert not strutils.approx_eq(100.0, 105.0, absdiff=1.0, percent=2.0)
assert strutils.approx_eq(100.0, 102.0, absdiff=1.0, percent=3.0)
def test_sanitize_string():
assert strutils.sanitize_string(" hello \n world!@#$%^&*()_+ ") == "hello world_"
def test_md5():
assert strutils.md5(b"test") == "098f6bcd4621d373cade4e832627b4f6"
assert strutils.md5("test") == "098f6bcd4621d373cade4e832627b4f6"
with tempfile.NamedTemporaryFile(delete=False) as tf:
tf.write(b"test")
tf.close()
assert strutils.md5(Path(tf.name)) == "098f6bcd4621d373cade4e832627b4f6"
os.remove(tf.name)
def test_cumsum():
assert strutils.cumsum([1, 2, 3]) == [1, 3, 6]
def test_naturally_sorted():
assert strutils.naturally_sorted(["item10", "item1", "item2"]) == ["item1", "item2", "item10"]
def test_fully_encode_url():
assert strutils.fully_encode_url("https://example.com/Nibbāna%20and%20Abhidhamma") == "https://example.com/Nibb%C4%81na%20and%20Abhidhamma"
def test_FileSyncedSet():
with tempfile.NamedTemporaryFile(delete=False) as tf:
fname = tf.name
s = strutils.FileSyncedSet(fname)
s.add("hello")
s.add("world")
assert "hello" in s
assert len(s) == 2
s2 = strutils.FileSyncedSet(fname)
assert "hello" in s2
assert "world" in s2
s2.remove("hello")
assert "hello" not in s2
assert len(s2) == 1
s2.delete_file()
def test_FileSyncedSet_normalizer():
with tempfile.NamedTemporaryFile(delete=False) as tf:
fname = tf.name
# Test default normalizer (replaces \n with space)
s = strutils.FileSyncedSet(fname)
s.add("hello\nworld")
assert "hello world" in s
assert "hello\nworld" in s
assert len(s) == 1
# Test custom normalizer (lowercase)
s_norm = strutils.FileSyncedSet(fname, normalizer=lambda x: x.lower())
s_norm.add("HELLO")
assert "hello" in s_norm
assert "HELLO" in s_norm
assert len(s_norm.items) == 2 # "hello world" and "hello"
s_norm.remove("Hello")
assert "hello" not in s_norm
os.remove(fname)
def test_FileSyncedMap():
with tempfile.NamedTemporaryFile(delete=False) as tf:
fname = tf.name
tf.write(b"{}")
tf.close()
m = strutils.FileSyncedMap(fname)
m.set("key", "value")
assert m.get("key") == "value"
assert "key" in m
assert m["key"] == "value"
m2 = strutils.FileSyncedMap(fname)
assert m2["key"] == "value"
m2.delete_file()
def test_FileSyncedMap_normalizer():
with tempfile.NamedTemporaryFile(delete=False) as tf:
fname = tf.name
tf.write(b"{}")
tf.close()
# Test custom keynormalizer (lowercase)
m = strutils.FileSyncedMap(fname, keynormalizer=lambda x: x.lower())
m.set("Key", "Value")
assert "key" in m
assert "KEY" in m
assert m["kEy"] == "Value"
assert m.get("KEY") == "Value"
m["AnotherKey"] = "AnotherValue"
assert "anotherkey" in m
# Test update (now uses normalization)
m.update({"UPPER": "value"})
assert "upper" in m.items
assert "UPPER" not in m.items
del m["UPPER"]
assert "upper" not in m
m.delete_file()
def test_invert_inverted_index():
index = {"hello": [0, 2], "world": [1]}
assert strutils.invert_inverted_index(index) == ["hello", "world", "hello"]
def test_authorstr():
assert strutils.authorstr({"authorships": [{"author": {"display_name": "Smith, John"}}]}) == "John Smith"
assert strutils.authorstr({"authors": [{"name": "Smith, John"}]}) == "John Smith"
assert strutils.authorstr({"authors": [{"name": "Smith, John"}, {"name": "Doe, Jane"}, {"name": "Foo, Bar"}]}) == "John Smith, et al"
assert strutils.authorstr({"authors": [{"name": "Smith, John"}, {"name": "Doe, Jane"}, {"name": "Foo, Bar"}, {"name": "Extra, User"}]}, maxn=3) == "John Smith, Jane Doe, et al"
def test_yt_url_to_id_re():
# Standard YouTube URL
m = strutils.yt_url_to_id_re.search("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
assert m is not None
assert m.group(1) == "dQw4w9WgXcQ"
# youtu.be short URL
m = strutils.yt_url_to_id_re.search("https://youtu.be/dQw4w9WgXcQ")
assert m is not None
assert m.group(1) == "dQw4w9WgXcQ"
# YouTube Embed URL
m = strutils.yt_url_to_id_re.search("https://www.youtube.com/embed/dQw4w9WgXcQ")
assert m is not None
assert m.group(1) == "dQw4w9WgXcQ"
# YouTube No-cookie Embed URL
m = strutils.yt_url_to_id_re.search("https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ")
assert m is not None
assert m.group(1) == "dQw4w9WgXcQ"
# YouTube Live URL
m = strutils.yt_url_to_id_re.search("https://www.youtube.com/live/dQw4w9WgXcQ?feature=share")
assert m is not None
assert m.group(1) == "dQw4w9WgXcQ"
# YouTube v/ URL
m = strutils.yt_url_to_id_re.search("https://www.youtube.com/v/dQw4w9WgXcQ")
assert m is not None
assert m.group(1) == "dQw4w9WgXcQ"
# YouTube attribution URL
m = strutils.yt_url_to_id_re.search("https://www.youtube.com/attribution_link?a=xyz&u=/watch?v=dQw4w9WgXcQ&feature=share")
assert m is not None
assert m.group(1) == "dQw4w9WgXcQ"
# YouTube user/channel URL
m = strutils.yt_url_to_id_re.search("https://www.youtube.com/user/google/dQw4w9WgXcQ")
assert m is not None
assert m.group(1) == "dQw4w9WgXcQ"
def test_yt_url_to_plid_re():
# Standard Playlist URL
m = strutils.yt_url_to_plid_re.search("https://www.youtube.com/playlist?list=PL3oW2tjiIx0UvWb6e3qzWwpTMTsd-6O3R")
assert m is not None
assert m.group(1) == "PL3oW2tjiIx0UvWb6e3qzWwpTMTsd-6O3R"
# Video + Playlist URL
m = strutils.yt_url_to_plid_re.search("https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=PL3oW2tjiIx0UvWb6e3qzWwpTMTsd-6O3R")
assert m is not None
assert m.group(1) == "PL3oW2tjiIx0UvWb6e3qzWwpTMTsd-6O3R"
m = strutils.yt_url_to_plid_re.search("Check out this playlist: https://www.youtube.com/playlist?list=PLID123")
assert m is not None
assert m.group(1) == "PLID123"
def test_trunc():
assert strutils.trunc("hello world", 5) == "hell…"
assert strutils.trunc("hi", 5) == "hi"
def test_cout():
with patch('builtins.print') as mock_print:
strutils.cout("hello", "world")
mock_print.assert_called_once_with("hello", "world", flush=True, end="")
def test_prompt():
with patch('builtins.input', return_value='y'):
assert strutils.prompt("Do you agree?") == True
with patch('builtins.input', return_value='n'):
assert strutils.prompt("Do you agree?") == False
with patch('builtins.input', side_effect=['', 'y']):
assert strutils.prompt("Do you agree?", default='y') == True
def test_get_terminal_size():
with patch('strutils.termios.tcgetwinsize', return_value=(24, 80)):
assert strutils.get_terminal_size() == (24, 80)
@contextmanager
def mock_terminal(read_side_effect):
with patch('strutils.os.get_terminal_size') as mock_gts, \
patch('strutils.get_terminal_size', return_value=(80, 24)), \
patch('strutils.get_cursor_position', return_value=(24, 80)), \
patch('strutils.sys.stdin.read', side_effect=read_side_effect), \
patch('strutils.sys.stdout.write'), \
patch('strutils.termios.tcgetattr', return_value=[0,0,0,0,0,0,0]), \
patch('strutils.termios.tcsetattr'), \
patch('strutils.tty.setraw'), \
patch('strutils.sys.stdin.fileno', return_value=0):
mock_gts.return_value.lines = 24
mock_gts.return_value.columns = 80
yield
def test_radio_dial():
with mock_terminal(read_side_effect=['\n']):
assert strutils.radio_dial(["Opt A", "Opt B"]) == 0
with mock_terminal(read_side_effect=['2', '\n']):
assert strutils.radio_dial(["Opt A", "Opt B", "Opt C"]) == 1
with mock_terminal(read_side_effect=['\x1b', '[', 'B', '\x1b', '[', 'B', '\n']):
assert strutils.radio_dial(["Opt A", "Opt B", "Opt C"]) == 2
with mock_terminal(read_side_effect=['5', '3']):
assert strutils.radio_dial(["Opt A", "Opt B", "Opt C"]) == 2
def test_checklist_prompt_end_brings_to_submit():
with mock_terminal(read_side_effect=['\x1b', '[', 'F', '\n']):
res = strutils.checklist_prompt(["A", "B"], default=False)
assert res == [False, False]
def test_checklist_prompt_navigation_and_selection():
# Sequence:
# 1. Space (select A)
# 2. Down (to B)
# 3. Space (select B)
# 4. Down (to C)
# 5. Space (select C)
# 6. Up (to B)
# 7. Space (unselect B)
# 8. Down, Down (to Accept)
# 9. Enter
sequence = [
' ',
'\x1b', '[', 'B',
' ',
'\x1b', '[', 'B',
' ',
'\x1b', '[', 'A',
' ',
'\x1b', '[', 'B', '\x1b', '[', 'B',
'\n'
]
with mock_terminal(read_side_effect=sequence):
res = strutils.checklist_prompt(["A", "B", "C"], default=False)
assert res == [True, False, True]
def test_input_list():
with patch('builtins.input', side_effect=['item1', 'item2', '']), \
patch('builtins.print'):
assert strutils.input_list("Enter items:") == ['item1', 'item2']
def test_format_size():
# Bytes: no decimal places
assert strutils.format_size(0) == "0 B"
assert strutils.format_size(512) == "512 B"
assert strutils.format_size(999) == "999 B"
# 1000 B crosses into KB (still divided by 1024)
assert strutils.format_size(1000) == "0.98 KB"
assert strutils.format_size(1024) == "1.00 KB"
assert strutils.format_size(1536) == "1.50 KB"
# 1000 KB crosses into MB
assert strutils.format_size(1000 * 1024) == "0.98 MB"
assert strutils.format_size(1024 ** 2) == "1.00 MB"
assert strutils.format_size(2.5 * 1024 ** 2) == "2.50 MB"
# Gigabytes
assert strutils.format_size(1024 ** 3) == "1.00 GB"
# Terabytes
assert strutils.format_size(1024 ** 4) == "1.00 TB"
# Petabytes — beyond TB the loop exits and the fallback PB line is used
assert strutils.format_size(1024 ** 5) == "1.00 PB"
def _make_md(tmp_path, content: str) -> Path:
"""Write *content* to a temp .md file and return the Path."""
p = tmp_path / "test.md"
p.write_text(content)
return p
def test_write_frontmatter_key_update_existing_list(tmp_path):
"""Overwrite an existing list key with new values."""
md = _make_md(tmp_path, "---\ntags:\n - \"old\"\ntitle: hello\n---\nbody\n")
strutils.write_frontmatter_key(md, "tags", ["new1", "new2"])
result = md.read_text()
assert " - \"new1\"" in result
assert " - \"new2\"" in result
assert "old" not in result
assert "title: hello" in result # other keys preserved
def test_write_frontmatter_key_insert_new_list_default(tmp_path):
"""Insert a new key when insert_after_key is not specified (defaults to "---").
With no insert_after_key the function uses "---" as the sentinel, which
means insertafterkeyendsline is set to the *closing* "---" line. The new
key is therefore inserted just before that closing "---", i.e. after all
existing keys.
"""
md = _make_md(tmp_path, "---\ntitle: hello\n---\nbody\n")
strutils.write_frontmatter_key(md, "tags", ["a", "b"])
result = md.read_text()
lines = result.split("\n")
assert lines[0] == "---"
# tags: must appear somewhere inside the frontmatter block
closing = lines.index("---", 1)
tags_idx = lines.index("tags:")
assert 0 < tags_idx < closing
assert " - \"a\"" in result
assert " - \"b\"" in result
assert "title: hello" in result
def test_write_frontmatter_key_insert_after_specific_key(tmp_path):
"""insert_after_key places the new key right after the named key."""
md = _make_md(tmp_path, "---\ntitle: hello\nauthor: bob\n---\nbody\n")
strutils.write_frontmatter_key(md, "tags", ["x"], insert_after_key="title")
result = md.read_text()
lines = result.split("\n")
title_idx = lines.index("title: hello")
tags_idx = lines.index("tags:")
assert tags_idx == title_idx + 1
def test_write_frontmatter_key_remove_existing_key(tmp_path):
"""Passing a falsy non-list value removes the key from frontmatter.
"""
md = _make_md(tmp_path, "---\ntags:\n - \"old\"\ntitle: hello\n---\nbody\n")
strutils.write_frontmatter_key(md, "tags", None)
result = md.read_text()
assert "tags:" not in result
assert "old" not in result
assert "title: hello" in result
def test_write_frontmatter_key_remove_nonexistent_key_is_noop(tmp_path):
"""Passing a falsy non-list value for a key that doesn't exist is a no-op."""
original = "---\ntitle: hello\n---\nbody\n"
md = _make_md(tmp_path, original)
strutils.write_frontmatter_key(md, "tags", None)
assert md.read_text() == original
def test_write_frontmatter_key_invalid_key_raises(tmp_path):
"""An invalid key name raises ValueError."""
md = _make_md(tmp_path, "---\ntitle: hello\n---\n")
import pytest
with pytest.raises(ValueError, match="valid yaml key"):
strutils.write_frontmatter_key(md, "Invalid Key!", ["v"])
def test_write_frontmatter_key_invalid_insert_after_key_raises(tmp_path):
"""An invalid insert_after_key raises ValueError."""
md = _make_md(tmp_path, "---\ntitle: hello\n---\n")
import pytest
with pytest.raises(ValueError, match="valid yaml key"):
strutils.write_frontmatter_key(md, "tags", ["v"], insert_after_key="Bad Key!")
def test_write_frontmatter_key_unknown_value_type_raises(tmp_path):
"""A non-list, non-string, non-falsy value (e.g. int) raises ValueError."""
md = _make_md(tmp_path, "---\ntitle: hello\n---\n")
import pytest
with pytest.raises(ValueError, match="Unknown value type"):
strutils.write_frontmatter_key(md, "tags", 42)
def test_write_frontmatter_key_list_with_quotes(tmp_path):
"""List values containing double-quotes are properly escaped."""
md = _make_md(tmp_path, "---\ntitle: hello\n---\nbody\n")
strutils.write_frontmatter_key(md, "tags", ['say "hi"'])
result = md.read_text()
assert ' - "say \\"hi\\""' in result
def test_write_frontmatter_key_insert_new_string(tmp_path):
"""Insert a brand-new string key into the frontmatter."""
md = _make_md(tmp_path, "---\ntitle: hello\n---\nbody\n")
strutils.write_frontmatter_key(md, "source", "https://example.com")
result = md.read_text()
assert 'source: "https://example.com"' in result
assert "title: hello" in result
def test_write_frontmatter_key_update_existing_string(tmp_path):
"""Overwrite an existing single-line string key."""
md = _make_md(tmp_path, '---\nsource: "old-url"\ntitle: hello\n---\nbody\n')
strutils.write_frontmatter_key(md, "source", "new-url")
result = md.read_text()
assert 'source: "new-url"' in result
assert "old-url" not in result
assert "title: hello" in result
def test_write_frontmatter_key_overwrite_list_with_string(tmp_path):
"""Replace a multi-line list key with a single string value."""
md = _make_md(tmp_path, '---\ntags:\n - "a"\n - "b"\ntitle: hello\n---\nbody\n')
strutils.write_frontmatter_key(md, "tags", "single")
result = md.read_text()
assert 'tags: "single"' in result
assert ' - ' not in result
assert "title: hello" in result
def test_write_frontmatter_key_string_with_quotes(tmp_path):
"""Double-quotes inside a string value are escaped."""
md = _make_md(tmp_path, "---\ntitle: hello\n---\n")
strutils.write_frontmatter_key(md, "source", 'say "hi"')
result = md.read_text()
assert 'source: "say \\"hi\\""' in result
def test_write_frontmatter_key_string_with_newline_raises(tmp_path):
"""A string value containing a newline raises ValueError."""
md = _make_md(tmp_path, "---\ntitle: hello\n---\n")
import pytest
with pytest.raises(ValueError, match="newlines"):
strutils.write_frontmatter_key(md, "source", "line1\nline2")