feature test su institution

This commit is contained in:
Giuseppe Naponiello
2026-06-22 22:14:15 +02:00
parent cc4e174880
commit fe73662903
39 changed files with 2432 additions and 1 deletions

View File

@@ -0,0 +1,168 @@
<?php
namespace Tests\Feature\Etl;
use App\Etl\Importers\InstitutionImporter;
use App\Models\Institution;
use Database\Seeders\InstitutionCategorySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Concerns\CreatesLegacyDatabase;
use Tests\TestCase;
class InstitutionImporterTest extends TestCase
{
use CreatesLegacyDatabase;
use RefreshDatabase;
/** Categorie v2 con gli id legacy fissi (2,3,4,6). */
private function seedV2Categories(): void
{
$this->seed(InstitutionCategorySeeder::class);
}
public function test_it_imports_an_institution_and_creates_the_official_link(): void
{
$this->seedV2Categories();
$this->fakeLegacyConnection();
$this->insertLegacyInstitution([
'id' => 1,
'category' => 3,
'name' => 'Blekinge Museum',
'abbreviation' => 'BLM',
'city' => 'Karlskrona',
'url' => 'https://blekingemuseum.se/',
]);
$summary = (new InstitutionImporter)->import(false);
$this->assertSame(1, $summary->created);
$this->assertDatabaseHas('institutions', [
'legacy_id' => 1,
'category_id' => 3,
'name' => 'Blekinge Museum',
'city' => 'Karlskrona',
]);
$institution = Institution::where('legacy_id', 1)->firstOrFail();
$this->assertNotEmpty($institution->uuid);
$this->assertDatabaseHas('institution_links', [
'institution_id' => $institution->id,
'type' => 'official',
'url' => 'https://blekingemuseum.se/',
]);
}
public function test_it_creates_no_link_when_the_url_is_blank(): void
{
$this->seedV2Categories();
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 21, 'url' => ' ']);
(new InstitutionImporter)->import(false);
$this->assertDatabaseCount('institution_links', 0);
}
public function test_it_trims_textual_fields(): void
{
$this->seedV2Categories();
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 20, 'name' => 'Lund University Library ']);
(new InstitutionImporter)->import(false);
$this->assertDatabaseHas('institutions', ['legacy_id' => 20, 'name' => 'Lund University Library']);
}
public function test_it_falls_back_to_the_default_color_when_legacy_color_is_blank(): void
{
$this->seedV2Categories();
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 1, 'color' => null]);
(new InstitutionImporter)->import(false);
$this->assertDatabaseHas('institutions', ['legacy_id' => 1, 'color' => '#c5cae9']);
}
public function test_it_skips_institutions_with_an_unknown_category(): void
{
$this->seedV2Categories();
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 99, 'category' => 999]);
$summary = (new InstitutionImporter)->import(false);
$this->assertSame(1, $summary->skipped);
$this->assertNotEmpty($summary->warnings);
$this->assertDatabaseMissing('institutions', ['legacy_id' => 99]);
}
public function test_it_regenerates_the_uuid_instead_of_copying_the_legacy_one(): void
{
$this->seedV2Categories();
$this->fakeLegacyConnection();
$legacyUuid = '00000000-0000-0000-0000-000000000000';
$this->insertLegacyInstitution(['id' => 1, 'uuid' => $legacyUuid]);
(new InstitutionImporter)->import(false);
$this->assertNotSame($legacyUuid, Institution::where('legacy_id', 1)->value('uuid'));
}
public function test_it_is_idempotent_and_keeps_the_uuid_stable(): void
{
$this->seedV2Categories();
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 1]);
$first = (new InstitutionImporter)->import(false);
$uuid = Institution::where('legacy_id', 1)->value('uuid');
$second = (new InstitutionImporter)->import(false);
$this->assertSame(1, $first->created);
$this->assertSame(0, $second->created);
$this->assertSame(1, $second->updated);
$this->assertDatabaseCount('institutions', 1);
$this->assertSame($uuid, Institution::where('legacy_id', 1)->value('uuid'));
}
public function test_dry_run_writes_nothing(): void
{
$this->seedV2Categories();
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 1]);
$summary = (new InstitutionImporter)->import(true);
$this->assertSame(1, $summary->created);
$this->assertDatabaseCount('institutions', 0);
}
public function test_dry_run_counts_an_existing_institution_as_updated(): void
{
$this->seedV2Categories();
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 1]);
(new InstitutionImporter)->import(false);
$summary = (new InstitutionImporter)->import(true);
$this->assertSame(0, $summary->created);
$this->assertSame(1, $summary->updated);
$this->assertDatabaseCount('institutions', 1);
}
public function test_it_warns_and_skips_when_the_v2_lookup_is_empty(): void
{
// Nessun seed delle categorie v2.
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 1]);
$summary = (new InstitutionImporter)->import(false);
$this->assertNotEmpty($summary->warnings);
$this->assertSame(0, $summary->total());
$this->assertDatabaseCount('institutions', 0);
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Tests\Feature\Etl;
use Database\Seeders\InstitutionCategorySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\CreatesLegacyDatabase;
use Tests\TestCase;
class V1ImportCommandTest extends TestCase
{
use CreatesLegacyDatabase;
use RefreshDatabase;
public function test_it_imports_and_reports_counts(): void
{
$this->seed(InstitutionCategorySeeder::class);
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 1]);
$this->artisan('v1:import')
->expectsOutputToContain('created 1, updated 0, skipped 0')
->assertSuccessful();
$this->assertDatabaseCount('institutions', 1);
}
public function test_dry_run_writes_nothing(): void
{
$this->seed(InstitutionCategorySeeder::class);
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 1]);
$this->artisan('v1:import --dry-run')
->expectsOutputToContain('DRY-RUN')
->assertSuccessful();
$this->assertDatabaseCount('institutions', 0);
}
public function test_only_filter_skips_unmatched_importers(): void
{
$this->seed(InstitutionCategorySeeder::class);
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 1]);
$this->artisan('v1:import --only=nonexistent')->assertSuccessful();
$this->assertDatabaseCount('institutions', 0);
}
public function test_it_prints_importer_warnings(): void
{
$this->seed(InstitutionCategorySeeder::class);
$this->fakeLegacyConnection();
$this->insertLegacyInstitution(['id' => 99, 'category' => 999]);
$this->artisan('v1:import')
->expectsOutputToContain('category 999')
->assertSuccessful();
}
public function test_it_fails_when_an_importer_throws(): void
{
// Connessione raggiungibile (getPdo ok) ma senza la tabella `institution`:
// l'import lancia, il comando intercetta e fallisce.
config()->set('database.connections.legacy', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
DB::purge('legacy');
$this->seed(InstitutionCategorySeeder::class);
$this->artisan('v1:import')
->expectsOutputToContain('failed')
->assertFailed();
}
public function test_it_fails_when_the_legacy_connection_is_unavailable(): void
{
config()->set('database.connections.legacy', [
'driver' => 'sqlite',
'database' => '/nonexistent/path/legacy.sqlite',
'prefix' => '',
]);
DB::purge('legacy');
$this->artisan('v1:import')
->expectsOutputToContain('Legacy connection unavailable')
->assertFailed();
}
}