OCRRecordResourceTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\OCRRecord;
  4. use App\Models\Student;
  5. use App\Models\Teacher;
  6. use App\Models\User;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Illuminate\Http\UploadedFile;
  9. use Illuminate\Support\Facades\Storage;
  10. use Tests\TestCase;
  11. class OCRRecordResourceTest extends TestCase
  12. {
  13. use RefreshDatabase;
  14. protected User $adminUser;
  15. protected Student $student;
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. // 创建管理员用户
  20. $this->adminUser = User::factory()->create([
  21. 'role' => 'admin',
  22. 'is_active' => true,
  23. ]);
  24. // 创建测试教师
  25. $teacher = Teacher::create([
  26. 'teacher_id' => 'teacher_test_001',
  27. 'name' => '测试教师',
  28. 'user_id' => $this->adminUser->id,
  29. ]);
  30. // 创建测试学生
  31. $this->student = Student::create([
  32. 'student_id' => 'stu_test_001',
  33. 'name' => '测试学生',
  34. 'grade' => '七年级',
  35. 'class_name' => '1班',
  36. 'teacher_id' => $teacher->teacher_id,
  37. ]);
  38. Storage::fake('public');
  39. }
  40. /** @test */
  41. public function it_can_list_ocr_records()
  42. {
  43. // 创建测试OCR记录
  44. OCRRecord::create([
  45. 'student_id' => $this->student->student_id,
  46. 'image_filename' => 'test.jpg',
  47. 'image_path' => 'ocr-uploads/test.jpg',
  48. 'status' => 'pending',
  49. 'total_questions' => 10,
  50. 'processed_questions' => 0,
  51. ]);
  52. $response = $this->actingAs($this->adminUser)
  53. ->get('/admin/ocr-records');
  54. $response->assertStatus(200);
  55. }
  56. /** @test */
  57. public function it_can_access_create_page()
  58. {
  59. $response = $this->actingAs($this->adminUser)
  60. ->get('/admin/ocr-records/create');
  61. $response->assertStatus(200);
  62. }
  63. /** @test */
  64. public function it_can_create_ocr_record()
  65. {
  66. $file = UploadedFile::fake()->image('exam.jpg', 800, 600);
  67. $response = $this->actingAs($this->adminUser)
  68. ->post('/admin/ocr-records', [
  69. 'student_id' => $this->student->student_id,
  70. 'image_path' => $file,
  71. 'status' => 'pending',
  72. 'total_questions' => 0,
  73. 'processed_questions' => 0,
  74. ]);
  75. $this->assertDatabaseHas('ocr_records', [
  76. 'student_id' => $this->student->student_id,
  77. 'status' => 'pending',
  78. ]);
  79. }
  80. /** @test */
  81. public function it_can_view_ocr_record()
  82. {
  83. $record = OCRRecord::create([
  84. 'student_id' => $this->student->student_id,
  85. 'image_filename' => 'test.jpg',
  86. 'image_path' => 'ocr-uploads/test.jpg',
  87. 'status' => 'completed',
  88. 'total_questions' => 10,
  89. 'processed_questions' => 10,
  90. 'confidence_avg' => 0.85,
  91. ]);
  92. $response = $this->actingAs($this->adminUser)
  93. ->get("/admin/ocr-records/{$record->id}");
  94. $response->assertStatus(200);
  95. }
  96. /** @test */
  97. public function it_shows_student_information_in_list()
  98. {
  99. OCRRecord::create([
  100. 'student_id' => $this->student->student_id,
  101. 'image_filename' => 'test.jpg',
  102. 'status' => 'pending',
  103. 'total_questions' => 10,
  104. ]);
  105. $response = $this->actingAs($this->adminUser)
  106. ->get('/admin/ocr-records');
  107. $response->assertStatus(200)
  108. ->assertSee($this->student->name)
  109. ->assertSee($this->student->grade)
  110. ->assertSee($this->student->class_name);
  111. }
  112. /** @test */
  113. public function it_requires_student_id()
  114. {
  115. $response = $this->actingAs($this->adminUser)
  116. ->post('/admin/ocr-records', [
  117. 'image_path' => UploadedFile::fake()->image('exam.jpg'),
  118. 'status' => 'pending',
  119. ]);
  120. $response->assertSessionHasErrors(['student_id']);
  121. }
  122. /** @test */
  123. public function it_requires_image_file()
  124. {
  125. $response = $this->actingAs($this->adminUser)
  126. ->post('/admin/ocr-records', [
  127. 'student_id' => $this->student->student_id,
  128. 'status' => 'pending',
  129. ]);
  130. $response->assertSessionHasErrors(['image_path']);
  131. }
  132. /** @test */
  133. public function it_calculates_progress_percentage_correctly()
  134. {
  135. $record = OCRRecord::create([
  136. 'student_id' => $this->student->student_id,
  137. 'image_filename' => 'test.jpg',
  138. 'status' => 'processing',
  139. 'total_questions' => 10,
  140. 'processed_questions' => 7,
  141. ]);
  142. $this->assertEquals(70, $record->progress_percentage);
  143. }
  144. /** @test */
  145. public function it_returns_zero_progress_when_no_questions()
  146. {
  147. $record = OCRRecord::create([
  148. 'student_id' => $this->student->student_id,
  149. 'image_filename' => 'test.jpg',
  150. 'status' => 'pending',
  151. 'total_questions' => 0,
  152. 'processed_questions' => 0,
  153. ]);
  154. $this->assertEquals(0, $record->progress_percentage);
  155. }
  156. /** @test */
  157. public function it_has_correct_student_relationship()
  158. {
  159. $record = OCRRecord::create([
  160. 'student_id' => $this->student->student_id,
  161. 'image_filename' => 'test.jpg',
  162. 'status' => 'pending',
  163. 'total_questions' => 10,
  164. ]);
  165. $this->assertInstanceOf(Student::class, $record->student);
  166. $this->assertEquals($this->student->student_id, $record->student->student_id);
  167. $this->assertEquals($this->student->name, $record->student->name);
  168. }
  169. }