CoolFace
Apppublic

ShahanMalik/Bowling_Action_Analyzer

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
test_json_serialization.py53 linesDownload Raw Back to root
1#!/usr/bin/env python3
2"""
3Test JSON serialization of bowling analysis results
4"""
5
6import json
7import numpy as np
8from bowling_analyzer_hf import convert_numpy_types
9
10def test_numpy_conversion():
11    """Test the NumPy type conversion function"""
12    
13    # Test data with NumPy types
14    test_data = {
15        'bool_value': np.bool_(True),
16        'int_value': np.int64(42),
17        'float_value': np.float64(3.14159),
18        'array_value': np.array([1, 2, 3]),
19        'nested': {
20            'another_bool': np.bool_(False),
21            'another_float': np.float32(2.71)
22        },
23        'list_with_numpy': [np.int32(10), np.float64(20.5), np.bool_(True)]
24    }
25    
26    print("Original data types:")
27    print(f"bool_value: {type(test_data['bool_value'])}")
28    print(f"int_value: {type(test_data['int_value'])}")
29    print(f"float_value: {type(test_data['float_value'])}")
30    print(f"array_value: {type(test_data['array_value'])}")
31    
32    # Convert NumPy types
33    converted_data = convert_numpy_types(test_data)
34    
35    print("\nConverted data types:")
36    print(f"bool_value: {type(converted_data['bool_value'])}")
37    print(f"int_value: {type(converted_data['int_value'])}")
38    print(f"float_value: {type(converted_data['float_value'])}")
39    print(f"array_value: {type(converted_data['array_value'])}")
40    
41    # Test JSON serialization
42    try:
43        json_string = json.dumps(converted_data, indent=2)
44        print("\n✅ JSON serialization successful!")
45        print("Sample JSON:")
46        print(json_string[:200] + "...")
47        return True
48    except Exception as e:
49        print(f"\n❌ JSON serialization failed: {e}")
50        return False
51
52if __name__ == "__main__":
53    test_numpy_conversion()