-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_cli.py
More file actions
129 lines (101 loc) · 3.47 KB
/
test_cli.py
File metadata and controls
129 lines (101 loc) · 3.47 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
"""Tests for the click CLI wiring in ``unstract.clone.cli``.
Coverage:
- ``_parse_size`` accepts bare integers, K/M/G suffixes, decimals.
- ``--max-file-size 0`` propagates as 0 (force every file to manual list),
not the default cap — distinguished from the unparseable case.
"""
from __future__ import annotations
import pytest
from click.testing import CliRunner
from unstract.clone.cli import _parse_size, cli
from unstract.clone.context import DEFAULT_MAX_FILE_SIZE, CloneOptions
from unstract.clone.report import CloneReport, Endpoint
def test_parse_size_bare_int_is_bytes():
assert _parse_size("25") == 25
def test_parse_size_accepts_kb_mb_gb_units():
assert _parse_size("25MB") == 25 * 1024 * 1024
assert _parse_size("1.5GB") == int(1.5 * 1024 * 1024 * 1024)
assert _parse_size("512K") == 512 * 1024
def test_parse_size_zero_returns_zero():
# Regression for `cap_bytes or DEFAULT` — must not coerce 0 to the
# default. CLI flag --max-file-size 0 means "every file goes to the
# oversize/manual-upload list".
assert _parse_size("0") == 0
def test_parse_size_unknown_unit_raises():
import click
with pytest.raises(click.BadParameter):
_parse_size("10XB")
def test_parse_size_unparseable_raises():
import click
with pytest.raises(click.BadParameter):
_parse_size("not-a-size")
def test_cli_max_file_size_zero_propagates_to_options(monkeypatch):
captured: dict = {}
def fake_clone(source, target, options=None):
captured["options"] = options
return CloneReport(
source=Endpoint(
base_url=source.base_url, organization_id=source.organization_id
),
target=Endpoint(
base_url=target.base_url, organization_id=target.organization_id
),
)
monkeypatch.setattr("unstract.clone.cli.run_clone", fake_clone)
result = CliRunner().invoke(
cli,
[
"clone",
"--source-url",
"http://src",
"--source-org",
"src",
"--source-key",
"sk",
"--target-url",
"http://tgt",
"--target-org",
"tgt",
"--target-key",
"tk",
"--max-file-size",
"0",
],
)
assert result.exit_code == 0, result.output
opts: CloneOptions = captured["options"]
assert opts.max_file_size == 0
def test_cli_max_file_size_default_when_flag_omitted(monkeypatch):
captured: dict = {}
def fake_clone(source, target, options=None):
captured["options"] = options
return CloneReport(
source=Endpoint(
base_url=source.base_url, organization_id=source.organization_id
),
target=Endpoint(
base_url=target.base_url, organization_id=target.organization_id
),
)
monkeypatch.setattr("unstract.clone.cli.run_clone", fake_clone)
result = CliRunner().invoke(
cli,
[
"clone",
"--source-url",
"http://src",
"--source-org",
"src",
"--source-key",
"sk",
"--target-url",
"http://tgt",
"--target-org",
"tgt",
"--target-key",
"tk",
],
)
assert result.exit_code == 0, result.output
opts: CloneOptions = captured["options"]
assert opts.max_file_size == DEFAULT_MAX_FILE_SIZE