-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_crud.py
More file actions
59 lines (48 loc) · 2.1 KB
/
test_crud.py
File metadata and controls
59 lines (48 loc) · 2.1 KB
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
import os
import shutil
from minidb import MiniDB
def test_crud_operations():
test_dir = "test_crud_data"
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
db = MiniDB(data_dir=test_dir)
db.execute_query("CREATE TABLE students (id, name, grade)")
# 1. Test INSERT
db.execute_query("INSERT INTO students VALUES (1, 'Alice', 'A')")
db.execute_query("INSERT INTO students VALUES (2, 'Bob', 'B')")
db.execute_query("INSERT INTO students VALUES (3, 'Charlie', 'C')")
# 2. Test UPDATE
print("Executing UPDATE: UPDATE students SET grade = 'A+' WHERE id = 2")
res = db.execute_query("UPDATE students SET grade = 'A+' WHERE id = 2")
print(f"Result: {res}")
# Verify Update
records = db.execute_query("SELECT * FROM students WHERE id = 2")
if records and records[0]['grade'] == 'A+':
print("[v] Update successful: Bob's grade is now A+.")
else:
print(f"[x] Update failed: {records}")
# 3. Test DELETE
print("\nExecuting DELETE: DELETE FROM students WHERE id = 3")
res = db.execute_query("DELETE FROM students WHERE id = 3")
print(f"Result: {res}")
# Verify Delete
records = db.execute_query("SELECT * FROM students WHERE id = 3")
if not records:
print("[v] Delete successful: Charlie is gone.")
else:
print(f"[x] Delete failed: {records}")
# 4. Test Multi-Row Update
db.execute_query("INSERT INTO students VALUES (4, 'Dave', 'B')")
print("\nExecuting Multi-UPDATE: UPDATE students SET grade = 'Pass' WHERE grade = 'B'")
db.execute_query("UPDATE students SET grade = 'Pass' WHERE grade = 'B'")
records = db.execute_query("SELECT * FROM students WHERE grade = 'Pass'")
if len(records) == 1: # Dave (id=4) should be 'Pass', Bob was already updated to 'A+'
print("[v] Multi-Update successful.")
else:
print(f"[x] Multi-Update result count mismatch: {len(records)}")
# Cleanup
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
print("\n--- CRUD Verification Complete ---")
if __name__ == "__main__":
test_crud_operations()