fix(demography): verify D01-D06 in isolated QA with INE traceability

This commit is contained in:
2026-11-20 20:07:16 +02:00
parent 558c11ef17
commit 3f66201cbe
17 changed files with 570 additions and 89 deletions
+48 -7
View File
@@ -2,16 +2,20 @@
namespace Tests\Feature;
use App\Models\Sector;
use App\Models\User;
use App\Services\DemographicEvidence;
use App\Services\DemographicImport;
use App\Services\InvestmentPreparationWorkspace;
use App\Services\Malware\MalwareScanner;
use App\Services\SectorContentAnalysis;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\TestCase;
final class DemographyTest extends TestCase
@@ -77,6 +81,35 @@ final class DemographyTest extends TestCase
$this->assertSame([], app(DemographicEvidence::class)->analyse([$this->district], 2026, 2027)['territories']);
}
public function test_tourism_is_explicit_and_comparisons_include_intermediate_years(): void
{
$this->assertSame('Turismo', DemographicEvidence::sectors()['tourism']);
$response = $this->getJson('/api/v1/investment/demography?'.http_build_query(['territories' => [$this->district], 'start_year' => 2026, 'end_year' => 2028, 'sector' => 'tourism']))->assertOk();
$response->assertJsonPath('sector', 'tourism')->assertJsonPath('territories.0.series.0.values.0.original_value', '100.125');
$other = (array) DB::table('geo_locations')->where('id', $this->district)->first();
$other['id'] = (string) Str::uuid();
$other['code'] = $other['id'];
$other['external_id'] = '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:QA:'.$other['id'];
DB::table('geo_locations')->insert($other);
foreach (DB::table('demographic_observations')->where('geo_location_id', $this->district)->get() as $row) {
$row = (array) $row;
unset($row['id']);
DB::table('demographic_observations')->insert([...$row, 'geo_location_id' => $other['id']]);
}
$analysis = app(DemographicEvidence::class)->analyse([$this->district, $other['id']], 2026, 2028, 'commerce');
$this->assertSame([2026, 2027], array_column($analysis['comparisons'], 'year'));
$this->assertSame([], app(SectorContentAnalysis::class)->forTerritories([$this->district], 'commerce'));
}
public function test_commerce_and_territory_reach_the_normal_investor_entry(): void
{
$sector = Sector::firstOrCreate(['slug' => 'comercio-e-servicos'], ['name' => 'Comércio e serviços', 'is_active' => true]);
$this->get('/localizacoes/demografia?'.http_build_query(['territories' => [$this->district], 'sector' => 'commerce']))
->assertOk()->assertInertia(fn ($page) => $page->component('investment/demography')->where('entrySector', $sector->slug)->where('sectorReadings', []));
$this->actingAs(User::factory()->create())->get('/investimentos/iniciar?'.http_build_query(['source' => 'territory:'.$this->district, 'sector' => $sector->slug]))
->assertOk()->assertInertia(fn ($page) => $page->component('investor-path/context-start')->where('entryQuery.sector_id', (string) $sector->id)->where('source.id', $this->district)->etc());
}
public function test_new_editions_remain_separate_and_divergence_is_reported(): void
{
$other = $this->batch('approved');
@@ -202,8 +235,10 @@ final class DemographyTest extends TestCase
{
Storage::fake('local');
$disk = Storage::disk('local');
$original = 'SYNTHETIC prepared original'; $sha = hash('sha256', $original);
$directory = 'prepared/'.$sha; $disk->makeDirectory($directory);
$original = 'SYNTHETIC prepared original';
$sha = hash('sha256', $original);
$directory = 'prepared/'.$sha;
$disk->makeDirectory($directory);
$disk->put('synthetic.xlsx', $original);
$row = ['province' => 'SYNTHETIC province', 'territory' => 'SYNTHETIC province', 'level' => 'province',
'year' => 2026, 'indicator' => 'population', 'sex' => 'all', 'residence' => 'all', 'age_group' => 'all', 'unit' => 'pessoas',
@@ -214,10 +249,11 @@ final class DemographyTest extends TestCase
$preview = json_encode($report, JSON_THROW_ON_ERROR);
$receipt = ['sha256' => $sha, 'baseline_year' => null, 'parser_sha256' => hash_file('sha256', base_path('tools/demography_workbook.py')),
'preview_sha256' => hash('sha256', $preview), 'records_sha256' => hash('sha256', $records), 'record_count' => 1];
$disk->put($directory.'/preview.json', $preview); $disk->put($directory.'/records.ndjson', $records);
$disk->put($directory.'/preview.json', $preview);
$disk->put($directory.'/records.ndjson', $records);
$disk->put($directory.'/preparation.json', json_encode($receipt, JSON_THROW_ON_ERROR));
config(['demography.prepared_directory' => $disk->path('prepared'), 'demography.python' => '/missing-python-must-not-run']);
$this->mock(\App\Services\Malware\MalwareScanner::class)->shouldReceive('scan')->once()->andReturn([
$this->mock(MalwareScanner::class)->shouldReceive('scan')->once()->andReturn([
'result' => 'clean', 'engine' => 'synthetic', 'engine_version' => 'test']);
$service = app(DemographicImport::class);
$this->assertTrue($service->preparedSourcesReady(['synthetic.xlsx' => ['sha256' => $sha, 'records' => 1]]));
@@ -227,9 +263,14 @@ final class DemographyTest extends TestCase
$this->assertSame('100.125', DB::table('demographic_observations')->where('import_id', $result['id'])->value('numeric_value'));
$this->assertSame('imported', DB::table('demographic_imports')->where('id', $result['id'])->value('status'));
$disk->put($directory.'/records.ndjson', 'TAMPERED');
try { $service->preparedSource($sha, null); $this->fail('Changed prepared records must be refused.'); }
catch (\Symfony\Component\HttpKernel\Exception\HttpException $error) { $this->assertSame(409, $error->getStatusCode()); }
$receipt['parser_sha256'] = str_repeat('0', 64); $disk->put($directory.'/preparation.json', json_encode($receipt, JSON_THROW_ON_ERROR));
try {
$service->preparedSource($sha, null);
$this->fail('Changed prepared records must be refused.');
} catch (HttpException $error) {
$this->assertSame(409, $error->getStatusCode());
}
$receipt['parser_sha256'] = str_repeat('0', 64);
$disk->put($directory.'/preparation.json', json_encode($receipt, JSON_THROW_ON_ERROR));
$this->assertFalse($service->preparedSourcesReady(['synthetic.xlsx' => ['sha256' => $sha, 'records' => 1]]));
}