#!/usr/bin/env python3 """ Test script for the Tablet Management System """ import sqlite3 from datetime import datetime def test_tablet_management(): """Test the tablet management system functionality""" # Initialize database conn = sqlite3.connect('tablets.db') cursor = conn.cursor() # Create tables cursor.execute(''' CREATE TABLE IF NOT EXISTS tablets ( id INTEGER PRIMARY KEY AUTOINCREMENT, brand TEXT NOT NULL, model TEXT NOT NULL, serial_number TEXT UNIQUE NOT NULL, status TEXT DEFAULT 'available' ) ''') cursor.execute(''' CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, identification TEXT UNIQUE ) ''') cursor.execute(''' CREATE TABLE IF NOT EXISTS loans ( id INTEGER PRIMARY KEY AUTOINCREMENT, tablet_id INTEGER NOT NULL, user_id INTEGER NOT NULL, loan_date TEXT NOT NULL, return_date TEXT, status TEXT DEFAULT 'active', FOREIGN KEY (tablet_id) REFERENCES tablets (id), FOREIGN KEY (user_id) REFERENCES users (id) ) ''') conn.commit() print("=== Testing Tablet Management System ===") # Test 1: Add tablets print("\n1. Adding tablets...") tablets = [ ("Samsung", "Galaxy Tab S7", "SN001"), ("Apple", "iPad Pro", "SN002"), ("Lenovo", "Tab P11", "SN003") ] for brand, model, serial in tablets: try: cursor.execute(''' INSERT INTO tablets (brand, model, serial_number, status) VALUES (?, ?, ?, 'available') ''', (brand, model, serial)) print(f" ✓ Added: {brand} {model} ({serial})") except sqlite3.IntegrityError: print(f" ✗ Duplicate: {serial}") conn.commit() # Test 2: Add users print("\n2. Adding users...") users = [ ("John Doe", "ID001"), ("Jane Smith", "ID002"), ("Bob Johnson", "ID003") ] for name, identification in users: try: cursor.execute(''' INSERT INTO users (name, identification) VALUES (?, ?) ''', (name, identification)) print(f" ✓ Added: {name} ({identification})") except sqlite3.IntegrityError: print(f" ✗ Duplicate: {identification}") conn.commit() # Test 3: Show available tablets print("\n3. Available tablets:") cursor.execute("SELECT id, brand, model, serial_number FROM tablets WHERE status = 'available'") available_tablets = cursor.fetchall() for tablet in available_tablets: print(f" ID {tablet[0]}: {tablet[1]} {tablet[2]} ({tablet[3]})") # Test 4: Loan tablets print("\n4. Loaning tablets...") # Loan tablet 1 to user 1 tablet_id = 1 user_id = 1 cursor.execute("UPDATE tablets SET status = 'loaned' WHERE id = ?", (tablet_id,)) loan_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') cursor.execute(''' INSERT INTO loans (tablet_id, user_id, loan_date, status) VALUES (?, ?, ?, 'active') ''', (tablet_id, user_id, loan_date)) print(f" ✓ Loaned tablet {tablet_id} to user {user_id}") # Loan tablet 2 to user 2 tablet_id = 2 user_id = 2 cursor.execute("UPDATE tablets SET status = 'loaned' WHERE id = ?", (tablet_id,)) loan_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') cursor.execute(''' INSERT INTO loans (tablet_id, user_id, loan_date, status) VALUES (?, ?, ?, 'active') ''', (tablet_id, user_id, loan_date)) print(f" ✓ Loaned tablet {tablet_id} to user {user_id}") conn.commit() # Test 5: Show active loans print("\n5. Active loans:") cursor.execute(''' SELECT l.id, t.brand, t.model, t.serial_number, u.name, l.loan_date FROM loans l JOIN tablets t ON l.tablet_id = t.id JOIN users u ON l.user_id = u.id WHERE l.status = 'active' ''') active_loans = cursor.fetchall() for loan in active_loans: print(f" Loan {loan[0]}: {loan[1]} {loan[2]} ({loan[3]}) to {loan[4]} on {loan[5]}") # Test 6: Return a tablet print("\n6. Returning tablet...") loan_id = 1 cursor.execute("SELECT tablet_id FROM loans WHERE id = ?", (loan_id,)) loan = cursor.fetchone() if loan: tablet_id = loan[0] return_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') cursor.execute(''' UPDATE loans SET status = 'returned', return_date = ? WHERE id = ? ''', (return_date, loan_id)) cursor.execute("UPDATE tablets SET status = 'available' WHERE id = ?", (tablet_id,)) print(f" ✓ Returned tablet for loan {loan_id}") conn.commit() # Test 7: Show loan history print("\n7. Complete loan history:") cursor.execute(''' SELECT l.id, t.brand, t.model, t.serial_number, u.name, l.loan_date, l.return_date, l.status FROM loans l JOIN tablets t ON l.tablet_id = t.id JOIN users u ON l.user_id = u.id ORDER BY l.loan_date DESC ''') loans = cursor.fetchall() for loan in loans: return_date = loan[6] or 'Not returned' print(f" Loan {loan[0]}: {loan[1]} {loan[2]} ({loan[3]}) to {loan[4]}") print(f" Loan: {loan[5]}, Return: {return_date}, Status: {loan[7]}") # Test 8: Show final status print("\n8. Final status:") # Available tablets cursor.execute("SELECT COUNT(*) FROM tablets WHERE status = 'available'") available_count = cursor.fetchone()[0] print(f" Available tablets: {available_count}") # Loaned tablets cursor.execute("SELECT COUNT(*) FROM tablets WHERE status = 'loaned'") loaned_count = cursor.fetchone()[0] print(f" Loaned tablets: {loaned_count}") # Active loans cursor.execute("SELECT COUNT(*) FROM loans WHERE status = 'active'") active_loans_count = cursor.fetchone()[0] print(f" Active loans: {active_loans_count}") # Returned loans cursor.execute("SELECT COUNT(*) FROM loans WHERE status = 'returned'") returned_loans_count = cursor.fetchone()[0] print(f" Returned loans: {returned_loans_count}") conn.close() print("\n=== Test completed successfully! ===") print("\nYou can now run the interactive application:") print(" python3 minimal_app.py") print("\nOr use the database directly with SQLite:") print(" sqlite3 tablets.db") if __name__ == '__main__': test_tablet_management()