DATACOUCH-615 Use query in removeByQuery (#264)

* DATACOUCH-615 Use query in removeByQuery

Use provided query instead of hardcoded statement.

* DATACOUCH-615 Better method naming in Query class

* DATACOUCH-615 Replace hard-coded meta fields

* DATACOUCH-615 Test findByQuery.matching

* DATACOUCH-615 Remove failing test
This commit is contained in:
Haris Alesevic
2020-10-05 19:11:37 +02:00
committed by GitHub
parent 2a06578145
commit fab72ee9c9
7 changed files with 54 additions and 28 deletions

View File

@@ -32,9 +32,10 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.core.query.QueryCriteria;
import org.springframework.data.couchbase.domain.Config;
import org.springframework.data.couchbase.domain.NaiveAuditorAware;
import org.springframework.data.couchbase.domain.User;
@@ -46,13 +47,13 @@ import org.springframework.data.couchbase.util.IgnoreWhen;
import com.couchbase.client.core.error.IndexExistsException;
import com.couchbase.client.java.query.QueryScanConsistency;
import reactor.core.publisher.Mono;
/**
* Query tests Theses tests rely on a cb server running
*
* @author Michael Nitschinger
* @author Michael Reiche
* @author Haris Alesevic
*/
@IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED)
class CouchbaseTemplateQueryIntegrationTests extends ClusterAwareIntegrationTests {
@@ -82,10 +83,12 @@ class CouchbaseTemplateQueryIntegrationTests extends ClusterAwareIntegrationTest
ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE);
// ensure each test starts with clean state
couchbaseTemplate.removeByQuery(User.class).all();
}
@Test
void findByQuery() {
void findByQueryAll() {
try {
User user1 = new User(UUID.randomUUID().toString(), "user1", "user1");
User user2 = new User(UUID.randomUUID().toString(), "user2", "user2");
@@ -128,7 +131,7 @@ class CouchbaseTemplateQueryIntegrationTests extends ClusterAwareIntegrationTest
}
@Test
void removeByQuery() {
void removeByQueryAll() {
User user1 = new User(UUID.randomUUID().toString(), "user1", "user1");
User user2 = new User(UUID.randomUUID().toString(), "user2", "user2");
@@ -144,4 +147,28 @@ class CouchbaseTemplateQueryIntegrationTests extends ClusterAwareIntegrationTest
}
@Test
void removeByMatchingQuery() {
User user1 = new User(UUID.randomUUID().toString(), "user1", "user1");
User user2 = new User(UUID.randomUUID().toString(), "user2", "user2");
User specialUser = new User(UUID.randomUUID().toString(), "special", "special");
couchbaseTemplate.upsertById(User.class).all(Arrays.asList(user1, user2, specialUser));
assertTrue(couchbaseTemplate.existsById().one(user1.getId()));
assertTrue(couchbaseTemplate.existsById().one(user2.getId()));
assertTrue(couchbaseTemplate.existsById().one(specialUser.getId()));
Query nonSpecialUsers = new Query(QueryCriteria.where("firstname").notLike("special"));
couchbaseTemplate.removeByQuery(User.class).consistentWith(QueryScanConsistency.REQUEST_PLUS)
.matching(nonSpecialUsers)
.all();
assertNull(couchbaseTemplate.findById(User.class).one(user1.getId()));
assertNull(couchbaseTemplate.findById(User.class).one(user2.getId()));
assertNotNull(couchbaseTemplate.findById(User.class).one(specialUser.getId()));
}
}

View File

@@ -22,22 +22,16 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.ExecutableFindByQueryOperation;
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.domain.Airline;
import org.springframework.data.couchbase.domain.AirlineRepository;
import org.springframework.data.couchbase.domain.User;
import org.springframework.data.couchbase.domain.UserRepository;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.couchbase.util.Capabilities;
import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.core.NamedQueries;
@@ -46,9 +40,7 @@ import org.springframework.data.repository.core.support.PropertiesBasedNamedQuer
import org.springframework.data.repository.query.*;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.Properties;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
@@ -88,7 +80,7 @@ class StringN1qlQueryCreatorMockedTests extends ClusterAwareIntegrationTests {
Query query = creator.createQuery();
assertEquals(
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `travel-sample`.* FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and firstname = $1 and lastname = $2",
query.toN1qlString(couchbaseTemplate.reactive(), User.class, false));
query.toN1qlSelectString(couchbaseTemplate.reactive(), User.class, false));
}
@Test
@@ -106,7 +98,7 @@ class StringN1qlQueryCreatorMockedTests extends ClusterAwareIntegrationTests {
Query query = creator.createQuery();
assertEquals(
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `travel-sample`.* FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and (firstname = $first or lastname = $last)",
query.toN1qlString(couchbaseTemplate.reactive(), User.class, false));
query.toN1qlSelectString(couchbaseTemplate.reactive(), User.class, false));
}
@Test

View File

@@ -25,7 +25,6 @@ import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
@@ -91,7 +90,7 @@ class StringN1qlQueryCreatorTests extends ClusterAwareIntegrationTests {
queryMethod, converter, config().bucketname(), QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
Query query = creator.createQuery();
System.out.println(query.toN1qlString(couchbaseTemplate.reactive(), Airline.class, false));
System.out.println(query.toN1qlSelectString(couchbaseTemplate.reactive(), Airline.class, false));
try {
Thread.sleep(3000);