Search in sources :

Example 1 with PaginationImpl

use of com.yahoo.elide.core.pagination.PaginationImpl in project elide by yahoo.

the class InMemoryStoreTransactionTest method testSortingRequiresInMemoryPagination.

@Test
public void testSortingRequiresInMemoryPagination() {
    PaginationImpl pagination = new PaginationImpl(ClassType.of(Book.class), 0, 3, 10, 10, true, false);
    Map<String, Sorting.SortOrder> sortOrder = new HashMap<>();
    sortOrder.put("title", Sorting.SortOrder.desc);
    Sorting sorting = new SortingImpl(sortOrder, Book.class, dictionary);
    EntityProjection projection = EntityProjection.builder().type(Book.class).sorting(sorting).pagination(pagination).build();
    DataStoreIterable sortInMemory = new DataStoreIterableBuilder(books).sortInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(sortInMemory);
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    verify(wrappedTransaction, times(1)).loadObjects(any(EntityProjection.class), eq(scope));
    assertEquals(3, loaded.size());
    List<String> bookTitles = loaded.stream().map((o) -> ((Book) o).getTitle()).collect(Collectors.toList());
    assertEquals(Lists.newArrayList("Book 3", "Book 2", "Book 1"), bookTitles);
    assertEquals(3, pagination.getPageTotals());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Path(com.yahoo.elide.core.Path) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Publisher(example.Publisher) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) ClassType(com.yahoo.elide.core.type.ClassType) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Relationship(com.yahoo.elide.core.request.Relationship) RequestScope(com.yahoo.elide.core.RequestScope) ImmutableSet(com.google.common.collect.ImmutableSet) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) HashMap(java.util.HashMap) Author(example.Author) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Editor(example.Editor) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) ElideSettings(com.yahoo.elide.ElideSettings) Price(example.Price) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Sorting(com.yahoo.elide.core.request.Sorting) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Mockito.times(org.mockito.Mockito.times) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Address(example.Address) Mockito.reset(org.mockito.Mockito.reset) EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) HashMap(java.util.HashMap) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Sorting(com.yahoo.elide.core.request.Sorting) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Book(example.Book) Test(org.junit.jupiter.api.Test)

Example 2 with PaginationImpl

use of com.yahoo.elide.core.pagination.PaginationImpl in project elide by yahoo.

the class InMemoryStoreTransactionTest method testFilteringRequiresInMemoryPagination.

@Test
public void testFilteringRequiresInMemoryPagination() {
    FilterExpression expression = new InPredicate(new Path(Book.class, dictionary, "genre"), "Literary Fiction");
    PaginationImpl pagination = new PaginationImpl(ClassType.of(Book.class), 0, 2, 10, 10, true, false);
    EntityProjection projection = EntityProjection.builder().type(Book.class).filterExpression(expression).pagination(pagination).build();
    DataStoreIterable filterInMemory = new DataStoreIterableBuilder(books).filterInMemory(true).build();
    when(wrappedTransaction.loadObjects(any(), eq(scope))).thenReturn(filterInMemory);
    Collection<Object> loaded = Lists.newArrayList(inMemoryStoreTransaction.loadObjects(projection, scope));
    verify(wrappedTransaction, times(1)).loadObjects(any(EntityProjection.class), eq(scope));
    assertEquals(2, loaded.size());
    assertTrue(loaded.contains(book1));
    assertTrue(loaded.contains(book3));
    assertEquals(2, pagination.getPageTotals());
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Test(org.junit.jupiter.api.Test)

Example 3 with PaginationImpl

use of com.yahoo.elide.core.pagination.PaginationImpl in project elide by yahoo.

the class SubCollectionFetchQueryBuilderTest method testFetchJoinExcludesParent.

@Test
public void testFetchJoinExcludesParent() {
    Publisher publisher = new Publisher();
    publisher.setId(1);
    Book book = new Book();
    book.setId(2);
    Pagination pagination = new PaginationImpl(Book.class, 0, 10, 10, 10, false, false);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).pagination(pagination).build();
    Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Publisher.class), publisher, relationshipProjection);
    SubCollectionFetchQueryBuilder builder = new SubCollectionFetchQueryBuilder(relationship, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String expected = "SELECT example_Book FROM example.Publisher example_Publisher__fetch " + "JOIN example_Publisher__fetch.books example_Book " + "WHERE example_Publisher__fetch=:example_Publisher__fetch";
    String actual = query.getQueryText();
    actual = actual.replaceFirst(":publisher_name_\\w+", ":publisher_name_XXX");
    assertEquals(expected, actual);
}
Also used : Pagination(com.yahoo.elide.core.request.Pagination) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Publisher(example.Publisher) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) Test(org.junit.jupiter.api.Test)

