Save workspace changes
This commit is contained in:
86
tests/Feature/Ranking/RankBuildListsDispatchTest.php
Normal file
86
tests/Feature/Ranking/RankBuildListsDispatchTest.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Ranking;
|
||||
|
||||
use App\Jobs\RankBuildListsJob;
|
||||
use App\Jobs\RankBuildScopeListsJob;
|
||||
use App\Models\ContentType;
|
||||
use App\Models\RankList;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RankBuildListsDispatchTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_dispatcher_fans_out_only_active_category_scopes(): void
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
$contentType = ContentType::create([
|
||||
'name' => 'Wallpapers',
|
||||
'slug' => 'wallpapers',
|
||||
]);
|
||||
|
||||
$activeCategoryIds = [
|
||||
(int) $contentType->categories()->create([
|
||||
'name' => 'Abstract',
|
||||
'slug' => 'abstract',
|
||||
'is_active' => true,
|
||||
])->id,
|
||||
(int) $contentType->categories()->create([
|
||||
'name' => 'Nature',
|
||||
'slug' => 'nature',
|
||||
'is_active' => true,
|
||||
])->id,
|
||||
];
|
||||
|
||||
$contentType->categories()->create([
|
||||
'name' => 'Hidden',
|
||||
'slug' => 'hidden',
|
||||
'is_active' => false,
|
||||
]);
|
||||
|
||||
(new RankBuildListsJob)->handle();
|
||||
|
||||
Queue::assertPushed(RankBuildScopeListsJob::class, 4);
|
||||
Queue::assertPushed(RankBuildScopeListsJob::class, fn (RankBuildScopeListsJob $job) => $job->scopeType === 'global' && $job->scopeId === 0);
|
||||
Queue::assertPushed(RankBuildScopeListsJob::class, fn (RankBuildScopeListsJob $job) => $job->scopeType === 'content_type' && $job->scopeId === (int) $contentType->id);
|
||||
|
||||
foreach ($activeCategoryIds as $categoryId) {
|
||||
Queue::assertPushed(RankBuildScopeListsJob::class, fn (RankBuildScopeListsJob $job) => $job->scopeType === 'category' && $job->scopeId === $categoryId);
|
||||
}
|
||||
|
||||
Queue::assertNotPushed(RankBuildScopeListsJob::class, fn (RankBuildScopeListsJob $job) => $job->scopeType === 'category' && ! in_array($job->scopeId, $activeCategoryIds, true));
|
||||
}
|
||||
|
||||
public function test_scope_job_upserts_rows_even_when_scope_has_no_candidates(): void
|
||||
{
|
||||
$contentType = ContentType::create([
|
||||
'name' => 'Photography',
|
||||
'slug' => 'photography',
|
||||
]);
|
||||
|
||||
$category = $contentType->categories()->create([
|
||||
'name' => 'Macro',
|
||||
'slug' => 'macro',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
app()->call([new RankBuildScopeListsJob('category', (int) $category->id), 'handle']);
|
||||
|
||||
$rows = RankList::query()
|
||||
->where('scope_type', 'category')
|
||||
->where('scope_id', $category->id)
|
||||
->where('model_version', config('ranking.model_version'))
|
||||
->orderBy('list_type')
|
||||
->get();
|
||||
|
||||
$this->assertCount(3, $rows);
|
||||
$this->assertSame(['best', 'new_hot', 'trending'], $rows->pluck('list_type')->all());
|
||||
$this->assertTrue($rows->every(fn (RankList $row) => $row->artwork_ids === []));
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use App\Models\RankList;
|
||||
use App\Models\User;
|
||||
use App\Services\RankingService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
@@ -23,6 +24,13 @@ class RankGlobalTrendingTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Cache::flush();
|
||||
}
|
||||
|
||||
// ── Test 1: ranked order ───────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
@@ -52,7 +60,7 @@ class RankGlobalTrendingTest extends TestCase
|
||||
'scope_type' => 'global',
|
||||
'scope_id' => 0,
|
||||
'list_type' => 'trending',
|
||||
'model_version' => 'rank_v1',
|
||||
'model_version' => (string) config('ranking.model_version', 'rank_v1'),
|
||||
'artwork_ids' => $rankedOrder,
|
||||
'computed_at' => now(),
|
||||
]);
|
||||
@@ -78,7 +86,7 @@ class RankGlobalTrendingTest extends TestCase
|
||||
// Meta block is present
|
||||
$response->assertJsonPath('meta.list_type', 'trending');
|
||||
$response->assertJsonPath('meta.fallback', false);
|
||||
$response->assertJsonPath('meta.model_version', 'rank_v1');
|
||||
$response->assertJsonPath('meta.model_version', (string) config('ranking.model_version', 'rank_v1'));
|
||||
}
|
||||
|
||||
// ── Test 2: diversity constraint ───────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user