Files
sandbox/tests/unit/test_utils.py
Alexander Domene 1f786a83ce Initial commit: Create comprehensive Git playground
- Multi-language source code (Python, JavaScript, Java, C++, Go)
- Configuration files (JSON, YAML, INI)
- Sample data files (CSV, JSON, logs)
- Binary and media assets (PNG, SVG, PDF)
- Test files and deployment scripts
- Nested directory structure for testing Git features

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-25 20:37:14 +02:00

101 lines
2.8 KiB
Python

import pytest
import sys
import os
# Add src directory to path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'src'))
from utils import generate_random_string, hash_string, flatten_dict, chunk_list, DataProcessor
class TestUtils:
def test_generate_random_string(self):
# Test default length
result = generate_random_string()
assert len(result) == 10
assert result.isalnum()
# Test custom length
result = generate_random_string(5)
assert len(result) == 5
# Test uniqueness
result1 = generate_random_string(20)
result2 = generate_random_string(20)
assert result1 != result2
def test_hash_string(self):
# Test SHA256 (default)
result = hash_string("hello")
assert len(result) == 64 # SHA256 produces 64 character hex string
# Test MD5
result = hash_string("hello", "md5")
assert len(result) == 32 # MD5 produces 32 character hex string
# Test consistency
result1 = hash_string("test")
result2 = hash_string("test")
assert result1 == result2
def test_flatten_dict(self):
nested = {
"a": 1,
"b": {
"c": 2,
"d": {
"e": 3
}
}
}
expected = {
"a": 1,
"b.c": 2,
"b.d.e": 3
}
result = flatten_dict(nested)
assert result == expected
# Test custom separator
result = flatten_dict(nested, sep="_")
assert "b_c" in result
assert "b_d_e" in result
def test_chunk_list(self):
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
result = chunk_list(data, 3)
expected = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
assert result == expected
# Test uneven chunks
result = chunk_list(data, 4)
expected = [[1, 2, 3, 4], [5, 6, 7, 8], [9]]
assert result == expected
# Test empty list
result = chunk_list([], 3)
assert result == []
class TestDataProcessor:
def test_process_numbers(self):
processor = DataProcessor()
numbers = [1.0, 2.0, 3.0, 4.0, 5.0]
result = processor.process_numbers(numbers)
assert result['count'] == 5
assert result['sum'] == 15.0
assert result['average'] == 3.0
assert result['min'] == 1.0
assert result['max'] == 5.0
assert processor.processed_count == 1
def test_process_numbers_empty_list(self):
processor = DataProcessor()
result = processor.process_numbers([])
assert result == {}
assert processor.processed_count == 0