musicbrainz-cleaner/test_db_connection.py

105 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""
Simple test script to verify database connection and fuzzy search.
"""
import psycopg2
from fuzzywuzzy import fuzz
import sys
def test_db_connection():
"""Test database connection."""
try:
# Try different connection methods
hosts = ["localhost", "172.18.0.5", "musicbrainz-docker-db-1"]
for host in hosts:
print(f"Trying to connect to {host}...")
try:
conn = psycopg2.connect(
host=host,
port=5432,
database="musicbrainz",
user="musicbrainz",
password="musicbrainz",
connect_timeout=5
)
print(f"✅ Successfully connected to {host}")
# Test a simple query
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM artist")
count = cursor.fetchone()[0]
print(f"📊 Found {count:,} artists in database")
# Test fuzzy search
test_fuzzy_search(cursor)
conn.close()
return True
except Exception as e:
print(f"❌ Failed to connect to {host}: {e}")
continue
except Exception as e:
print(f"❌ Database connection failed: {e}")
return False
def test_fuzzy_search(cursor):
"""Test fuzzy search functionality."""
print("\n🔍 Testing fuzzy search...")
test_artists = [
"The Beatles",
"Queen",
"Pink Floyd",
"Coldplay",
"AC/DC",
"Bruno Mars",
"Taylor Swift"
]
for artist in test_artists:
print(f"\nSearching for: {artist}")
# First try exact match
cursor.execute("SELECT name FROM artist WHERE name = %s", (artist,))
exact_matches = cursor.fetchall()
if exact_matches:
print(f" ✅ Exact match found: {exact_matches[0][0]}")
continue
# If no exact match, try fuzzy search with more artists
cursor.execute("SELECT name FROM artist LIMIT 10000") # Increased limit
artists = [row[0] for row in cursor.fetchall()]
# Find best match
best_match = None
best_score = 0
for db_artist in artists:
score = fuzz.ratio(artist.lower(), db_artist.lower())
if score > best_score:
best_score = score
best_match = db_artist
if best_score >= 80:
print(f" ✅ Found: {best_match} (score: {best_score})")
else:
print(f" ❌ No good match found (best: {best_match} with score {best_score})")
if __name__ == "__main__":
print("🧪 Testing MusicBrainz Database Connection and Fuzzy Search")
print("=" * 60)
success = test_db_connection()
if not success:
print("\n❌ All connection attempts failed")
print("💡 The database might not be accessible from the host")
print("💡 Try running the script inside the Docker container")
sys.exit(1)
else:
print("\n✅ Database connection and fuzzy search test completed!")