90 lines
5.6 KiB
PHP
90 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Integration;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\EcommerceFinance\Services\GoodsProfitAfsLookupSchema;
|
|
use Modules\EcommerceFinance\Services\BusinessMirrorDatasetRegistry;
|
|
use Modules\EcommerceFinance\Services\BusinessMirrorSchemaManager;
|
|
use Tests\TestCase;
|
|
|
|
/** Run explicitly against the disposable MariaDB fixture, never a system DB. */
|
|
class GoodsProfitAfsLookupTest extends TestCase
|
|
{
|
|
public function test_online_lookup_preserves_parser_results_and_future_mirror_updates(): void
|
|
{
|
|
if (getenv('AFS_LOOKUP_TEST_HOST') !== 'caiwu_afs_generated_test') {
|
|
$this->markTestSkipped('Requires the isolated caiwu_afs_generated_test MariaDB fixture.');
|
|
}
|
|
config(['database.connections.afs_lookup_fixture' => [
|
|
'driver' => 'mysql', 'host' => 'caiwu_afs_generated_test', 'database' => 'qa_integration',
|
|
'username' => 'root', 'password' => '', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci',
|
|
'prefix' => '', 'strict' => true,
|
|
], 'upstream_mirrors.connection' => 'afs_lookup_fixture', 'system_database.mode' => 'shared']);
|
|
$db = DB::connection('afs_lookup_fixture');
|
|
$this->assertSame('qa_integration', $db->selectOne('SELECT DATABASE() AS name')->name);
|
|
$schema = Schema::connection('afs_lookup_fixture');
|
|
$schema->dropIfExists(GoodsProfitAfsLookupSchema::TABLE);
|
|
$schema->create(GoodsProfitAfsLookupSchema::TABLE, function (Blueprint $table): void {
|
|
$table->char('_mirror_key', 64)->primary();
|
|
$table->integer('id'); $table->string('undefined7', 50)->nullable();
|
|
$table->string('undefined8', 50)->nullable(); $table->string('amount', 50)->nullable();
|
|
});
|
|
try {
|
|
$rawDates = ['2026/8/23 14:58:51', '2026-10-19 23:59:59', '2026/10/20 0:0:0',
|
|
'2026-10-19', '2026/10/19 2:3:4', '2026/10/19 00:00:00.123',
|
|
'2026-10-19 01', '2026-10-19 01:02', '2026/2/31 00:00:00',
|
|
null, '', 'not a date', '2026/99/99 50:00:00'];
|
|
foreach ($rawDates as $id => $raw) {
|
|
$db->table(GoodsProfitAfsLookupSchema::TABLE)->insert([
|
|
'_mirror_key' => hash('sha256', (string) $id), 'id' => $id, 'undefined7' => $raw,
|
|
'undefined8' => 'fixture shop', 'amount' => '-31.20',
|
|
]);
|
|
}
|
|
$before = $db->table(GoodsProfitAfsLookupSchema::TABLE)->orderBy('id')->get()->toJson();
|
|
$migration = require base_path('database/migrations/2026_10_22_150000_index_goods_profit_afs_order_time.php');
|
|
$migration->up();
|
|
$migration->up();
|
|
$this->assertTrue($schema->hasIndex(GoodsProfitAfsLookupSchema::TABLE, 'idx_afs_order_time_id'));
|
|
$rows = $db->select('SELECT id, `_mirror_afs_order_time` AS indexed_date, '
|
|
. "CAST(STR_TO_DATE(REPLACE(`undefined7`, '/', '-'), '%Y-%m-%d %H:%i:%s') AS CHAR(19)) AS original_date "
|
|
. 'FROM `' . GoodsProfitAfsLookupSchema::TABLE . '` ORDER BY id');
|
|
foreach ($rows as $row) {
|
|
$this->assertSame($row->original_date, $row->indexed_date, 'date fixture ' . $row->id);
|
|
}
|
|
$db->table(GoodsProfitAfsLookupSchema::TABLE)->where('id', 0)->update(['undefined7' => '2026/10/19 1:2:3']);
|
|
$this->assertSame('2026-10-19 01:02:03', $db->table(GoodsProfitAfsLookupSchema::TABLE)->where('id', 0)->value('_mirror_afs_order_time'));
|
|
$db->table(GoodsProfitAfsLookupSchema::TABLE)->where('id', 0)->update(['undefined7' => 'bad timestamp']);
|
|
$this->assertNull($db->table(GoodsProfitAfsLookupSchema::TABLE)->where('id', 0)->value('_mirror_afs_order_time'));
|
|
$db->table(GoodsProfitAfsLookupSchema::TABLE)->where('id', 0)->update(['undefined7' => $rawDates[0]]);
|
|
$migration->down();
|
|
$this->assertSame($before, $db->table(GoodsProfitAfsLookupSchema::TABLE)->orderBy('id')->get()->toJson());
|
|
|
|
// The same indexes must also exist when a new mirror is created
|
|
// after this migration has already been recorded.
|
|
$schema->drop(GoodsProfitAfsLookupSchema::TABLE);
|
|
config(['upstream_mirrors.datasets' => ['fixture.rpt.afs' => [
|
|
'source_key' => 'fixture', 'schema' => 'rpt', 'object' => 'afs',
|
|
'target' => GoodsProfitAfsLookupSchema::TABLE, 'keys' => ['id'], 'watermark' => null,
|
|
]]]);
|
|
foreach (['id' => 'int', 'undefined7' => 'nvarchar', 'undefined8' => 'nvarchar'] as $column => $type) {
|
|
DB::table('upstream_schema_columns')->insert([
|
|
'source_key' => 'fixture', 'schema_name' => 'rpt', 'object_name' => 'afs', 'object_type' => 'table',
|
|
'column_name' => $column, 'ordinal_position' => $column === 'id' ? 1 : (int) substr($column, 9),
|
|
'data_type' => $type, 'max_length' => 50, 'is_nullable' => true, 'synchronized_at' => now(),
|
|
'created_at' => now(), 'updated_at' => now(),
|
|
]);
|
|
}
|
|
$dataset = (new BusinessMirrorDatasetRegistry())->get('fixture.rpt.afs');
|
|
app(BusinessMirrorSchemaManager::class)->ensure($dataset);
|
|
app(BusinessMirrorSchemaManager::class)->ensure($dataset);
|
|
$this->assertTrue($schema->hasIndex(GoodsProfitAfsLookupSchema::TABLE, 'idx_afs_shop_order_time_id'));
|
|
} finally {
|
|
$schema->dropIfExists(GoodsProfitAfsLookupSchema::TABLE);
|
|
DB::purge('afs_lookup_fixture');
|
|
}
|
|
}
|
|
}
|