-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_join.py
More file actions
50 lines (39 loc) · 1.6 KB
/
test_join.py
File metadata and controls
50 lines (39 loc) · 1.6 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
from minidb import MiniDB
import os
import shutil
def test_joins_and_uniqueness():
print("--- Testing MiniDB Joins & Uniqueness ---")
test_dir = "test_join_data"
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
db = MiniDB(data_dir=test_dir)
# Setup tables
db.execute_query("CREATE TABLE users (id, name)")
db.execute_query("CREATE TABLE orders (order_id, user_id, amount)")
# 1. Test Uniqueness (DuplicateKeyError)
print("Inserting user 1...")
db.execute_query("INSERT INTO users VALUES (1, 'Alice')")
print("Inserting user 1 again (should fail)...")
res = db.execute_query("INSERT INTO users VALUES (1, 'Bob')")
print(f"Result: {res}")
# 2. Test Join
db.execute_query("INSERT INTO users VALUES (2, 'Bob')")
db.execute_query("INSERT INTO orders VALUES (101, 1, 50.0)")
db.execute_query("INSERT INTO orders VALUES (102, 1, 75.0)")
db.execute_query("INSERT INTO orders VALUES (103, 2, 20.0)")
print("\nExecuting Join: SELECT * FROM users JOIN orders ON users.id = orders.user_id")
join_res = db.execute_query("SELECT * FROM users JOIN orders ON users.id = orders.user_id")
print(f"Found {len(join_res)} joined rows:")
for row in join_res:
print(f" {row}")
# Verify counts
if len(join_res) == 3:
print("[v] Join results look correct.")
else:
print(f"[x] Expected 3 rows, got {len(join_res)}")
# Cleanup
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
print("--- Testing Complete ---")
if __name__ == "__main__":
test_joins_and_uniqueness()