Files
fuel-price/tests/Feature/Commands/ArchiveOldPricesCommandTest.php
Ovidiu U 48af2083f3 feat: add fuel:archive command and monthly scheduler entry
Completes Tasks 12 + 13 from docs/superpowers/plans/2026-04-03-fuel-api-ingestion.md.

- ArchiveOldPricesCommand moves station_prices rows older than 12 months
  into station_prices_archive in chunks of 1000, wrapping each chunk's
  insert + delete in a DB::transaction so a partial failure can't
  duplicate rows.
- StationPriceArchive: add $table = 'station_prices_archive'; without it
  Eloquent infers 'station_price_archives' and the insert would fail.
- routes/console.php: register fuel:archive monthly on the 1st at 04:00
  UTC, alongside the other fuel/oil scheduler entries.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 18:33:05 +01:00

70 lines
2.1 KiB
PHP

<?php
use App\Models\Station;
use App\Models\StationPrice;
use App\Models\StationPriceArchive;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('moves prices older than 12 months to archive', function (): void {
$station = Station::factory()->create();
StationPrice::factory()->create([
'station_id' => $station->node_id,
'price_effective_at' => now()->subMonths(13),
'price_reported_at' => now()->subMonths(13),
'recorded_at' => now()->subMonths(13),
]);
StationPrice::factory()->create([
'station_id' => $station->node_id,
'price_effective_at' => now()->subMonths(6),
'price_reported_at' => now()->subMonths(6),
'recorded_at' => now()->subMonths(6),
]);
$this->artisan('fuel:archive')->assertSuccessful();
expect(StationPrice::count())->toBe(1)
->and(StationPriceArchive::count())->toBe(1);
});
it('outputs no-op message when nothing qualifies', function (): void {
$station = Station::factory()->create();
StationPrice::factory()->create([
'station_id' => $station->node_id,
'price_effective_at' => now()->subMonths(3),
'price_reported_at' => now()->subMonths(3),
'recorded_at' => now()->subMonths(3),
]);
$this->artisan('fuel:archive')
->expectsOutputToContain('No prices to archive.')
->assertSuccessful();
expect(StationPrice::count())->toBe(1)
->and(StationPriceArchive::count())->toBe(0);
});
it('preserves the row data when archiving', function (): void {
$station = Station::factory()->create();
$original = StationPrice::factory()->create([
'station_id' => $station->node_id,
'price_pence' => 14523,
'price_effective_at' => now()->subMonths(13),
'price_reported_at' => now()->subMonths(13),
'recorded_at' => now()->subMonths(13),
]);
$this->artisan('fuel:archive')->assertSuccessful();
$archived = StationPriceArchive::first();
expect($archived)->not->toBeNull()
->and($archived->station_id)->toBe($original->station_id)
->and($archived->price_pence)->toBe(14523);
});