-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_methods.py
919 lines (780 loc) · 24.9 KB
/
access_methods.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
import psycopg2
import random
from db_setup import DATABASE_URL
# DATABASE_URL = "postgresql://jae_test_user:HtuRJ21AGVyAfz4e2rERt9n8ErU7Wupf@dpg-ct85p923esus73a5lba0-a.frankfurt-postgres.render.com/jae_test"
def createCourt(
courtName,
avStar,
nets,
level,
clean,
ada,
inOut,
hours,
price,
location,
description,
):
"""
Inserts a new court into the database and returns its ID.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
INSERT INTO courts (courtName, avStar, nets, level, clean, ada, inOut, hours, price, location, description)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING courtID;
""",
(
courtName,
avStar,
int(nets),
int(level),
int(clean),
int(ada),
int(inOut),
hours,
price,
location,
description,
),
)
court_id = cursor.fetchone()[0]
connection.commit()
connection.close()
return court_id
"""
def createCourt(courtName, nets, level, clean, ada, inOut, hours, price, location):
connection = psycopg2.connect(DATABASE_URL)
try:
with connection.cursor() as cursor:
cursor.execute('''
INSERT INTO courts (courtName, avStar, nets, level, clean, ada, inOut, hours, price, location, description)
VALUES (%s, NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING courtID;
''', (courtName, nets, level, clean, ada, inOut, hours, price, location, description))
court_id = cursor.fetchone()[0]
connection.commit()
return court_id
except Exception as e:
connection.rollback()
raise e
finally:
connection.close()
"""
def getCourts():
"""
Retrieve all courts. Returns a tuple with court ids.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
SELECT * FROM courts;
"""
)
courts = cursor.fetchall() # Fetch one row directly
connection.commit()
connection.close()
return courts
def getCourt(courtID_get):
"""
Retrieve all details of a court by its ID. Returns a tuple with all court elements.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
SELECT * FROM courts WHERE courtID = %s;
""",
(courtID_get,),
)
court = cursor.fetchone() # Fetch one row directly
connection.commit()
connection.close()
return court
def deleteCourt(courtID_del):
"""
Deletes the court with the specified ID from the database.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
DELETE FROM courts WHERE courtID = %s;
""",
(courtID_del,),
)
connection.commit()
connection.close()
def calcAvStar(courtID_calc):
"""
Get the star rating from reviews for the court given. Calculate the average and return the average.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
SELECT star FROM reviews WHERE courtID = %s;
""",
(courtID_calc,),
)
stars_tup = cursor.fetchall()
stars = []
for row in stars_tup:
stars.append(row[0])
avStar = sum(stars) / len(stars)
# May want to also edit courts entry to include this new value
connection.commit()
connection.close()
return avStar
def editCourt(
courtID_edit,
courtName,
avStar,
nets,
level,
clean,
ada,
inOut,
hours,
price,
location,
description,
):
"""
Edit the court with the ID given so that all the parameters are now used.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
UPDATE courts SET courtName=%s, avStar=%s, nets=%s, level=%s, clean=%s, ada=%s, inOut=%s, hours=%s, price=%s, location=%s, description=%s WHERE courtID=%s;
""",
(
courtName,
avStar,
nets,
level,
clean,
ada,
inOut,
hours,
price,
location,
description,
courtID_edit,
),
)
connection.commit()
connection.close()
return
def findCourts(search_term):
"""
Search the courts table in both the name and location atributes and return a list of IDs that pulled up matches.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
f"""
SELECT courtID FROM courts WHERE courtName LIKE '%{search_term}%';
"""
)
names_tup = cursor.fetchall()
names = []
for row in names_tup:
names.append(row[0])
cursor.execute(
f"""
SELECT courtID FROM courts WHERE location LIKE '%{search_term}%';
"""
)
loc_tup = cursor.fetchall()
locs = []
for row in loc_tup:
locs.append(row[0])
bothlists = list(set(names) | set(locs))
connection.commit()
connection.close()
return bothlists
def createReview(userID, courtID, star, comment):
"""
Create a review with a comment and a star review. Creates a random unique id. Returns that id.
"""
"""
cur.execute('''
SELECT reviewID FROM reviews;
''')
ids_tup = cur.fetchall()
ids = []
for row in ids_tup:
ids.append(row[0])
maybe_id = random.randint(10000, 99999)
while maybe_id in ids:
maybe_id = random.randint(10000, 99999)
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
INSERT INTO reviews (userID, courtID, star, comment) VALUES (%s, %s, %s, %s) RETURNING reviewID;
""",
(userID, courtID, star, comment),
)
id_of_new_row = cursor.fetchone()[0]
connection.commit()
connection.close()
return id_of_new_row
def getReviews(courtID):
"""
Get the reviews for a given court. Returns a dictionary with the key being the review IDs,
and the values a list of the other elements including the username, rating, and comment.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
SELECT r.reviewid, u.name, r.star, r.comment
FROM reviews r
JOIN users u ON r.userid = u.userid
WHERE r.courtid = %s;
""",
(courtID,),
)
rev_tup = cursor.fetchall()
reviews = {}
for row in rev_tup:
reviews[row[0]] = [row[1], row[2], row[3]] # Ensure the comment is captured as row[3]
connection.commit()
connection.close()
return reviews
def getUserReviews(userID):
"""
Get the reviews made by a given user. Return a dictionary with the key being the review ids, and the values a list of the other elements.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
SELECT * FROM reviews WHERE userID = %s;
""",
(userID,),
)
rev_tup = cursor.fetchall()
reviews = {}
for row in rev_tup:
reviews[row[0]] = [row[1], row[2], row[3], row[4]]
connection.commit()
connection.close()
return reviews
def deleteReview(reviewID_del):
"""
Delete a review using the reviewID)
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
DELETE FROM reviews WHERE reviewID = %s;
""",
(reviewID_del,),
)
connection.commit()
connection.close()
return
# CreateUser Method
def createUser(name, password):
"""
Inserts a new user into the database.
Returns the userID of the created user.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
try:
cursor.execute(
"""
INSERT INTO users (name, password)
VALUES (%s, %s)
RETURNING userID;
""",
(name, password),
)
user_id = cursor.fetchone()[0]
connection.commit()
connection.close()
return user_id
except psycopg2.IntegrityError as e:
raise ValueError(f"User with name '{name}' already exists.") from e
# Checks to see User
def checkUser(name, password):
"""
Checks if a user exists with the given name and password.
Returns the userID if the user exists, otherwise None.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
SELECT userID FROM users
WHERE name = %s AND password = %s;
""",
(name, password),
)
result = cursor.fetchone()
connection.commit()
connection.close()
return result[0] if result else None
def createUserFavorite(userID, courtID):
"""
Create a Favorite entry for the court belonging to the user. If the user has reviewed it, it records the star value. No return.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
# reviews = getUserReviews(userID)
cursor.execute(
"""
SELECT * FROM reviews WHERE userID = %s;
""",
(userID,),
)
rev_tup = cursor.fetchall()
reviews = {}
for row in rev_tup:
reviews[row[0]] = [row[1], row[2], row[3], row[4]]
review_for_court = None
for key in reviews:
if reviews[key][1] == courtID:
review_for_court = reviews[key][2]
if review_for_court != None:
cursor.execute(
"""
INSERT INTO favorites (userID, courtID, review) VALUES (%s, %s, %s)
""",
(userID, courtID, review_for_court),
)
else:
cursor.execute(
"""
INSERT INTO favorites (userID, courtID) VALUES (%s, %s)
""",
(userID, courtID),
)
connection.commit()
connection.close()
return
def getUserFavorites(userID):
"""
Get the users favorites based on id. Return as dictionary with key as courtID and star as the value.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
SELECT * FROM favorites WHERE userID = %s;
""",
(userID,),
)
fav_tup = cursor.fetchall()
favorites = {}
for row in fav_tup:
favorites[row[1]] = row[2]
connection.commit()
connection.close()
return favorites
def editUserFavorite(userID, courtID, star):
"""
Edit a users favorite entry to include a star value if they reviewed after favoriting.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
UPDATE favorites SET review=%s WHERE courtID=%s AND userID=%s;
""",
(star, courtID, userID),
)
connection.commit()
connection.close()
return
def deleteUserFavorite(userID, courtID):
"""
Delete a users favorite based on the userid and courtid.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
DELETE FROM favorites WHERE userID = %s AND courtID = %s;
""",
(userID, courtID),
)
connection.commit()
connection.close()
return
# Adding Photo
def addPhoto(courtID, photo_path):
"""
Adds a photo to the photos table for a specific court.
Returns the photoID of the newly added photo.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
INSERT INTO photos (courtID, photo)
VALUES (%s, %s)
RETURNING photoID;
""",
(courtID, photo_path),
)
photo_id = cursor.fetchone()[0]
connection.commit()
connection.close()
return photo_id
# Getting a Specific User
def getUser(cursor, userID):
"""
Retrieves a user by their userID.
Returns a tuple containing the user's details (userID, name, password).
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
SELECT * FROM users
WHERE userID = %s;
""",
(userID,),
)
user = cursor.fetchone()
connection.commit()
connection.close()
return user
# Getting photo
def getPhotos(courtID=None):
"""
Retrieves photos.
If courtID is provided, retrieves photos for that court.
Otherwise, retrieves all photos in the database.
Returns a list of tuples containing photoID and photo paths.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
if courtID:
cursor.execute(
"""
SELECT photoID, photo FROM photos
WHERE courtID = %s;
""",
(courtID,),
)
else:
cursor.execute(
"""
SELECT photoID, photo FROM photos;
"""
)
photos = cursor.fetchall()
connection.commit()
connection.close()
return photos
# Deleting User
def deleteUser(userID):
"""
Deletes a user by their userID.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
DELETE FROM users
WHERE userID = %s;
""",
(userID,),
)
print(f"User with ID {userID} deleted successfully.")
connection.commit()
connection.close()
# Deleting Photo
def deletePhoto(photoID):
"""
Deletes a photo by its photoID.
"""
connection = psycopg2.connect(DATABASE_URL)
cursor = connection.cursor()
cursor.execute(
"""
DELETE FROM photos
WHERE photoID = %s;
""",
(photoID,),
)
print(f"Photo with ID {photoID} deleted successfully.")
connection.commit()
connection.close()
if __name__ == "__main__":
"""
Main function to test functions.
"""
# Define the database URL
DATABASE_URL = "postgresql://jjjohnywaffles_k8io_user:vaeBbrGmOq2g6GVR7zttI2g2bsf7Gh8f@dpg-ct1nsddumphs738rb1f0-a.oregon-postgres.render.com/jjjohnywaffles_k8io"
try:
# Connect to the database
connection = psycopg2.connect(DATABASE_URL)
with connection.cursor() as cursor:
# TESTING CREATE COURT
# Define test data for creating a court
test_court = {
"courtName": "Test Court",
"nets": 1,
"level": 1,
"clean": 1,
"ada": 1,
"inOut": 0,
"hours": "9 AM - 9 PM",
"price": "Free",
"location": "123 Test Street",
"description": "test description",
}
# Call the createCourt function
print("\nTesting createCourt:")
court_id = createCourt(
test_court["courtName"],
None,
test_court["nets"],
test_court["level"],
test_court["clean"],
test_court["ada"],
test_court["inOut"],
test_court["hours"],
test_court["price"],
test_court["location"],
test_court["description"],
)
print(f"Court created with ID: {court_id}")
# TESTING GETCOURT
# Call the getCourt function
print("\nTesting getCourt:")
court_details = getCourt(court_id)
print(f"Retrieved court: {court_details}")
# Verify the retrieved court matches the input
expected_result = (
court_id,
test_court["courtName"],
None, # avStar defaults to NULL
test_court["nets"],
test_court["level"],
test_court["clean"],
test_court["ada"],
test_court["inOut"],
test_court["hours"],
test_court["price"],
test_court["location"],
test_court["description"],
)
assert (
court_details == expected_result
), f"Test failed: {court_details} != {expected_result}"
print("getCourt test passed successfully.")
# Testing Edit Court
# Call the editCourt function
test_court2 = {
"courtName": "New Court",
"star": 5.0,
"nets": 0,
"level": 0,
"clean": 0,
"ada": 0,
"inOut": 1,
"hours": "Always",
"price": "$5",
"location": "123 Somewhere Street",
"description": "other describe",
}
print("\nTesting editCourt:")
editCourt(
court_id,
test_court2["courtName"],
test_court2["star"],
test_court2["nets"],
test_court2["level"],
test_court2["clean"],
test_court2["ada"],
test_court2["inOut"],
test_court2["hours"],
test_court2["price"],
test_court2["location"],
test_court2["description"],
)
print(f"Court edited with ID: {court_id}")
court_details2 = getCourt(court_id)
expected_result2 = (
court_id,
test_court2["courtName"],
test_court2["star"], # added a star value
test_court2["nets"],
test_court2["level"],
test_court2["clean"],
test_court2["ada"],
test_court2["inOut"],
test_court2["hours"],
test_court2["price"],
test_court2["location"],
test_court2["description"],
)
assert (
court_details2 == expected_result2
), f"Test failed: {court_details2} != {expected_result2}"
print("editCourt test passed successfully.")
# Testing Search
print("\nTesting findCourts:")
search_results = findCourts("Somewhere")
assert (
court_id in search_results
), f"Test failed: {court_id} not in {search_results}"
print("findCourts passed successfully")
# TESTING REVIEWS
# Create 3 reviews
test_review_1 = {
"userID": 14,
"courtID": court_id,
"star": 5.0,
"review": "test review",
}
test_review_2 = {
"userID": 15,
"courtID": court_id,
"star": 4.0,
"review": "another test",
}
print("\nTesting createReview:")
review1id = createReview(
test_review_1["userID"],
test_review_1["courtID"],
test_review_1["star"],
test_review_1["review"],
)
print(f"Review created with ID: {review1id}")
review2id = createReview(
test_review_2["userID"],
test_review_2["courtID"],
test_review_2["star"],
test_review_2["review"],
)
print(f"Review created with ID: {review2id}")
print("createReview passed successfully")
# Testing Average Star
print("\nTesting calcAvStar:")
test_average = calcAvStar(court_id)
assert test_average == 4.5, f"Test failed: {test_average} != 4.5"
print("calcAvStar passed successfully")
# Testing Get Review
print("\nTesting getReviews:")
court_reviews = getReviews(court_id)
assert court_reviews[review1id] == [
test_review_1["testUser14"],
test_review_1["star"],
test_review_1["review"],
], f"Test failed: Review {review1id} wrong"
print("getReviews passed successfully")
# Testing Get Users Review
print("\nTesting getUserReviews:")
court_reviews = getUserReviews(14)
assert court_reviews[review1id] == [
test_review_1["userID"],
test_review_1["courtID"],
test_review_1["star"],
test_review_1["review"],
], f"Test failed: Review {review1id} wrong"
print("getUserReviews passed successfully")
# Test Create and Get Favorites
print("\nTesting createUserFavorite:")
createUserFavorite(14, court_id)
print("User Favorite Created")
userFave = getUserFavorites(14)
assert (
userFave[court_id] == 5.0
), f"Test failed: User Favorite for User9 and {court_id} is {userFave[court_id]} instead of 5.0"
print("create and get UserFavorite passed successfully")
# Delete Reviews
print("\nTesting deleteReview:")
deleteReview(review1id)
print(f"Review with ID {review1id} deleted successfully.")
deleteReview(review2id)
print(f"Review with ID {review2id} deleted successfully.")
# Verify the reviews no longer exists
cursor.execute("SELECT * FROM reviews WHERE reviewID = %s;", (review1id,))
deleted_review1 = cursor.fetchone()
assert (
deleted_review1 is None
), f"Test failed: Review with ID {review1id} still exists."
cursor.execute("SELECT * FROM reviews WHERE reviewID = %s;", (review2id,))
deleted_review2 = cursor.fetchone()
assert (
deleted_review2 is None
), f"Test failed: Review with ID {review2id} still exists."
print("deleteReview test passed successfully.")
# Test no star favorite get
print("\nTesting getUserFavorite with no star:")
createUserFavorite(16, court_id)
print("User Favorite Created No Star")
userFave2 = getUserFavorites(16)
assert (
userFave2[court_id] == None
), f"Test failed: User Favorite for User12 and {court_id} is {userFave2[court_id]} instead of None"
createUserFavorite(15, court_id)
print("User Favorite Created No Star")
userFave3 = getUserFavorites(15)
assert (
userFave3[court_id] == None
), f"Test failed: User Favorite for User11 and {court_id} is {userFave3[court_id]} instead of None"
print("getUserFavorites passes with null star")
# Test Delete Favorite
print("\nTesting deleteUserFavorite:")
deleteUserFavorite(14, court_id)
print("Favorite Deleted Successfully")
cursor.execute(
"SELECT * FROM favorites WHERE userID = 14 AND courtID = %s;",
(court_id,),
)
deleted_fave = cursor.fetchone()
assert deleted_fave is None, "Test failed: Favorite still exists."
deleteUserFavorite(16, court_id)
print("Favorite Deleted Successfully")
cursor.execute(
"SELECT * FROM favorites WHERE userID = 16 AND courtID = %s;",
(court_id,),
)
deleted_fave2 = cursor.fetchone()
assert deleted_fave2 is None, "Test failed: Favorite still exists."
deleteUserFavorite(15, court_id)
print("Favorite Deleted Successfully")
cursor.execute(
"SELECT * FROM favorites WHERE userID = 15 AND courtID = %s;",
(court_id,),
)
deleted_fave3 = cursor.fetchone()
assert deleted_fave3 is None, "Test failed: Favorite still exists."
# TESTING DELETE Court
# Call the deleteCourt function
print("\nTesting deleteCourt:")
deleteCourt(court_id)
print(f"Court with ID {court_id} deleted successfully.")
# Verify the court no longer exists
cursor.execute("SELECT * FROM courts WHERE courtID = %s;", (court_id,))
deleted_court = cursor.fetchone()
assert (
deleted_court is None
), f"Test failed: Court with ID {court_id} still exists."
print("deleteCourt test passed successfully.")
connection.commit()
except Exception as e:
print(f"Error during test: {e}")
if "connection" in locals():
connection.rollback() # Rollback transaction on error
finally:
# Close the connection
if "connection" in locals() and connection:
connection.close()
print("Connection closed.")