Files
ppf/tests/conftest.py
Username 44604f1ce3 tests: add unit test infrastructure
pytest-based test suite with fixtures for database testing.
Covers misc.py utilities, dbs.py operations, and fetch.py validation.
Includes mock_network.py for future network testing.
2026-01-08 01:42:38 +01:00

130 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
"""Shared pytest fixtures for PPF tests."""
from __future__ import print_function
import os
import sys
import tempfile
import shutil
import pytest
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import mysqlite
import dbs
import misc
@pytest.fixture(autouse=True)
def mock_logging(monkeypatch):
"""Mock _log to avoid Python 2 print syntax errors in Python 3.
Must patch in both misc and dbs modules since dbs uses 'from misc import _log'.
"""
def noop_log(msg, level='info'):
pass
monkeypatch.setattr(misc, '_log', noop_log)
monkeypatch.setattr(dbs, '_log', noop_log)
@pytest.fixture
def temp_db():
"""Create a temporary SQLite database for testing.
Yields a tuple of (sqlite_wrapper, db_path).
Database and file are cleaned up after test.
"""
fd, db_path = tempfile.mkstemp(suffix='.sqlite')
os.close(fd)
sqlite = mysqlite.mysqlite(db_path)
yield sqlite, db_path
# Cleanup
sqlite.close()
if os.path.exists(db_path):
os.unlink(db_path)
@pytest.fixture
def proxy_db(temp_db):
"""Create a temporary database with proxylist table initialized.
Yields a tuple of (sqlite_wrapper, db_path).
"""
sqlite, db_path = temp_db
dbs.create_table_if_not_exists(sqlite, 'proxylist')
yield sqlite, db_path
@pytest.fixture
def uri_db(temp_db):
"""Create a temporary database with uris table initialized.
Yields a tuple of (sqlite_wrapper, db_path).
"""
sqlite, db_path = temp_db
dbs.create_table_if_not_exists(sqlite, 'uris')
yield sqlite, db_path
@pytest.fixture
def full_db(temp_db):
"""Create a temporary database with both proxylist and uris tables.
Yields a tuple of (sqlite_wrapper, db_path).
"""
sqlite, db_path = temp_db
dbs.create_table_if_not_exists(sqlite, 'proxylist')
dbs.create_table_if_not_exists(sqlite, 'uris')
yield sqlite, db_path
@pytest.fixture
def temp_dir():
"""Create a temporary directory for testing.
Yields the directory path. Cleaned up after test.
"""
dirpath = tempfile.mkdtemp()
yield dirpath
shutil.rmtree(dirpath, ignore_errors=True)
@pytest.fixture
def sample_proxies():
"""Return a list of sample proxy strings for testing."""
return [
'1.2.3.4:8080',
'5.6.7.8:3128',
'9.10.11.12:1080',
]
@pytest.fixture
def sample_private_ips():
"""Return a list of private/reserved IP proxies that should be rejected."""
return [
'10.0.0.1:8080', # Private class A
'172.16.0.1:8080', # Private class B
'192.168.1.1:8080', # Private class C
'127.0.0.1:8080', # Loopback
'169.254.1.1:8080', # Link-local
'224.0.0.1:8080', # Multicast
'100.64.0.1:8080', # CGNAT
]
@pytest.fixture
def sample_cdn_ips():
"""Return a list of CDN IP proxies that should be filtered."""
return [
'141.101.1.1:8080', # Cloudflare
'151.101.1.1:8080', # Fastly
'23.32.1.1:8080', # Akamai
'13.32.1.1:8080', # AWS CloudFront
]