OCRRecordModelTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Models\OCRRecord;
  4. use App\Models\OCRQuestionResult;
  5. use App\Models\Student;
  6. use App\Models\Teacher;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Tests\TestCase;
  9. class OCRRecordModelTest extends TestCase
  10. {
  11. use RefreshDatabase;
  12. protected Student $student;
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $teacher = Teacher::create([
  17. 'teacher_id' => 'teacher_001',
  18. 'name' => '测试教师',
  19. ]);
  20. $this->student = Student::create([
  21. 'student_id' => 'stu_001',
  22. 'name' => '张三',
  23. 'grade' => '七年级',
  24. 'class_name' => '1班',
  25. 'teacher_id' => $teacher->teacher_id,
  26. ]);
  27. }
  28. /** @test */
  29. public function it_can_create_ocr_record()
  30. {
  31. $record = OCRRecord::create([
  32. 'student_id' => $this->student->student_id,
  33. 'image_filename' => 'test_exam.jpg',
  34. 'image_path' => 'uploads/test_exam.jpg',
  35. 'status' => 'pending',
  36. 'total_questions' => 20,
  37. 'processed_questions' => 0,
  38. ]);
  39. $this->assertInstanceOf(OCRRecord::class, $record);
  40. $this->assertDatabaseHas('ocr_records', [
  41. 'student_id' => $this->student->student_id,
  42. 'image_filename' => 'test_exam.jpg',
  43. 'status' => 'pending',
  44. ]);
  45. }
  46. /** @test */
  47. public function it_has_student_relationship()
  48. {
  49. $record = OCRRecord::create([
  50. 'student_id' => $this->student->student_id,
  51. 'image_filename' => 'test.jpg',
  52. 'status' => 'pending',
  53. 'total_questions' => 10,
  54. ]);
  55. $this->assertNotNull($record->student);
  56. $this->assertEquals($this->student->name, $record->student->name);
  57. }
  58. /** @test */
  59. public function it_has_questions_relationship()
  60. {
  61. $record = OCRRecord::create([
  62. 'student_id' => $this->student->student_id,
  63. 'image_filename' => 'test.jpg',
  64. 'status' => 'processing',
  65. 'total_questions' => 2,
  66. ]);
  67. OCRQuestionResult::create([
  68. 'ocr_record_id' => $record->id,
  69. 'question_number' => 1,
  70. 'kp_code' => 'KP001',
  71. 'score_value' => 5,
  72. ]);
  73. OCRQuestionResult::create([
  74. 'ocr_record_id' => $record->id,
  75. 'question_number' => 2,
  76. 'kp_code' => 'KP002',
  77. 'score_value' => 10,
  78. ]);
  79. $this->assertCount(2, $record->questions);
  80. }
  81. /** @test */
  82. public function it_casts_qr_code_data_to_array()
  83. {
  84. $qrData = ['exam_id' => 'EXAM001', 'paper_code' => 'PAPER123'];
  85. $record = OCRRecord::create([
  86. 'student_id' => $this->student->student_id,
  87. 'image_filename' => 'test.jpg',
  88. 'status' => 'pending',
  89. 'total_questions' => 10,
  90. 'qr_code_data' => json_encode($qrData),
  91. ]);
  92. $this->assertIsArray($record->qr_code_data);
  93. $this->assertEquals($qrData, $record->qr_code_data);
  94. }
  95. /** @test */
  96. public function it_calculates_progress_percentage()
  97. {
  98. $record = OCRRecord::create([
  99. 'student_id' => $this->student->student_id,
  100. 'image_filename' => 'test.jpg',
  101. 'status' => 'processing',
  102. 'total_questions' => 20,
  103. 'processed_questions' => 15,
  104. ]);
  105. $this->assertEquals(75, $record->progress_percentage);
  106. }
  107. /** @test */
  108. public function it_returns_zero_progress_when_total_is_zero()
  109. {
  110. $record = OCRRecord::create([
  111. 'student_id' => $this->student->student_id,
  112. 'image_filename' => 'test.jpg',
  113. 'status' => 'pending',
  114. 'total_questions' => 0,
  115. 'processed_questions' => 0,
  116. ]);
  117. $this->assertEquals(0, $record->progress_percentage);
  118. }
  119. /** @test */
  120. public function it_can_update_status()
  121. {
  122. $record = OCRRecord::create([
  123. 'student_id' => $this->student->student_id,
  124. 'image_filename' => 'test.jpg',
  125. 'status' => 'pending',
  126. 'total_questions' => 10,
  127. ]);
  128. $record->update(['status' => 'completed']);
  129. $this->assertEquals('completed', $record->fresh()->status);
  130. }
  131. /** @test */
  132. public function it_can_update_confidence_average()
  133. {
  134. $record = OCRRecord::create([
  135. 'student_id' => $this->student->student_id,
  136. 'image_filename' => 'test.jpg',
  137. 'status' => 'processing',
  138. 'total_questions' => 10,
  139. 'confidence_avg' => null,
  140. ]);
  141. $record->update(['confidence_avg' => 0.8523]);
  142. $this->assertEquals(0.8523, $record->fresh()->confidence_avg);
  143. }
  144. /** @test */
  145. public function it_stores_processed_at_timestamp()
  146. {
  147. $now = now();
  148. $record = OCRRecord::create([
  149. 'student_id' => $this->student->student_id,
  150. 'image_filename' => 'test.jpg',
  151. 'status' => 'completed',
  152. 'total_questions' => 10,
  153. 'processed_questions' => 10,
  154. 'processed_at' => $now,
  155. ]);
  156. $this->assertNotNull($record->processed_at);
  157. $this->assertEquals($now->format('Y-m-d H:i:s'), $record->processed_at->format('Y-m-d H:i:s'));
  158. }
  159. /** @test */
  160. public function it_can_store_error_message()
  161. {
  162. $record = OCRRecord::create([
  163. 'student_id' => $this->student->student_id,
  164. 'image_filename' => 'test.jpg',
  165. 'status' => 'failed',
  166. 'total_questions' => 0,
  167. 'error_message' => 'OCR API timeout',
  168. ]);
  169. $this->assertEquals('OCR API timeout', $record->error_message);
  170. }
  171. /** @test */
  172. public function it_deletes_related_questions_on_cascade()
  173. {
  174. $record = OCRRecord::create([
  175. 'student_id' => $this->student->student_id,
  176. 'image_filename' => 'test.jpg',
  177. 'status' => 'completed',
  178. 'total_questions' => 1,
  179. ]);
  180. $question = OCRQuestionResult::create([
  181. 'ocr_record_id' => $record->id,
  182. 'question_number' => 1,
  183. 'kp_code' => 'KP001',
  184. ]);
  185. $questionId = $question->id;
  186. $record->delete();
  187. $this->assertDatabaseMissing('ocr_question_results', ['id' => $questionId]);
  188. }
  189. }