#!/usr/bin/env python3 """ Test script for JSON storage functionality in Frigate MQTT Counter Service """ import os import sys import tempfile import json from datetime import datetime import threading # Add the current directory to Python path to import our module sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) def test_json_storage(): """Test that JSON storage works correctly""" print("Testing JSON storage functionality...") # Create a temporary directory to test with tempfile.TemporaryDirectory() as temp_dir: # Change to temp directory to avoid conflicts original_dir = os.getcwd() os.chdir(temp_dir) try: from frigate_counter import FrigateCounter # Create a counter instance counter = FrigateCounter() # Test JSON storage initialization if os.path.exists(counter.json_storage_path): print("✓ JSON storage file created successfully") # Test writing to JSON storage counter.counter = 5 counter.save_to_json('test_camera') # Test reading from JSON storage loaded_counter = counter.load_from_json('test_camera') if loaded_counter == 5: print("✓ JSON storage read/write works correctly") else: print(f"✗ JSON storage read failed: expected 5, got {loaded_counter}") # Test multiple cameras counter.counter = 10 counter.save_to_json('camera_1') counter.counter = 15 counter.save_to_json('camera_2') loaded_1 = counter.load_from_json('camera_1') loaded_2 = counter.load_from_json('camera_2') if loaded_1 == 10 and loaded_2 == 15: print("✓ Multiple camera JSON storage works correctly") else: print(f"✗ Multiple camera JSON storage failed: {loaded_1}, {loaded_2}") # Test reading non-existent camera non_existent = counter.load_from_json('non_existent_camera') if non_existent == 0: print("✓ Reading non-existent camera returns 0 correctly") else: print(f"✗ Reading non-existent camera failed: {non_existent}") else: print("✗ JSON storage file was not created") except Exception as e: print(f"✗ Error in JSON storage test: {e}") import traceback traceback.print_exc() finally: os.chdir(original_dir) if __name__ == "__main__": print("Running JSON Storage Tests\n") test_json_storage() print("\nTest completed.")