Query with select() in Pixel
`Session.query()` is the legacy API in SQLAlchemy 2.x. The addresses with restaurants are now selected with a subquery in one statement. The `_restaurants_mock` fixture mocks the new call chain.
This commit is contained in:
parent
0dc3e11a63
commit
2eb6b5769f
2 changed files with 32 additions and 38 deletions
|
|
@ -140,14 +140,15 @@ class Pixel(meta.Base):
|
||||||
@functools.cached_property
|
@functools.cached_property
|
||||||
def restaurants(self) -> list["db.Restaurant"]: # pragma: no cover
|
def restaurants(self) -> list["db.Restaurant"]: # pragma: no cover
|
||||||
"""Obtain all `Restaurant`s in `self`."""
|
"""Obtain all `Restaurant`s in `self`."""
|
||||||
return (
|
return list(
|
||||||
db.session.query(db.Restaurant)
|
db.session.scalars(
|
||||||
.join(
|
sa.select(db.Restaurant)
|
||||||
db.AddressPixelAssociation,
|
.join(
|
||||||
db.Restaurant.address_id == db.AddressPixelAssociation.address_id,
|
db.AddressPixelAssociation,
|
||||||
)
|
db.Restaurant.address_id == db.AddressPixelAssociation.address_id,
|
||||||
.filter(db.AddressPixelAssociation.pixel_id == self.id)
|
)
|
||||||
.all()
|
.where(db.AddressPixelAssociation.pixel_id == self.id),
|
||||||
|
).all(),
|
||||||
)
|
)
|
||||||
|
|
||||||
def clear_map(self) -> t.Self: # pragma: no cover
|
def clear_map(self) -> t.Self: # pragma: no cover
|
||||||
|
|
@ -203,38 +204,32 @@ class Pixel(meta.Base):
|
||||||
if restaurants:
|
if restaurants:
|
||||||
# Obtain all primary `Address`es in the city that host `Restaurant`s
|
# Obtain all primary `Address`es in the city that host `Restaurant`s
|
||||||
# and are in the `self` `Pixel`.
|
# and are in the `self` `Pixel`.
|
||||||
addresses = (
|
addresses = db.session.scalars(
|
||||||
db.session.query(db.Address)
|
sa.select(db.Address).where(
|
||||||
.filter(
|
|
||||||
db.Address.id.in_(
|
db.Address.id.in_(
|
||||||
(
|
sa.select(db.Address.primary_id)
|
||||||
db.session.query(db.Address.primary_id)
|
.join(
|
||||||
.join(
|
db.Restaurant,
|
||||||
db.Restaurant,
|
db.Address.id == db.Restaurant.address_id,
|
||||||
db.Address.id == db.Restaurant.address_id,
|
|
||||||
)
|
|
||||||
.join(
|
|
||||||
db.AddressPixelAssociation,
|
|
||||||
db.Address.id == db.AddressPixelAssociation.address_id,
|
|
||||||
)
|
|
||||||
.filter(db.AddressPixelAssociation.pixel_id == self.id)
|
|
||||||
)
|
)
|
||||||
.distinct()
|
.join(
|
||||||
.all(),
|
db.AddressPixelAssociation,
|
||||||
|
db.Address.id == db.AddressPixelAssociation.address_id,
|
||||||
|
)
|
||||||
|
.where(db.AddressPixelAssociation.pixel_id == self.id)
|
||||||
|
.distinct(),
|
||||||
),
|
),
|
||||||
)
|
),
|
||||||
.all()
|
).all()
|
||||||
)
|
|
||||||
|
|
||||||
for address in addresses:
|
for address in addresses:
|
||||||
# Show the restaurant's name if there is only one.
|
# Show the restaurant's name if there is only one.
|
||||||
# Otherwise, list all the restaurants' ID's.
|
# Otherwise, list all the restaurants' ID's.
|
||||||
restaurants_at_address = (
|
restaurants_at_address = db.session.scalars(
|
||||||
db.session.query(db.Restaurant)
|
sa.select(db.Restaurant)
|
||||||
.join(db.Address, db.Restaurant.address_id == db.Address.id)
|
.join(db.Address, db.Restaurant.address_id == db.Address.id)
|
||||||
.filter(db.Address.primary_id == address.id)
|
.where(db.Address.primary_id == address.id),
|
||||||
.all()
|
).all()
|
||||||
)
|
|
||||||
if len(restaurants_at_address) == 1:
|
if len(restaurants_at_address) == 1:
|
||||||
restaurant = restaurants_at_address[0]
|
restaurant = restaurants_at_address[0]
|
||||||
tooltip = f"{restaurant.name} (#{restaurant.id})"
|
tooltip = f"{restaurant.name} (#{restaurant.id})"
|
||||||
|
|
@ -244,12 +239,11 @@ class Pixel(meta.Base):
|
||||||
|
|
||||||
if order_counts:
|
if order_counts:
|
||||||
# Calculate the number of orders for ALL restaurants ...
|
# Calculate the number of orders for ALL restaurants ...
|
||||||
n_orders = (
|
n_orders = db.session.execute(
|
||||||
db.session.query(db.Order.id)
|
sa.select(sa.func.count(db.Order.id))
|
||||||
.join(db.Address, db.Order.pickup_address_id == db.Address.id)
|
.join(db.Address, db.Order.pickup_address_id == db.Address.id)
|
||||||
.filter(db.Address.primary_id == address.id)
|
.where(db.Address.primary_id == address.id),
|
||||||
.count()
|
).scalar_one()
|
||||||
)
|
|
||||||
# ... and adjust the size of the red dot on the `.map`.
|
# ... and adjust the size of the red dot on the `.map`.
|
||||||
if n_orders >= 1000:
|
if n_orders >= 1000:
|
||||||
radius = 20
|
radius = 20
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ class TestProperties:
|
||||||
def _restaurants_mock(self, mocker, monkeypatch, restaurant):
|
def _restaurants_mock(self, mocker, monkeypatch, restaurant):
|
||||||
"""A `Mock` whose `.return_value` is `[restaurant]`."""
|
"""A `Mock` whose `.return_value` is `[restaurant]`."""
|
||||||
mock = mocker.Mock()
|
mock = mocker.Mock()
|
||||||
query = mock.query.return_value.join.return_value.filter.return_value.all
|
query = mock.scalars.return_value.all
|
||||||
query.return_value = [restaurant]
|
query.return_value = [restaurant]
|
||||||
monkeypatch.setattr(db, "session", mock)
|
monkeypatch.setattr(db, "session", mock)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue