Explorar el Código

feat(search): search-pali-wbw ref 的 type 映射为缩写 (PTS/My/VRI/Thai/Other) 并补充全类型测试

visuddhinanda hace 3 días
padre
commit
71e31f5c92

+ 15 - 2
api-v13/app/Http/Resources/SearchPaliWbwResource.php

@@ -11,6 +11,19 @@ use Illuminate\Http\Resources\Json\JsonResource;
 
 class SearchPaliWbwResource extends JsonResource
 {
+    /**
+     * page_numbers.type 单字母代号到缩写/名称的映射。
+     *
+     * @var array<string, string>
+     */
+    private const TYPE_ABBR = [
+        'M' => 'My',
+        'P' => 'PTS',
+        'V' => 'VRI',
+        'T' => 'Thai',
+        'O' => 'Other',
+    ];
+
     /**
      * Transform the resource into an array.
      *
@@ -71,7 +84,7 @@ class SearchPaliWbwResource extends JsonResource
             ->get()
             ->unique('type')
             ->map(fn ($pageNumber) => [
-                'type' => $pageNumber->type,
+                'type' => self::TYPE_ABBR[$pageNumber->type] ?? $pageNumber->type,
                 'page' => $pageNumber->page,
                 'title' => match ($pageNumber->type) {
                     'M' => $series['abbr_my'] ?? null,
@@ -83,7 +96,7 @@ class SearchPaliWbwResource extends JsonResource
             ->all();
 
         $ref[] = [
-            'type' => 'wp',
+            'type' => 'WP',
             'page' => $this->paragraph,
             'title' => $series['abbr_wp'] ?? null,
         ];

+ 26 - 23
api-v13/tests/Feature/SearchPaliWbwRefTest.php

@@ -5,8 +5,9 @@
  *
  * 每个 (book, paragraph) 在 page_numbers 里可能因为 wid 不同而有多行,
  * 输出时每个 type 只保留 wid 最小的那一行,并且暴露 type / page / title 三个字段;
+ * type 把单字母代号映射成缩写(M→My、P→PTS、V→VRI、T→Thai、O→Other),
  * title 是书缩写:type 为 M 时取 abbr_my、为 P 时取 abbr_pts。
- * 另外恒有一条 type='wp' 的条目,page 为段落号、title 为 abbr_wp。
+ * 另外恒有一条 type='WP' 的条目,page 为段落号、title 为 abbr_wp。
  */
 use Illuminate\Foundation\Testing\RefreshDatabase;
 use Illuminate\Support\Facades\DB;
@@ -66,34 +67,35 @@ function pageNumberRow(string $type, int $wid, int $page, int $paragraph = 1): a
 }
 
 it('adds ref with the smallest wid page for each type', function () {
-    DB::table('wbw_templates')->insert([wbwRow()]);
-    DB::table('pali_texts')->insert([paliTextRow()]);
+    DB::table('wbw_templates')->insert([wbwRow(10)]);
+    DB::table('pali_texts')->insert([paliTextRow(10)]);
     DB::table('page_numbers')->insert([
-        pageNumberRow('a', 5, 111),
-        pageNumberRow('a', 2, 222),
-        pageNumberRow('a', 9, 333),
-        pageNumberRow('b', 4, 444),
-        pageNumberRow('b', 1, 555),
+        pageNumberRow('M', 5, 111, 10),
+        pageNumberRow('M', 2, 222, 10),
+        pageNumberRow('M', 9, 333, 10),
+        pageNumberRow('P', 4, 444, 10),
+        pageNumberRow('P', 1, 555, 10),
     ]);
 
     $response = $this->getJson('/api/v2/search-pali-wbw?key=dhammo')
         ->assertOk();
 
-    $ref = $response->json('data.rows.0.ref');
-    $byType = collect($ref)->keyBy('type');
+    $byType = collect($response->json('data.rows.0.ref'))->keyBy('type');
 
-    expect($byType->get('a'))->toBe(['type' => 'a', 'page' => 222, 'title' => null])
-        ->and($byType->get('b'))->toBe(['type' => 'b', 'page' => 555, 'title' => null])
-        ->and($byType->get('wp'))->toBe(['type' => 'wp', 'page' => 1, 'title' => null]);
+    expect($byType->get('My'))->toBe(['type' => 'My', 'page' => 222, 'title' => 'namakkāra'])
+        ->and($byType->get('PTS'))->toBe(['type' => 'PTS', 'page' => 555, 'title' => 'Nam'])
+        ->and($byType->get('WP'))->toBe(['type' => 'WP', 'page' => 10, 'title' => 'namakkāra.']);
 });
 
-it('adds the series abbreviation as title for M and P types', function () {
+it('maps all page number types to their abbreviations', function () {
     DB::table('wbw_templates')->insert([wbwRow(10)]);
     DB::table('pali_texts')->insert([paliTextRow(10)]);
     DB::table('page_numbers')->insert([
-        pageNumberRow('M', 1, 111, 10),
-        pageNumberRow('P', 2, 222, 10),
-        pageNumberRow('V', 3, 333, 10),
+        pageNumberRow('P', 1, 101, 10),
+        pageNumberRow('M', 2, 102, 10),
+        pageNumberRow('V', 3, 103, 10),
+        pageNumberRow('T', 4, 104, 10),
+        pageNumberRow('O', 5, 105, 10),
     ]);
 
     $response = $this->getJson('/api/v2/search-pali-wbw?key=dhammo')
@@ -101,13 +103,14 @@ it('adds the series abbreviation as title for M and P types', function () {
 
     $byType = collect($response->json('data.rows.0.ref'))->keyBy('type');
 
-    expect($byType->get('M'))->toBe(['type' => 'M', 'page' => 111, 'title' => 'namakkāra'])
-        ->and($byType->get('P'))->toBe(['type' => 'P', 'page' => 222, 'title' => 'Nam'])
-        ->and($byType->get('V'))->toBe(['type' => 'V', 'page' => 333, 'title' => null])
-        ->and($byType->get('wp'))->toBe(['type' => 'wp', 'page' => 10, 'title' => 'namakkāra.']);
+    expect($byType->get('PTS'))->toBe(['type' => 'PTS', 'page' => 101, 'title' => 'Nam'])
+        ->and($byType->get('My'))->toBe(['type' => 'My', 'page' => 102, 'title' => 'namakkāra'])
+        ->and($byType->get('VRI'))->toBe(['type' => 'VRI', 'page' => 103, 'title' => null])
+        ->and($byType->get('Thai'))->toBe(['type' => 'Thai', 'page' => 104, 'title' => null])
+        ->and($byType->get('Other'))->toBe(['type' => 'Other', 'page' => 105, 'title' => null]);
 });
 
-it('adds the wp entry even when there are no page_numbers rows', function () {
+it('adds the WP entry even when there are no page_numbers rows', function () {
     DB::table('wbw_templates')->insert([wbwRow(10)]);
     DB::table('pali_texts')->insert([paliTextRow(10)]);
 
@@ -115,6 +118,6 @@ it('adds the wp entry even when there are no page_numbers rows', function () {
         ->assertOk();
 
     expect($response->json('data.rows.0.ref'))->toBe([
-        ['type' => 'wp', 'page' => 10, 'title' => 'namakkāra.'],
+        ['type' => 'WP', 'page' => 10, 'title' => 'namakkāra.'],
     ]);
 });