|
| 1 | +"""Optional real-Postgres regression for JSON-backed live snapshot queries.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from datetime import UTC, datetime |
| 5 | +from uuid import uuid4 |
| 6 | + |
| 7 | +import pytest |
| 8 | +import sqlalchemy as sa |
| 9 | + |
| 10 | +from seekandscore.acquisition.models import ( |
| 11 | + NormalizedParcelObservation, |
| 12 | + RawArtifact, |
| 13 | + SourceRun, |
| 14 | + SourceRunProfile, |
| 15 | + SourceRunStatus, |
| 16 | +) |
| 17 | +from seekandscore.acquisition.repository import ( |
| 18 | + PostgresAcquisitionRepository, |
| 19 | + observation_table, |
| 20 | + raw_artifact_table, |
| 21 | + source_run_table, |
| 22 | +) |
| 23 | +from seekandscore.readmodels import LiveCandidateRepository |
| 24 | +from seekandscore.registry import InMemorySourceRegistry |
| 25 | + |
| 26 | +TEST_DATABASE_URL = os.getenv("SEEKANDSCORE_TEST_DATABASE_URL") |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.skipif(not TEST_DATABASE_URL, reason="SEEKANDSCORE_TEST_DATABASE_URL is not set") |
| 30 | +def test_artifact_scoped_snapshot_queries_execute_on_postgres() -> None: |
| 31 | + assert TEST_DATABASE_URL is not None |
| 32 | + database_name = sa.engine.make_url(TEST_DATABASE_URL).database or "" |
| 33 | + if not database_name.endswith("_test"): |
| 34 | + pytest.fail("SEEKANDSCORE_TEST_DATABASE_URL must name a database ending in _test") |
| 35 | + |
| 36 | + engine = sa.create_engine(TEST_DATABASE_URL) |
| 37 | + repository = PostgresAcquisitionRepository(engine) |
| 38 | + now = datetime.now(UTC) |
| 39 | + artifact_id = uuid4() |
| 40 | + observation_id = uuid4() |
| 41 | + run_id = uuid4() |
| 42 | + sha256 = artifact_id.hex * 2 |
| 43 | + artifact = RawArtifact( |
| 44 | + id=artifact_id, |
| 45 | + source_id="travis_tcad_parcels", |
| 46 | + sha256=sha256, |
| 47 | + byte_count=2, |
| 48 | + media_type="application/json", |
| 49 | + storage_uri=f"s3://private-test/{sha256}.json", |
| 50 | + original_uri="https://example.invalid/query", |
| 51 | + request_params={}, |
| 52 | + retrieved_at=now, |
| 53 | + ) |
| 54 | + observation = NormalizedParcelObservation( |
| 55 | + id=observation_id, |
| 56 | + source_id="travis_tcad_parcels", |
| 57 | + source_record_id=str(observation_id.int), |
| 58 | + artifact_id=artifact_id, |
| 59 | + artifact_sha256=sha256, |
| 60 | + parser_version="travis-tcad-parcel-v1", |
| 61 | + jurisdiction_id="us-tx-travis", |
| 62 | + local_parcel_id=str(observation_id.int), |
| 63 | + geographic_id=None, |
| 64 | + situs_address="100 TEST RD", |
| 65 | + situs_city="DEL VALLE", |
| 66 | + situs_zip="78617", |
| 67 | + tcad_acres=2.0, |
| 68 | + observed_at=now, |
| 69 | + ) |
| 70 | + run = SourceRun( |
| 71 | + id=run_id, |
| 72 | + source_id="travis_tcad_parcels", |
| 73 | + status=SourceRunStatus.SUCCEEDED, |
| 74 | + requested_at=now, |
| 75 | + started_at=now, |
| 76 | + completed_at=now, |
| 77 | + retrieved_at=now, |
| 78 | + records_fetched=1, |
| 79 | + observations_created=1, |
| 80 | + artifact_ids=(artifact_id,), |
| 81 | + adapter_version="travis-tcad-arcgis-v1", |
| 82 | + parser_version="travis-tcad-parcel-v1", |
| 83 | + run_profile=SourceRunProfile.COHORT, |
| 84 | + configuration_hash="a" * 64, |
| 85 | + activation_id="test-only", |
| 86 | + ) |
| 87 | + |
| 88 | + try: |
| 89 | + repository.save_artifact(artifact) |
| 90 | + assert repository.save_observations((observation,)) == 1 |
| 91 | + repository.save_run(run) |
| 92 | + |
| 93 | + assert ( |
| 94 | + repository.latest_complete_run( |
| 95 | + "travis_tcad_parcels", run_profile=SourceRunProfile.COHORT |
| 96 | + ) |
| 97 | + == run |
| 98 | + ) |
| 99 | + assert repository.list_latest_observations( |
| 100 | + limit=25, |
| 101 | + artifact_ids=(artifact_id,), |
| 102 | + cities=("DEL VALLE", "MANOR"), |
| 103 | + ) == (observation,) |
| 104 | + assert ( |
| 105 | + repository.count_latest_observations( |
| 106 | + artifact_ids=(artifact_id,), |
| 107 | + cities=("DEL VALLE", "MANOR"), |
| 108 | + ) |
| 109 | + == 1 |
| 110 | + ) |
| 111 | + candidates = LiveCandidateRepository( |
| 112 | + repository, |
| 113 | + InMemorySourceRegistry(), |
| 114 | + display_enabled=True, |
| 115 | + ) |
| 116 | + page = candidates.list(limit=25, cursor=None, dataset_mode="live") |
| 117 | + assert page.dataset_status == "current" |
| 118 | + assert page.total == 1 |
| 119 | + assert len(page.items) == 1 |
| 120 | + assert candidates.get(page.items[0].id) == page.items[0] |
| 121 | + finally: |
| 122 | + with engine.begin() as connection: |
| 123 | + connection.execute(sa.delete(source_run_table).where(source_run_table.c.id == run_id)) |
| 124 | + connection.execute( |
| 125 | + sa.delete(observation_table).where(observation_table.c.id == observation_id) |
| 126 | + ) |
| 127 | + connection.execute( |
| 128 | + sa.delete(raw_artifact_table).where(raw_artifact_table.c.id == artifact_id) |
| 129 | + ) |
| 130 | + engine.dispose() |
0 commit comments