Skip to content

Commit d4f37c4

Browse files
committed
Have tests remove the documents they insert. (#1336)
Closes #1335.
1 parent 6759dfa commit d4f37c4

9 files changed

+173
-118
lines changed

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@
2727
import java.lang.reflect.Constructor;
2828
import java.lang.reflect.InvocationTargetException;
2929
import java.time.Duration;
30-
import java.time.Instant;
3130
import java.util.Arrays;
3231
import java.util.Collection;
3332
import java.util.HashSet;
@@ -289,13 +288,16 @@ void withExpiryAndExpiryAnnotation()
289288
User found = couchbaseTemplate.findById(user.getClass()).one(user.getId());
290289
if (user.getId().endsWith(UserAnnotated3.class.getSimpleName())) {
291290
if (found == null) {
292-
errorList.add("\nfound should be non null as it was set to have no expiry " + user.getId() );
291+
errorList.add("\nfound should be non null as it was set to have no expiry " + user.getId());
293292
}
294293
} else {
295294
if (found != null) {
296295
errorList.add("\nfound should have been null as document should be expired " + user.getId());
297296
}
298297
}
298+
if (found != null) {
299+
couchbaseTemplate.removeById(user.getClass()).one(user.getId());
300+
}
299301
}
300302

301303
if (!errorList.isEmpty()) {
@@ -310,7 +312,7 @@ void findDocWhichDoesNotExist() {
310312

311313
@Test
312314
void upsertAndReplaceById() {
313-
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
315+
User user = new User(UUID.randomUUID().toString(), "firstname_upsertAndReplaceById", "lastname");
314316
User modified = couchbaseTemplate.upsertById(User.class).one(user);
315317
assertEquals(user, modified);
316318

@@ -335,7 +337,6 @@ void upsertAndRemoveById() {
335337
assertEquals(user.getId(), removeResult.getId());
336338
assertTrue(removeResult.getCas() != 0);
337339
assertTrue(removeResult.getMutationToken().isPresent());
338-
339340
assertNull(couchbaseTemplate.findById(User.class).one(user.getId()));
340341
}
341342
{
@@ -360,6 +361,7 @@ void insertById() {
360361
User inserted = couchbaseTemplate.insertById(User.class).one(user);
361362
assertEquals(user, inserted);
362363
assertThrows(DuplicateKeyException.class, () -> couchbaseTemplate.insertById(User.class).one(user));
364+
couchbaseTemplate.removeById(User.class).one(user.getId());
363365
}
364366

365367
@Test
@@ -384,6 +386,7 @@ void insertByIdwithDurability() {
384386
}
385387
assertEquals(user, inserted);
386388
assertThrows(DuplicateKeyException.class, () -> couchbaseTemplate.insertById(User.class).one(user));
389+
couchbaseTemplate.removeById(User.class).one(user.getId());
387390
}
388391

389392
@Test
@@ -395,12 +398,13 @@ void existsById() {
395398
User inserted = couchbaseTemplate.insertById(User.class).one(user);
396399
assertEquals(user, inserted);
397400
assertTrue(couchbaseTemplate.existsById().one(id));
401+
couchbaseTemplate.removeById(User.class).one(user.getId());
398402
}
399403

400404
@Test
401405
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
402406
void saveAndFindImmutableById() {
403-
PersonValue personValue = new PersonValue(UUID.randomUUID().toString(), 123, "f", "l");
407+
PersonValue personValue = new PersonValue(UUID.randomUUID().toString(), 123, "408", "l");
404408
PersonValue inserted = null;
405409
PersonValue upserted = null;
406410
PersonValue replaced = null;
@@ -431,7 +435,7 @@ void saveAndFindImmutableById() {
431435
PersonValue foundReplaced = couchbaseTemplate.findById(PersonValue.class).one(replaced.getId());
432436
assertNotNull(foundReplaced, "replaced personValue not found");
433437
assertEquals(replaced, foundReplaced);
434-
438+
couchbaseTemplate.removeById(PersonValue.class).one(replaced.getId());
435439
}
436440

437441
private void sleepSecs(int i) {

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryCollectionIntegrationTests.java

+32-28
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,16 @@ public void beforeEach() {
103103
// first call the super method
104104
super.beforeEach();
105105
// then do processing for this class
106-
couchbaseTemplate.removeByQuery(User.class).inCollection(collectionName).all();
106+
couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS)
107+
.inCollection(collectionName).all();
107108
couchbaseTemplate.findByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS)
108109
.inCollection(collectionName).all();
109-
couchbaseTemplate.removeByQuery(Airport.class).inScope(scopeName).inCollection(collectionName).all();
110+
couchbaseTemplate.removeByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(scopeName)
111+
.inCollection(collectionName).all();
110112
couchbaseTemplate.findByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(scopeName)
111113
.inCollection(collectionName).all();
112-
couchbaseTemplate.removeByQuery(Airport.class).inScope(otherScope).inCollection(otherCollection).all();
114+
couchbaseTemplate.removeByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS)
115+
.inScope(otherScope).inCollection(otherCollection).all();
113116
couchbaseTemplate.findByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(otherScope)
114117
.inCollection(otherCollection).all();
115118
}
@@ -355,7 +358,7 @@ void distinctReactive() {
355358
assertEquals(2, count1);
356359

357360
// count (distinct { iata, icao } )
358-
Long count2 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] {"iata", "icao"})
361+
Long count2 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "iata", "icao" })
359362
.withConsistency(QueryScanConsistency.REQUEST_PLUS).inCollection(collectionName).count().block();
360363
assertEquals(7, count2);
361364

