101 lines
2.9 KiB
Python
101 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script for the MariaDB Database Manager
|
|
This script tests the basic functionality without the GUI
|
|
"""
|
|
|
|
from database_manager import DatabaseManager
|
|
|
|
def test_database_operations():
|
|
print("Testing MariaDB Database Manager...")
|
|
print("-" * 40)
|
|
|
|
# Initialize database
|
|
db = DatabaseManager()
|
|
|
|
# Test 1: Add some initial data
|
|
print("1. Adding initial test data...")
|
|
test_data = [
|
|
("SYS001", 125.5),
|
|
("SYS002", 89.7),
|
|
("SYS003", 234.1)
|
|
]
|
|
|
|
for record_id, mass in test_data:
|
|
success = db.add_or_update_record(record_id, mass)
|
|
print(f" Added {record_id}: {'✓' if success else '✗'}")
|
|
|
|
print()
|
|
|
|
# Test 2: Read all data
|
|
print("2. Reading all data...")
|
|
records = db.read_all_data()
|
|
for record in records:
|
|
print(f" ID: {record[0]}, Mass: {record[1]}")
|
|
print(f" Total records: {len(records)}")
|
|
print()
|
|
|
|
# Test 3: Search for existing record
|
|
print("3. Searching for existing record 'SYS001'...")
|
|
result = db.search_by_id("SYS001")
|
|
if result:
|
|
print(f" Found: {result[0]} = {result[1]}")
|
|
else:
|
|
print(" Not found")
|
|
print()
|
|
|
|
# Test 4: Search for non-existing record
|
|
print("4. Searching for non-existing record 'SYS999'...")
|
|
result = db.search_by_id("SYS999")
|
|
if result:
|
|
print(f" Found: {result[0]} = {result[1]}")
|
|
else:
|
|
print(" Not found - this is expected!")
|
|
print()
|
|
|
|
# Test 5: Add the missing record
|
|
print("5. Adding the missing record 'SYS999'...")
|
|
success = db.add_or_update_record("SYS999", 456.8)
|
|
print(f" Added SYS999: {'✓' if success else '✗'}")
|
|
|
|
# Verify it was added
|
|
result = db.search_by_id("SYS999")
|
|
if result:
|
|
print(f" Verification: Found {result[0]} = {result[1]}")
|
|
print()
|
|
|
|
# Test 6: Update existing record
|
|
print("6. Updating existing record 'SYS001'...")
|
|
success = db.add_or_update_record("SYS001", 150.0)
|
|
print(f" Updated SYS001: {'✓' if success else '✗'}")
|
|
|
|
result = db.search_by_id("SYS001")
|
|
if result:
|
|
print(f" New value: {result[0]} = {result[1]}")
|
|
print()
|
|
|
|
# Test 7: Final count
|
|
print("7. Final database state...")
|
|
records = db.read_all_data()
|
|
print(f" Total records: {len(records)}")
|
|
for record in records:
|
|
print(f" {record[0]} = {record[1]}")
|
|
print()
|
|
|
|
# Test 8: Delete a record
|
|
print("8. Testing delete functionality...")
|
|
success = db.delete_record("SYS999")
|
|
print(f" Deleted SYS999: {'✓' if success else '✗'}")
|
|
|
|
final_records = db.read_all_data()
|
|
print(f" Final record count: {len(final_records)}")
|
|
print()
|
|
|
|
# Close connection
|
|
db.close_connection()
|
|
|
|
print("✓ All tests completed successfully!")
|
|
print("Note: Test records remain in the MariaDB database.")
|
|
|
|
if __name__ == "__main__":
|
|
test_database_operations() |