| 1 |
import pytest |
| 2 |
from .fixtures import make_app_client |
| 3 |
|
| 4 |
|
| 5 |
@pytest.mark.parametrize("trace_debug", (True, False)) |
| 6 |
def test_trace(trace_debug): |
| 7 |
with make_app_client(settings={"trace_debug": trace_debug}) as client: |
| 8 |
response = client.get("/fixtures/simple_primary_key.json?_trace=1") |
| 9 |
assert response.status == 200 |
| 10 |
|
| 11 |
data = response.json |
| 12 |
if not trace_debug: |
| 13 |
assert "_trace" not in data |
| 14 |
return |
| 15 |
|
| 16 |
assert "_trace" in data |
| 17 |
trace_info = data["_trace"] |
| 18 |
assert isinstance(trace_info["request_duration_ms"], float) |
| 19 |
assert isinstance(trace_info["sum_trace_duration_ms"], float) |
| 20 |
assert isinstance(trace_info["num_traces"], int) |
| 21 |
assert isinstance(trace_info["traces"], list) |
| 22 |
traces = trace_info["traces"] |
| 23 |
assert len(traces) == trace_info["num_traces"] |
| 24 |
for trace in traces: |
| 25 |
assert isinstance(trace["type"], str) |
| 26 |
assert isinstance(trace["start"], float) |
| 27 |
assert isinstance(trace["end"], float) |
| 28 |
assert trace["duration_ms"] == (trace["end"] - trace["start"]) * 1000 |
| 29 |
assert isinstance(trace["traceback"], list) |
| 30 |
assert isinstance(trace["database"], str) |
| 31 |
assert isinstance(trace["sql"], str) |
| 32 |
assert isinstance(trace.get("params"), (list, dict, None.__class__)) |
| 33 |
|
| 34 |
sqls = [trace["sql"] for trace in traces if "sql" in trace] |
| 35 |
# There should be a mix of different types of SQL statement |
| 36 |
expected = ( |
| 37 |
"CREATE TABLE ", |
| 38 |
"PRAGMA ", |
| 39 |
"INSERT OR REPLACE INTO ", |
| 40 |
"INSERT INTO", |
| 41 |
"select ", |
| 42 |
) |
| 43 |
for prefix in expected: |
| 44 |
assert any( |
| 45 |
sql.startswith(prefix) for sql in sqls |
| 46 |
), "No trace beginning with: {}".format(prefix) |
| 47 |
|
| 48 |
# Should be at least one executescript |
| 49 |
assert any(trace for trace in traces if trace.get("executescript")) |
| 50 |
# And at least one executemany |
| 51 |
execute_manys = [trace for trace in traces if trace.get("executemany")] |
| 52 |
assert execute_manys |
| 53 |
assert all(isinstance(trace["count"], int) for trace in execute_manys) |
| 54 |
|
| 55 |
|
| 56 |
def test_trace_parallel_queries(): |
| 57 |
with make_app_client(settings={"trace_debug": True}) as client: |
| 58 |
response = client.get("/parallel-queries?_trace=1") |
| 59 |
assert response.status == 200 |
| 60 |
|
| 61 |
data = response.json |
| 62 |
assert data["one"] == 1 |
| 63 |
assert data["two"] == 2 |
| 64 |
trace_info = data["_trace"] |
| 65 |
traces = [trace for trace in trace_info["traces"] if "sql" in trace] |
| 66 |
one, two = traces |
| 67 |
# "two" should have started before "one" ended |
| 68 |
assert two["start"] < one["end"] |