feat: use EIA as primary Brent crude source with FRED fallback

This commit is contained in:
Ovidiu U
2026-04-14 16:23:06 +01:00
parent 4220b1b86a
commit a7ee9f4557
2 changed files with 142 additions and 17 deletions

View File

@@ -20,11 +20,32 @@ beforeEach(function (): void {
// --- fetchBrentPrices ---
it('fetches and stores brent prices from FRED', function (): void {
it('fetches and stores brent prices from EIA when EIA succeeds', function (): void {
Http::fake([
'*eia.gov/*' => Http::response([
'response' => [
'data' => [
['period' => '2026-04-02', 'value' => '73.80'],
['period' => '2026-04-01', 'value' => '75.10'],
['period' => '2026-03-31', 'value' => '74.50'],
],
],
]),
'*/fred/*' => Http::response([], 500),
]);
$this->service->fetchBrentPrices();
expect(BrentPrice::count())->toBe(3)
->and(BrentPrice::find('2026-04-02')->price_usd)->toBe('73.80');
Http::assertNotSent(fn ($request) => str_contains($request->url(), 'stlouisfed'));
});
it('falls back to FRED when EIA returns a 500', function (): void {
Http::fake([
'*eia.gov/*' => Http::response([], 500),
'*/fred/series/observations*' => Http::response([
'observations' => [
['date' => '2026-03-31', 'value' => '74.50'],
['date' => '2026-04-01', 'value' => '75.10'],
['date' => '2026-04-02', 'value' => '73.80'],
],
@@ -33,17 +54,44 @@ it('fetches and stores brent prices from FRED', function (): void {
$this->service->fetchBrentPrices();
expect(BrentPrice::count())->toBe(3)
->and(BrentPrice::find('2026-04-02')->price_usd)->toBe('73.80');
expect(BrentPrice::count())->toBe(2);
});
it('filters out FRED missing value markers', function (): void {
it('falls back to FRED when EIA returns empty data', function (): void {
Http::fake([
'*eia.gov/*' => Http::response(['response' => ['data' => []]]),
'*/fred/series/observations*' => Http::response([
'observations' => [
['date' => '2026-04-01', 'value' => '75.10'],
['date' => '2026-04-02', 'value' => '.'],
['date' => '2026-04-03', 'value' => '74.20'],
],
]),
]);
$this->service->fetchBrentPrices();
expect(BrentPrice::count())->toBe(1);
});
it('stores no rows and logs error when both EIA and FRED fail', function (): void {
Http::fake([
'*eia.gov/*' => Http::response([], 500),
'*/fred/series/observations*' => Http::response([], 500),
]);
$this->service->fetchBrentPrices();
expect(BrentPrice::count())->toBe(0);
});
it('filters out EIA missing value markers', function (): void {
Http::fake([
'*eia.gov/*' => Http::response([
'response' => [
'data' => [
['period' => '2026-04-01', 'value' => '75.10'],
['period' => '2026-04-02', 'value' => '.'],
['period' => '2026-04-03', 'value' => '74.20'],
],
],
]),
]);
@@ -54,11 +102,11 @@ it('filters out FRED missing value markers', function (): void {
->and(BrentPrice::find('2026-04-02'))->toBeNull();
});
it('upserts existing brent price rows on refetch', function (): void {
it('upserts existing brent price rows on refetch via EIA', function (): void {
Http::fake([
'*/fred/series/observations*' => Http::sequence()
->push(['observations' => [['date' => '2026-04-01', 'value' => '74.00']]])
->push(['observations' => [['date' => '2026-04-01', 'value' => '75.50']]]),
'*eia.gov/*' => Http::sequence()
->push(['response' => ['data' => [['period' => '2026-04-01', 'value' => '74.00']]]])
->push(['response' => ['data' => [['period' => '2026-04-01', 'value' => '75.50']]]]),
]);
$this->service->fetchBrentPrices();