test_json_compressor.py 3.45 KB
Newer Older
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
import pytest
import json
from magic_pdf.libs.json_compressor import JsonCompressor

# Test data fixtures
@pytest.fixture
def test_cases():
    return [
        # Simple dictionary
        {"name": "John", "age": 30},
        
        # Nested dictionary
        {
            "person": {
                "name": "Alice",
                "address": {
                    "street": "123 Main St",
                    "city": "New York"
                }
            }
        },
        
        # List of dictionaries
        [
            {"id": 1, "value": "first"},
            {"id": 2, "value": "second"}
        ],
        
        # Dictionary with various data types
        {
            "string": "hello",
            "integer": 42,
            "float": 3.14,
            "boolean": True,
            "null": None,
            "array": [1, 2, 3],
            "nested": {"key": "value"}
        },
        
        # Empty structures
        {},
        [],
        {"empty_list": [], "empty_dict": {}}
    ]

@pytest.fixture
def large_data():
    return {
        "data": ["test" * 100] * 100  # Create a large repeated string
    }

def test_compression_decompression_cycle(test_cases):
    """Test that data remains intact after compression and decompression"""
    for test_data in test_cases:
        # Compress the data
        compressed = JsonCompressor.compress_json(test_data)
        
        # Verify compressed string is not empty and is a string
        assert isinstance(compressed, str)
        assert len(compressed) > 0
        
        # Decompress the data
        decompressed = JsonCompressor.decompress_json(compressed)
        
        # Verify the decompressed data matches original
        assert test_data == decompressed

def test_compression_reduces_size(large_data):
    """Test that compression actually reduces data size for large enough input"""
    original_size = len(json.dumps(large_data))
    compressed = JsonCompressor.compress_json(large_data)
    compressed_size = len(compressed)
    
    # Verify compression actually saved space
    assert compressed_size < original_size

def test_invalid_json_serializable():
    """Test handling of non-JSON serializable input"""
    with pytest.raises(TypeError):
        JsonCompressor.compress_json(set([1, 2, 3]))  # sets are not JSON serializable

def test_invalid_compressed_string():
    """Test handling of invalid compressed string"""
    with pytest.raises(Exception):
        JsonCompressor.decompress_json("invalid_base64_string")

def test_empty_string_input():
    """Test handling of empty string input"""
    with pytest.raises(Exception):
        JsonCompressor.decompress_json("")

def test_special_characters():
    """Test handling of special characters"""
    test_data = {
        "special": "!@#$%^&*()_+-=[]{}|;:,.<>?",
        "unicode": "Hello 世界 🌍"
    }
    
    compressed = JsonCompressor.compress_json(test_data)
    decompressed = JsonCompressor.decompress_json(compressed)
    assert test_data == decompressed

# Parametrized test for different types of input
@pytest.mark.parametrize("test_input", [
    {"simple": "value"},
    [1, 2, 3],
    {"nested": {"key": "value"}},
    ["mixed", 1, True, None],
    {"unicode": "🌍"}
])
def test_various_input_types(test_input):
    """Test compression and decompression with various input types"""
    compressed = JsonCompressor.compress_json(test_input)
    decompressed = JsonCompressor.decompress_json(compressed)
    assert test_input == decompressed