""" PunimTag Test Configuration Shared fixtures and configuration for all tests. """ import pytest import sqlite3 import tempfile import os import sys from pathlib import Path # Add src to path for imports sys.path.insert(0, str(Path(__file__).parent.parent / "src")) from backend.app import app @pytest.fixture def test_db(): """Create a temporary test database.""" db_fd, db_path = tempfile.mkstemp() # Create test database schema conn = sqlite3.connect(db_path) cursor = conn.cursor() # Create tables cursor.execute(''' CREATE TABLE images ( id INTEGER PRIMARY KEY, filename TEXT NOT NULL, path TEXT NOT NULL, date_taken TEXT ) ''') cursor.execute(''' CREATE TABLE faces ( id INTEGER PRIMARY KEY, image_id INTEGER, person_id INTEGER, encoding BLOB, left INTEGER, top INTEGER, right INTEGER, bottom INTEGER ) ''') cursor.execute(''' CREATE TABLE people ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ''') cursor.execute(''' CREATE TABLE tags ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ) ''') cursor.execute(''' CREATE TABLE image_tags ( image_id INTEGER, tag_id INTEGER, PRIMARY KEY (image_id, tag_id) ) ''') conn.commit() conn.close() yield db_path # Cleanup os.close(db_fd) os.unlink(db_path) @pytest.fixture def client(test_db): """Create a test client with test database.""" app.config['TESTING'] = True app.config['DATABASE_PATH'] = test_db with app.test_client() as client: yield client @pytest.fixture def sample_photos(test_db): """Add sample photos to the test database.""" conn = sqlite3.connect(test_db) cursor = conn.cursor() photos = [ ('photo1.jpg', '/test/path/photo1.jpg', '2023-01-01'), ('photo2.jpg', '/test/path/photo2.jpg', '2023-01-02'), ('photo3.jpg', '/test/path/photo3.jpg', '2023-01-03') ] cursor.executemany( 'INSERT INTO images (filename, path, date_taken) VALUES (?, ?, ?)', photos ) conn.commit() conn.close() return photos @pytest.fixture def sample_faces(test_db): """Add sample faces to the test database.""" conn = sqlite3.connect(test_db) cursor = conn.cursor() # Add a person first cursor.execute('INSERT INTO people (name) VALUES (?)', ('Test Person',)) person_id = cursor.lastrowid # Add faces faces = [ (1, person_id, b'fake_encoding_1', 100, 100, 200, 200), (2, person_id, b'fake_encoding_2', 150, 150, 250, 250), (3, None, b'fake_encoding_3', 200, 200, 300, 300), # Unidentified face ] cursor.executemany( 'INSERT INTO faces (image_id, person_id, encoding, left, top, right, bottom) VALUES (?, ?, ?, ?, ?, ?, ?)', faces ) conn.commit() conn.close() return faces