|
| 1 | +"""Unit tests for require_valid_resource_name validation helper.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pinecone._internal.validation import require_valid_resource_name |
| 8 | +from pinecone.errors.exceptions import ValidationError |
| 9 | + |
| 10 | + |
| 11 | +def test_valid_name_lowercase_alphanumeric() -> None: |
| 12 | + require_valid_resource_name("name", "mycollection") |
| 13 | + |
| 14 | + |
| 15 | +def test_valid_name_with_hyphens() -> None: |
| 16 | + require_valid_resource_name("name", "my-collection-2025") |
| 17 | + |
| 18 | + |
| 19 | +def test_valid_name_max_length() -> None: |
| 20 | + require_valid_resource_name("name", "a" * 45) |
| 21 | + |
| 22 | + |
| 23 | +def test_valid_name_single_char() -> None: |
| 24 | + require_valid_resource_name("name", "a") |
| 25 | + |
| 26 | + |
| 27 | +def test_empty_string_raises() -> None: |
| 28 | + with pytest.raises(ValidationError, match="non-empty"): |
| 29 | + require_valid_resource_name("name", "") |
| 30 | + |
| 31 | + |
| 32 | +def test_whitespace_only_raises() -> None: |
| 33 | + with pytest.raises(ValidationError, match="non-empty"): |
| 34 | + require_valid_resource_name("name", " ") |
| 35 | + |
| 36 | + |
| 37 | +def test_too_long_raises() -> None: |
| 38 | + with pytest.raises(ValidationError, match="too long"): |
| 39 | + require_valid_resource_name("name", "a" * 46) |
| 40 | + |
| 41 | + |
| 42 | +def test_leading_hyphen_raises() -> None: |
| 43 | + with pytest.raises(ValidationError, match="must not start with a hyphen"): |
| 44 | + require_valid_resource_name("name", "-leading") |
| 45 | + |
| 46 | + |
| 47 | +def test_trailing_hyphen_raises() -> None: |
| 48 | + with pytest.raises(ValidationError, match="must not end with a hyphen"): |
| 49 | + require_valid_resource_name("name", "trailing-") |
| 50 | + |
| 51 | + |
| 52 | +def test_uppercase_raises() -> None: |
| 53 | + with pytest.raises(ValidationError, match="invalid characters"): |
| 54 | + require_valid_resource_name("name", "MY_COLLECTION") |
| 55 | + |
| 56 | + |
| 57 | +def test_underscore_raises() -> None: |
| 58 | + with pytest.raises(ValidationError, match="invalid characters"): |
| 59 | + require_valid_resource_name("name", "under_score") |
| 60 | + |
| 61 | + |
| 62 | +def test_at_symbol_raises() -> None: |
| 63 | + with pytest.raises(ValidationError, match="invalid characters"): |
| 64 | + require_valid_resource_name("name", "test@name") |
| 65 | + |
| 66 | + |
| 67 | +def test_space_raises() -> None: |
| 68 | + with pytest.raises(ValidationError, match="invalid characters"): |
| 69 | + require_valid_resource_name("name", "my collection") |
| 70 | + |
| 71 | + |
| 72 | +def test_error_message_includes_param_name() -> None: |
| 73 | + with pytest.raises(ValidationError) as exc_info: |
| 74 | + require_valid_resource_name("collection_name", "INVALID!") |
| 75 | + assert "collection_name" in str(exc_info.value) |
0 commit comments