| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
|
| 5 |
import pytest |
| 6 |
import sqlalchemy as sa |
| 7 |
import sqlalchemy.orm as sa_orm |
| 8 |
from flask import Flask |
| 9 |
from werkzeug.exceptions import NotFound |
| 10 |
|
| 11 |
from flask_sqlalchemy import SQLAlchemy |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.usefixtures("app_ctx") |
| 15 |
def test_view_get_or_404(db: SQLAlchemy, Todo: t.Any) -> None: |
| 16 |
item = Todo() |
| 17 |
db.session.add(item) |
| 18 |
db.session.commit() |
| 19 |
assert db.get_or_404(Todo, 1) is item |
| 20 |
with pytest.raises(NotFound): |
| 21 |
assert db.get_or_404(Todo, 2) |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.usefixtures("app_ctx") |
| 25 |
def test_first_or_404(db: SQLAlchemy, Todo: t.Any) -> None: |
| 26 |
db.session.add(Todo(title="a")) |
| 27 |
db.session.commit() |
| 28 |
result = db.first_or_404(db.select(Todo).filter_by(title="a")) |
| 29 |
assert result.title == "a" |
| 30 |
|
| 31 |
with pytest.raises(NotFound): |
| 32 |
db.first_or_404(db.select(Todo).filter_by(title="b")) |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.usefixtures("app_ctx") |
| 36 |
def test_view_one_or_404(db: SQLAlchemy, Todo: t.Any) -> None: |
| 37 |
db.session.add(Todo(title="a")) |
| 38 |
db.session.add(Todo(title="b")) |
| 39 |
db.session.add(Todo(title="b")) |
| 40 |
db.session.commit() |
| 41 |
result = db.one_or_404(db.select(Todo).filter_by(title="a")) |
| 42 |
assert result.title == "a" |
| 43 |
|
| 44 |
with pytest.raises(NotFound): |
| 45 |
# MultipleResultsFound |
| 46 |
db.one_or_404(db.select(Todo).filter_by(title="b")) |
| 47 |
|
| 48 |
with pytest.raises(NotFound): |
| 49 |
# NoResultFound |
| 50 |
db.one_or_404(db.select(Todo).filter_by(title="c")) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.usefixtures("app_ctx") |
| 54 |
def test_paginate(db: SQLAlchemy, Todo: t.Any) -> None: |
| 55 |
db.session.add_all(Todo() for _ in range(150)) |
| 56 |
db.session.commit() |
| 57 |
p = db.paginate(db.select(Todo)) |
| 58 |
assert p.total == 150 |
| 59 |
assert len(p.items) == 20 |
| 60 |
p2 = p.next() |
| 61 |
assert p2.page == 2 |
| 62 |
assert p2.total == 150 |
| 63 |
|
| 64 |
|
| 65 |
# This test creates its own inline model so that it can use that as the type |
| 66 |
@pytest.mark.usefixtures("app_ctx") |
| 67 |
def test_view_get_or_404_typed(db: SQLAlchemy, app: Flask) -> None: |
| 68 |
# Copied and pasted from conftest.py |
| 69 |
if issubclass(db.Model, (sa_orm.MappedAsDataclass)): |
| 70 |
|
| 71 |
class Todo(db.Model): |
| 72 |
id: sa_orm.Mapped[int] = sa_orm.mapped_column( |
| 73 |
sa.Integer, init=False, primary_key=True |
| 74 |
) |
| 75 |
title: sa_orm.Mapped[str] = sa_orm.mapped_column( |
| 76 |
sa.String, nullable=True, default=None |
| 77 |
) |
| 78 |
|
| 79 |
elif issubclass(db.Model, (sa_orm.DeclarativeBase, sa_orm.DeclarativeBaseNoMeta)): |
| 80 |
|
| 81 |
class Todo(db.Model): # type: ignore[no-redef] |
| 82 |
id: sa_orm.Mapped[int] = sa_orm.mapped_column(sa.Integer, primary_key=True) |
| 83 |
title: sa_orm.Mapped[str] = sa_orm.mapped_column(sa.String, nullable=True) |
| 84 |
|
| 85 |
else: |
| 86 |
|
| 87 |
class Todo(db.Model): # type: ignore[no-redef] |
| 88 |
id = sa.Column(sa.Integer, primary_key=True) |
| 89 |
title = sa.Column(sa.String) |
| 90 |
|
| 91 |
db.create_all() |
| 92 |
|
| 93 |
todo = Todo() |
| 94 |
todo.title = "Python" |
| 95 |
db.session.add(todo) |
| 96 |
db.session.commit() |
| 97 |
result = db.get_or_404(Todo, 1) |
| 98 |
assert result.title == "Python" |
| 99 |
assert result is todo |
| 100 |
if hasattr(t, "assert_type"): |
| 101 |
t.assert_type(result, Todo) |
| 102 |
with pytest.raises(NotFound): |
| 103 |
assert db.get_or_404(Todo, 2) |
| 104 |
db.drop_all() |