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
|
||||
def restaurants(self) -> list["db.Restaurant"]: # pragma: no cover
|
||||
"""Obtain all `Restaurant`s in `self`."""
|
||||
return (
|
||||
db.session.query(db.Restaurant)
|
||||
.join(
|
||||
db.AddressPixelAssociation,
|
||||
db.Restaurant.address_id == db.AddressPixelAssociation.address_id,
|
||||
)
|
||||
.filter(db.AddressPixelAssociation.pixel_id == self.id)
|
||||
.all()
|
||||
return list(
|
||||
db.session.scalars(
|
||||
sa.select(db.Restaurant)
|
||||
.join(
|
||||
db.AddressPixelAssociation,
|
||||
db.Restaurant.address_id == db.AddressPixelAssociation.address_id,
|
||||
)
|
||||
.where(db.AddressPixelAssociation.pixel_id == self.id),
|
||||
).all(),
|
||||
)
|
||||
|
||||
def clear_map(self) -> t.Self: # pragma: no cover
|
||||
|
|
@ -203,38 +204,32 @@ class Pixel(meta.Base):
|
|||
if restaurants:
|
||||
# Obtain all primary `Address`es in the city that host `Restaurant`s
|
||||
# and are in the `self` `Pixel`.
|
||||
addresses = (
|
||||
db.session.query(db.Address)
|
||||
.filter(
|
||||
addresses = db.session.scalars(
|
||||
sa.select(db.Address).where(
|
||||
db.Address.id.in_(
|
||||
(
|
||||
db.session.query(db.Address.primary_id)
|
||||
.join(
|
||||
db.Restaurant,
|
||||
db.Address.id == db.Restaurant.address_id,
|
||||
)
|
||||
.join(
|
||||
db.AddressPixelAssociation,
|
||||
db.Address.id == db.AddressPixelAssociation.address_id,
|
||||
)
|
||||
.filter(db.AddressPixelAssociation.pixel_id == self.id)
|
||||
sa.select(db.Address.primary_id)
|
||||
.join(
|
||||
db.Restaurant,
|
||||
db.Address.id == db.Restaurant.address_id,
|
||||
)
|
||||
.distinct()
|
||||
.all(),
|
||||
.join(
|
||||
db.AddressPixelAssociation,
|
||||
db.Address.id == db.AddressPixelAssociation.address_id,
|
||||
)
|
||||
.where(db.AddressPixelAssociation.pixel_id == self.id)
|
||||
.distinct(),
|
||||
),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
),
|
||||
).all()
|
||||
|
||||
for address in addresses:
|
||||
# Show the restaurant's name if there is only one.
|
||||
# Otherwise, list all the restaurants' ID's.
|
||||
restaurants_at_address = (
|
||||
db.session.query(db.Restaurant)
|
||||
restaurants_at_address = db.session.scalars(
|
||||
sa.select(db.Restaurant)
|
||||
.join(db.Address, db.Restaurant.address_id == db.Address.id)
|
||||
.filter(db.Address.primary_id == address.id)
|
||||
.all()
|
||||
)
|
||||
.where(db.Address.primary_id == address.id),
|
||||
).all()
|
||||
if len(restaurants_at_address) == 1:
|
||||
restaurant = restaurants_at_address[0]
|
||||
tooltip = f"{restaurant.name} (#{restaurant.id})"
|
||||
|
|
@ -244,12 +239,11 @@ class Pixel(meta.Base):
|
|||
|
||||
if order_counts:
|
||||
# Calculate the number of orders for ALL restaurants ...
|
||||
n_orders = (
|
||||
db.session.query(db.Order.id)
|
||||
n_orders = db.session.execute(
|
||||
sa.select(sa.func.count(db.Order.id))
|
||||
.join(db.Address, db.Order.pickup_address_id == db.Address.id)
|
||||
.filter(db.Address.primary_id == address.id)
|
||||
.count()
|
||||
)
|
||||
.where(db.Address.primary_id == address.id),
|
||||
).scalar_one()
|
||||
# ... and adjust the size of the red dot on the `.map`.
|
||||
if n_orders >= 1000:
|
||||
radius = 20
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ class TestProperties:
|
|||
def _restaurants_mock(self, mocker, monkeypatch, restaurant):
|
||||
"""A `Mock` whose `.return_value` is `[restaurant]`."""
|
||||
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]
|
||||
monkeypatch.setattr(db, "session", mock)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue