-
Notifications
You must be signed in to change notification settings - Fork 838
Expand file tree
/
Copy pathtests_utils.py
More file actions
129 lines (116 loc) · 4.42 KB
/
Copy pathtests_utils.py
File metadata and controls
129 lines (116 loc) · 4.42 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
# Copyright The IETF Trust 2026, All Rights Reserved
from pathlib import Path
from tempfile import TemporaryDirectory
from django.test import override_settings
from ietf import settings
from ietf.doc.factories import RfcFactory
from ietf.doc.storage_utils import exists_in_storage, retrieve_str
from ietf.sync.utils import (
build_from_file_content,
expand_rfc_number_range_list,
load_rfcs_into_blobdb,
rsync_helper,
)
from ietf.utils.test_utils import TestCase
class ExpandRfcNumberRangeListTests(TestCase):
def test_expands_bare_numbers_and_ranges(self):
# ranges follow Python range() conventions: right value is excluded
self.assertEqual(
expand_rfc_number_range_list("[1,100,1000-1004]"),
[1, 100, 1000, 1001, 1002, 1003],
)
def test_accepts_input_without_brackets_and_whitespace(self):
self.assertEqual(
expand_rfc_number_range_list(" 1 , 3 - 5 , 9 "),
[1, 3, 4, 9],
)
def test_overlapping_ranges_are_sorted_and_deduplicated(self):
self.assertEqual(
expand_rfc_number_range_list("[5,1-4,3-6,2]"),
[1, 2, 3, 4, 5],
)
def test_empty_input_yields_empty_list(self):
self.assertEqual(expand_rfc_number_range_list("[]"), [])
self.assertEqual(expand_rfc_number_range_list(""), [])
def test_rejects_invalid_input(self):
for bad in [
"[0]", # zero is not positive
"[-5]", # negatives are not accepted
"[abc]", # non-numeric
"[5-3]", # reversed range
"[5-5]", # empty range (start not less than end)
"[1-]", # missing end
"[-1]", # missing start
"[1-2-3]", # malformed range
"[1.5]", # not an integer
]:
with self.assertRaises(ValueError, msg=f"expected ValueError for {bad!r}"):
expand_rfc_number_range_list(bad)
class RsyncHelperTests(TestCase):
def test_rsync_helper(self):
with (
TemporaryDirectory() as source_dir,
TemporaryDirectory() as dest_dir,
):
with (Path(source_dir) / "canary.txt").open("w") as canary_source_file:
canary_source_file.write("chirp")
rsync_helper(
[
"-a",
f"{source_dir}/",
f"{dest_dir}/",
]
)
with (Path(dest_dir) / "canary.txt").open("r") as canary_dest_file:
chirp = canary_dest_file.read()
self.assertEqual(chirp, "chirp")
def test_build_from_file_content(self):
content = build_from_file_content([12345, 54321])
self.assertEqual(
content,
"""prerelease/
rfc12345.txt
rfc12345.html
rfc12345.xml
rfc12345.pdf
rfc12345.ps
rfc12345.json
prerelease/rfc12345.notprepped.xml
rfc54321.txt
rfc54321.html
rfc54321.xml
rfc54321.pdf
rfc54321.ps
rfc54321.json
prerelease/rfc54321.notprepped.xml
""",
)
class RfcBlobUploadTests(TestCase):
def test_load_rfcs_into_blobdb(self):
with TemporaryDirectory() as faux_rfc_path:
with override_settings(RFC_PATH=faux_rfc_path):
rfc_path = Path(faux_rfc_path)
(rfc_path / "prerelease").mkdir()
for num in [12345, 54321]:
RfcFactory(rfc_number=num)
for ext in settings.RFC_FILE_TYPES + ("json",):
with (rfc_path / f"rfc{num}.{ext}").open("w") as f:
f.write(ext)
with (rfc_path / "rfc{num}.bogon").open("w") as f:
f.write("bogon")
with (rfc_path / "prerelease" / f"rfc{num}.notprepped.xml").open(
"w"
) as f:
f.write("notprepped")
load_rfcs_into_blobdb([12345, 54321])
for num in [12345, 54321]:
for ext in settings.RFC_FILE_TYPES + ("json",):
self.assertEqual(
retrieve_str("rfc", f"{ext}/rfc{num}.{ext}"),
ext,
)
self.assertFalse(exists_in_storage("rfc", f"bogon/rfc{num}.bogon"))
self.assertEqual(
retrieve_str("rfc", f"notprepped/rfc{num}.notprepped.xml"),
"notprepped",
)