File size: 4,307 Bytes
58f6928
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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()