QuestionBankOcrGenerationTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Tests\Feature;
  3. use Tests\TestCase;
  4. use App\Services\QuestionBankService;
  5. use Illuminate\Support\Facades\DB;
  6. class QuestionBankOcrGenerationTest extends TestCase
  7. {
  8. /**
  9. * 不使用数据库事务,使用真实数据库
  10. */
  11. protected $connectionsToTransact = [];
  12. /**
  13. * 测试从MySQL OCR数据生成题库
  14. *
  15. * 此测试使用MySQL中已OCR识别的题目,避免重复调用第三方OCR接口
  16. */
  17. public function test_generate_questions_from_mysql_ocr_data()
  18. {
  19. // 强制使用MySQL连接
  20. config(['database.default' => 'mysql']);
  21. echo "\n" . str_repeat("=", 80) . "\n";
  22. echo "🧪 OCR题目生成题库测试 (使用MySQL已有数据)\n";
  23. echo str_repeat("=", 80) . "\n\n";
  24. // 1. 从MySQL读取OCR识别的题目
  25. $recordId = 12; // 使用已有的OCR记录
  26. echo "📚 从MySQL读取OCR记录 #{$recordId} 的题目...\n";
  27. $ocrQuestions = DB::connection('mysql')
  28. ->table('ocr_question_results')
  29. ->where('ocr_record_id', $recordId)
  30. ->orderBy('question_number')
  31. ->get();
  32. if ($ocrQuestions->isEmpty()) {
  33. echo "❌ 未找到OCR记录 {$recordId} 的题目数据\n";
  34. $this->markTestSkipped("No OCR data found for record {$recordId}");
  35. return;
  36. }
  37. echo "✅ 获取到 {$ocrQuestions->count()} 道题目\n\n";
  38. // 2. 构建请求数据(只需要题目文本,不需要学生答案)
  39. $questions = [];
  40. foreach ($ocrQuestions as $oq) {
  41. $questions[] = [
  42. 'question_number' => $oq->question_number,
  43. 'question_text' => $oq->question_text ?? ''
  44. ];
  45. echo " 题目 {$oq->question_number}: " . mb_substr($oq->question_text ?? '', 0, 50) . "...\n";
  46. }
  47. echo "\n";
  48. // 3. 调用QuestionBankService生成题目
  49. echo "🤖 调用题库服务生成题目...\n";
  50. echo "API地址: " . config('services.question_bank.base_url', 'http://localhost:5015') . "\n\n";
  51. $service = app(QuestionBankService::class);
  52. $result = $service->generateQuestionsFromOcr($questions, '高一', '数学');
  53. // 4. 验证结果
  54. echo str_repeat("=", 80) . "\n";
  55. echo "📊 分析结果\n";
  56. echo str_repeat("=", 80) . "\n\n";
  57. // 输出完整响应用于调试
  58. echo "API返回数据:\n";
  59. echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n\n";
  60. $this->assertIsArray($result, "返回结果应该是数组");
  61. $this->assertEquals('success', $result['status'] ?? '', "生成应该成功");
  62. if ($result['status'] !== 'success') {
  63. echo "❌ 生成失败: " . ($result['message'] ?? 'Unknown error') . "\n";
  64. $this->fail("OCR题目生成失败: " . ($result['message'] ?? 'Unknown error'));
  65. return;
  66. }
  67. echo "✅ 生成成功\n";
  68. echo "使用模型: " . ($result['model_used'] ?? 'unknown') . "\n";
  69. echo "生成数量: " . ($result['generated_count'] ?? 0) . "/" . ($result['total_count'] ?? 0) . "\n\n";
  70. $generatedQuestions = $result['questions'] ?? [];
  71. $this->assertNotEmpty($generatedQuestions, "应该返回生成的题目");
  72. // 5. 验证每道生成的题目
  73. foreach ($generatedQuestions as $index => $generated) {
  74. echo str_repeat("-", 60) . "\n";
  75. echo "题目 " . ($index + 1) . "\n";
  76. echo str_repeat("-", 60) . "\n";
  77. // 验证必要字段
  78. $this->assertArrayHasKey('question_id', $generated, "题目应该有题库ID");
  79. $this->assertArrayHasKey('question_type', $generated, "题目应该有题型");
  80. $this->assertArrayHasKey('kp_code', $generated, "题目应该有知识点");
  81. $this->assertArrayHasKey('saved', $generated, "题目应该有保存状态");
  82. // 显示生成结果
  83. echo "📝 题目ID: " . ($generated['question_id'] ?? 'N/A') . "\n";
  84. echo "📊 题型: " . ($generated['question_type'] ?? 'N/A') . "\n";
  85. echo "🎯 知识点: " . ($generated['kp_code'] ?? 'N/A') . "\n";
  86. echo " 难度: " . ($generated['difficulty'] ?? 'N/A') . "\n";
  87. echo "💾 已保存: " . ($generated['saved'] ? '是' : '否') . "\n\n";
  88. // 验证已保存
  89. $this->assertTrue($generated['saved'] ?? false, "题目应该已保存到题库");
  90. }
  91. // 6. 验证题目已保存到QuestionBankService数据库
  92. echo str_repeat("=", 80) . "\n";
  93. echo "🔍 验证题目已保存到题库\n";
  94. echo str_repeat("=", 80) . "\n\n";
  95. $savedCount = 0;
  96. foreach ($generatedQuestions as $generated) {
  97. if ($generated['saved'] ?? false) {
  98. $savedCount++;
  99. }
  100. }
  101. echo "✅ 成功保存 {$savedCount}/{$result['generated_count']} 道题目到题库\n";
  102. $this->assertEquals($result['generated_count'], $savedCount, "所有生成的题目都应该已保存");
  103. echo str_repeat("=", 80) . "\n";
  104. echo "✅ 测试完成\n";
  105. echo str_repeat("=", 80) . "\n\n";
  106. // 最终断言
  107. $this->assertTrue(true, "OCR题目生成题库测试通过");
  108. }
  109. /**
  110. * 测试QuestionBankService健康检查
  111. */
  112. public function test_question_bank_service_health()
  113. {
  114. echo "\n🏥 检查题库服务健康状态...\n";
  115. $service = app(QuestionBankService::class);
  116. $isHealthy = $service->checkHealth();
  117. $this->assertTrue($isHealthy, "题库服务应该健康");
  118. echo "✅ 题库服务运行正常\n\n";
  119. }
  120. }