-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_engine.py
More file actions
57 lines (46 loc) · 1.79 KB
/
test_engine.py
File metadata and controls
57 lines (46 loc) · 1.79 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
from minidb import MiniDB
import os
import shutil
import time
def test_engine():
print("--- Testing MiniDB Engine & Indexing ---")
test_dir = "test_engine_data"
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
db = MiniDB(data_dir=test_dir)
# 1. Create Table
print(db.execute_query("CREATE TABLE users (id, name, email)"))
# 2. Insert Data
db.execute_query("INSERT INTO users VALUES (1, 'Alice', 'alice@test.com')")
db.execute_query("INSERT INTO users VALUES (2, 'Bob', 'bob@test.com')")
db.execute_query("INSERT INTO users VALUES (3, 'Charlie', 'charlie@test.com')")
# 3. Select All
all_users = db.execute_query("SELECT * FROM users")
print(f"All users: {len(all_users)}")
# 4. Select by Primary Key (id=1) - O(1) Internal test
print("Testing O(1) Index Lookup for id=2...")
start_time = time.perf_counter()
alice = db.execute_query("SELECT * FROM users WHERE id=2")
end_time = time.perf_counter()
print(f"Result: {alice}")
print(f"Lookup took: {end_time - start_time:.6f}s")
# 5. Persistence Check
print("\nRestarting DB...")
db2 = MiniDB(data_dir=test_dir)
bob = db2.execute_query("SELECT * FROM users WHERE id=2")
if bob and bob[0]['name'] == 'Bob':
print("[v] Persistence verified after restart.")
else:
print(f"[x] Persistence failed: {bob}")
# 6. Duplicate PK Check
res = db2.execute_query("INSERT INTO users VALUES (1, 'Clone', 'clone@test.com')")
if "Error" in res:
print(f"[v] Duplicate PK caught: {res}")
else:
print("[x] Failed to catch duplicate PK.")
# Cleanup
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
print("--- Testing Complete ---")
if __name__ == "__main__":
test_engine()