Skip to content

Commit 8d5a22f

Browse files
committed
feat: add count method for SelectQuery
Implemented a count method in DefaultArangoDBDocumentManager to count documents based on a SelectQuery, ensuring null queries are handled appropriately. Signed-off-by: Maximillian Arruda <[email protected]>
1 parent ecf2b18 commit 8d5a22f

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/DefaultArangoDBDocumentManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ public long count(String documentCollection) {
148148
.map(Number::longValue).orElse(0L);
149149
}
150150

151+
@Override
152+
public long count(SelectQuery query) {
153+
requireNonNull(query, "query is required");
154+
checkCollection(query.name());
155+
AQLQueryResult aqlQuery = QueryAQLConverter.count(query);
156+
LOGGER.finest("Executing AQL: " + aqlQuery.query());
157+
return aql(aqlQuery.query(), aqlQuery.values(), Long.class).findFirst().orElse(0L);
158+
}
151159

152160
@Override
153161
public Stream<CommunicationEntity> aql(String query, Map<String, Object> params) throws NullPointerException {

jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBDocumentManagerTest.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import static java.util.Collections.emptyMap;
4747
import static java.util.Collections.singletonMap;
4848
import static org.assertj.core.api.Assertions.assertThat;
49+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
4950
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
5051
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
5152
import static org.eclipse.jnosql.communication.semistructured.DeleteQuery.delete;
@@ -225,6 +226,22 @@ void shouldCount() {
225226
assertTrue(entityManager.count(COLLECTION_NAME) > 0);
226227
}
227228

229+
@Test
230+
void shouldCountWithSelectQuery() {
231+
CommunicationEntity entity = entityManager.insert(createDocumentListNotHavingId());
232+
Element key = entity.find(KEY_NAME).get();
233+
SelectQuery query = select().from("AppointmentBook").where(key.name()).eq(key.get()).build();
234+
235+
assertSoftly(softly -> {
236+
softly.assertThat(entityManager.count(query))
237+
.as("should count documents matching the query")
238+
.isEqualTo(1L);
239+
softly.assertThatThrownBy(() -> entityManager.count((SelectQuery) null))
240+
.as("must not accept null query")
241+
.isInstanceOf(NullPointerException.class);
242+
});
243+
}
244+
228245
@Test
229246
void shouldReadFromDifferentBaseDocumentUsingInstance() {
230247
entityManager.insert(getEntity());
@@ -273,7 +290,7 @@ void shouldInsertNull() {
273290
entity.add(Element.of("name", null));
274291
CommunicationEntity documentEntity = entityManager.insert(entity);
275292
Optional<Element> name = documentEntity.find("name");
276-
SoftAssertions.assertSoftly(soft -> {
293+
assertSoftly(soft -> {
277294
soft.assertThat(name).isPresent();
278295
soft.assertThat(name).get().extracting(Element::name).isEqualTo("name");
279296
soft.assertThat(name).get().extracting(Element::get).isNull();
@@ -286,7 +303,7 @@ void shouldUpdateNull(){
286303
entity.add(Element.of("name", null));
287304
var documentEntity = entityManager.update(entity);
288305
Optional<Element> name = documentEntity.find("name");
289-
SoftAssertions.assertSoftly(soft -> {
306+
assertSoftly(soft -> {
290307
soft.assertThat(name).isPresent();
291308
soft.assertThat(name).get().extracting(Element::name).isEqualTo("name");
292309
soft.assertThat(name).get().extracting(Element::get).isNull();
@@ -361,7 +378,7 @@ void shouldInsertUUID() {
361378
entity.add("uuid", UUID.randomUUID());
362379
var documentEntity = entityManager.insert(entity);
363380
Optional<Element> uuid = documentEntity.find("uuid");
364-
SoftAssertions.assertSoftly(soft -> {
381+
assertSoftly(soft -> {
365382
soft.assertThat(uuid).isPresent();
366383
Element element = uuid.orElseThrow();
367384
soft.assertThat(element.name()).isEqualTo("uuid");
@@ -382,7 +399,7 @@ void shouldFindBetween() {
382399

383400
var result = entityManager.select(query).toList();
384401

385-
SoftAssertions.assertSoftly(softly -> {
402+
assertSoftly(softly -> {
386403
softly.assertThat(result).hasSize(2);
387404
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).contains(22, 23);
388405
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).doesNotContain(25);
@@ -402,7 +419,7 @@ void shouldFindBetween2() {
402419

403420
var result = entityManager.select(query).toList();
404421

405-
SoftAssertions.assertSoftly(softly -> {
422+
assertSoftly(softly -> {
406423
softly.assertThat(result).hasSize(2);
407424
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).contains(22, 23);
408425
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).doesNotContain(25);
@@ -418,7 +435,7 @@ void shouldFindContains() {
418435
"lia")), COLLECTION_NAME, Collections.emptyList());
419436

420437
var result = entityManager.select(query).toList();
421-
SoftAssertions.assertSoftly(softly -> {
438+
assertSoftly(softly -> {
422439
softly.assertThat(result).hasSize(1);
423440
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
424441
});
@@ -433,7 +450,7 @@ void shouldStartsWith() {
433450
"Pol")), COLLECTION_NAME, Collections.emptyList());
434451

435452
var result = entityManager.select(query).toList();
436-
SoftAssertions.assertSoftly(softly -> {
453+
assertSoftly(softly -> {
437454
softly.assertThat(result).hasSize(1);
438455
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
439456
});
@@ -448,7 +465,7 @@ void shouldEndsWith() {
448465
"ana")), COLLECTION_NAME, Collections.emptyList());
449466

450467
var result = entityManager.select(query).toList();
451-
SoftAssertions.assertSoftly(softly -> {
468+
assertSoftly(softly -> {
452469
softly.assertThat(result).hasSize(1);
453470
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
454471
});
@@ -481,7 +498,7 @@ void shouldCreateEdge() {
481498
);
482499

483500
var result = entityManager.aql(aql, parameters).toList();
484-
SoftAssertions.assertSoftly(softly -> softly.assertThat(result).isNotEmpty());
501+
assertSoftly(softly -> softly.assertThat(result).isNotEmpty());
485502

486503
entityManager.remove(person1, "FRIEND", person2);
487504
}
@@ -504,7 +521,7 @@ void shouldRemoveEdge() {
504521
""";
505522
Map<String, Object> parameters = Map.of("edgeId", edge.id());
506523
var result = entityManager.aql(aql, parameters).toList();
507-
SoftAssertions.assertSoftly(softly -> softly.assertThat(result).isEmpty());
524+
assertSoftly(softly -> softly.assertThat(result).isEmpty());
508525
}
509526

510527
@Test
@@ -520,7 +537,7 @@ void shouldDeleteEdgeById() {
520537
""";
521538
Map<String, Object> parameters = Map.of("id", edge.id());
522539
var result = entityManager.aql(aql, parameters).toList();
523-
SoftAssertions.assertSoftly(softly -> softly.assertThat(result).isEmpty());
540+
assertSoftly(softly -> softly.assertThat(result).isEmpty());
524541
}
525542

526543
@Test
@@ -532,7 +549,7 @@ void shouldFindEdgeById() {
532549
var edgeId = edge.id();
533550
var retrievedEdge = entityManager.findEdgeById(edgeId);
534551

535-
SoftAssertions.assertSoftly(softly -> {
552+
assertSoftly(softly -> {
536553
softly.assertThat(retrievedEdge).isPresent();
537554
softly.assertThat(retrievedEdge.get().label()).isEqualTo("FRIEND");
538555
softly.assertThat(retrievedEdge.get().properties()).containsEntry("since", 2020);
@@ -555,7 +572,7 @@ void shouldCreateEdgeWithProperties() {
555572
Map<String, Object> parameters = Map.of("edgeId", edge.id());
556573

557574
var result = entityManager.aql(aql, parameters).toList();
558-
SoftAssertions.assertSoftly(softly -> {
575+
assertSoftly(softly -> {
559576
softly.assertThat(result).isNotEmpty();
560577
softly.assertThat(edge.properties()).containsEntry("since", 2019);
561578
softly.assertThat(edge.properties()).containsEntry("strength", "strong");

0 commit comments

Comments
 (0)