Copilot commented on code in PR #16140:
URL: https://github.com/apache/grails-core/pull/16140#discussion_r3769330345


##########
grails-datamapping-core/src/test/groovy/grails/gorm/CriteriaBuilderSpec.groovy:
##########
@@ -0,0 +1,994 @@
+/*
+ *  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 grails.gorm
+
+import org.grails.datastore.mapping.core.Session
+import org.grails.datastore.mapping.model.MappingContext
+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.AssociationQuery
+import org.grails.datastore.mapping.query.Query
+import org.grails.datastore.mapping.query.QueryCreator
+import org.grails.datastore.mapping.query.api.BuildableCriteria
+import org.grails.datastore.mapping.query.api.QueryableCriteria
+import spock.lang.Specification
+import spock.lang.Unroll
+
+/**
+ * Exercises {@link CriteriaBuilder}, and through it its abstract superclass
+ * {@link org.grails.datastore.gorm.query.criteria.AbstractCriteriaBuilder}, 
which cannot be
+ * instantiated directly. Collaborators 
(MappingContext/PersistentEntity/QueryCreator/Query) are
+ * mocked since this class's own responsibility is translating DSL calls into 
Query.Criterion
+ * objects and delegating to a Query, not persistence itself.
+ */
+class CriteriaBuilderSpec extends Specification {
+
+    PersistentProperty idProperty = Stub(PersistentProperty) {
+        getName() >> 'id'
+    }
+    PersistentProperty nameProperty = Stub(PersistentProperty) {
+        getName() >> 'name'
+    }
+    PersistentEntity persistentEntity = Stub(PersistentEntity) {
+        getIdentity() >> idProperty
+        getPropertyByName(_) >> nameProperty
+    }
+    MappingContext mappingContext = Stub(MappingContext) {
+        getPersistentEntity(CriteriaBuilderTestPerson.name) >> persistentEntity
+    }
+    Query query = Mock(Query)
+    QueryCreator queryCreator = Stub(QueryCreator) {
+        createQuery(CriteriaBuilderTestPerson) >> query
+        isSchemaless() >> false
+    }
+
+    CriteriaBuilder<CriteriaBuilderTestPerson> newBuilder() {
+        def criteria = new 
CriteriaBuilder<CriteriaBuilderTestPerson>(CriteriaBuilderTestPerson, 
queryCreator, mappingContext)
+        criteria.@query = query
+        criteria
+    }

Review Comment:
   The spec injects a mocked `query` by writing to the builder's internal field 
(`criteria.@query = query`). This couples the test to implementation details 
and bypasses public APIs. Prefer using the existing public constructor that 
accepts an existing Query (`new CriteriaBuilder(targetClass, session, query)`) 
or initializing via normal execution paths so the test remains resilient to 
internal field changes.



##########
grails-datamapping-rx/src/test/groovy/grails/gorm/rx/CriteriaBuilderSpec.groovy:
##########
@@ -0,0 +1,266 @@
+/*
+ *  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 grails.gorm.rx
+
+import org.grails.datastore.mapping.model.MappingContext
+import org.grails.datastore.mapping.model.PersistentEntity
+import org.grails.datastore.mapping.model.PersistentProperty
+import org.grails.datastore.mapping.query.Query
+import org.grails.datastore.mapping.query.QueryCreator
+import org.grails.datastore.rx.query.RxQuery
+import rx.Observable
+import spock.lang.Specification
+
+/**
+ * Exercises {@link CriteriaBuilder}'s own reactive terminal operations. The 
shared query-DSL
+ * methods it inherits from AbstractCriteriaBuilder (eq, gt, and/or/not, 
projections, etc.) are
+ * already fully covered by grails.gorm.CriteriaBuilderSpec in 
grails-datamapping-core, against
+ * the same base class -- coverage there applies regardless of which subclass 
exercises it, so
+ * this spec only needs to cover the methods declared directly on this class.
+ */
+class CriteriaBuilderSpec extends Specification {
+
+    PersistentProperty idProperty = Stub(PersistentProperty) {
+        getName() >> 'id'
+    }
+    PersistentProperty nameProperty = Stub(PersistentProperty) {
+        getName() >> 'name'
+    }
+    PersistentEntity persistentEntity = Stub(PersistentEntity) {
+        getIdentity() >> idProperty
+        getPropertyByName(_) >> nameProperty
+    }
+    MappingContext mappingContext = Stub(MappingContext) {
+        getPersistentEntity(CriteriaBuilderTestPerson.name) >> persistentEntity
+    }
+    Query query = Mock(Query, additionalInterfaces: [RxQuery])
+    QueryCreator queryCreator = Stub(QueryCreator) {
+        createQuery(CriteriaBuilderTestPerson) >> query
+        isSchemaless() >> false
+    }
+
+    def setup() {
+        // Field initializers run top-to-bottom, so this mutual reference has 
to be wired up
+        // after both fields exist rather than inside either Stub() block.
+        persistentEntity.getMappingContext() >> mappingContext
+        query.getEntity() >> persistentEntity
+    }
+
+    CriteriaBuilder<CriteriaBuilderTestPerson> newBuilder() {
+        def criteria = new 
CriteriaBuilder<CriteriaBuilderTestPerson>(CriteriaBuilderTestPerson, 
queryCreator, mappingContext)
+        criteria.@query = query
+        criteria
+    }

Review Comment:
   The spec sets the builder's internal `query` field directly 
(`criteria.@query = query`). This makes the test brittle to internal refactors 
and bypasses the public construction/init path. Since this builder already 
calls `ensureQueryIsInitialized()` in its public methods (using the provided 
`queryCreator` stub), the direct field write can be removed and the tests can 
rely on normal initialization.



-- 
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]

Reply via email to