#!/usr/bin/env python3 """ Simple test script to verify persistence improvements without MQTT connections """ import os import sys import tempfile import json import sqlite3 from datetime import date # Add the current directory to Python path to import our module sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) def test_persistence_improvements(): """Test that the persistence improvements work correctly""" print("Testing persistence improvements...") # Create a temporary directory to avoid conflicts with tempfile.TemporaryDirectory() as temp_dir: # Change to temp directory original_dir = os.getcwd() os.chdir(temp_dir) try: from frigate_counter import FrigateCounter # Test 1: Check that we can create counter instance without MQTT connection print("1. Testing counter instantiation...") # Mock the MQTT connection to avoid errors during testing counter = FrigateCounter() print("✓ Counter instantiated successfully") # Test 2: Test that counter value can be loaded from JSON print("2. Testing JSON storage functionality...") # Create a test JSON file with some data test_data = {"frigate_camera": {"karung": 42}} with open('karung_counters.json', 'w') as f: json.dump(test_data, f) # Create a new counter instance to test loading counter2 = FrigateCounter() print(f"✓ Counter loaded from JSON: {counter2.counter}") assert counter2.counter == 42, f"Expected 42, got {counter2.counter}" # Test 3: Test save to JSON functionality print("3. Testing save to JSON functionality...") counter2.counter = 100 counter2.save_to_json('test_camera') # Verify the data was saved with open('karung_counters.json', 'r') as f: data = json.load(f) assert data['test_camera']['karung'] == 100, "Save to JSON failed" print("✓ Save to JSON works correctly") # Test 4: Test database functionality print("4. Testing database functionality...") counter2.save_to_database() print("✓ Database save works correctly") # Test 5: Test reset_counter functionality print("5. Testing reset_counter functionality...") counter2.counter = 50 counter2.reset_counter() print("✓ Reset counter works correctly") print("\n✓ All persistence improvements tests passed!") except Exception as e: print(f"✗ Error in persistence test: {e}") import traceback traceback.print_exc() return False finally: os.chdir(original_dir) return True if __name__ == "__main__": test_persistence_improvements()