@@ -384,7 +387,7 @@ public void existsById() { // 1
384387
GetOptions options = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
385388
ExistsOptions existsOptions = ExistsOptions.existsOptions().timeout(Duration.ofSeconds(10));
386389
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
387-
.one(vie);
390+
.one(vie.withIcao("398"));
388391
try {
389392
Boolean exists = couchbaseTemplate.existsById().inScope(scopeName).inCollection(collectionName)
390393
.withOptions(existsOptions).one(saved.getId());
@@ -399,7 +402,7 @@ public void existsById() { // 1
399402
public void findByAnalytics() { // 2
400403
AnalyticsOptions options = AnalyticsOptions.analyticsOptions().timeout(Duration.ofSeconds(10));
401404
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
402-
.one(vie);
405+
.one(vie.withIcao("413"));
403406
try {
404407
List<Airport> found = couchbaseTemplate.findByAnalytics(Airport.class).inScope(scopeName)
405408
.inCollection(collectionName).withOptions(options).all();
@@ -413,7 +416,7 @@ public void findByAnalytics() { // 2
413416
public void findById() { // 3
414417
GetOptions options = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
415418
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
416-
.one(vie);
419+
.one(vie.withIcao("427"));
417420
try {
418421
Airport found = couchbaseTemplate.findById(Airport.class).inScope(scopeName).inCollection(collectionName)
419422
.withOptions(options).one(saved.getId());
@@ -427,7 +430,7 @@ public void findById() { // 3
427430
public void findByQuery() { // 4
428431
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
429432
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
430-
.one(vie);
433+
.one(vie.withIcao("441"));
431434
try {
432435
List<Airport> found = couchbaseTemplate.findByQuery(Airport.class)
433436
.withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(scopeName).inCollection(collectionName)
@@ -442,7 +445,7 @@ public void findByQuery() { // 4
442445
public void findFromReplicasById() { // 5
443446
GetAnyReplicaOptions options = GetAnyReplicaOptions.getAnyReplicaOptions().timeout(Duration.ofSeconds(10));
444447
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
445-
.one(vie);
448+
.one(vie.withIcao("456"));
446449
try {
447450
Airport found = couchbaseTemplate.findFromReplicasById(Airport.class).inScope(scopeName)
448451
.inCollection(collectionName).withOptions(options).any(saved.getId());
@@ -471,7 +474,7 @@ public void insertById() { // 6
471474
public void removeById() { // 7
472475
RemoveOptions options = RemoveOptions.removeOptions().timeout(Duration.ofSeconds(10));
473476
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
474-
.one(vie);
477+
.one(vie.withIcao("485"));
475478
RemoveResult removeResult = couchbaseTemplate.removeById().inScope(scopeName).inCollection(collectionName)
476479
.withOptions(options).one(saved.getId());
477480
assertEquals(saved.getId(), removeResult.getId());
@@ -481,7 +484,7 @@ public void removeById() { // 7
481484
public void removeByQuery() { // 8
482485
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
483486
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
484-
.one(vie);
487+
.one(vie.withIcao("495"));
485488
List<RemoveResult> removeResults = couchbaseTemplate.removeByQuery(Airport.class)
486489
.withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(scopeName).inCollection(collectionName)
487490
.withOptions(options).matching(Query.query(QueryCriteria.where("iata").is(vie.getIata()))).all();
@@ -494,7 +497,7 @@ public void replaceById() { // 9
494497
ReplaceOptions options = ReplaceOptions.replaceOptions().timeout(Duration.ofSeconds(10));
495498
GetOptions getOptions = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
496499
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
497-
.withOptions(insertOptions).one(vie);
500+
.withOptions(insertOptions).one(vie.withIcao("508"));
498501
Airport replaced = couchbaseTemplate.replaceById(Airport.class).inScope(scopeName).inCollection(collectionName)
499502
.withOptions(options).one(vie.withIcao("newIcao"));
500503
try {
@@ -512,7 +515,7 @@ public void upsertById() { // 10
512515
GetOptions getOptions = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
513516

514517
Airport saved = couchbaseTemplate.upsertById(Airport.class).inScope(scopeName).inCollection(collectionName)
515-
.withOptions(options).one(vie);
518+
.withOptions(options).one(vie.withIcao("526"));
516519
try {
517520
Airport found = couchbaseTemplate.findById(Airport.class).inScope(scopeName).inCollection(collectionName)
518521
.withOptions(getOptions).one(saved.getId());
@@ -527,13 +530,14 @@ public void existsByIdOther() { // 1
527530
GetOptions options = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
528531
ExistsOptions existsOptions = ExistsOptions.existsOptions().timeout(Duration.ofSeconds(10));
529532
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
530-
.one(vie);
533+
.one(vie.withIcao("lowg"));
534+
531535
try {
532536
Boolean exists = couchbaseTemplate.existsById().inScope(otherScope).inCollection(otherCollection)
533-
.withOptions(existsOptions).one(saved.getId());
534-
assertTrue(exists, "Airport should exist: " + saved.getId());
537+
.withOptions(existsOptions).one(vie.getId());
538+
assertTrue(exists, "Airport should exist: " + vie.getId());
535539
} finally {
536-
couchbaseTemplate.removeById().inScope(otherScope).inCollection(otherCollection).one(saved.getId());
540+
couchbaseTemplate.removeById().inScope(otherScope).inCollection(otherCollection).one(vie.getId());
537541
}
538542
}
539543

@@ -542,7 +546,7 @@ public void existsByIdOther() { // 1
542546
public void findByAnalyticsOther() { // 2
543547
AnalyticsOptions options = AnalyticsOptions.analyticsOptions().timeout(Duration.ofSeconds(10));
544548
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
545-
.one(vie);
549+
.one(vie.withIcao("566"));
546550
try {
547551
List<Airport> found = couchbaseTemplate.findByAnalytics(Airport.class).inScope(otherScope)
548552
.inCollection(otherCollection).withOptions(options).all();
@@ -556,7 +560,7 @@ public void findByAnalyticsOther() { // 2
556560
public void findByIdOther() { // 3
557561
GetOptions options = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
558562
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
559-
.one(vie);
563+
.one(vie.withIcao("580"));
560564
try {
561565
Airport found = couchbaseTemplate.findById(Airport.class).inScope(otherScope).inCollection(otherCollection)
562566
.withOptions(options).one(saved.getId());
@@ -570,7 +574,7 @@ public void findByIdOther() { // 3
570574
public void findByQueryOther() { // 4
571575
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
572576
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
573-
.one(vie);
577+
.one(vie.withIcao("594"));
574578
try {
575579
List<Airport> found = couchbaseTemplate.findByQuery(Airport.class)
576580
.withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(otherScope).inCollection(otherCollection)
@@ -585,7 +589,7 @@ public void findByQueryOther() { // 4
585589
public void findFromReplicasByIdOther() { // 5
586590
GetAnyReplicaOptions options = GetAnyReplicaOptions.getAnyReplicaOptions().timeout(Duration.ofSeconds(10));
587591
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
588-
.one(vie);
592+
.one(vie.withIcao("609"));
589593
try {
590594
Airport found = couchbaseTemplate.findFromReplicasById(Airport.class).inScope(otherScope)
591595
.inCollection(otherCollection).withOptions(options).any(saved.getId());
@@ -614,7 +618,7 @@ public void insertByIdOther() { // 6
614618
public void removeByIdOther() { // 7
615619
RemoveOptions options = RemoveOptions.removeOptions().timeout(Duration.ofSeconds(10));
616620
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
617-
.one(vie);
621+
.one(vie.withIcao("638"));
618622
RemoveResult removeResult = couchbaseTemplate.removeById().inScope(otherScope).inCollection(otherCollection)
619623
.withOptions(options).one(saved.getId());
620624
assertEquals(saved.getId(), removeResult.getId());
@@ -624,7 +628,7 @@ public void removeByIdOther() { // 7
624628
public void removeByQueryOther() { // 8
625629
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
626630
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
627-
.one(vie);
631+
.one(vie.withIcao("648"));
628632
List<RemoveResult> removeResults = couchbaseTemplate.removeByQuery(Airport.class)
629633
.withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(otherScope).inCollection(otherCollection)
630634
.withOptions(options).matching(Query.query(QueryCriteria.where("iata").is(vie.getIata()))).all();
@@ -637,7 +641,7 @@ public void replaceByIdOther() { // 9
637641
ReplaceOptions options = ReplaceOptions.replaceOptions().timeout(Duration.ofSeconds(10));
638642
GetOptions getOptions = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
639643
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
640-
.withOptions(insertOptions).one(vie);
644+
.withOptions(insertOptions).one(vie.withIcao("661"));
641645
Airport replaced = couchbaseTemplate.replaceById(Airport.class).inScope(otherScope).inCollection(otherCollection)
642646
.withOptions(options).one(vie.withIcao("newIcao"));
643647
try {
@@ -655,7 +659,7 @@ public void upsertByIdOther() { // 10
655659
GetOptions getOptions = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
656660

657661
Airport saved = couchbaseTemplate.upsertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
658-
.withOptions(options).one(vie);
662+
.withOptions(options).one(vie.withIcao("679"));
659663
try {
660664
Airport found = couchbaseTemplate.findById(Airport.class).inScope(otherScope).inCollection(otherCollection)
661665
.withOptions(getOptions).one(saved.getId());
@@ -699,7 +703,7 @@ public void findByQueryOptions() { // 4
699703
public void findFromReplicasByIdOptions() { // 5
700704
GetAnyReplicaOptions options = GetAnyReplicaOptions.getAnyReplicaOptions().timeout(Duration.ofNanos(1000));
701705
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
702-
.one(vie);
706+
.one(vie.withIcao("723"));
703707
try {
704708
Airport found = couchbaseTemplate.findFromReplicasById(Airport.class).inScope(otherScope)
705709
.inCollection(otherCollection).withOptions(options).any(saved.getId());
@@ -719,7 +723,7 @@ public void insertByIdOptions() { // 6
719723
@Test
720724
public void removeByIdOptions() { // 7 - options
721725
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
722-
.one(vie);
726+
.one(vie.withIcao("743"));
723727
RemoveOptions options = RemoveOptions.removeOptions().timeout(Duration.ofNanos(10));
724728
assertThrows(AmbiguousTimeoutException.class, () -> couchbaseTemplate.removeById().inScope(otherScope)
725729
.inCollection(otherCollection).withOptions(options).one(vie.getId()));
@@ -746,7 +750,7 @@ public void replaceByIdOptions() { // 9 - options
746750
public void upsertByIdOptions() { // 10 - options
747751
UpsertOptions options = UpsertOptions.upsertOptions().timeout(Duration.ofNanos(10));
748752
assertThrows(AmbiguousTimeoutException.class, () -> couchbaseTemplate.upsertById(Airport.class).inScope(otherScope)
749-
.inCollection(otherCollection).withOptions(options).one(vie));
753+
.inCollection(otherCollection).withOptions(options).one(vie.withIcao("770")));
750754
}
751755

752756
@Test

0 commit comments

Comments
 (0)