test_json_compressor.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import pytest
  2. import json
  3. from magic_pdf.libs.json_compressor import JsonCompressor
  4. # Test data fixtures
  5. @pytest.fixture
  6. def test_cases():
  7. return [
  8. # Simple dictionary
  9. {"name": "John", "age": 30},
  10. # Nested dictionary
  11. {
  12. "person": {
  13. "name": "Alice",
  14. "address": {
  15. "street": "123 Main St",
  16. "city": "New York"
  17. }
  18. }
  19. },
  20. # List of dictionaries
  21. [
  22. {"id": 1, "value": "first"},
  23. {"id": 2, "value": "second"}
  24. ],
  25. # Dictionary with various data types
  26. {
  27. "string": "hello",
  28. "integer": 42,
  29. "float": 3.14,
  30. "boolean": True,
  31. "null": None,
  32. "array": [1, 2, 3],
  33. "nested": {"key": "value"}
  34. },
  35. # Empty structures
  36. {},
  37. [],
  38. {"empty_list": [], "empty_dict": {}}
  39. ]
  40. @pytest.fixture
  41. def large_data():
  42. return {
  43. "data": ["test" * 100] * 100 # Create a large repeated string
  44. }
  45. def test_compression_decompression_cycle(test_cases):
  46. """Test that data remains intact after compression and decompression"""
  47. for test_data in test_cases:
  48. # Compress the data
  49. compressed = JsonCompressor.compress_json(test_data)
  50. # Verify compressed string is not empty and is a string
  51. assert isinstance(compressed, str)
  52. assert len(compressed) > 0
  53. # Decompress the data
  54. decompressed = JsonCompressor.decompress_json(compressed)
  55. # Verify the decompressed data matches original
  56. assert test_data == decompressed
  57. def test_compression_reduces_size(large_data):
  58. """Test that compression actually reduces data size for large enough input"""
  59. original_size = len(json.dumps(large_data))
  60. compressed = JsonCompressor.compress_json(large_data)
  61. compressed_size = len(compressed)
  62. # Verify compression actually saved space
  63. assert compressed_size < original_size
  64. def test_invalid_json_serializable():
  65. """Test handling of non-JSON serializable input"""
  66. with pytest.raises(TypeError):
  67. JsonCompressor.compress_json(set([1, 2, 3])) # sets are not JSON serializable
  68. def test_invalid_compressed_string():
  69. """Test handling of invalid compressed string"""
  70. with pytest.raises(Exception):
  71. JsonCompressor.decompress_json("invalid_base64_string")
  72. def test_empty_string_input():
  73. """Test handling of empty string input"""
  74. with pytest.raises(Exception):
  75. JsonCompressor.decompress_json("")
  76. def test_special_characters():
  77. """Test handling of special characters"""
  78. test_data = {
  79. "special": "!@#$%^&*()_+-=[]{}|;:,.<>?",
  80. "unicode": "Hello 世界 🌍"
  81. }
  82. compressed = JsonCompressor.compress_json(test_data)
  83. decompressed = JsonCompressor.decompress_json(compressed)
  84. assert test_data == decompressed
  85. # Parametrized test for different types of input
  86. @pytest.mark.parametrize("test_input", [
  87. {"simple": "value"},
  88. [1, 2, 3],
  89. {"nested": {"key": "value"}},
  90. ["mixed", 1, True, None],
  91. {"unicode": "🌍"}
  92. ])
  93. def test_various_input_types(test_input):
  94. """Test compression and decompression with various input types"""
  95. compressed = JsonCompressor.compress_json(test_input)
  96. decompressed = JsonCompressor.decompress_json(compressed)
  97. assert test_input == decompressed