Files
project-replica/tests/Feature/ModelsTest.php
2026-03-23 00:51:38 +09:00

140 lines
3.8 KiB
PHP

<?php
use App\Models\SourceDatabase;
use App\Models\TargetDatabase;
use App\Models\Table;
use App\Models\Column;
use App\Models\MigrationSchedule;
use App\Models\MigrationRun;
beforeEach(function () {
// Clean up before each test
MigrationRun::truncate();
MigrationSchedule::truncate();
Table::truncate();
Column::truncate();
TargetDatabase::truncate();
SourceDatabase::truncate();
});
it('can create a source database', function () {
$sourceDb = SourceDatabase::factory()->create([
'name' => 'Test Source DB',
]);
expect($sourceDb)->toBeInstanceOf(SourceDatabase::class);
expect($sourceDb->name)->toBe('Test Source DB');
expect($sourceDb->is_active)->toBeTrue();
});
it('can create a target database', function () {
$targetDb = TargetDatabase::factory()->create([
'name' => 'Test Target DB',
]);
expect($targetDb)->toBeInstanceOf(TargetDatabase::class);
expect($targetDb->name)->toBe('Test Target DB');
});
it('can create a table with columns', function () {
$sourceDb = SourceDatabase::factory()->create();
$table = Table::create([
'source_database_id' => $sourceDb->id,
'schema_name' => 'public',
'table_name' => 'users',
'comment' => 'User table',
]);
$column = Column::create([
'table_id' => $table->id,
'column_name' => 'id',
'data_type' => 'integer',
'ordinal_position' => 1,
'is_nullable' => false,
'is_primary_key' => true,
]);
expect($table)->toBeInstanceOf(Table::class);
expect($table->columns)->toHaveCount(1);
expect($column->is_primary_key)->toBeTrue();
});
it('can create a migration schedule', function () {
$sourceDb = SourceDatabase::factory()->create();
$targetDb = TargetDatabase::factory()->create();
$schedule = MigrationSchedule::create([
'name' => 'Daily User Sync',
'source_database_id' => $sourceDb->id,
'target_database_id' => $targetDb->id,
'tables' => [1, 2, 3],
'cron_expression' => '0 0 * * *',
'timezone' => 'UTC',
'is_active' => true,
'batch_size' => 1000,
]);
expect($schedule)->toBeInstanceOf(MigrationSchedule::class);
expect($schedule->is_active)->toBeTrue();
expect($schedule->cron_expression)->toBe('0 0 * * *');
});
it('can create a migration run', function () {
$schedule = MigrationSchedule::factory()->create();
$run = MigrationRun::create([
'schedule_id' => $schedule->id,
'status' => 'pending',
'total_tables' => 5,
]);
expect($run)->toBeInstanceOf(MigrationRun::class);
expect($run->status)->toBe('pending');
expect($run->progress)->toBe(0.0);
});
it('calculates migration progress correctly', function () {
$schedule = MigrationSchedule::factory()->create();
$run = MigrationRun::create([
'schedule_id' => $schedule->id,
'status' => 'running',
'total_tables' => 10,
'processed_tables' => 5,
]);
expect($run->progress)->toBe(50.0);
});
it('has many tables relationship', function () {
$sourceDb = SourceDatabase::factory()->create();
Table::create([
'source_database_id' => $sourceDb->id,
'schema_name' => 'public',
'table_name' => 'users',
]);
Table::create([
'source_database_id' => $sourceDb->id,
'schema_name' => 'public',
'table_name' => 'posts',
]);
expect($sourceDb->tables)->toHaveCount(2);
});
it('belongs to source database', function () {
$sourceDb = SourceDatabase::factory()->create();
$table = Table::create([
'source_database_id' => $sourceDb->id,
'schema_name' => 'public',
'table_name' => 'users',
]);
expect($table->sourceDatabase)->toBeInstanceOf(SourceDatabase::class);
expect($table->sourceDatabase->id)->toBe($sourceDb->id);
});