-
-
Notifications
You must be signed in to change notification settings - Fork 901
Expand file tree
/
Copy pathtest_record_queries.py
More file actions
52 lines (40 loc) · 1.74 KB
/
test_record_queries.py
File metadata and controls
52 lines (40 loc) · 1.74 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
from __future__ import annotations
import os
import pytest
import sqlalchemy as sa
import sqlalchemy.orm as sa_orm
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_sqlalchemy.record_queries import get_recorded_queries
@pytest.mark.usefixtures("app_ctx")
def test_query_info(app: Flask) -> None:
app.config["SQLALCHEMY_RECORD_QUERIES"] = True
db = SQLAlchemy(app)
# Copied and pasted from conftest.py
if issubclass(db.Model, (sa_orm.MappedAsDataclass)):
class Todo(db.Model):
id: sa_orm.Mapped[int] = sa_orm.mapped_column(
sa.Integer, init=False, primary_key=True
)
title: sa_orm.Mapped[str] = sa_orm.mapped_column(
sa.String, nullable=True, default=None
)
elif issubclass(db.Model, (sa_orm.DeclarativeBase, sa_orm.DeclarativeBaseNoMeta)):
class Todo(db.Model): # type: ignore[no-redef]
id: sa_orm.Mapped[int] = sa_orm.mapped_column(sa.Integer, primary_key=True)
title: sa_orm.Mapped[str] = sa_orm.mapped_column(sa.String, nullable=True)
else:
class Todo(db.Model): # type: ignore[no-redef]
id = sa.Column(sa.Integer, primary_key=True)
title = sa.Column(sa.String)
db.create_all()
db.session.execute(sa.select(Todo).filter(Todo.id < 5)).scalars()
info = get_recorded_queries()[-1]
assert info.statement is not None
assert "SELECT" in info.statement
assert "FROM todo" in info.statement
assert info.parameters[0][0] == 5
assert info.duration == info.end_time - info.start_time
assert os.path.join("tests", "test_record_queries.py:") in info.location
assert "(test_query_info)" in info.location
assert info.bind_key is None