-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: Add Tests for Search and SyncNotion Actions #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Feature\Actions\SearchPage; | ||
|
|
||
| use App\Actions\SearchPage\SearchAction; | ||
| use App\Enums\PakSlug; | ||
| use App\Enums\SiteName; | ||
| use App\Models\Page; | ||
| use App\Models\Pak; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Tests\Feature\TestCase; | ||
|
|
||
| final class SearchActionTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| private Pak $pak128; | ||
|
|
||
| private Pak $pak64; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->pak128 = Pak::factory()->create(['slug' => PakSlug::Pak128]); | ||
| $this->pak64 = Pak::factory()->create(['slug' => PakSlug::Pak64]); | ||
| } | ||
|
|
||
| public function test_filters_by_site_and_pak(): void | ||
| { | ||
| $page1 = Page::factory()->create(['site_name' => SiteName::Japan]); | ||
| $page1->paks()->attach($this->pak128); | ||
|
|
||
| $page2 = Page::factory()->create(['site_name' => SiteName::Portal]); | ||
| $page2->paks()->attach($this->pak128); | ||
|
|
||
| $page3 = Page::factory()->create(['site_name' => SiteName::Japan]); | ||
| $page3->paks()->attach($this->pak64); | ||
|
|
||
| $searchAction = new SearchAction; | ||
|
|
||
| $result = $searchAction([ | ||
| 'keyword' => '', | ||
| 'sites' => [SiteName::Japan->value], | ||
| 'paks' => [PakSlug::Pak128->value], | ||
| ]); | ||
|
|
||
| $this->assertCount(1, $result); | ||
| $this->assertSame($page1->id, $result->first()->id); | ||
| } | ||
|
|
||
| public function test_filters_by_keyword_including_exclude_logic(): void | ||
| { | ||
| $page1 = Page::factory()->create([ | ||
| 'site_name' => SiteName::Japan, | ||
| 'title' => 'Train Addon', | ||
| 'text' => 'This is a train', | ||
| ]); | ||
| $page1->paks()->attach($this->pak128); | ||
|
|
||
| $page2 = Page::factory()->create([ | ||
| 'site_name' => SiteName::Japan, | ||
| 'title' => 'Bus Addon', | ||
| 'text' => 'This is a bus', | ||
| ]); | ||
| $page2->paks()->attach($this->pak128); | ||
|
|
||
| $page3 = Page::factory()->create([ | ||
| 'site_name' => SiteName::Japan, | ||
| 'title' => 'Electric Train Addon', | ||
| 'text' => 'This is an electric train', | ||
| ]); | ||
| $page3->paks()->attach($this->pak128); | ||
|
|
||
| $searchAction = new SearchAction; | ||
|
|
||
| $baseQuery = [ | ||
| 'sites' => [SiteName::Japan->value], | ||
| 'paks' => [PakSlug::Pak128->value], | ||
| ]; | ||
|
|
||
| // Search for 'Train' | ||
| $result = $searchAction(array_merge($baseQuery, ['keyword' => 'Train'])); | ||
| $this->assertCount(2, $result); // page1, page3 | ||
|
|
||
| // Search for 'Train -Electric' | ||
| $result = $searchAction(array_merge($baseQuery, ['keyword' => 'Train -Electric'])); | ||
| $this->assertCount(1, $result); // page1 only | ||
|
|
||
| // Search for 'Bus' | ||
| $result = $searchAction(array_merge($baseQuery, ['keyword' => 'Bus'])); | ||
| $this->assertCount(1, $result); // page2 | ||
| $this->assertSame($page2->id, $result->first()->id); | ||
| } | ||
|
|
||
| public function test_orders_by_last_modified_desc(): void | ||
| { | ||
| $page1 = Page::factory()->create([ | ||
| 'site_name' => SiteName::Japan, | ||
| 'last_modified' => now()->subDays(2), | ||
| ]); | ||
| $page1->paks()->attach($this->pak128); | ||
|
|
||
| $page2 = Page::factory()->create([ | ||
| 'site_name' => SiteName::Japan, | ||
| 'last_modified' => now(), | ||
| ]); | ||
| $page2->paks()->attach($this->pak128); | ||
|
|
||
| $page3 = Page::factory()->create([ | ||
| 'site_name' => SiteName::Japan, | ||
| 'last_modified' => now()->subDays(1), | ||
| ]); | ||
| $page3->paks()->attach($this->pak128); | ||
|
|
||
| $searchAction = new SearchAction; | ||
|
|
||
| $result = $searchAction([ | ||
| 'keyword' => '', | ||
| 'sites' => [SiteName::Japan->value], | ||
| 'paks' => [PakSlug::Pak128->value], | ||
| ]); | ||
|
|
||
| $this->assertCount(3, $result); | ||
| $this->assertSame($page2->id, $result->items()[0]->id); | ||
| $this->assertSame($page3->id, $result->items()[1]->id); | ||
| $this->assertSame($page1->id, $result->items()[2]->id); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Feature\Actions\SyncNotion; | ||
|
|
||
| use App\Actions\SyncNotion\SyncAction; | ||
| use App\Enums\PakSlug; | ||
| use App\Enums\SiteName; | ||
| use App\Models\Page; | ||
| use App\Models\Pak; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Mockery; | ||
| use Notion\Databases\Database; | ||
| use Notion\Notion; | ||
| use Notion\Pages\Page as NotionPage; | ||
| use Notion\Pages\PageParent; | ||
| use Notion\Pages\Properties\Url; | ||
| use Tests\Feature\TestCase; | ||
|
|
||
| final class SyncActionTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| public function test_sync_action_creates_updates_and_deletes_pages(): void | ||
| { | ||
| $pak = Pak::factory()->create(['slug' => PakSlug::Pak128]); | ||
|
|
||
| // This page should be created in Notion | ||
| $pageToCreate = Page::factory()->create([ | ||
| 'title' => 'New Addon', | ||
| 'site_name' => SiteName::Japan, | ||
| 'url' => 'https://example.com/new', | ||
| 'last_modified' => now(), | ||
| ]); | ||
| $pageToCreate->paks()->attach($pak); | ||
|
|
||
| // This page exists in both DB and Notion, should be updated | ||
| Page::factory()->create([ | ||
| 'title' => 'Updated Addon', | ||
| 'site_name' => SiteName::Japan, | ||
| 'url' => 'https://example.com/update', | ||
| 'last_modified' => now(), | ||
| ]); | ||
|
|
||
| $database = Database::fromArray([ | ||
| 'id' => 'test_database_id', | ||
| 'created_time' => '2023-01-01T00:00:00.000Z', | ||
| 'last_edited_time' => '2023-01-01T00:00:00.000Z', | ||
| 'title' => [], | ||
| 'description' => [], | ||
| 'icon' => null, | ||
| 'cover' => null, | ||
| 'properties' => [ | ||
| 'Title' => [ | ||
| 'id' => 'title', | ||
| 'type' => 'title', | ||
| 'name' => 'Title', | ||
| 'title' => [], | ||
| ], | ||
| 'パックセット' => [ | ||
| 'id' => 'prop_id', | ||
| 'type' => 'multi_select', | ||
| 'name' => 'パックセット', | ||
| 'multi_select' => [ | ||
| 'options' => [ | ||
| ['name' => '128', 'id' => 'opt_128', 'color' => 'default'], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| 'parent' => ['type' => 'workspace', 'workspace' => true], | ||
| 'url' => 'https://notion.so', | ||
| 'is_inline' => false, | ||
| ]); | ||
|
|
||
| $notionPageToUpdate = NotionPage::create(PageParent::database('test_database_id')); | ||
| $notionPageToUpdate = $notionPageToUpdate->addProperty('URL', Url::create('https://example.com/update')); | ||
|
|
||
| $notionPageToDelete = NotionPage::create(PageParent::database('test_database_id')); | ||
| $notionPageToDelete = $notionPageToDelete->addProperty('URL', Url::create('https://example.com/delete')); | ||
|
|
||
| $mock = Mockery::mock(Notion::class); | ||
| $mock->shouldReceive('databases->find')->with('test_database_id')->andReturn($database); | ||
| $mock->shouldReceive('databases->queryAllPages')->with($database)->andReturn([$notionPageToUpdate, $notionPageToDelete]); | ||
|
|
||
| $mock->shouldReceive('pages->delete')->once()->with($notionPageToDelete); | ||
|
|
||
| $mock->shouldReceive('pages->update')->once()->with(Mockery::on(fn (NotionPage $notionPage): bool => $notionPage->getProperty('URL')->url === 'https://example.com/update')); | ||
|
|
||
| $mock->shouldReceive('pages->create')->once()->with(Mockery::on(fn (NotionPage $notionPage): bool => $notionPage->getProperty('URL')->url === 'https://example.com/new')); | ||
|
|
||
| $syncAction = new SyncAction($mock); | ||
| $syncAction('test_database_id', 10); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
キーワードに複数の連続したスペースが含まれている場合、
explode(' ', $keyword)によって空文字列""が生成されます。このとき、str_starts_with($word, '-')はfalseとなり、elseブロックが実行されてLIKE '%%'という不要なクエリが追加されてしまいます。これを防ぐために、ループの先頭で空文字列をスキップすることをお勧めします。
また、日本語環境では全角スペース(
)が区切り文字として使われることが多いため、将来的に全角スペースへの対応(preg_splitへの変更など)も検討するとより良くなります。