Files
wehub-resource-sync e0e362d700
Deploy Docs Pages / build (push) Waiting to run
Deploy Docs Pages / deploy (push) Blocked by required conditions
Real E2E Tests / Real E2E CI (push) Blocked by required conditions
SDK Tests / SDK CI (push) Blocked by required conditions
SDK Tests / changes (push) Successful in 2m29s
Real E2E Tests / changes (push) Successful in 2m29s
Real E2E Tests / Python E2E (docker bridge) (push) Waiting to run
Real E2E Tests / C# E2E (docker bridge) (push) Waiting to run
Real E2E Tests / Go E2E (docker bridge) (push) Waiting to run
Real E2E Tests / Java E2E (docker bridge) (push) Waiting to run
Real E2E Tests / JavaScript E2E (docker bridge) (push) Waiting to run
SDK Tests / Python SDK Tests (sandbox) (push) Waiting to run
SDK Tests / CLI Quality (push) Waiting to run
SDK Tests / CLI Tests (push) Waiting to run
SDK Tests / Python SDK Quality (code-interpreter) (push) Waiting to run
SDK Tests / Python SDK Quality (sandbox) (push) Waiting to run
SDK Tests / Python SDK Tests (code-interpreter) (push) Waiting to run
SDK Tests / JavaScript SDK Quality And Tests (code-interpreter) (push) Waiting to run
SDK Tests / JavaScript SDK Quality And Tests (sandbox) (push) Waiting to run
SDK Tests / Kotlin SDK Quality And Tests (sandbox) (push) Waiting to run
SDK Tests / Kotlin SDK Quality And Tests (code-interpreter) (push) Waiting to run
SDK Tests / C# SDK Quality And Tests (code-interpreter) (push) Waiting to run
SDK Tests / C# SDK Quality And Tests (sandbox) (push) Waiting to run
SDK Tests / Go SDK Quality And Tests (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:39:33 +08:00

109 lines
3.6 KiB
Python

# Copyright 2026 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for opensandbox_cli.utils — duration parsing, key-value type, error handling."""
from __future__ import annotations
from datetime import timedelta
import click
import pytest
from opensandbox_cli.utils import DURATION, KEY_VALUE, parse_duration
# ---------------------------------------------------------------------------
# parse_duration
# ---------------------------------------------------------------------------
class TestParseDuration:
@pytest.mark.parametrize(
"input_str, expected",
[
("10", timedelta(seconds=10)),
("0", timedelta(seconds=0)),
("10s", timedelta(seconds=10)),
("5m", timedelta(minutes=5)),
("2h", timedelta(hours=2)),
("1h30m", timedelta(hours=1, minutes=30)),
("1h30m45s", timedelta(hours=1, minutes=30, seconds=45)),
("90s", timedelta(seconds=90)),
],
)
def test_valid_durations(self, input_str: str, expected: timedelta) -> None:
assert parse_duration(input_str) == expected
@pytest.mark.parametrize(
"input_str",
[
"",
"abc",
"10x",
"m10",
"-5m",
],
)
def test_invalid_durations(self, input_str: str) -> None:
with pytest.raises(click.BadParameter):
parse_duration(input_str)
def test_strips_whitespace(self) -> None:
assert parse_duration(" 10m ") == timedelta(minutes=10)
# ---------------------------------------------------------------------------
# DurationType (Click param type)
# ---------------------------------------------------------------------------
class TestDurationType:
def test_converts_string(self) -> None:
result = DURATION.convert("5m", None, None)
assert result == timedelta(minutes=5)
def test_passes_through_timedelta(self) -> None:
td = timedelta(hours=1)
result = DURATION.convert(td, None, None) # type: ignore[arg-type]
assert result is td
def test_invalid_raises_bad_parameter(self) -> None:
with pytest.raises(click.exceptions.BadParameter):
DURATION.convert("invalid", None, None)
# ---------------------------------------------------------------------------
# KeyValueType (Click param type)
# ---------------------------------------------------------------------------
class TestKeyValueType:
def test_parses_simple_kv(self) -> None:
assert KEY_VALUE.convert("FOO=bar", None, None) == ("FOO", "bar")
def test_value_can_contain_equals(self) -> None:
assert KEY_VALUE.convert("key=a=b=c", None, None) == ("key", "a=b=c")
def test_empty_value(self) -> None:
assert KEY_VALUE.convert("key=", None, None) == ("key", "")
def test_missing_equals_fails(self) -> None:
with pytest.raises(click.exceptions.BadParameter):
KEY_VALUE.convert("no-equals", None, None)
def test_passes_through_tuple(self) -> None:
t = ("key", "val")
result = KEY_VALUE.convert(t, None, None) # type: ignore[arg-type]
assert result is t