|
39 | 39 | import org.springframework.data.couchbase.core.query.Query;
|
40 | 40 | import org.springframework.data.couchbase.domain.Airline;
|
41 | 41 | import org.springframework.data.couchbase.domain.AirlineRepository;
|
| 42 | +import org.springframework.data.couchbase.domain.User; |
| 43 | +import org.springframework.data.couchbase.domain.UserRepository; |
42 | 44 | import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
|
43 | 45 | import org.springframework.data.couchbase.util.Capabilities;
|
44 | 46 | import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
|
|
61 | 63 | * @author Michael Nitschinger
|
62 | 64 | * @author Michael Reiche
|
63 | 65 | */
|
64 |
| -@SpringJUnitConfig(StringN1qlQueryCreatorTests.Config.class) |
| 66 | +@SpringJUnitConfig(StringN1qlQueryCreatorIntegrationTests.Config.class) |
65 | 67 | @IgnoreWhen(clusterTypes = ClusterType.MOCKED)
|
66 |
| -class StringN1qlQueryCreatorTests extends ClusterAwareIntegrationTests { |
| 68 | +class StringN1qlQueryCreatorIntegrationTests extends ClusterAwareIntegrationTests { |
67 | 69 |
|
68 | 70 | MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> context;
|
69 | 71 | CouchbaseConverter converter;
|
@@ -114,6 +116,68 @@ queryMethod, converter, config().bucketname(), new SpelExpressionParser(),
|
114 | 116 | }
|
115 | 117 | }
|
116 | 118 |
|
| 119 | + |
| 120 | + @Test |
| 121 | + void createsQueryCorrectly() throws Exception { |
| 122 | + String input = "getByFirstnameAndLastname"; |
| 123 | + Method method = UserRepository.class.getMethod(input, String.class, String.class); |
| 124 | + |
| 125 | + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, |
| 126 | + new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(), |
| 127 | + converter.getMappingContext()); |
| 128 | + |
| 129 | + StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver", "Twist"), |
| 130 | + queryMethod, converter, config().bucketname(), new SpelExpressionParser(), QueryMethodEvaluationContextProvider.DEFAULT, namedQueries); |
| 131 | + |
| 132 | + Query query = creator.createQuery(); |
| 133 | + assertEquals( |
| 134 | + "SELECT `_class`, META(`" + bucketName() |
| 135 | + + "`).`cas` AS __cas, `createdBy`, `createdDate`, `lastModifiedBy`, `lastModifiedDate`, META(`" |
| 136 | + + bucketName() + "`).`id` AS __id, `firstname`, `lastname`, `subtype` FROM `" + bucketName() |
| 137 | + + "` where `_class` = \"abstractuser\" and firstname = $1 and lastname = $2", |
| 138 | + query.toN1qlSelectString(couchbaseTemplate.reactive(), config().bucketname(), User.class, false)); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void createsQueryCorrectly2() throws Exception { |
| 143 | + String input = "getByFirstnameOrLastname"; |
| 144 | + Method method = UserRepository.class.getMethod(input, String.class, String.class); |
| 145 | + |
| 146 | + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, |
| 147 | + new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(), |
| 148 | + converter.getMappingContext()); |
| 149 | + |
| 150 | + StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver", "Twist"), |
| 151 | + queryMethod, converter, config().bucketname(), new SpelExpressionParser(), QueryMethodEvaluationContextProvider.DEFAULT, namedQueries); |
| 152 | + |
| 153 | + Query query = creator.createQuery(); |
| 154 | + assertEquals( |
| 155 | + "SELECT `_class`, META(`" + bucketName() |
| 156 | + + "`).`cas` AS __cas, `createdBy`, `createdDate`, `lastModifiedBy`, `lastModifiedDate`, META(`" |
| 157 | + + bucketName() + "`).`id` AS __id, `firstname`, `lastname`, `subtype` FROM `" + bucketName() |
| 158 | + + "` where `_class` = \"abstractuser\" and (firstname = $first or lastname = $last)", |
| 159 | + query.toN1qlSelectString(couchbaseTemplate.reactive(), config().bucketname(), User.class, false)); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void spelTests() throws Exception { |
| 164 | + String input = "spelTests"; |
| 165 | + Method method = UserRepository.class.getMethod(input); |
| 166 | + CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, |
| 167 | + new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(), |
| 168 | + converter.getMappingContext()); |
| 169 | + |
| 170 | + StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method)), queryMethod, |
| 171 | + converter, config().bucketname(), new SpelExpressionParser(), QueryMethodEvaluationContextProvider.DEFAULT, namedQueries); |
| 172 | + |
| 173 | + Query query = creator.createQuery(); |
| 174 | + |
| 175 | + String s = query.toN1qlSelectString(couchbaseTemplate.reactive(), "myCollection", User.class, |
| 176 | + false ); |
| 177 | + System.out.println("query: " + s); |
| 178 | + |
| 179 | + } |
| 180 | + |
117 | 181 | private ParameterAccessor getAccessor(Parameters<?, ?> params, Object... values) {
|
118 | 182 | return new ParametersParameterAccessor(params, values);
|
119 | 183 | }
|
|
0 commit comments