| import sqlite3 |
|
|
| def grade_easy_task(db_path: str) -> float: |
| """Agent must trim whitespace from names and standardize dates to YYYY-MM-DD.""" |
| actual_score = 0.0 |
| try: |
| with sqlite3.connect(db_path) as conn: |
| c = conn.cursor() |
| |
| |
| c.execute("SELECT COUNT(*) FROM customers") |
| total_rows = c.fetchone()[0] |
| |
| if total_rows > 0: |
| |
| c.execute("SELECT COUNT(*) FROM customers WHERE name = TRIM(name)") |
| trimmed_names = c.fetchone()[0] |
| |
| |
| |
| c.execute(""" |
| SELECT COUNT(*) FROM customers |
| WHERE length(signup_date) = 10 |
| AND substr(signup_date, 5, 1) = '-' |
| AND substr(signup_date, 8, 1) = '-' |
| """) |
| formatted_dates = c.fetchone()[0] |
| |
| |
| name_score = trimmed_names / total_rows |
| date_score = formatted_dates / total_rows |
| actual_score = (name_score + date_score) / 2.0 |
| except sqlite3.Error: |
| actual_score = 0.0 |
|
|
| |
| if actual_score >= 1.0: return 0.99 |
| if actual_score <= 0.0: return 0.01 |
| return actual_score |
|
|
|
|
| def grade_medium_task(db_path: str) -> float: |
| """Agent must create 'customers' and 'orders' tables with proper references.""" |
| score = 0.0 |
| try: |
| with sqlite3.connect(db_path) as conn: |
| c = conn.cursor() |
| |
| |
| c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name IN ('customers', 'orders')") |
| tables = [row[0] for row in c.fetchall()] |
| if 'customers' in tables and 'orders' in tables: |
| score += 0.4 |
| |
| |
| c.execute("SELECT COUNT(*) FROM (SELECT name FROM customers GROUP BY name HAVING COUNT(*) > 1)") |
| duplicate_groups = c.fetchone()[0] |
| if duplicate_groups == 0: |
| score += 0.3 |
| |
| |
| |
| |
| try: |
| c.execute(""" |
| SELECT COUNT(*) FROM orders |
| WHERE customer_id NOT IN (SELECT id FROM customers) |
| """) |
| orphaned_orders = c.fetchone()[0] |
| if orphaned_orders == 0: |
| score += 0.3 |
| except sqlite3.Error: |
| |
| pass |
| except sqlite3.Error: |
| pass |
| |
| |
| if score >= 1.0: return 0.99 |
| if score <= 0.0: return 0.01 |
| return score |
|
|
|
|
| def grade_hard_task(db_path: str) -> float: |
| """Agent must create a view 'account_balances' calculating net balance (credit - debit).""" |
| actual_score = 0.0 |
| try: |
| with sqlite3.connect(db_path) as conn: |
| c = conn.cursor() |
| |
| |
| |
| golden_query = """ |
| SELECT |
| account_id, |
| SUM(CASE WHEN type = 'credit' THEN amount ELSE -amount END) as true_balance |
| FROM transactions |
| GROUP BY account_id |
| ORDER BY account_id |
| """ |
| c.execute(golden_query) |
| golden_rows = c.fetchall() |
| |
| |
| c.execute("SELECT account_id, net_balance FROM account_balances ORDER BY account_id") |
| agent_rows = c.fetchall() |
| |
| |
| if len(golden_rows) > 0 and agent_rows == golden_rows: |
| actual_score = 1.0 |
| elif len(agent_rows) > 0: |
| |
| matches = len(set(agent_rows) & set(golden_rows)) |
| actual_score = matches / len(golden_rows) |
|
|
| except sqlite3.Error: |
| actual_score = 0.0 |
|
|
| |
| if actual_score >= 1.0: return 0.99 |
| if actual_score <= 0.0: return 0.01 |
| return actual_score |