-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_index.py
More file actions
59 lines (47 loc) · 1.89 KB
/
test_index.py
File metadata and controls
59 lines (47 loc) · 1.89 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
from minidb import MiniDB
import os
import shutil
def test_disk_index():
data_dir = "test_index_data"
if os.path.exists(data_dir):
shutil.rmtree(data_dir)
db = MiniDB(data_dir=data_dir)
db.execute_query("CREATE TABLE users (id int, name str, email str)")
# Insert some data
print("Inserting rows...")
db.execute_query("INSERT INTO users VALUES (1, 'Alice', 'alice@example.com')")
db.execute_query("INSERT INTO users VALUES (3, 'Charlie', 'charlie@example.com')")
db.execute_query("INSERT INTO users VALUES (2, 'Bob', 'bob@example.com')")
print("\nVerifying Index lookups (O(log N) path)...")
# Check Bob (ID 2)
res = db.execute_query("SELECT * FROM users WHERE id = 2")
print(f"ID 2 Result: {res}")
assert len(res) == 1
assert res[0]['name'] == 'Bob'
# Check Alice (ID 1)
res = db.execute_query("SELECT * FROM users WHERE id = 1")
print(f"ID 1 Result: {res}")
assert len(res) == 1
assert res[0]['name'] == 'Alice'
# Check Non-existent
res = db.execute_query("SELECT * FROM users WHERE id = 99")
print(f"ID 99 Result: {res}")
assert len(res) == 0
# Verify index file exists
idx_path = os.path.join(data_dir, "users.idx")
assert os.path.exists(idx_path)
idx_size = os.path.getsize(idx_path)
print(f"Index file size: {idx_size} bytes (Expected 24 for 3 entries)")
assert idx_size == 24
# Re-initialize to test rebuild and loading
print("\nRe-initializing database (testing index persistence)...")
db2 = MiniDB(data_dir=data_dir)
res = db2.execute_query("SELECT * FROM users WHERE id = 3")
print(f"ID 3 Result from new instance: {res}")
assert len(res) == 1
assert res[0]['name'] == 'Charlie'
print("\nAll Disk-Based Index tests passed!")
# Cleanup
shutil.rmtree(data_dir)
if __name__ == "__main__":
test_disk_index()