-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,139 @@ | ||
<?php | ||
|
||
namespace MongoDB\Laravel\Tests; | ||
|
||
use MongoDB\Builder\Search; | ||
use MongoDB\Collection; | ||
use MongoDB\Driver\Exception\ServerException; | ||
use MongoDB\Laravel\Tests\Models\Book; | ||
|
||
use function assert; | ||
use function in_array; | ||
use function usleep; | ||
|
||
class AtlasSearchTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Book::insert([ | ||
['title' => 'Introduction to Algorithms'], | ||
['title' => 'Clean Code: A Handbook of Agile Software Craftsmanship'], | ||
['title' => 'Design Patterns: Elements of Reusable Object-Oriented Software'], | ||
['title' => 'The Pragmatic Programmer: Your Journey to Mastery'], | ||
['title' => 'Artificial Intelligence: A Modern Approach'], | ||
['title' => 'Structure and Interpretation of Computer Programs'], | ||
['title' => 'Code Complete: A Practical Handbook of Software Construction'], | ||
['title' => 'The Art of Computer Programming'], | ||
['title' => 'Computer Networks'], | ||
['title' => 'Operating System Concepts'], | ||
['title' => 'Database System Concepts'], | ||
['title' => 'Compilers: Principles, Techniques, and Tools'], | ||
['title' => 'Introduction to the Theory of Computation'], | ||
['title' => 'Modern Operating Systems'], | ||
['title' => 'Computer Organization and Design'], | ||
['title' => 'The Mythical Man-Month: Essays on Software Engineering'], | ||
['title' => 'Algorithms'], | ||
['title' => 'Understanding Machine Learning: From Theory to Algorithms'], | ||
['title' => 'Deep Learning'], | ||
['title' => 'Pattern Recognition and Machine Learning'], | ||
]); | ||
|
||
$collection = $this->getConnection('mongodb')->getCollection('books'); | ||
assert($collection instanceof Collection); | ||
try { | ||
$collection->createSearchIndex([ | ||
'mappings' => [ | ||
'fields' => [ | ||
'title' => [ | ||
[ | ||
'type' => 'string', | ||
'analyzer' => 'lucene.english', | ||
], | ||
[ | ||
'type' => 'autocomplete', | ||
'analyzer' => 'lucene.english', | ||
], | ||
], | ||
], | ||
], | ||
]); | ||
} catch (ServerException $e) { | ||
if (in_array($e->getCode(), [40324, 115, 6047401, 31082])) { | ||
self::markTestSkipped('Atlas Search not supported'); | ||
} | ||
|
||
throw $e; | ||
} | ||
|
||
// Wait for the index to be ready | ||
do { | ||
usleep(10_000); | ||
$index = $collection->listSearchIndexes(['name' => 'default'])->current(); | ||
} while ($index['status'] !== 'READY'); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
$this->getConnection('mongodb')->getCollection('books')->drop(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testEloquentBuilderSearch() | ||
{ | ||
$results = Book::search(Search::text('title', 'systems')); | ||
|
||
self::assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $results); | ||
self::assertCount(3, $results); | ||
self::assertInstanceOf(Book::class, $results->first()); | ||
self::assertSame([ | ||
'Operating System Concepts', | ||
'Database System Concepts', | ||
'Modern Operating Systems', | ||
], $results->pluck('title')->all()); | ||
} | ||
|
||
public function testDatabaseBuilderSearch() | ||
{ | ||
$results = $this->getConnection('mongodb')->table('books') | ||
->search(Search::text('title', 'systems')); | ||
|
||
self::assertInstanceOf(\Illuminate\Support\Collection::class, $results); | ||
self::assertCount(3, $results); | ||
self::assertIsArray($results->first()); | ||
self::assertSame([ | ||
'Operating System Concepts', | ||
'Database System Concepts', | ||
'Modern Operating Systems', | ||
], $results->pluck('title')->all()); | ||
} | ||
|
||
public function testEloquentBuilderAutocomplete() | ||
{ | ||
$results = Book::autocomplete('title', 'system'); | ||
|
||
self::assertInstanceOf(\Illuminate\Support\Collection::class, $results); | ||
self::assertCount(3, $results); | ||
self::assertSame([ | ||
'Operating System Concepts', | ||
'Database System Concepts', | ||
'Modern Operating Systems', | ||
], $results->all()); | ||
} | ||
|
||
public function testDatabaseBuilderAutocomplete() | ||
{ | ||
$results = $this->getConnection('mongodb')->table('books') | ||
->autocomplete('title', 'system'); | ||
|
||
self::assertInstanceOf(\Illuminate\Support\Collection::class, $results); | ||
self::assertCount(3, $results); | ||
self::assertSame([ | ||
'Operating System Concepts', | ||
'Database System Concepts', | ||
'Modern Operating Systems', | ||
], $results->all()); | ||
} | ||
} |