Skip to content

Commit

Permalink
Merge pull request #283 from eclipse/550-arangodb-pagination
Browse files Browse the repository at this point in the history
ArangoDB pagination [issues]
  • Loading branch information
otaviojava authored Sep 3, 2024
2 parents 5b5e833 + fb2dd0b commit f25a650
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public void delete(DeleteQuery query) {
@Override
public Stream<CommunicationEntity> select(SelectQuery query) throws NullPointerException {
requireNonNull(query, "query is required");

AQLQueryResult result = QueryAQLConverter.select(query);
LOGGER.finest("Executing AQL: " + result.query());
ArangoCursor<BaseDocument> documents = arangoDB.db(database).query(result.query(),
BaseDocument.class,
result.values(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ private static AQLQueryResult convert(String documentCollection,
.append(", ").append(maxResult);
} else if (maxResult > 0) {
aql.append(LIMIT).append(maxResult);
} else if(firstResult > 0) {
aql.append(LIMIT).append(firstResult).append(", null");
}

aql.append(conclusion).append(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.jnosql.communication.semistructured.Elements;
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
Expand All @@ -36,6 +37,7 @@
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;
Expand Down Expand Up @@ -278,6 +280,48 @@ void shouldDeleteAll() {
assertThat(entities).isEmpty();
}


@Test
void shouldIncludeLimit() {
for (int index = 0; index < 20; index++) {
var entity = getEntity();
entity.add("index", index);
entityManager.insert(entity);
}
var select = select().from(COLLECTION_NAME).orderBy("index").asc().limit(4).build();
var entities = entityManager.select(select).toList();
var indexes = entities.stream().map(e -> e.find("index").orElseThrow().get()).toList();
org.assertj.core.api.Assertions.assertThat(indexes).hasSize(4).contains(0, 1, 2, 3);
DeleteQuery deleteQuery = delete().from(COLLECTION_NAME).build();
entityManager.delete(deleteQuery);
}

@Test
void shouldIncludeSkipLimit() {
for (int index = 0; index < 20; index++) {
var entity = getEntity();
entity.add("index", index);
entityManager.insert(entity);
}
var select = select().from(COLLECTION_NAME).orderBy("index").asc().skip(3).limit(4).build();
var entities = entityManager.select(select).toList();
var indexes = entities.stream().map(e -> e.find("index").orElseThrow().get()).toList();
org.assertj.core.api.Assertions.assertThat(indexes).hasSize(4).contains(3, 4, 5, 6);
}

@Test
void shouldIncludeSkip() {
for (int index = 0; index < 20; index++) {
var entity = getEntity();
entity.add("index", index);
entityManager.insert(entity);
}
var select = select().from(COLLECTION_NAME).orderBy("index").asc().skip(5).build();
var entities = entityManager.select(select).toList();
var indexes = entities.stream().map(e -> e.find("index").orElseThrow().get()).toList();
org.assertj.core.api.Assertions.assertThat(indexes).hasSize(15);
}

private CommunicationEntity getEntity() {
CommunicationEntity entity = CommunicationEntity.of(COLLECTION_NAME);
Map<String, Object> map = new HashMap<>();
Expand Down Expand Up @@ -307,4 +351,4 @@ private CommunicationEntity createDocumentList() {
return entity;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package org.eclipse.jnosql.databases.arangodb.integration;


import jakarta.data.page.CursoredPage;
import jakarta.data.page.PageRequest;
import jakarta.inject.Inject;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
import org.eclipse.jnosql.databases.arangodb.communication.ArangoDBConfigurations;
import org.eclipse.jnosql.databases.arangodb.mapping.ArangoDBTemplate;
import org.eclipse.jnosql.mapping.Database;
Expand All @@ -30,9 +33,11 @@
import org.jboss.weld.junit5.auto.AddExtensions;
import org.jboss.weld.junit5.auto.AddPackages;
import org.jboss.weld.junit5.auto.EnableAutoWeld;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

import java.util.List;
import java.util.Optional;

import static java.util.UUID.randomUUID;
Expand Down Expand Up @@ -61,6 +66,11 @@ class ArangoDBTemplateIntegrationTest {
System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), "library");
}

@BeforeEach
void setUp() {
this.template.delete(Book.class).execute();
}

@Test
void shouldInsert() {
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
Expand Down Expand Up @@ -112,7 +122,7 @@ void shouldDelete() {
}

@Test
void shouldDeleteAll(){
void shouldDeleteAll() {
for (int index = 0; index < 20; index++) {
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
assertThat(template.insert(book))
Expand All @@ -126,7 +136,7 @@ void shouldDeleteAll(){


@Test
void shouldUpdateNullValues(){
void shouldUpdateNullValues() {
var book = new Book(randomUUID().toString(), "Effective Java", 1);
template.insert(book);
template.update(new Book(book.id(), null, 2));
Expand All @@ -138,7 +148,76 @@ void shouldUpdateNullValues(){
softly.assertThat(optional).get().extracting(Book::edition).isEqualTo(2);
});
}


@Test
void shouldExecuteLimit() {

for (int index = 1; index < 10; index++) {
var book = new Book(randomUUID().toString(), "Effective Java", index);
template.insert(book);
}

List<Book> books = template.select(Book.class).orderBy("edition")
.asc().limit(4).result();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(books).hasSize(4);
var editions = books.stream().map(Book::edition).toList();
soft.assertThat(editions).hasSize(4).contains(1, 2, 3, 4);
});

}

@Test
void shouldExecuteSkip() {
for (int index = 1; index < 10; index++) {
var book = new Book(randomUUID().toString(), "Effective Java", index);
template.insert(book);
}

List<Book> books = template.select(Book.class).orderBy("edition")
.asc().skip(4).result();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(books).hasSize(5);
var editions = books.stream().map(Book::edition).toList();
soft.assertThat(editions).hasSize(5).contains(5, 6, 7, 8, 9);
});
}

@Test
void shouldExecuteLimitStart() {
for (int index = 1; index < 10; index++) {
var book = new Book(randomUUID().toString(), "Effective Java", index);
template.insert(book);
}

List<Book> books = template.select(Book.class).orderBy("edition")
.asc().skip(4).limit(3).result();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(books).hasSize(3);
var editions = books.stream().map(Book::edition).toList();
soft.assertThat(editions).hasSize(3).contains(5, 6, 7);
});
}

@Test
void shouldSelectCursorSize() {
for (int index = 1; index < 10; index++) {
var book = new Book(randomUUID().toString(), "Effective Java", index);
template.insert(book);
}
var select = SelectQuery.select().from("Book").orderBy("edition").asc()
.skip(4).limit(3).build();
var pageRequest = PageRequest.ofSize(3);
CursoredPage<Book> entities = template.selectCursor(select, pageRequest);

SoftAssertions.assertSoftly(soft -> {
var content = entities.content();
soft.assertThat(content).hasSize(3);
var editions = content.stream().map(Book::edition).toList();
soft.assertThat(editions).hasSize(3).contains(1, 2, 3);
});
}
}

0 comments on commit f25a650

Please sign in to comment.