Skip to content

Commit

Permalink
Sort search results to have deterministic order
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jan 8, 2025
1 parent d2c8b22 commit 6b05310
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ jobs:
until docker exec --tty mongodb mongosh 127.0.0.1:27017 --eval "db.runCommand({ ping: 1 })"; do
sleep 1
done
until docker exec --tty mongodb mongosh 127.0.0.1:27017 --eval "db.createCollection('connection_test') && db.getCollection('connection_test').createSearchIndex({mappings:{dynamic: true}})"; do
sleep 1
done
- name: "Show MongoDB server status"
run: |
Expand Down
117 changes: 48 additions & 69 deletions tests/Scout/ScoutIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ protected function getEnvironmentSetUp($app): void
$app['config']->set('scout.prefix', 'prefix_');
}

public function testItCannotIndexInTheSameNamespace()
{
self::expectException(LogicException::class);
self::expectExceptionMessage(sprintf(
'The MongoDB Scout collection "%s.searchable_in_same_namespaces" must use a different collection from the collection name of the model "%s". Set the "scout.prefix" configuration or use a distinct MongoDB database',
env('MONGODB_DATABASE', 'unittest'),
SearchableInSameNamespace::class,
),);

SearchableInSameNamespace::create(['name' => 'test']);
}

public function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -144,100 +132,79 @@ public function testItCanCreateTheCollection()
#[Depends('testItCanCreateTheCollection')]
public function testItCanUseBasicSearch()
{
$results = ScoutUser::search('lar')->take(10)->get();
// All the search queries use "sort" option to ensure the results are deterministic
$results = ScoutUser::search('lar')->take(10)->orderBy('id')->get();

self::assertSame([
42 => 'Dax Larkin',
1 => 'Laravel Framework',
11 => 'Larry Casper',
12 => 'Reta Larkin',
20 => 'Prof. Larry Prosacco DVM',
44 => 'Amos Larson Sr.',
43 => 'Dana Larson Sr.',
41 => 'Gudrun Larkin',
40 => 'Otis Larson MD',
39 => 'Linkwood Larkin',
12 => 'Reta Larkin',
1 => 'Laravel Framework',
40 => 'Otis Larson MD',
41 => 'Gudrun Larkin',
42 => 'Dax Larkin',
43 => 'Dana Larson Sr.',
44 => 'Amos Larson Sr.',
], $results->pluck('name', 'id')->all());
}

#[Depends('testItCanCreateTheCollection')]
public function testItCanUseBasicSearchWithQueryCallback()
{
$results = ScoutUser::search('lar')->take(10)->query(function ($query) {
$results = ScoutUser::search('lar')->take(10)->orderBy('id')->query(function ($query) {
return $query->whereNotNull('email_verified_at');
})->get();

self::assertSame([
1 => 'Laravel Framework',
12 => 'Reta Larkin',
40 => 'Otis Larson MD',
41 => 'Gudrun Larkin',
42 => 'Dax Larkin',
44 => 'Amos Larson Sr.',
43 => 'Dana Larson Sr.',
41 => 'Gudrun Larkin',
40 => 'Otis Larson MD',
12 => 'Reta Larkin',
1 => 'Laravel Framework',
44 => 'Amos Larson Sr.',
], $results->pluck('name', 'id')->all());
}

#[Depends('testItCanCreateTheCollection')]
public function testItCanUseBasicSearchToFetchKeys()
{
$results = ScoutUser::search('lar')->take(10)->keys();
$results = ScoutUser::search('lar')->orderBy('id')->take(10)->keys();

self::assertSame([
42,
11,
20,
44,
43,
41,
40,
39,
12,
1,
], $results->all());
self::assertSame([1, 11, 12, 20, 39, 40, 41, 42, 43, 44], $results->all());
}

#[Depends('testItCanCreateTheCollection')]
public function testItCanUseBasicSearchWithQueryCallbackToFetchKeys()
{
$results = ScoutUser::search('lar')->take(10)->query(function ($query) {
$results = ScoutUser::search('lar')->take(10)->orderBy('id', 'desc')->query(function ($query) {
return $query->whereNotNull('email_verified_at');
})->keys();

self::assertSame([
42,
11,
20,
44,
43,
41,
40,
39,
12,
1,
], $results->all());
self::assertSame([44, 43, 42, 41, 40, 39, 20, 12, 11, 1], $results->all());
}

#[Depends('testItCanCreateTheCollection')]
public function testItCanUsePaginatedSearch()
{
$page1 = ScoutUser::search('lar')->take(10)->paginate(5, 'page', 1);
$page2 = ScoutUser::search('lar')->take(10)->paginate(5, 'page', 2);
$page1 = ScoutUser::search('lar')->take(10)->orderBy('id')->paginate(5, 'page', 1);
$page2 = ScoutUser::search('lar')->take(10)->orderBy('id')->paginate(5, 'page', 2);

self::assertSame([
42 => 'Dax Larkin',
1 => 'Laravel Framework',
11 => 'Larry Casper',
12 => 'Reta Larkin',
20 => 'Prof. Larry Prosacco DVM',
44 => 'Amos Larson Sr.',
43 => 'Dana Larson Sr.',
39 => 'Linkwood Larkin',
], $page1->pluck('name', 'id')->all());

self::assertSame([
41 => 'Gudrun Larkin',
40 => 'Otis Larson MD',
39 => 'Linkwood Larkin',
12 => 'Reta Larkin',
1 => 'Laravel Framework',
41 => 'Gudrun Larkin',
42 => 'Dax Larkin',
43 => 'Dana Larson Sr.',
44 => 'Amos Larson Sr.',
], $page2->pluck('name', 'id')->all());
}

Expand All @@ -248,20 +215,32 @@ public function testItCanUsePaginatedSearchWithQueryCallback()
return $query->whereNotNull('email_verified_at');
};

$page1 = ScoutUser::search('lar')->take(10)->query($queryCallback)->paginate(5, 'page', 1);
$page2 = ScoutUser::search('lar')->take(10)->query($queryCallback)->paginate(5, 'page', 2);
$page1 = ScoutUser::search('lar')->take(10)->orderBy('id')->query($queryCallback)->paginate(5, 'page', 1);
$page2 = ScoutUser::search('lar')->take(10)->orderBy('id')->query($queryCallback)->paginate(5, 'page', 2);

self::assertSame([
42 => 'Dax Larkin',
44 => 'Amos Larson Sr.',
43 => 'Dana Larson Sr.',
1 => 'Laravel Framework',
12 => 'Reta Larkin',
], $page1->pluck('name', 'id')->all());

self::assertSame([
41 => 'Gudrun Larkin',
40 => 'Otis Larson MD',
12 => 'Reta Larkin',
1 => 'Laravel Framework',
41 => 'Gudrun Larkin',
42 => 'Dax Larkin',
43 => 'Dana Larson Sr.',
44 => 'Amos Larson Sr.',
], $page2->pluck('name', 'id')->all());
}

public function testItCannotIndexInTheSameNamespace()
{
self::expectException(LogicException::class);
self::expectExceptionMessage(sprintf(
'The MongoDB Scout collection "%s.searchable_in_same_namespaces" must use a different collection from the collection name of the model "%s". Set the "scout.prefix" configuration or use a distinct MongoDB database',
env('MONGODB_DATABASE', 'unittest'),
SearchableInSameNamespace::class,
),);

SearchableInSameNamespace::create(['name' => 'test']);
}
}

0 comments on commit 6b05310

Please sign in to comment.