velofy/tests/console/test_gridify.py
Alexander Hess 2add322a25
Test the revision lock of the CLI stages
The `@db_revision` decorator no longer skips its check under `TESTING`.

The tests replace `current_db_revision()` with the revision they need.
2026-12-02 13:42:04 +01:00

62 lines
1.9 KiB
Python

"""Integration test for the `velofy.console.gridify` module."""
import pytest
import sqlalchemy as sa
import velofy
from velofy import db
from velofy.console import gridify
@pytest.mark.db
def test_two_pixels_with_two_addresses(
cli,
db_session,
monkeypatch,
city,
make_address,
make_restaurant,
make_order,
):
"""Two `Address` objects in distinct `Pixel` objects.
This is roughly the same test case as
`tests.db.test_grids.test_two_pixels_with_two_addresses`.
The difference is that the result is written to the database.
"""
# Create two `Address` objects in distinct `Pixel`s.
# One `Address` in the lower-left `Pixel`, ...
address1 = make_address(latitude=48.8357377, longitude=2.2517412)
# ... and another one in the upper-right one.
address2 = make_address(latitude=48.8898312, longitude=2.4357622)
# Locate a `Restaurant` at the two `Address` objects and
# place one `Order` for each of them so that the `Address`
# objects are used as `Order.pickup_address`s.
restaurant1 = make_restaurant(address=address1)
restaurant2 = make_restaurant(address=address2)
order1 = make_order(restaurant=restaurant1)
order2 = make_order(restaurant=restaurant2)
db_session.add(order1)
db_session.add(order2)
db_session.commit()
side_length = max(city.total_x // 2, city.total_y // 2) + 1
# Hack the configuration regarding the grids to be created.
monkeypatch.setattr(velofy.config, "GRID_SIDE_LENGTHS", (side_length,))
# The `gridify` stage is locked to the revision it was carried out at.
monkeypatch.setattr(
gridify.decorators,
"current_db_revision",
lambda: "e86290e7305e",
)
result = cli.invoke(gridify.gridify)
assert result.exit_code == 0
assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Grid)) == 1
assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Pixel)) == 2