"""Tests for the Cloud SQL emulator."""


def test_create_and_get_sql_instance(client):
    resp = client.post(
        "/sql/v1beta4/projects/test-proj/instances",
        json={"name": "my-db", "databaseVersion": "POSTGRES_15"},
    )
    assert resp.status_code == 200
    op = resp.get_json()
    assert op["status"] == "DONE"

    resp = client.get("/sql/v1beta4/projects/test-proj/instances/my-db")
    assert resp.status_code == 200
    inst = resp.get_json()
    assert inst["name"] == "my-db"
    assert inst["databaseVersion"] == "POSTGRES_15"
    assert inst["state"] == "RUNNABLE"


def test_create_database(client):
    client.post(
        "/sql/v1beta4/projects/test-proj/instances",
        json={"name": "db-inst"},
    )
    resp = client.post(
        "/sql/v1beta4/projects/test-proj/instances/db-inst/databases",
        json={"name": "myapp"},
    )
    assert resp.status_code == 200

    resp = client.get("/sql/v1beta4/projects/test-proj/instances/db-inst/databases/myapp")
    assert resp.status_code == 200
    assert resp.get_json()["name"] == "myapp"


def test_delete_sql_instance(client):
    client.post(
        "/sql/v1beta4/projects/test-proj/instances",
        json={"name": "doomed-db"},
    )
    resp = client.delete("/sql/v1beta4/projects/test-proj/instances/doomed-db")
    assert resp.status_code == 200

    resp = client.get("/sql/v1beta4/projects/test-proj/instances/doomed-db")
    assert resp.status_code == 404
