Skip to content

Commit d6889ea

Browse files
committed
feat: enhance count tests in CouchbaseDocumentManager
Refactored count tests to use assertSoftly for better readability and added additional assertions to verify count behavior with null values and specific queries. Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 67f414a commit d6889ea

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

jnosql-couchbase/src/test/java/org/eclipse/jnosql/databases/couchbase/communication/CouchbaseDocumentManagerTest.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.couchbase.client.core.error.DocumentNotFoundException;
1818
import com.couchbase.client.java.json.JsonObject;
1919
import org.assertj.core.api.Assertions;
20-
import org.assertj.core.api.SoftAssertions;
2120
import org.eclipse.jnosql.communication.Settings;
2221
import org.eclipse.jnosql.communication.TypeReference;
2322
import org.eclipse.jnosql.communication.keyvalue.BucketManager;
@@ -44,6 +43,7 @@
4443

4544
import static java.util.Arrays.asList;
4645
import static org.assertj.core.api.Assertions.assertThat;
46+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
4747
import static org.awaitility.Awaitility.await;
4848
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
4949
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
@@ -80,7 +80,7 @@ public class CouchbaseDocumentManagerTest {
8080

8181
@BeforeEach
8282
@AfterEach
83-
void cleanUpData() throws InterruptedException {
83+
void cleanUpData() throws InterruptedException {
8484
try {
8585
keyValueEntityManagerForPerson.delete("id");
8686
keyValueEntityManagerForPerson.delete("id2");
@@ -206,11 +206,29 @@ void shouldRetrieveListDocumentList() {
206206
}
207207

208208
@Test
209-
void shouldCount() {
210-
CommunicationEntity entity = getEntity();
211-
entityManager.insert(entity);
212-
long counted = entityManager.count(COLLECTION_PERSON_NAME);
213-
assertTrue(counted > 0);
209+
void shouldCount() {
210+
CommunicationEntity entity = entityManager.insert(createSubdocumentList());
211+
Element key = entity.find("_id").get();
212+
var query = select().from(COLLECTION_APP_NAME).where(key.name()).eq(key.get()).build();
213+
214+
assertSoftly(softly -> {
215+
softly.assertThat(entityManager.count(entity.name()))
216+
.as("must return more than zero for person collection")
217+
.isEqualTo(1L);
218+
219+
softly.assertThat(entityManager.count(query))
220+
.isEqualTo(1L);
221+
222+
softly.assertThatThrownBy(()->
223+
entityManager.count((String) null))
224+
.isInstanceOf(NullPointerException.class);
225+
226+
softly.assertThatThrownBy(()->
227+
entityManager.count((SelectQuery) null))
228+
.isInstanceOf(NullPointerException.class);
229+
230+
});
231+
214232
}
215233

216234
private CommunicationEntity createSubdocumentList() {
@@ -277,7 +295,7 @@ void shouldInsertNull() {
277295
entity.add(Element.of("name", null));
278296
CommunicationEntity documentEntity = entityManager.insert(entity);
279297
Optional<Element> name = documentEntity.find("name");
280-
SoftAssertions.assertSoftly(soft -> {
298+
assertSoftly(soft -> {
281299
soft.assertThat(name).isPresent();
282300
soft.assertThat(name).get().extracting(Element::name).isEqualTo("name");
283301
soft.assertThat(name).get().extracting(Element::get).isNull();
@@ -290,7 +308,7 @@ void shouldUpdateNull(){
290308
entity.add(Element.of("name", null));
291309
var documentEntity = entityManager.update(entity);
292310
Optional<Element> name = documentEntity.find("name");
293-
SoftAssertions.assertSoftly(soft -> {
311+
assertSoftly(soft -> {
294312
soft.assertThat(name).isPresent();
295313
soft.assertThat(name).get().extracting(Element::name).isEqualTo("name");
296314
soft.assertThat(name).get().extracting(Element::get).isNull();
@@ -302,11 +320,16 @@ void shouldFindContains() {
302320
var entity = getEntity();
303321

304322
entityManager.insert(entity);
323+
324+
await().until(
325+
() -> !(entityManager.select(select().from(entity.name()).build()).toList().isEmpty())
326+
);
327+
305328
var query = new MappingQuery(Collections.emptyList(), 0L, 0L, CriteriaCondition.contains(Element.of("name",
306329
"lia")), COLLECTION_PERSON_NAME, Collections.emptyList());
307330

308331
var result = entityManager.select(query).toList();
309-
SoftAssertions.assertSoftly(softly -> {
332+
assertSoftly(softly -> {
310333
softly.assertThat(result).hasSize(1);
311334
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
312335
});
@@ -317,11 +340,15 @@ void shouldStartsWith() {
317340
var entity = getEntity();
318341

319342
entityManager.insert(entity);
343+
await().until(
344+
() -> !(entityManager.select(select().from(entity.name()).build()).toList().isEmpty())
345+
);
346+
320347
var query = new MappingQuery(Collections.emptyList(), 0L, 0L, CriteriaCondition.startsWith(Element.of("name",
321348
"Pol")), COLLECTION_PERSON_NAME, Collections.emptyList());
322349

323350
var result = entityManager.select(query).toList();
324-
SoftAssertions.assertSoftly(softly -> {
351+
assertSoftly(softly -> {
325352
softly.assertThat(result).hasSize(1);
326353
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
327354
});
@@ -332,11 +359,16 @@ void shouldEndsWith() {
332359
var entity = getEntity();
333360

334361
entityManager.insert(entity);
362+
363+
await().until(
364+
() -> !(entityManager.select(select().from(entity.name()).build()).toList().isEmpty())
365+
);
366+
335367
var query = new MappingQuery(Collections.emptyList(), 0L, 0L, CriteriaCondition.endsWith(Element.of("name",
336368
"ana")), COLLECTION_PERSON_NAME, Collections.emptyList());
337369

338370
var result = entityManager.select(query).toList();
339-
SoftAssertions.assertSoftly(softly -> {
371+
assertSoftly(softly -> {
340372
softly.assertThat(result).hasSize(1);
341373
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
342374
});

0 commit comments

Comments
 (0)