jdaugherty commented on code in PR #16143: URL: https://github.com/apache/grails-core/pull/16143#discussion_r3812775298
########## grails-datamapping-rx/src/test/groovy/org/grails/gorm/rx/api/DetachedCriteriaQuerySpec.groovy: ########## @@ -0,0 +1,295 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gorm.rx.api + +import grails.gorm.rx.DetachedCriteria +import jakarta.persistence.criteria.JoinType +import org.grails.datastore.mapping.core.connections.ConnectionSource +import org.grails.datastore.mapping.model.MappingContext +import org.grails.datastore.mapping.model.PersistentEntity +import org.grails.datastore.mapping.query.Query +import org.grails.datastore.mapping.query.api.QueryArgumentsAware +import org.grails.datastore.rx.internal.RxDatastoreClientImplementor +import org.grails.datastore.rx.query.RxQuery +import org.springframework.core.convert.ConversionService +import rx.Observable +import rx.Subscriber +import spock.lang.Specification + +/** + * Exercises the reactive query-execution methods on {@link DetachedCriteria} (rx) - the ones + * that go through {@code prepareQuery} and {@link RxGormEnhancer#findStaticApi}. This requires + * a fake entity registered with {@link RxGormEnhancer}, since those static lookups can't be + * intercepted from outside the {@code CompileStatic} call site with metaclass-based mocking. + * Package-local so the test can call the {@code protected static} + * {@code RxGormEnhancer.registerEntityWithConnectionSource} directly rather than driving the + * full {@code registerEntity} multi-tenancy/connection-source resolution machinery. + */ +class DetachedCriteriaQuerySpec extends Specification { + + Query mockQuery + RxDatastoreClientImplementor datastoreClient + + void setup() { + def queryEntity = Mock(PersistentEntity) { + getMappingContext() >> Mock(MappingContext) { + getConversionService() >> Mock(ConversionService) + } + } + mockQuery = Mock(Query, additionalInterfaces: [RxQuery, QueryArgumentsAware]) { + getEntity() >> queryEntity + } + datastoreClient = Mock(RxDatastoreClientImplementor) { + createQuery(QueryTestEntity, _ as Map) >> mockQuery + } + def persistentEntity = Mock(PersistentEntity) { + getJavaClass() >> QueryTestEntity + getName() >> QueryTestEntity.name + } + def staticApi = Mock(RxGormStaticApi) { + getDatastoreClient() >> datastoreClient + } + datastoreClient.createStaticApi(persistentEntity, ConnectionSource.DEFAULT) >> staticApi + datastoreClient.createInstanceApi(persistentEntity, ConnectionSource.DEFAULT) >> Mock(RxGormInstanceApi) + datastoreClient.createValidationApi(persistentEntity, ConnectionSource.DEFAULT) >> Mock(RxGormValidationApi) + RxGormEnhancer.registerEntityWithConnectionSource(persistentEntity, ConnectionSource.DEFAULT, ConnectionSource.DEFAULT, datastoreClient) Review Comment: Almost all of this spec duplicates coverage that `grails.gorm.rx.DetachedCriteriaSpec` already provides on `8.1.x` (find/findAll/get/toList/list/getCount/updateAll/deleteAll/toQuery/toObservable/subscribe, the prepareQuery max/offset and fetch-strategy paths, and additional-criteria merging). The only new behavior covered here is the custom `JoinType` passthrough. That existing spec also shows this setup works through the public `RxGormEnhancer.registerEntity(entity, datastoreClient)` with a stubbed `ConnectionSources`, so the package-local placement to reach the protected `registerEntityWithConnectionSource` is not needed. Suggest moving the "prepareQuery passes a custom JoinType through to the query" feature method into the existing `DetachedCriteriaSpec` and dropping this file. This is the reason I think this should have been merged into the other PR ########## grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/query/criteria/AbstractDetachedCriteriaSpec.groovy: ########## @@ -0,0 +1,1167 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.gorm.query.criteria + +import grails.gorm.DetachedCriteria + +import jakarta.persistence.FetchType +import jakarta.persistence.criteria.JoinType + +import org.grails.datastore.gorm.finders.FinderMethod +import org.grails.datastore.mapping.model.PersistentEntity +import org.grails.datastore.mapping.model.PersistentProperty +import org.grails.datastore.mapping.model.types.Association +import org.grails.datastore.mapping.query.Query +import org.grails.datastore.mapping.query.api.QueryableCriteria +import spock.lang.Specification + +/** + * Exercises {@link AbstractDetachedCriteria} through its concrete subclass {@link DetachedCriteria}. + */ +class AbstractDetachedCriteriaSpec extends Specification { + + void "eq adds an Equals criterion"() { + given: + def criteria = new DetachedCriteria(TestEntity) Review Comment: `TestEntity` here resolves to the class declared at the bottom of `DetachedCriteriaCloneSpec.groovy`. Depending on a fixture defined inside another spec's file is fragile - refactoring that spec would silently break this one. Suggest either moving `TestEntity` into its own file in this package or declaring a dedicated fixture class in this file. ########## grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/criteria/AbstractDetachedCriteria.groovy: ########## @@ -216,7 +216,7 @@ abstract class AbstractDetachedCriteria<T> implements Criteria, Cloneable { } } if (junctions) { - junctions[-1].add(criterion) + junctions.getLast().add(criterion) Review Comment: -1 is an accepted way to get the last when the array is defined, why change it? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
