| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- <?php
- namespace Tests\Feature\Services;
- use App\Models\Channel;
- use App\Services\PacketService;
- use App\Services\SentenceService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- use Tests\TestCase;
- use ZipArchive;
- use App\Http\Api\ChannelApi;
- /**
- * PacketService单元测试
- *
- * 测试PacketService的数据导出和打包功能
- */
- class PacketServiceTest extends TestCase
- {
- use RefreshDatabase;
- /**
- * 巴利文channel
- */
- private Channel $paliChannel;
- /**
- * 译文channel
- */
- private Channel $translationChannel;
- /**
- * 测试用的editor_uid
- */
- private string $editorUid;
- /**
- * SentenceService实例
- */
- private SentenceService $sentenceService;
- /**
- * 设置测试环境
- *
- * @return void
- */
- protected function setUp(): void
- {
- parent::setUp();
- // 调试:检查数据库连接
- dump(config('database.default')); // 应该是 pgsql
- dump(config('database.connections.pgsql.database')); // 应该是 mint_test
- // 生成测试用的editor_uid
- $this->editorUid = Str::uuid()->toString();
- // 初始化SentenceService
- $this->sentenceService = app(SentenceService::class);
- // 创建测试用的channels
- $orgChannelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
- $this->paliChannel = Channel::find($orgChannelId);
- $this->translationChannel = Channel::find('00ae2c48-c204-4082-ae79-79ba2740d506');
- }
- /**
- * 清理测试环境
- *
- * @return void
- */
- protected function tearDown(): void
- {
- // 清理生成的文件
- Storage::deleteDirectory('packet');
- Storage::deleteDirectory('temp');
- parent::tearDown();
- }
- /**
- * 测试基本的导出功能
- *
- * @return void
- */
- public function test_export_creates_zip_file(): void
- {
- // 创建测试数据
- $this->createTestSentences();
- // 执行导出
- $service = new PacketService(
- $this->paliChannel->uid,
- [$this->translationChannel->uid]
- );
- $zipPath = $service->export();
- // 断言ZIP文件已创建
- $this->assertTrue(Storage::exists($zipPath));
- $this->assertStringStartsWith('packet/training_data_', $zipPath);
- $this->assertStringEndsWith('.zip', $zipPath);
- }
- /**
- * 测试ZIP文件内容结构
- *
- * @return void
- */
- public function test_zip_contains_correct_structure(): void
- {
- // 创建测试数据
- $this->createTestSentences();
- // 执行导出
- $service = new PacketService(
- $this->paliChannel->uid,
- [$this->translationChannel->uid]
- );
- $zipPath = $service->export();
- $fullPath = Storage::path($zipPath);
- // 打开ZIP文件检查内容
- $zip = new ZipArchive();
- $this->assertTrue($zip->open($fullPath));
- // 检查是否包含translations目录
- $expectedFile = 'translations/Chinese Translation.jsonl';
- $this->assertNotFalse($zip->locateName($expectedFile));
- $zip->close();
- }
- /**
- * 测试JSONL文件内容格式
- *
- * @return void
- */
- public function test_jsonl_file_format(): void
- {
- // 创建测试数据
- $this->createTestSentences();
- // 执行导出
- $service = new PacketService(
- $this->paliChannel->uid,
- [$this->translationChannel->uid]
- );
- $zipPath = $service->export();
- $fullPath = Storage::path($zipPath);
- // 解压并读取JSONL文件
- $zip = new ZipArchive();
- $zip->open($fullPath);
- $jsonlContent = $zip->getFromName('translations/Chinese Translation.jsonl');
- $zip->close();
- // 解析JSONL内容
- $lines = explode("\n", trim($jsonlContent));
- $this->assertGreaterThan(0, count($lines));
- // 检查第一行的格式
- $firstLine = json_decode($lines[0], true);
- $this->assertArrayHasKey('id', $firstLine);
- $this->assertArrayHasKey('pali', $firstLine);
- $this->assertArrayHasKey('translation', $firstLine);
- // 检查ID格式
- $this->assertMatchesRegularExpression('/^\d+-\d+-\d+-\d+$/', $firstLine['id']);
- }
- /**
- * 测试数据排序
- *
- * @return void
- */
- public function test_data_is_sorted_correctly(): void
- {
- // 创建乱序的测试数据
- $this->sentenceService->save([
- 'book_id' => 2,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Pali text 2',
- 'channel_uid' => $this->paliChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 2,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Translation 2',
- 'channel_uid' => $this->translationChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Pali text 1',
- 'channel_uid' => $this->paliChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Translation 1',
- 'channel_uid' => $this->translationChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- // 执行导出
- $service = new PacketService(
- $this->paliChannel->uid,
- [$this->translationChannel->uid]
- );
- $zipPath = $service->export();
- $fullPath = Storage::path($zipPath);
- // 读取JSONL内容
- $zip = new ZipArchive();
- $zip->open($fullPath);
- $jsonlContent = $zip->getFromName('translations/Chinese Translation.jsonl');
- $zip->close();
- $lines = explode("\n", trim($jsonlContent));
- $firstLine = json_decode($lines[0], true);
- $secondLine = json_decode($lines[1], true);
- // 第一条应该是book_id=1的记录
- $this->assertEquals('1-1-1-5', $firstLine['id']);
- $this->assertEquals('2-1-1-5', $secondLine['id']);
- }
- /**
- * 测试跳过空译文
- *
- * @return void
- */
- public function test_skips_empty_translations(): void
- {
- // 创建有空译文的测试数据
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Pali text',
- 'channel_uid' => $this->paliChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => '', // 空译文
- 'channel_uid' => $this->translationChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 2,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Pali text 2',
- 'channel_uid' => $this->paliChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 2,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Valid translation',
- 'channel_uid' => $this->translationChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- // 执行导出
- $service = new PacketService(
- $this->paliChannel->uid,
- [$this->translationChannel->uid]
- );
- $zipPath = $service->export();
- $fullPath = Storage::path($zipPath);
- // 读取JSONL内容
- $zip = new ZipArchive();
- $zip->open($fullPath);
- $jsonlContent = $zip->getFromName('translations/Chinese Translation.jsonl');
- $zip->close();
- $lines = array_filter(explode("\n", trim($jsonlContent)));
- // 应该只有一条记录(跳过了空译文)
- $this->assertCount(1, $lines);
- }
- /**
- * 测试多个译文版本
- *
- * @return void
- */
- public function test_multiple_translation_channels(): void
- {
- // 创建第二个译文channel
- $secondTranslation = Channel::create([
- 'uid' => 'translation-2-test-uid',
- 'name' => 'English Translation',
- 'lang' => 'en',
- ]);
- // 创建测试数据
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Pali text',
- 'channel_uid' => $this->paliChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Chinese translation',
- 'channel_uid' => $this->translationChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'English translation',
- 'channel_uid' => $secondTranslation->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- // 执行导出
- $service = new PacketService(
- $this->paliChannel->uid,
- [
- $this->translationChannel->uid,
- $secondTranslation->uid
- ]
- );
- $zipPath = $service->export();
- $fullPath = Storage::path($zipPath);
- // 检查ZIP包含两个文件
- $zip = new ZipArchive();
- $zip->open($fullPath);
- $this->assertNotFalse($zip->locateName('translations/Chinese Translation.jsonl'));
- $this->assertNotFalse($zip->locateName('translations/English Translation.jsonl'));
- $zip->close();
- }
- /**
- * 测试channel不存在时使用uid作为文件名
- *
- * @return void
- */
- public function test_uses_uid_when_channel_not_found(): void
- {
- // 创建测试数据,使用不存在的channel_uid
- $nonExistentUid = 'non-existent-uid';
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Pali text',
- 'channel_uid' => $this->paliChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Translation',
- 'channel_uid' => $nonExistentUid,
- 'editor_uid' => $this->editorUid,
- ]);
- // 执行导出
- $service = new PacketService(
- $this->paliChannel->uid,
- [$nonExistentUid]
- );
- $zipPath = $service->export();
- $fullPath = Storage::path($zipPath);
- // 检查使用uid作为文件名
- $zip = new ZipArchive();
- $zip->open($fullPath);
- $expectedFile = "translations/{$nonExistentUid}.jsonl";
- $this->assertNotFalse($zip->locateName($expectedFile));
- $zip->close();
- }
- /**
- * 测试临时文件清理
- *
- * @return void
- */
- public function test_cleanup_temp_files(): void
- {
- // 创建测试数据
- $this->createTestSentences();
- // 执行导出
- $service = new PacketService(
- $this->paliChannel->uid,
- [$this->translationChannel->uid]
- );
- $service->export();
- // 检查临时目录已被清理
- $tempPath = storage_path('app/temp/packet');
- $this->assertDirectoryDoesNotExist($tempPath);
- }
- /**
- * 创建基础测试数据
- *
- * @return void
- */
- private function createTestSentences(): void
- {
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Pali sentence content',
- 'channel_uid' => $this->paliChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- $this->sentenceService->save([
- 'book_id' => 1,
- 'paragraph' => 1,
- 'word_start' => 1,
- 'word_end' => 5,
- 'content' => 'Chinese translation content',
- 'channel_uid' => $this->translationChannel->uid,
- 'editor_uid' => $this->editorUid,
- ]);
- }
- }
|