Example 4 with PaginationImpl

use of com.yahoo.elide.core.pagination.PaginationImpl in project elide by yahoo.

the class SubCollectionPageTotalsQueryBuilderTest method testSubCollectionPageTotalsWithPagination.

@Test
public void testSubCollectionPageTotalsWithPagination() {
    Author author = new Author();
    author.setId(1L);
    Book book = new Book();
    book.setId(2);
    PaginationImpl pagination = mock(PaginationImpl.class);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).pagination(pagination).build();
    Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
    TestQueryWrapper query = (TestQueryWrapper) new SubCollectionPageTotalsQueryBuilder(relationship, dictionary, new TestSessionWrapper()).build();
    String expected = "SELECT COUNT(DISTINCT example_Author_books) " + "FROM example.Author AS example_Author " + "JOIN example_Author.books example_Author_books " + "WHERE example_Author.id IN (:id_XXX)";
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    actual = actual.replaceFirst(":id_\\w+", ":id_XXX");
    assertEquals(expected, actual);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Author(example.Author) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) SubCollectionPageTotalsQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionPageTotalsQueryBuilder) Test(org.junit.jupiter.api.Test)

Example 5 with PaginationImpl

use of com.yahoo.elide.core.pagination.PaginationImpl in project elide by yahoo.

the class DataStoreLoadTest method testPaginationPageTwo.

@Test
public void testPaginationPageTwo() throws Exception {
    DataStoreTransaction testTransaction = searchStore.beginReadTransaction();
    Map<String, Sorting.SortOrder> sortRules = new HashMap<>();
    sortRules.put("name", Sorting.SortOrder.desc);
    sortRules.put("modifiedDate", Sorting.SortOrder.asc);
    Sorting sorting = new SortingImpl(sortRules, Item.class, dictionary);
    PaginationImpl pagination = new PaginationImpl(Item.class, 1, 1, PaginationImpl.DEFAULT_PAGE_LIMIT, PaginationImpl.MAX_PAGE_LIMIT, true, false);
    FilterExpression filter = filterParser.parseFilterExpression("name==cymbal*", ClassType.of(Item.class), false);
    Iterable<Object> loaded = testTransaction.loadObjects(EntityProjection.builder().type(Item.class).filterExpression(filter).sorting(sorting).pagination(pagination).build(), mockScope);
    assertListMatches(loaded, Lists.newArrayList(5L));
    assertEquals(pagination.getPageTotals(), 3);
    verify(wrappedTransaction, never()).loadObjects(any(), any());
}
Also used : Item(com.yahoo.elide.datastores.search.models.Item) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) HashMap(java.util.HashMap) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Sorting(com.yahoo.elide.core.request.Sorting) Test(org.junit.jupiter.api.Test)

Aggregations

PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)15 Test (org.junit.jupiter.api.Test)12 EntityProjection (com.yahoo.elide.core.request.EntityProjection)9 Book (example.Book)6 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)5 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)4 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)4 Pagination (com.yahoo.elide.core.request.Pagination)4 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)3 Relationship (com.yahoo.elide.core.request.Relationship)3 Sorting (com.yahoo.elide.core.request.Sorting)3 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)3 Author (example.Author)3 HashMap (java.util.HashMap)3 Path (com.yahoo.elide.core.Path)2 RequestScope (com.yahoo.elide.core.RequestScope)2 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)2 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)2 RelationshipImpl (com.yahoo.elide.datastores.jpql.query.RelationshipImpl)2 Item (com.yahoo.elide.datastores.search.models.Item)2