5
5
namespace App \Http \Controllers ;
6
6
7
7
use App \Models \Movie ;
8
+ use Illuminate \Support \Facades \DB ;
8
9
use MongoDB \Laravel \Tests \TestCase ;
9
10
10
11
class ReadOperationsTest extends TestCase
@@ -15,7 +16,10 @@ protected function setUp(): void
15
16
16
17
parent ::setUp ();
17
18
18
- Movie::truncate ();
19
+ $ moviesCollection = DB ::connection ('mongodb ' )->getCollection ('movies ' );
20
+ $ moviesCollection ->drop ();
21
+ $ moviesCollection ->createIndex (['plot ' => 'text ' ]);
22
+
19
23
Movie::insert ([
20
24
['year ' => 2010 , 'imdb ' => ['rating ' => 9 ]],
21
25
['year ' => 2010 , 'imdb ' => ['rating ' => 9.5 ]],
@@ -26,14 +30,10 @@ protected function setUp(): void
26
30
['year ' => 1999 , 'countries ' => ['Indonesia ' ], 'title ' => 'Title 4 ' ],
27
31
['year ' => 1999 , 'countries ' => ['Canada ' ], 'title ' => 'Title 5 ' ],
28
32
['year ' => 1999 , 'runtime ' => 30 ],
29
- ['title ' => 'movie_a ' , 'plot ' => 'this is a love story ' ],
30
33
['title ' => 'movie_b ' , 'plot ' => 'love is a long story ' ],
34
+ ['title ' => 'movie_a ' , 'plot ' => 'this is a love story ' ],
31
35
['title ' => 'movie_c ' , 'plot ' => 'went on a trip ' ],
32
36
]);
33
-
34
- $ collection = MongoDB::connection ('mongodb ' )->collection ('movies ' );
35
- $ index = ['plot ' => 'text ' ];
36
- $ collection ->createIndex ($ index );
37
37
}
38
38
39
39
/**
@@ -109,7 +109,7 @@ public function testFirst(): void
109
109
public function testText (): void
110
110
{
111
111
// start-text
112
- $ movies = Movie::where ('$text ' , ['$search ' => '" love story" ' ])
112
+ $ movies = Movie::where ('$text ' , ['$search ' => 'love story ' ])
113
113
->get ();
114
114
// end-text
115
115
@@ -124,13 +124,13 @@ public function testText(): void
124
124
public function testTextRelevance (): void
125
125
{
126
126
// start-text-relevance
127
- $ movies = Movie::where ('$text ' , ['$search ' => '" love story" ' ])
128
- ->orderBy ('score ' , ['$meta ' => 'textScore ' ])
127
+ $ movies = Movie::where ('$text ' , ['$search ' => 'love story ' ])
128
+ ->orderBy ('score ' , ['$meta ' => 'textScore ' ])
129
129
->get ();
130
130
// end-text-relevance
131
131
132
132
$ this ->assertNotNull ($ movies );
133
133
$ this ->assertCount (2 , $ movies );
134
+ $ this ->assertEquals ('this is a love story ' , $ movies [0 ]->plot );
134
135
}
135
-
136
136
}
0 commit comments