"""Tests on Lab 04's recorded run of 03_indexes.py (October 2026, Apple M4 Max, Docker Desktop,
Postgres 18.6, shared_buffers 1 GB): four queries on a 10M-row orders table with one index at a
time, what each index cost inserts, and the three useful indexes kept together.
Run: pytest test_index_practice.py"""
from index_practice import (added_up_rate, covers, fastest, gives_order, insert_cost,
                            narrowing_columns, speedup, table_mb)

# A customer's latest 20 orders: median ms with each index (one at a time)
LATEST_20 = {
    "none": 106.05799999999999,
    "(customer_id)": 0.13,
    "(customer_id, created_at)": 0.036,
    "(customer_id, created_at) INCLUDE (total_cents)": 0.012,
    "(customer_id, status)": 0.27,
    "(status, created_at)": 118.338,  # Seq Scan: no help
    "(created_at, status)": 169.454,  # Index Scan, 80,546 pages: slower than no index
}
# Cancelled orders in one day
CANCELLED_DAY = {"none": 126.57249999999999, "(status, created_at)": 0.076, "(created_at, status)": 0.311}

INSERTS = {  # inserts a second, median of 3 batches of 20,000
    "none": 326305.30459522526,
    "(customer_id)": 213714.97927709925,
    "(customer_id, created_at)": 146407.70432079028,
    "(customer_id, created_at) INCLUDE (total_cents)": 119620.14599379565,
    "(customer_id, status)": 194220.48396253574,
    "(status, created_at)": 234498.4313590501,
    "(created_at, status)": 271785.13818316587,
}
THREE = ["(customer_id, created_at) INCLUDE (total_cents)", "(customer_id, status)", "(status, created_at)"]
TOGETHER_BASE, TOGETHER = 320021.76134743076, 22964.039357903453  # Exercise 2's own baseline, then all three


def test_speedup():
    base = LATEST_20["none"]
    assert round(speedup(base, LATEST_20["(customer_id)"])) == 816
    assert round(speedup(base, LATEST_20["(customer_id, created_at)"])) == 2946
    assert round(speedup(base, LATEST_20["(customer_id, created_at) INCLUDE (total_cents)"])) == 8838
    assert speedup(base, LATEST_20["(created_at, status)"]) < 1  # the index that made it slower
    assert round(1 / speedup(base, LATEST_20["(created_at, status)"]), 1) == 1.6
    assert round(CANCELLED_DAY["(created_at, status)"] / CANCELLED_DAY["(status, created_at)"], 1) == 4.1


def test_table_mb():
    assert round(table_mb(114629)) == 939  # the 10M-row table's 114,629 pages of 8 KB
    assert table_mb(1) == 0.008192


def test_insert_cost():
    base = INSERTS["none"]
    costs = {k: round(100 * insert_cost(base, v)) for k, v in INSERTS.items()}
    assert costs["(customer_id)"] == -35
    assert costs["(customer_id, created_at)"] == -55
    assert costs["(customer_id, created_at) INCLUDE (total_cents)"] == -63
    assert costs["(created_at, status)"] == -17  # the cheapest: new orders go at the end
    assert [costs[k] for k in THREE] == [-63, -40, -28]


def test_added_up_rate():
    predicted = added_up_rate(INSERTS["none"], [INSERTS[k] for k in THREE])
    assert round(predicted, -2) == 85900
    assert added_up_rate(1000, []) == 1000
    assert round(added_up_rate(1000, [500, 500])) == 333  # each index doubles the time: 1 + 1 + 1 ms
    # Measured with all three: 13.9x slower than its own baseline, far worse than the added-up costs
    assert round(TOGETHER_BASE / TOGETHER, 1) == 13.9
    assert predicted / TOGETHER > 3.7


def test_narrowing_columns():
    # Cancelled orders in one day: status = ? and a range of created_at
    assert narrowing_columns(["status", "created_at"], {"status"}, "created_at") == 2
    assert narrowing_columns(["created_at", "status"], {"status"}, "created_at") == 1  # every card of the day
    assert narrowing_columns(["customer_id"], {"status"}, "created_at") == 0  # no help at all
    # A customer's new orders: customer_id = ? and status = ?
    assert narrowing_columns(["customer_id", "status"], {"customer_id", "status"}) == 2
    assert narrowing_columns(["customer_id", "created_at"], {"customer_id", "status"}) == 1
    # A customer's spend in a year: customer_id = ? and a range of created_at
    assert narrowing_columns(["customer_id", "created_at"], {"customer_id"}, "created_at") == 2


def test_gives_order():
    # A customer's latest 20: customer_id = ?, newest first
    assert gives_order(["customer_id", "created_at"], {"customer_id"}, "created_at")
    assert not gives_order(["customer_id"], {"customer_id"}, "created_at")  # read all, sort, keep 20
    assert not gives_order(["customer_id", "status"], {"customer_id"}, "created_at")
    # The trap: date first gives the order but doesn't narrow by customer at all
    assert gives_order(["created_at", "status"], {"customer_id"}, "created_at")
    assert narrowing_columns(["created_at", "status"], {"customer_id"}) == 0


def test_covers():
    latest_20_needs = ["customer_id", "created_at", "total_cents"]
    assert covers(["customer_id", "created_at"], ["total_cents"], latest_20_needs)
    assert not covers(["customer_id", "created_at"], [], latest_20_needs)
    assert covers(["status", "created_at"], [], ["status", "created_at"])  # count(*) needs no other column


def test_fastest():
    assert fastest(LATEST_20) == "(customer_id, created_at) INCLUDE (total_cents)"
    assert fastest(CANCELLED_DAY) == "(status, created_at)"
