|
1 | 1 | import datetime |
| 2 | +import os |
2 | 3 | import numpy as np |
3 | 4 | import pandas as pd |
4 | 5 | import pathlib |
@@ -296,3 +297,61 @@ def test_read_gtfs_date_exists_feed_is_read(self): |
296 | 297 | assert "Harvard" in result.trips_by_route_id("1")["trip_headsign"].values |
297 | 298 |
|
298 | 299 | shutil.rmtree(expected_path) |
| 300 | + |
| 301 | + |
| 302 | +class TestCleanupOldGtfsArchives: |
| 303 | + """Test cleanup_old_gtfs_archives function""" |
| 304 | + |
| 305 | + @pytest.fixture |
| 306 | + def temp_gtfs_dir(self, tmp_path, monkeypatch): |
| 307 | + """Point the GTFS archive dir at a temp directory with a known retention.""" |
| 308 | + monkeypatch.setattr(gtfs, "MAIN_DIR", tmp_path) |
| 309 | + monkeypatch.setattr(gtfs, "GTFS_ARCHIVE_RETENTION_DAYS", 90) |
| 310 | + return tmp_path |
| 311 | + |
| 312 | + def _make_archive(self, base: pathlib.Path, name: str, age_days: int) -> pathlib.Path: |
| 313 | + """Create a fake archive directory whose mtime is age_days in the past.""" |
| 314 | + archive_dir = base / name |
| 315 | + archive_dir.mkdir() |
| 316 | + (archive_dir / "trips.txt").write_text("trip_id\n") |
| 317 | + mtime = (datetime.datetime.now() - datetime.timedelta(days=age_days)).timestamp() |
| 318 | + os.utime(archive_dir, (mtime, mtime)) |
| 319 | + return archive_dir |
| 320 | + |
| 321 | + def test_deletes_archives_older_than_retention(self, temp_gtfs_dir): |
| 322 | + self._make_archive(temp_gtfs_dir, "20200101", age_days=200) |
| 323 | + self._make_archive(temp_gtfs_dir, "20260601", age_days=1) |
| 324 | + |
| 325 | + deleted = gtfs.cleanup_old_gtfs_archives() |
| 326 | + |
| 327 | + assert deleted == 1 |
| 328 | + assert not (temp_gtfs_dir / "20200101").exists() |
| 329 | + assert (temp_gtfs_dir / "20260601").exists() |
| 330 | + |
| 331 | + def test_keeps_archives_within_retention(self, temp_gtfs_dir): |
| 332 | + self._make_archive(temp_gtfs_dir, "20260501", age_days=30) |
| 333 | + self._make_archive(temp_gtfs_dir, "20260601", age_days=1) |
| 334 | + |
| 335 | + deleted = gtfs.cleanup_old_gtfs_archives() |
| 336 | + |
| 337 | + assert deleted == 0 |
| 338 | + assert len([d for d in temp_gtfs_dir.iterdir() if d.is_dir()]) == 2 |
| 339 | + |
| 340 | + def test_always_keeps_most_recent_archive(self, temp_gtfs_dir): |
| 341 | + # Only one archive and it is very old—it must still be retained as the active feed. |
| 342 | + self._make_archive(temp_gtfs_dir, "20190101", age_days=500) |
| 343 | + |
| 344 | + deleted = gtfs.cleanup_old_gtfs_archives() |
| 345 | + |
| 346 | + assert deleted == 0 |
| 347 | + assert (temp_gtfs_dir / "20190101").exists() |
| 348 | + |
| 349 | + def test_ignores_archived_feeds_file(self, temp_gtfs_dir): |
| 350 | + (temp_gtfs_dir / gtfs.GTFS_ARCHIVES_FILENAME).write_text("data") |
| 351 | + self._make_archive(temp_gtfs_dir, "20200101", age_days=200) |
| 352 | + self._make_archive(temp_gtfs_dir, "20260601", age_days=1) |
| 353 | + |
| 354 | + deleted = gtfs.cleanup_old_gtfs_archives() |
| 355 | + |
| 356 | + assert deleted == 1 |
| 357 | + assert (temp_gtfs_dir / gtfs.GTFS_ARCHIVES_FILENAME).exists() |
0 commit comments