Skip to content

Have tests remove the documents they insert. #1336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors
* Copyright 2012-2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,6 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -289,13 +288,16 @@ void withExpiryAndExpiryAnnotation()
User found = couchbaseTemplate.findById(user.getClass()).one(user.getId());
if (user.getId().endsWith(UserAnnotated3.class.getSimpleName())) {
if (found == null) {
errorList.add("\nfound should be non null as it was set to have no expiry " + user.getId() );
errorList.add("\nfound should be non null as it was set to have no expiry " + user.getId());
}
} else {
if (found != null) {
errorList.add("\nfound should have been null as document should be expired " + user.getId());
}
}
if (found != null) {
couchbaseTemplate.removeById(user.getClass()).one(user.getId());
}
}

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

@Test
void upsertAndReplaceById() {
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
User user = new User(UUID.randomUUID().toString(), "firstname_upsertAndReplaceById", "lastname");
User modified = couchbaseTemplate.upsertById(User.class).one(user);
assertEquals(user, modified);

Expand All @@ -335,7 +337,6 @@ void upsertAndRemoveById() {
assertEquals(user.getId(), removeResult.getId());
assertTrue(removeResult.getCas() != 0);
assertTrue(removeResult.getMutationToken().isPresent());

assertNull(couchbaseTemplate.findById(User.class).one(user.getId()));
}
{
Expand All @@ -360,6 +361,7 @@ void insertById() {
User inserted = couchbaseTemplate.insertById(User.class).one(user);
assertEquals(user, inserted);
assertThrows(DuplicateKeyException.class, () -> couchbaseTemplate.insertById(User.class).one(user));
couchbaseTemplate.removeById(User.class).one(user.getId());
}

@Test
Expand All @@ -384,6 +386,7 @@ void insertByIdwithDurability() {
}
assertEquals(user, inserted);
assertThrows(DuplicateKeyException.class, () -> couchbaseTemplate.insertById(User.class).one(user));
couchbaseTemplate.removeById(User.class).one(user.getId());
}

@Test
Expand All @@ -395,12 +398,13 @@ void existsById() {
User inserted = couchbaseTemplate.insertById(User.class).one(user);
assertEquals(user, inserted);
assertTrue(couchbaseTemplate.existsById().one(id));
couchbaseTemplate.removeById(User.class).one(user.getId());
}

@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
void saveAndFindImmutableById() {
PersonValue personValue = new PersonValue(UUID.randomUUID().toString(), 123, "f", "l");
PersonValue personValue = new PersonValue(UUID.randomUUID().toString(), 123, "408", "l");
PersonValue inserted = null;
PersonValue upserted = null;
PersonValue replaced = null;
Expand Down Expand Up @@ -431,7 +435,7 @@ void saveAndFindImmutableById() {
PersonValue foundReplaced = couchbaseTemplate.findById(PersonValue.class).one(replaced.getId());
assertNotNull(foundReplaced, "replaced personValue not found");
assertEquals(replaced, foundReplaced);

couchbaseTemplate.removeById(PersonValue.class).one(replaced.getId());
}

private void sleepSecs(int i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,16 @@ public void beforeEach() {
// first call the super method
super.beforeEach();
// then do processing for this class
couchbaseTemplate.removeByQuery(User.class).inCollection(collectionName).all();
couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS)
.inCollection(collectionName).all();
couchbaseTemplate.findByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS)
.inCollection(collectionName).all();
couchbaseTemplate.removeByQuery(Airport.class).inScope(scopeName).inCollection(collectionName).all();
couchbaseTemplate.removeByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(scopeName)
.inCollection(collectionName).all();
couchbaseTemplate.findByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(scopeName)
.inCollection(collectionName).all();
couchbaseTemplate.removeByQuery(Airport.class).inScope(otherScope).inCollection(otherCollection).all();
couchbaseTemplate.removeByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS)
.inScope(otherScope).inCollection(otherCollection).all();
couchbaseTemplate.findByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(otherScope)
.inCollection(otherCollection).all();
}
Expand Down Expand Up @@ -355,7 +358,7 @@ void distinctReactive() {
assertEquals(2, count1);

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

Expand Down Expand Up @@ -384,7 +387,7 @@ public void existsById() { // 1
GetOptions options = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
ExistsOptions existsOptions = ExistsOptions.existsOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
.one(vie);
.one(vie.withIcao("398"));
try {
Boolean exists = couchbaseTemplate.existsById().inScope(scopeName).inCollection(collectionName)
.withOptions(existsOptions).one(saved.getId());
Expand All @@ -399,7 +402,7 @@ public void existsById() { // 1
public void findByAnalytics() { // 2
AnalyticsOptions options = AnalyticsOptions.analyticsOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
.one(vie);
.one(vie.withIcao("413"));
try {
List<Airport> found = couchbaseTemplate.findByAnalytics(Airport.class).inScope(scopeName)
.inCollection(collectionName).withOptions(options).all();
Expand All @@ -413,7 +416,7 @@ public void findByAnalytics() { // 2
public void findById() { // 3
GetOptions options = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
.one(vie);
.one(vie.withIcao("427"));
try {
Airport found = couchbaseTemplate.findById(Airport.class).inScope(scopeName).inCollection(collectionName)
.withOptions(options).one(saved.getId());
Expand All @@ -427,7 +430,7 @@ public void findById() { // 3
public void findByQuery() { // 4
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
.one(vie);
.one(vie.withIcao("441"));
try {
List<Airport> found = couchbaseTemplate.findByQuery(Airport.class)
.withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(scopeName).inCollection(collectionName)
Expand All @@ -442,7 +445,7 @@ public void findByQuery() { // 4
public void findFromReplicasById() { // 5
GetAnyReplicaOptions options = GetAnyReplicaOptions.getAnyReplicaOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
.one(vie);
.one(vie.withIcao("456"));
try {
Airport found = couchbaseTemplate.findFromReplicasById(Airport.class).inScope(scopeName)
.inCollection(collectionName).withOptions(options).any(saved.getId());
Expand Down Expand Up @@ -471,7 +474,7 @@ public void insertById() { // 6
public void removeById() { // 7
RemoveOptions options = RemoveOptions.removeOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
.one(vie);
.one(vie.withIcao("485"));
RemoveResult removeResult = couchbaseTemplate.removeById().inScope(scopeName).inCollection(collectionName)
.withOptions(options).one(saved.getId());
assertEquals(saved.getId(), removeResult.getId());
Expand All @@ -481,7 +484,7 @@ public void removeById() { // 7
public void removeByQuery() { // 8
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
.one(vie);
.one(vie.withIcao("495"));
List<RemoveResult> removeResults = couchbaseTemplate.removeByQuery(Airport.class)
.withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(scopeName).inCollection(collectionName)
.withOptions(options).matching(Query.query(QueryCriteria.where("iata").is(vie.getIata()))).all();
Expand All @@ -494,7 +497,7 @@ public void replaceById() { // 9
ReplaceOptions options = ReplaceOptions.replaceOptions().timeout(Duration.ofSeconds(10));
GetOptions getOptions = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName)
.withOptions(insertOptions).one(vie);
.withOptions(insertOptions).one(vie.withIcao("508"));
Airport replaced = couchbaseTemplate.replaceById(Airport.class).inScope(scopeName).inCollection(collectionName)
.withOptions(options).one(vie.withIcao("newIcao"));
try {
Expand All @@ -512,7 +515,7 @@ public void upsertById() { // 10
GetOptions getOptions = GetOptions.getOptions().timeout(Duration.ofSeconds(10));

Airport saved = couchbaseTemplate.upsertById(Airport.class).inScope(scopeName).inCollection(collectionName)
.withOptions(options).one(vie);
.withOptions(options).one(vie.withIcao("526"));
try {
Airport found = couchbaseTemplate.findById(Airport.class).inScope(scopeName).inCollection(collectionName)
.withOptions(getOptions).one(saved.getId());
Expand All @@ -527,13 +530,14 @@ public void existsByIdOther() { // 1
GetOptions options = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
ExistsOptions existsOptions = ExistsOptions.existsOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.one(vie);
.one(vie.withIcao("lowg"));

try {
Boolean exists = couchbaseTemplate.existsById().inScope(otherScope).inCollection(otherCollection)
.withOptions(existsOptions).one(saved.getId());
assertTrue(exists, "Airport should exist: " + saved.getId());
.withOptions(existsOptions).one(vie.getId());
assertTrue(exists, "Airport should exist: " + vie.getId());
} finally {
couchbaseTemplate.removeById().inScope(otherScope).inCollection(otherCollection).one(saved.getId());
couchbaseTemplate.removeById().inScope(otherScope).inCollection(otherCollection).one(vie.getId());
}
}

Expand All @@ -542,7 +546,7 @@ public void existsByIdOther() { // 1
public void findByAnalyticsOther() { // 2
AnalyticsOptions options = AnalyticsOptions.analyticsOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.one(vie);
.one(vie.withIcao("566"));
try {
List<Airport> found = couchbaseTemplate.findByAnalytics(Airport.class).inScope(otherScope)
.inCollection(otherCollection).withOptions(options).all();
Expand All @@ -556,7 +560,7 @@ public void findByAnalyticsOther() { // 2
public void findByIdOther() { // 3
GetOptions options = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.one(vie);
.one(vie.withIcao("580"));
try {
Airport found = couchbaseTemplate.findById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.withOptions(options).one(saved.getId());
Expand All @@ -570,7 +574,7 @@ public void findByIdOther() { // 3
public void findByQueryOther() { // 4
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.one(vie);
.one(vie.withIcao("594"));
try {
List<Airport> found = couchbaseTemplate.findByQuery(Airport.class)
.withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(otherScope).inCollection(otherCollection)
Expand All @@ -585,7 +589,7 @@ public void findByQueryOther() { // 4
public void findFromReplicasByIdOther() { // 5
GetAnyReplicaOptions options = GetAnyReplicaOptions.getAnyReplicaOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.one(vie);
.one(vie.withIcao("609"));
try {
Airport found = couchbaseTemplate.findFromReplicasById(Airport.class).inScope(otherScope)
.inCollection(otherCollection).withOptions(options).any(saved.getId());
Expand Down Expand Up @@ -614,7 +618,7 @@ public void insertByIdOther() { // 6
public void removeByIdOther() { // 7
RemoveOptions options = RemoveOptions.removeOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.one(vie);
.one(vie.withIcao("638"));
RemoveResult removeResult = couchbaseTemplate.removeById().inScope(otherScope).inCollection(otherCollection)
.withOptions(options).one(saved.getId());
assertEquals(saved.getId(), removeResult.getId());
Expand All @@ -624,7 +628,7 @@ public void removeByIdOther() { // 7
public void removeByQueryOther() { // 8
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.one(vie);
.one(vie.withIcao("648"));
List<RemoveResult> removeResults = couchbaseTemplate.removeByQuery(Airport.class)
.withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(otherScope).inCollection(otherCollection)
.withOptions(options).matching(Query.query(QueryCriteria.where("iata").is(vie.getIata()))).all();
Expand All @@ -637,7 +641,7 @@ public void replaceByIdOther() { // 9
ReplaceOptions options = ReplaceOptions.replaceOptions().timeout(Duration.ofSeconds(10));
GetOptions getOptions = GetOptions.getOptions().timeout(Duration.ofSeconds(10));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.withOptions(insertOptions).one(vie);
.withOptions(insertOptions).one(vie.withIcao("661"));
Airport replaced = couchbaseTemplate.replaceById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.withOptions(options).one(vie.withIcao("newIcao"));
try {
Expand All @@ -655,7 +659,7 @@ public void upsertByIdOther() { // 10
GetOptions getOptions = GetOptions.getOptions().timeout(Duration.ofSeconds(10));

Airport saved = couchbaseTemplate.upsertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.withOptions(options).one(vie);
.withOptions(options).one(vie.withIcao("679"));
try {
Airport found = couchbaseTemplate.findById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.withOptions(getOptions).one(saved.getId());
Expand Down Expand Up @@ -699,7 +703,7 @@ public void findByQueryOptions() { // 4
public void findFromReplicasByIdOptions() { // 5
GetAnyReplicaOptions options = GetAnyReplicaOptions.getAnyReplicaOptions().timeout(Duration.ofNanos(1000));
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.one(vie);
.one(vie.withIcao("723"));
try {
Airport found = couchbaseTemplate.findFromReplicasById(Airport.class).inScope(otherScope)
.inCollection(otherCollection).withOptions(options).any(saved.getId());
Expand All @@ -719,7 +723,7 @@ public void insertByIdOptions() { // 6
@Test
public void removeByIdOptions() { // 7 - options
Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection)
.one(vie);
.one(vie.withIcao("743"));
RemoveOptions options = RemoveOptions.removeOptions().timeout(Duration.ofNanos(10));
assertThrows(AmbiguousTimeoutException.class, () -> couchbaseTemplate.removeById().inScope(otherScope)
.inCollection(otherCollection).withOptions(options).one(vie.getId()));
Expand All @@ -746,7 +750,7 @@ public void replaceByIdOptions() { // 9 - options
public void upsertByIdOptions() { // 10 - options
UpsertOptions options = UpsertOptions.upsertOptions().timeout(Duration.ofNanos(10));
assertThrows(AmbiguousTimeoutException.class, () -> couchbaseTemplate.upsertById(Airport.class).inScope(otherScope)
.inCollection(otherCollection).withOptions(options).one(vie));
.inCollection(otherCollection).withOptions(options).one(vie.withIcao("770")));
}

@Test
Expand Down
Loading