22
33declare (strict_types=1 );
44
5- use Illuminate \Database \Migrations \Migration ;
5+ namespace App \Http \Controllers ;
6+
67use Illuminate \Support \Facades \Schema ;
8+ use MongoDB \Collection ;
79use MongoDB \Laravel \Schema \Blueprint ;
10+ use MongoDB \Laravel \Tests \TestCase ;
811
9- return new class extends Migration
10- {
11- protected $ connection = 'mongodb ' ;
12+ use function assert ;
1213
13- public function up (): void
14+ class AtlasIdxSchemaBuilderTest extends TestCase
15+ {
16+ /**
17+ * @runInSeparateProcess
18+ * @preserveGlobalState disabled
19+ */
20+ public function testAtlasSearchIdx (): void
1421 {
1522 // begin-create-search-indexes
1623 Schema::create ('galaxies ' , function (Blueprint $ collection ) {
@@ -33,6 +40,22 @@ public function up(): void
3340 });
3441 // end-create-search-indexes
3542
43+ $ index = $ this ->getSearchIndex ('galaxies ' , 'dynamic_index ' );
44+ self ::assertNotNull ($ index );
45+
46+ self ::assertSame ('dynamic_index ' , $ index ['name ' ]);
47+ self ::assertSame ('search ' , $ index ['type ' ]);
48+ self ::assertTrue ($ index ['latestDefinition ' ]['mappings ' ]['dynamic ' ]);
49+
50+ $ index = $ this ->getSearchIndex ('galaxies ' , 'auto_index ' );
51+ self ::assertNotNull ($ index );
52+
53+ self ::assertSame ('auto_index ' , $ index ['name ' ]);
54+ self ::assertSame ('search ' , $ index ['type ' ]);
55+ }
56+
57+ public function testVectorSearchIdx (): void
58+ {
3659 // begin-create-vs-index
3760 Schema::create ('galaxies ' , function (Blueprint $ collection ) {
3861 $ collection ->vectorSearchIndex ([
@@ -47,14 +70,50 @@ public function up(): void
4770 ], 'vs_index ' );
4871 });
4972 // end-create-vs-index
73+
74+ $ index = $ this ->getSearchIndex ('galaxies ' , 'vs_index ' );
75+ self ::assertNotNull ($ index );
76+
77+ self ::assertSame ('vs_index ' , $ index ['name ' ]);
78+ self ::assertSame ('vectorSearch ' , $ index ['type ' ]);
79+ self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
5080 }
5181
52- public function down (): void
82+ public function testDropIndexes (): void
5383 {
5484 // begin-drop-search-index
5585 Schema::table ('galaxies ' , function (Blueprint $ collection ) {
5686 $ collection ->dropSearchIndex ('auto_index ' );
5787 });
5888 // end-drop-search-index
89+
90+ Schema::table ('galaxies ' , function (Blueprint $ collection ) {
91+ $ collection ->dropSearchIndex ('dynamic_index ' );
92+ });
93+
94+ Schema::table ('galaxies ' , function (Blueprint $ collection ) {
95+ $ collection ->dropSearchIndex ('vs_index ' );
96+ });
97+
98+ $ index = $ this ->getSearchIndex ('galaxies ' , 'auto_index ' );
99+ self ::assertNull ($ index );
100+
101+ $ index = $ this ->getSearchIndex ('galaxies ' , 'dynamic_index ' );
102+ self ::assertNull ($ index );
103+
104+ $ index = $ this ->getSearchIndex ('galaxies ' , 'vs_index ' );
105+ self ::assertNull ($ index );
106+ }
107+
108+ protected function getSearchIndex (string $ collection , string $ name ): ?array
109+ {
110+ $ collection = $ this ->getConnection ('mongodb ' )->getCollection ($ collection );
111+ assert ($ collection instanceof Collection);
112+
113+ foreach ($ collection ->listSearchIndexes (['name ' => $ name , 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ]]) as $ index ) {
114+ return $ index ;
115+ }
116+
117+ return null ;
59118 }
60- };
119+ }
0 commit comments