Debian Codegraph @

tests/test_legacy_query.py

raw
1 from __future__ import annotations
2
3 import typing as t
4 import warnings
5
6 import pytest
7 import sqlalchemy as sa
8 import sqlalchemy.exc as sa_exc
9 from flask import Flask
10 from werkzeug.exceptions import NotFound
11
12 from flask_sqlalchemy import SQLAlchemy
13 from flask_sqlalchemy.query import Query
14
15
16 @pytest.fixture(autouse=True)
17 def ignore_query_warning() -> t.Generator[None, None, None]:
18 if hasattr(sa_exc, "LegacyAPIWarning"):
19 with warnings.catch_warnings():
20 exc = sa_exc.LegacyAPIWarning
21 warnings.simplefilter("ignore", exc)
22 yield
23 else:
24 yield
25
26
27 @pytest.mark.usefixtures("app_ctx")
28 def test_get_or_404(db: SQLAlchemy, Todo: t.Any) -> None:
29 item = Todo()
30 db.session.add(item)
31 db.session.commit()
32 assert Todo.query.get_or_404(1) is item
33
34 with pytest.raises(NotFound):
35 Todo.query.get_or_404(2)
36
37
38 @pytest.mark.usefixtures("app_ctx")
39 def test_first_or_404(db: SQLAlchemy, Todo: t.Any) -> None:
40 db.session.add(Todo(title="a"))
41 db.session.commit()
42 assert Todo.query.filter_by(title="a").first_or_404().title == "a"
43
44 with pytest.raises(NotFound):
45 Todo.query.filter_by(title="b").first_or_404()
46
47
48 @pytest.mark.usefixtures("app_ctx")
49 def test_one_or_404(db: SQLAlchemy, Todo: t.Any) -> None:
50 db.session.add(Todo(title="a"))
51 db.session.add(Todo(title="b"))
52 db.session.add(Todo(title="b"))
53 db.session.commit()
54 assert Todo.query.filter_by(title="a").one_or_404().title == "a"
55
56 with pytest.raises(NotFound):
57 # MultipleResultsFound
58 Todo.query.filter_by(title="b").one_or_404()
59
60 with pytest.raises(NotFound):
61 # NoResultFound
62 Todo.query.filter_by(title="c").one_or_404()
63
64
65 @pytest.mark.usefixtures("app_ctx")
66 def test_paginate(db: SQLAlchemy, Todo: t.Any) -> None:
67 db.session.add_all(Todo() for _ in range(150))
68 db.session.commit()
69 p = Todo.query.paginate()
70 assert p.total == 150
71 assert len(p.items) == 20
72 p2 = p.next()
73 assert p2.page == 2
74 assert p2.total == 150
75
76
77 @pytest.mark.usefixtures("app_ctx")
78 def test_default_query_class(db: SQLAlchemy) -> None:
79 class Parent(db.Model):
80 id = sa.Column(sa.Integer, primary_key=True)
81 children1 = db.relationship("Child", backref="parent1", lazy="dynamic")
82
83 class Child(db.Model):
84 id = sa.Column(sa.Integer, primary_key=True)
85 parent_id = sa.Column(sa.ForeignKey(Parent.id)) # type: ignore[var-annotated]
86 parent2 = db.relationship(
87 Parent,
88 backref=db.backref("children2", lazy="dynamic", viewonly=True),
89 viewonly=True,
90 )
91
92 p = Parent()
93 assert type(Parent.query) is Query
94 assert isinstance(p.children1, Query)
95 assert isinstance(p.children2, Query)
96 assert isinstance(db.session.query(Child), Query)
97
98
99 @pytest.mark.usefixtures("app_ctx")
100 def test_custom_query_class(app: Flask) -> None:
101 class CustomQuery(Query):
102 pass
103
104 db = SQLAlchemy(app, query_class=CustomQuery)
105
106 class Parent(db.Model):
107 id = sa.Column(sa.Integer, primary_key=True)
108 children1 = db.relationship("Child", backref="parent1", lazy="dynamic")
109
110 class Child(db.Model):
111 id = sa.Column(sa.Integer, primary_key=True)
112 parent_id = sa.Column(sa.ForeignKey(Parent.id)) # type: ignore[var-annotated]
113 parent2 = db.relationship(
114 Parent,
115 backref=db.backref("children2", lazy="dynamic", viewonly=True),
116 viewonly=True,
117 )
118
119 p = Parent()
120 assert type(Parent.query) is CustomQuery
121 assert isinstance(p.children1, CustomQuery)
122 assert isinstance(p.children2, CustomQuery)
123 assert isinstance(db.session.query(Child), CustomQuery)