TipitakaPageNumberV3Test.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. use App\Models\BookTitle;
  3. use App\Models\PageNumber;
  4. use App\Models\PaliText;
  5. use App\Models\Tag;
  6. use App\Models\TagMap;
  7. use App\Models\WbwTemplate;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Str;
  11. uses(RefreshDatabase::class);
  12. /*
  13. * GET /v3/tipitaka-page-numbers/{page}:按印本页码 / CS 段落号找段落(取代 v2 search-page-number)。
  14. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  15. */
  16. function pageMark(string $type, int $volume, int $page, int $book, int $para, int $bookId): void
  17. {
  18. PageNumber::forceCreate([
  19. 'type' => $type, 'volume' => $volume, 'page' => $page, 'book' => $book,
  20. 'paragraph' => $para, 'wid' => 1, 'pcd_book_id' => $bookId,
  21. ]);
  22. }
  23. /**
  24. * @param list<string> $tags
  25. */
  26. function pageBook(int $bookId, int $book, string $toc, array $tags = []): void
  27. {
  28. BookTitle::forceCreate(['sn' => $bookId, 'book' => $book, 'paragraph' => 3, 'title' => $toc]);
  29. $head = PaliText::forceCreate([
  30. 'book' => $book, 'paragraph' => 3, 'level' => 1, 'class' => '', 'toc' => $toc,
  31. 'text' => '', 'html' => '', 'uid' => (string) Str::uuid(),
  32. ]);
  33. foreach ($tags as $name) {
  34. $tag = Tag::forceCreate(['name' => $name, 'owner_id' => (string) Str::uuid()]);
  35. TagMap::forceCreate(['table_name' => 'pali_texts', 'anchor_id' => $head->uid, 'tag_id' => $tag->id]);
  36. }
  37. }
  38. it('finds a page across editions plus CS paragraph numbers, ordered by edition', function () {
  39. // book_id 100 在静态书名表里:PTS 书名 dīghanikāya
  40. pageBook(100, 94, '(DN) Mahāvaggapāḷi', ['sutta', 'mūla']);
  41. pageMark('M', 0, 100, 94, 50, 100);
  42. pageMark('P', 2, 100, 94, 40, 100);
  43. pageMark('P', 2, 101, 94, 41, 100); // 别的页
  44. WbwTemplate::forceCreate([
  45. 'book' => 94, 'paragraph' => 60, 'wid' => 1, 'word' => 'para100', 'real' => 'para100', 'type' => '',
  46. 'gramma' => '', 'part' => '', 'style' => '', 'pcd_book_id' => 100, 'weight' => 1,
  47. ]);
  48. $this->getJson('/api/v3/tipitaka-page-numbers/100')
  49. ->assertOk()
  50. ->assertJsonPath('meta.total', 3)
  51. ->assertJsonPath('data.*.edition', ['PTS', 'My', 'CS'])
  52. ->assertJsonPath('data.0', [
  53. 'edition' => 'PTS', 'volume' => 2, 'page' => 100, 'book' => 94, 'para' => 40, 'book_id' => 100,
  54. 'book_title' => '(DN) Mahāvaggapāḷi', 'book_para' => 3, 'edition_title' => 'dīghanikāya', 'layer' => 'mula',
  55. 'tags' => ['sutta', 'mūla'],
  56. ])
  57. ->assertJsonPath('data.1.volume', null) // 卷号 0 = 未知
  58. ->assertJsonPath('data.2.edition_title', null);
  59. });
  60. it('filters by edition and volume; a volume excludes CS paragraph numbers', function () {
  61. pageMark('P', 2, 100, 94, 40, 100);
  62. pageMark('P', 3, 100, 95, 40, 101);
  63. pageMark('M', 2, 100, 94, 50, 100);
  64. WbwTemplate::forceCreate([
  65. 'book' => 94, 'paragraph' => 60, 'wid' => 1, 'word' => 'para100', 'real' => 'para100', 'type' => '',
  66. 'gramma' => '', 'part' => '', 'style' => '', 'pcd_book_id' => 100, 'weight' => 1,
  67. ]);
  68. $this->getJson('/api/v3/tipitaka-page-numbers/100?edition=PTS')->assertJsonPath('data.*.volume', [2, 3]);
  69. $this->getJson('/api/v3/tipitaka-page-numbers/100?volume=2')->assertJsonPath('data.*.edition', ['PTS', 'My']);
  70. $this->getJson('/api/v3/tipitaka-page-numbers/100?edition=CS')->assertJsonPath('data.*.para', [60]);
  71. });
  72. it('rejects invalid filters and non-numeric pages', function () {
  73. $this->getJson('/api/v3/tipitaka-page-numbers/100?edition=P')->assertStatus(422)->assertJsonValidationErrors('edition');
  74. $this->getJson('/api/v3/tipitaka-page-numbers/100?volume=0')->assertStatus(422)->assertJsonValidationErrors('volume');
  75. $this->getJson('/api/v3/tipitaka-page-numbers/abc')->assertNotFound();
  76. });
  77. it('does not query per row', function () {
  78. $queries = function (int $rows): int {
  79. PageNumber::query()->delete();
  80. for ($i = 1; $i <= $rows; $i++) {
  81. pageMark('P', 1, 100, 1, $i, $i);
  82. }
  83. DB::flushQueryLog();
  84. DB::enableQueryLog();
  85. $this->getJson('/api/v3/tipitaka-page-numbers/100')->assertOk()->assertJsonCount($rows, 'data');
  86. return count(DB::getQueryLog());
  87. };
  88. expect($queries(2))->toBe($queries(12));
  89. });