|
38 | 38 | import static org.dizitart.no2.filters.Filter.and; |
39 | 39 | import static org.dizitart.no2.filters.Filter.or; |
40 | 40 | import static org.dizitart.no2.filters.FluentFilter.where; |
41 | | -import static org.junit.Assert.assertEquals; |
42 | | -import static org.junit.Assert.assertTrue; |
| 41 | +import static org.junit.Assert.*; |
43 | 42 |
|
44 | 43 | /** |
45 | 44 | * @author Anindya Chatterjee. |
@@ -420,4 +419,212 @@ public void testIssue45() { |
420 | 419 | cursor = collection.find(where("notes").text("lazy")); |
421 | 420 | assertEquals(cursor.size(), 3); |
422 | 421 | } |
| 422 | + |
| 423 | + @Test |
| 424 | + public void testSortByIndexDescendingLessThenEqual() { |
| 425 | + NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingLessThenEqual"); |
| 426 | + List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5); |
| 427 | + integerList.forEach(i -> { |
| 428 | + Document doc = Document.createDocument(); |
| 429 | + doc.put("name", i); |
| 430 | + nitriteCollection.insert(doc); |
| 431 | + }); |
| 432 | + |
| 433 | + DocumentCursor cursor = nitriteCollection.find(where("name").lte(3), |
| 434 | + orderBy("name", SortOrder.Descending)); |
| 435 | + |
| 436 | + List<Document> docIter = cursor.toList(); |
| 437 | + Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 438 | + |
| 439 | + nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name"); |
| 440 | + |
| 441 | + cursor = nitriteCollection.find(where("name").lte(3), |
| 442 | + orderBy("name", SortOrder.Descending)); |
| 443 | + docIter = cursor.toList(); |
| 444 | + Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 445 | + |
| 446 | + assertArrayEquals(nonIndexedResult, indexedResult); |
| 447 | + } |
| 448 | + |
| 449 | + @Test |
| 450 | + public void testSortByIndexAscendingLessThenEqual() { |
| 451 | + NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingLessThenEqual"); |
| 452 | + List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5); |
| 453 | + integerList.forEach(i -> { |
| 454 | + Document doc = Document.createDocument(); |
| 455 | + doc.put("name", i); |
| 456 | + nitriteCollection.insert(doc); |
| 457 | + }); |
| 458 | + |
| 459 | + DocumentCursor cursor = nitriteCollection.find(where("name").lte(3), |
| 460 | + orderBy("name", SortOrder.Ascending)); |
| 461 | + |
| 462 | + List<Document> docIter = cursor.toList(); |
| 463 | + Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 464 | + |
| 465 | + nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name"); |
| 466 | + |
| 467 | + cursor = nitriteCollection.find(where("name").lte(3), |
| 468 | + orderBy("name", SortOrder.Ascending)); |
| 469 | + docIter = cursor.toList(); |
| 470 | + Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 471 | + |
| 472 | + assertArrayEquals(nonIndexedResult, indexedResult); |
| 473 | + } |
| 474 | + |
| 475 | + @Test |
| 476 | + public void testSortByIndexDescendingGreaterThanEqual() { |
| 477 | + NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingGreaterThanEqual"); |
| 478 | + List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5); |
| 479 | + integerList.forEach(i -> { |
| 480 | + Document doc = Document.createDocument(); |
| 481 | + doc.put("name", i); |
| 482 | + nitriteCollection.insert(doc); |
| 483 | + }); |
| 484 | + |
| 485 | + DocumentCursor cursor = nitriteCollection.find(where("name").gte(3), |
| 486 | + orderBy("name", SortOrder.Descending)); |
| 487 | + |
| 488 | + List<Document> docIter = cursor.toList(); |
| 489 | + Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 490 | + |
| 491 | + nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name"); |
| 492 | + |
| 493 | + cursor = nitriteCollection.find(where("name").gte(3), |
| 494 | + orderBy("name", SortOrder.Descending)); |
| 495 | + docIter = cursor.toList(); |
| 496 | + Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 497 | + |
| 498 | + assertArrayEquals(nonIndexedResult, indexedResult); |
| 499 | + } |
| 500 | + |
| 501 | + @Test |
| 502 | + public void testSortByIndexAscendingGreaterThanEqual() { |
| 503 | + NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingGreaterThanEqual"); |
| 504 | + List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5); |
| 505 | + integerList.forEach(i -> { |
| 506 | + Document doc = Document.createDocument(); |
| 507 | + doc.put("name", i); |
| 508 | + nitriteCollection.insert(doc); |
| 509 | + }); |
| 510 | + |
| 511 | + DocumentCursor cursor = nitriteCollection.find(where("name").gte(3), |
| 512 | + orderBy("name", SortOrder.Ascending)); |
| 513 | + |
| 514 | + List<Document> docIter = cursor.toList(); |
| 515 | + Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 516 | + |
| 517 | + nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name"); |
| 518 | + |
| 519 | + cursor = nitriteCollection.find(where("name").gte(3), |
| 520 | + orderBy("name", SortOrder.Ascending)); |
| 521 | + docIter = cursor.toList(); |
| 522 | + Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 523 | + |
| 524 | + assertArrayEquals(nonIndexedResult, indexedResult); |
| 525 | + } |
| 526 | + |
| 527 | + @Test |
| 528 | + public void testSortByIndexDescendingGreaterThan() { |
| 529 | + NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingGreaterThan"); |
| 530 | + List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5); |
| 531 | + integerList.forEach(i -> { |
| 532 | + Document doc = Document.createDocument(); |
| 533 | + doc.put("name", i); |
| 534 | + nitriteCollection.insert(doc); |
| 535 | + }); |
| 536 | + |
| 537 | + DocumentCursor cursor = nitriteCollection.find(where("name").gt(3), |
| 538 | + orderBy("name", SortOrder.Descending)); |
| 539 | + |
| 540 | + List<Document> docIter = cursor.toList(); |
| 541 | + Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 542 | + |
| 543 | + nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name"); |
| 544 | + |
| 545 | + cursor = nitriteCollection.find(where("name").gt(3), |
| 546 | + orderBy("name", SortOrder.Descending)); |
| 547 | + docIter = cursor.toList(); |
| 548 | + Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 549 | + |
| 550 | + assertArrayEquals(nonIndexedResult, indexedResult); |
| 551 | + } |
| 552 | + |
| 553 | + @Test |
| 554 | + public void testSortByIndexAscendingGreaterThan() { |
| 555 | + NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingGreaterThan"); |
| 556 | + List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5); |
| 557 | + integerList.forEach(i -> { |
| 558 | + Document doc = Document.createDocument(); |
| 559 | + doc.put("name", i); |
| 560 | + nitriteCollection.insert(doc); |
| 561 | + }); |
| 562 | + |
| 563 | + DocumentCursor cursor = nitriteCollection.find(where("name").gt(3), |
| 564 | + orderBy("name", SortOrder.Ascending)); |
| 565 | + |
| 566 | + List<Document> docIter = cursor.toList(); |
| 567 | + Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 568 | + |
| 569 | + nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name"); |
| 570 | + |
| 571 | + cursor = nitriteCollection.find(where("name").gt(3), |
| 572 | + orderBy("name", SortOrder.Ascending)); |
| 573 | + docIter = cursor.toList(); |
| 574 | + Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 575 | + |
| 576 | + assertArrayEquals(nonIndexedResult, indexedResult); |
| 577 | + } |
| 578 | + |
| 579 | + @Test |
| 580 | + public void testSortByIndexDescendingLessThan() { |
| 581 | + NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingLessThan"); |
| 582 | + List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5); |
| 583 | + integerList.forEach(i -> { |
| 584 | + Document doc = Document.createDocument(); |
| 585 | + doc.put("name", i); |
| 586 | + nitriteCollection.insert(doc); |
| 587 | + }); |
| 588 | + |
| 589 | + DocumentCursor cursor = nitriteCollection.find(where("name").lt(3), |
| 590 | + orderBy("name", SortOrder.Descending)); |
| 591 | + |
| 592 | + List<Document> docIter = cursor.toList(); |
| 593 | + Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 594 | + |
| 595 | + nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name"); |
| 596 | + |
| 597 | + cursor = nitriteCollection.find(where("name").lt(3), |
| 598 | + orderBy("name", SortOrder.Descending)); |
| 599 | + docIter = cursor.toList(); |
| 600 | + Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 601 | + |
| 602 | + assertArrayEquals(nonIndexedResult, indexedResult); |
| 603 | + } |
| 604 | + |
| 605 | + @Test |
| 606 | + public void testSortByIndexAscendingLessThan() { |
| 607 | + NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingLessThan"); |
| 608 | + List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5); |
| 609 | + integerList.forEach(i -> { |
| 610 | + Document doc = Document.createDocument(); |
| 611 | + doc.put("name", i); |
| 612 | + nitriteCollection.insert(doc); |
| 613 | + }); |
| 614 | + |
| 615 | + DocumentCursor cursor = nitriteCollection.find(where("name").lt(3), |
| 616 | + orderBy("name", SortOrder.Ascending)); |
| 617 | + |
| 618 | + List<Document> docIter = cursor.toList(); |
| 619 | + Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 620 | + |
| 621 | + nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name"); |
| 622 | + |
| 623 | + cursor = nitriteCollection.find(where("name").lt(3), |
| 624 | + orderBy("name", SortOrder.Ascending)); |
| 625 | + docIter = cursor.toList(); |
| 626 | + Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new); |
| 627 | + |
| 628 | + assertArrayEquals(nonIndexedResult, indexedResult); |
| 629 | + } |
423 | 630 | } |
0 commit comments