1717import static org .hypertrace .core .documentstore .expression .operators .RelationalOperator .NOT_IN ;
1818import static org .hypertrace .core .documentstore .expression .operators .SortingOrder .ASC ;
1919import static org .hypertrace .core .documentstore .expression .operators .SortingOrder .DESC ;
20+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2021import static org .mockito .ArgumentMatchers .any ;
2122import static org .mockito .ArgumentMatchers .anyInt ;
2223import static org .mockito .ArgumentMatchers .anyList ;
3738import org .hypertrace .core .documentstore .expression .impl .LogicalExpression ;
3839import org .hypertrace .core .documentstore .expression .impl .RelationalExpression ;
3940import org .hypertrace .core .documentstore .expression .operators .SortingOrder ;
41+ import org .hypertrace .core .documentstore .query .Filter ;
4042import org .hypertrace .core .documentstore .query .Pagination ;
4143import org .hypertrace .core .documentstore .query .Query ;
44+ import org .hypertrace .core .documentstore .query .Selection ;
4245import org .hypertrace .core .documentstore .query .SelectionSpec ;
4346import org .hypertrace .core .documentstore .query .SortingSpec ;
4447import org .junit .jupiter .api .AfterEach ;
@@ -85,7 +88,6 @@ void setUp() {
8588
8689 @ AfterEach
8790 void tearDown () {
88- verify (collection ).getNamespace ();
8991 verifyNoMoreInteractions (collection , iterable , cursor , aggIterable );
9092 }
9193
@@ -98,6 +100,7 @@ public void testFindSimple() {
98100 BasicDBObject mongoQuery = new BasicDBObject ();
99101 BasicDBObject projection = new BasicDBObject ();
100102
103+ verify (collection ).getNamespace ();
101104 verify (collection ).find (mongoQuery );
102105 verify (iterable ).projection (projection );
103106 verify (iterable , NOT_INVOKED ).sort (any ());
@@ -119,6 +122,7 @@ public void testFindWithSelection() {
119122 BasicDBObject mongoQuery = new BasicDBObject ();
120123 BasicDBObject projection = BasicDBObject .parse ("{id: 1, name: \" $fname\" }" );
121124
125+ verify (collection ).getNamespace ();
122126 verify (collection ).find (mongoQuery );
123127 verify (iterable ).projection (projection );
124128 verify (iterable , NOT_INVOKED ).sort (any ());
@@ -159,6 +163,7 @@ public void testFindWithFilter() {
159163 + "}" );
160164 BasicDBObject projection = new BasicDBObject ();
161165
166+ verify (collection ).getNamespace ();
162167 verify (collection ).find (mongoQuery );
163168 verify (iterable ).projection (projection );
164169 verify (iterable , NOT_INVOKED ).sort (any ());
@@ -181,6 +186,7 @@ public void testFindWithSorting() {
181186 BasicDBObject sortQuery = BasicDBObject .parse ("{ marks: -1, name: 1}" );
182187 BasicDBObject projection = new BasicDBObject ();
183188
189+ verify (collection ).getNamespace ();
184190 verify (collection ).find (mongoQuery );
185191 verify (iterable ).projection (projection );
186192 verify (iterable ).sort (sortQuery );
@@ -199,6 +205,7 @@ public void testFindWithPagination() {
199205 BasicDBObject mongoQuery = new BasicDBObject ();
200206 BasicDBObject projection = new BasicDBObject ();
201207
208+ verify (collection ).getNamespace ();
202209 verify (collection ).find (mongoQuery );
203210 verify (iterable ).projection (projection );
204211 verify (iterable , NOT_INVOKED ).sort (any ());
@@ -245,6 +252,7 @@ public void testFindWithAllClauses() {
245252 BasicDBObject projection = BasicDBObject .parse ("{id: 1, name: \" $fname\" }" );
246253 BasicDBObject sortQuery = BasicDBObject .parse ("{ marks: -1, name: 1}" );
247254
255+ verify (collection ).getNamespace ();
248256 verify (collection ).find (mongoQuery );
249257 verify (iterable ).projection (projection );
250258 verify (iterable ).sort (sortQuery );
@@ -253,6 +261,40 @@ public void testFindWithAllClauses() {
253261 verify (iterable ).cursor ();
254262 }
255263
264+ @ Test
265+ public void testFindAndAggregateWithDuplicateAlias () {
266+ List <SelectionSpec > selectionSpecs =
267+ List .of (
268+ SelectionSpec .of (IdentifierExpression .of ("item" )),
269+ SelectionSpec .of (IdentifierExpression .of ("price" ), "value" ),
270+ SelectionSpec .of (IdentifierExpression .of ("quantity" ), "value" ),
271+ SelectionSpec .of (IdentifierExpression .of ("date" )));
272+ Selection selection = Selection .builder ().selectionSpecs (selectionSpecs ).build ();
273+ Filter filter =
274+ Filter .builder ()
275+ .expression (
276+ RelationalExpression .of (
277+ IdentifierExpression .of ("item" ),
278+ NOT_IN ,
279+ ConstantExpression .ofStrings (List .of ("Soap" , "Bottle" ))))
280+ .build ();
281+
282+ Query query = Query .builder ().setSelection (selection ).setFilter (filter ).build ();
283+
284+ assertThrows (IllegalArgumentException .class , () -> executor .find (query ));
285+ verify (collection , NOT_INVOKED ).getNamespace ();
286+ verify (collection , NOT_INVOKED ).find (any (BasicDBObject .class ));
287+ verify (iterable , NOT_INVOKED ).projection (any (BasicDBObject .class ));
288+ verify (iterable , NOT_INVOKED ).sort (any (BasicDBObject .class ));
289+ verify (iterable , NOT_INVOKED ).skip (anyInt ());
290+ verify (iterable , NOT_INVOKED ).limit (anyInt ());
291+ verify (iterable , NOT_INVOKED ).cursor ();
292+
293+ assertThrows (IllegalArgumentException .class , () -> executor .aggregate (query ));
294+ verify (collection , NOT_INVOKED ).aggregate (anyList ());
295+ verify (aggIterable , NOT_INVOKED ).cursor ();
296+ }
297+
256298 @ Test
257299 public void testSimpleAggregate () {
258300 Query query =
@@ -549,6 +591,7 @@ public void testGetDistinctCount() {
549591
550592 private void testAggregation (Query query , List <BasicDBObject > pipeline ) {
551593 executor .aggregate (query );
594+ verify (collection ).getNamespace ();
552595 verify (collection ).aggregate (pipeline );
553596 verify (aggIterable ).cursor ();
554597 }
0 commit comments