Repository navigation
Expand file tree
/
Copy pathmake_test_db.py
More file actions
72 lines (57 loc) · 1.82 KB
/
Copy pathmake_test_db.py
File metadata and controls
72 lines (57 loc) · 1.82 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
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
make_test_db.py — creates a small sample SQLite database with two tables:
- users (20 users)
- orders (100 orders, deliberately skewed: user_id=1 owns ~80% of them)
This is purely TEST data, so we can verify Chitral preserves the same
skewed distribution when it generates synthetic (fake) data.
Run with:
python3 make_test_db.py
"""
import sqlite3
import random
import os
DB_PATH = "test_data.db"
# Remove old test db if it exists, so we start fresh
if os.path.exists(DB_PATH):
os.remove(DB_PATH)
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
# ---- Create tables ----
cur.execute("""
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT
)
""")
cur.execute("""
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
user_id INTEGER,
amount REAL,
FOREIGN KEY (user_id) REFERENCES users(id)
)
""")
# ---- Insert 20 users ----
users = [(i, f"user_{i}", f"user_{i}@example.com") for i in range(1, 21)]
cur.executemany("INSERT INTO users (id, name, email) VALUES (?, ?, ?)", users)
# ---- Insert 100 orders, SKEWED: user_id=1 gets ~80 of them ----
order_id = 1
orders = []
# 80 orders for user_id = 1 (the "heavy" user)
for _ in range(80):
amount = round(random.uniform(10, 500), 2)
orders.append((order_id, 1, amount))
order_id += 1
# remaining 20 orders spread across the other 19 users
for _ in range(20):
user_id = random.randint(2, 20)
amount = round(random.uniform(10, 500), 2)
orders.append((order_id, user_id, amount))
order_id += 1
cur.executemany("INSERT INTO orders (id, user_id, amount) VALUES (?, ?, ?)", orders)
conn.commit()
conn.close()
print(f"Created {DB_PATH} with 20 users and 100 orders.")
print("user_id=1 owns 80 of the 100 orders (80% skew) — this is what we'll check Chitral reproduces.")
print(f"Full path: {os.path.abspath(DB_PATH)}")