from data.database import get_connection from data.seed_database import seed_books from tools import create_order, get_order_status, search_books def print_test_title(title: str) -> None: print("\n" + "=" * 80) print(title) print("=" * 80) def main() -> None: seed_books() created_order_id: int | None = None selected_book_id: int | None = None original_stock: int | None = None try: print_test_title("1. KİTAP ARAMA TESTİ") search_result = search_books( query="Dune", in_stock_only=True, ) print(search_result) assert search_result["success"] is True assert search_result["count"] >= 1 selected_book = search_result["books"][0] selected_book_id = selected_book["book_id"] original_stock = selected_book["stock"] print("\n✅ Kitap arama testi başarılı.") print_test_title("2. OLMAYAN KİTAP TESTİ") missing_book_result = search_books( query="Veritabanında Olmayan Hayali Kitap" ) print(missing_book_result) assert missing_book_result["success"] is True assert missing_book_result["count"] == 0 assert missing_book_result["books"] == [] print("\n✅ Olmayan kitap testi başarılı.") print_test_title("3. SİPARİŞ OLUŞTURMA TESTİ") order_result = create_order( book_id=selected_book_id, quantity=1, customer_name="Test Kullanıcısı", ) print(order_result) assert order_result["success"] is True created_order_id = order_result["order"]["order_id"] assert ( order_result["stock_update"]["new_stock"] == original_stock - 1 ) print("\n✅ Sipariş oluşturma testi başarılı.") print_test_title("4. SİPARİŞ DURUMU TESTİ") status_result = get_order_status(created_order_id) print(status_result) assert status_result["success"] is True assert status_result["order"]["order_id"] == created_order_id assert status_result["order"]["status"] == "Hazırlanıyor" print("\n✅ Sipariş durumu testi başarılı.") print_test_title("5. YETERSİZ STOK TESTİ") insufficient_stock_result = create_order( book_id=selected_book_id, quantity=10000, customer_name="Test Kullanıcısı", ) print(insufficient_stock_result) assert insufficient_stock_result["success"] is False assert insufficient_stock_result["error"] == ( "Yeterli stok bulunmuyor." ) print("\n✅ Yetersiz stok testi başarılı.") print_test_title("6. GEÇERSİZ SİPARİŞ NUMARASI TESTİ") missing_order_result = get_order_status(999999) print(missing_order_result) assert missing_order_result["success"] is False assert missing_order_result["error"] == "Sipariş bulunamadı." print("\n✅ Geçersiz sipariş numarası testi başarılı.") finally: # Test sırasında oluşturulan siparişi siler ve stoğu geri yükler. # Böylece test her çalıştırıldığında veritabanı değişmeden kalır. if ( created_order_id is not None and selected_book_id is not None and original_stock is not None ): with get_connection() as connection: connection.execute( """ DELETE FROM orders WHERE id = ? """, (created_order_id,), ) connection.execute( """ UPDATE books SET stock = ? WHERE id = ? """, ( original_stock, selected_book_id, ), ) connection.commit() print("\nTest siparişi silindi ve kitap stoğu geri yüklendi.") print("\n" + "#" * 80) print("🎉 BÜTÜN TOOL TESTLERİ BAŞARIYLA TAMAMLANDI") print("#" * 80) if __name__ == "__main__": main()