Added first tests #191
Some checks failed
Test before pr merge / test-lint (pull_request) Failing after 9s

This commit is contained in:
2025-12-10 23:20:25 +01:00
parent cc76227199
commit fa2adb8003
74 changed files with 303 additions and 3334 deletions

0
test/core/__init__.py Normal file
View File

View File

View File

@@ -0,0 +1,27 @@
from cpl.core.utils.base64 import Base64
def test_encode():
assert Base64.encode("hello world") == "aGVsbG8gd29ybGQ="
s = "foobar"
assert Base64.encode(s) and isinstance(Base64.encode(s), str)
def test_decode():
out = Base64.decode("aGVsbG8gd29ybGQ=")
if isinstance(out, bytes):
out = out.decode("utf-8")
assert out == "hello world"
orig = "äöü߀"
enc = Base64.encode(orig)
dec = Base64.decode(enc)
if isinstance(dec, bytes):
dec = dec.decode("utf-8")
assert dec == orig
def test_is_b64():
assert Base64.is_b64("Zm9vYmFy") # "foobar"
assert Base64.is_b64("Zm8=") # "fo"
assert not Base64.is_b64("not_base64??")

View File

@@ -0,0 +1,81 @@
import pytest
from enum import Enum
from typing import List
from cpl.core.utils.cast import cast
class Color(Enum):
RED = "red"
GREEN = "green"
class Status(Enum):
OK = 200
ERROR = 500
def test_cast_none_returns_none():
assert cast(None, int) is None
def test_cast_bool_true_values():
assert cast("true", bool) is True
assert cast("YES", bool) is True
assert cast("1", bool) is True
assert cast("On", bool) is True
def test_cast_bool_false_values():
assert cast("false", bool) is False
assert cast("0", bool) is False
assert cast("no", bool) is False
assert cast("off", bool) is False
def test_cast_list_of_str_with_brackets():
assert cast("[a,b,c]", List[str]) == ["a", "b", "c"]
def test_cast_list_of_str_with_delimiter_no_brackets():
assert cast("a,b,c", List[str]) == ["a", "b", "c"]
def test_cast_list_of_int_with_brackets():
assert cast("[1,2,3]", List[int]) == [1, 2, 3]
def test_cast_list_custom_delimiter():
assert cast("1;2;3", List[int], list_delimiter=";") == [1, 2, 3]
def test_cast_list_without_brackets_or_delimiter_errors():
with pytest.raises(ValueError):
cast("abc", List[str])
def test_cast_enum_by_value_case_insensitive():
assert cast("red", Color) is Color.RED
assert cast("RED", Color) is Color.RED
assert cast("Green", Color) is Color.GREEN
def test_cast_enum_by_name_case_insensitive():
assert cast("OK", Status) is Status.OK
assert cast("ok", Status) is Status.OK
assert cast("ERROR", Status) is Status.ERROR
def test_cast_enum_invalid_raises():
with pytest.raises(ValueError) as e:
cast("unknown", Color)
assert "Cannot cast value 'unknown' to enum 'Color'" in str(e.value)
def test_cast_primitives():
assert cast("42", int) == 42
assert cast("3.14", float) == 3.14
assert cast("abc", str) == "abc"
def test_cast_list_without_subtype_defaults_to_str():
assert cast("a,b", list) == ["a", "b"]

View File

@@ -0,0 +1,18 @@
from cryptography.fernet import Fernet
from cpl.core.utils.credential_manager import CredentialManager
def test_encrypt_decrypt_roundtrip(tmp_path):
secret_path = tmp_path / ".secret"
secret_path.write_text(Fernet.generate_key().decode())
CredentialManager.with_secret(str(secret_path))
plaintext = "hello-world"
token = CredentialManager.encrypt(plaintext)
assert isinstance(token, str)
assert token != plaintext
decrypted = CredentialManager.decrypt(token)
assert decrypted == plaintext

View File

@@ -0,0 +1,38 @@
from cpl.core.utils.number import Number
def test_is_number():
assert Number.is_number(10) is True
assert Number.is_number(3.14) is True
assert Number.is_number("42") is True
assert Number.is_number("3.14") is True
assert Number.is_number("-10") is True
assert Number.is_number("+5.5") is True
assert Number.is_number("abc") is False
assert Number.is_number("") is False
assert Number.is_number(None) is False
assert Number.is_number(True) is False
def test_to_number():
assert Number.to_number("42") == 42
assert Number.to_number("3.14") == 3.14
assert Number.to_number(10) == 10
assert Number.to_number(3.14) == 3.14
assert Number.to_number("-7") == -7
assert Number.to_number("+8.2") == 8.2
# Optional: define how errors behave
try:
Number.to_number("abc")
except ValueError:
pass
else:
assert False, "Expected ValueError for non-numeric input"
try:
Number.to_number(None)
except (TypeError, ValueError):
pass
else:
assert False, "Expected exception for None input"

View File

@@ -0,0 +1,73 @@
import pytest
from cpl.core.utils import String
def test_to_camel_case():
assert String.to_camel_case("hello world") == "helloWorld"
assert String.to_camel_case("hello-world") == "helloWorld"
assert String.to_camel_case("hello_world") == "helloWorld"
assert String.to_camel_case("HelloWorld") == "helloWorld"
assert String.to_camel_case("hello world") == "helloWorld"
assert String.to_camel_case("") == ""
assert String.to_camel_case(" ") == ""
assert String.to_camel_case("123-abc") == "123Abc"
def test_to_pascal_case():
assert String.to_pascal_case("hello world") == "HelloWorld"
assert String.to_pascal_case("hello-world") == "HelloWorld"
assert String.to_pascal_case("hello_world") == "HelloWorld"
assert String.to_pascal_case("helloWorld") == "HelloWorld"
assert String.to_pascal_case("") == ""
assert String.to_pascal_case(" ") == ""
assert String.to_pascal_case("123-abc") == "123Abc"
def test_to_snake_case():
assert String.to_snake_case("HelloWorld") == "hello_world"
assert String.to_snake_case("helloWorld") == "hello_world"
assert String.to_snake_case("hello-world") == "hello_world"
assert String.to_snake_case("hello_world") == "hello_world"
assert String.to_snake_case("hello world") == "hello_world"
assert String.to_snake_case("ABC123") == "abc123"
def test_first_to_upper():
assert String.first_to_upper("hello") == "Hello"
assert String.first_to_upper("Hello") == "Hello"
assert String.first_to_upper("") == ""
assert String.first_to_upper("a") == "A"
assert String.first_to_upper("123abc") == "123abc"
def test_first_to_lower():
assert String.first_to_lower("Hello") == "hello"
assert String.first_to_lower("hello") == "hello"
assert String.first_to_lower("") == ""
assert String.first_to_lower("A") == "a"
assert String.first_to_lower("123ABC") == "123ABC"
def test_random_length():
random_str = String.random(10)
assert len(random_str) == 10
def test_random_only_letters():
random_str = String.random(20, letters=True, digits=False, special_characters=False)
assert all(c.isalpha() for c in random_str)
def test_random_only_digits():
random_str = String.random(20, letters=False, digits=True, special_characters=False)
assert all(c.isdigit() for c in random_str)
def test_random_letters_and_digits():
random_str = String.random(20, letters=True, digits=True, special_characters=False)
assert all(c.isalnum() for c in random_str)
def test_random_no_characters():
with pytest.raises(Exception):
String.random(10, letters=False, digits=False, special_characters=False)