-
-
Notifications
You must be signed in to change notification settings - Fork 974
8.x Register CriteriaTypeCheckingExtension and cover createCriteria().<terminal>{} closures #15720
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
Open
codeconsole
wants to merge
2
commits into
apache:8.0.x
Choose a base branch
from
codeconsole:criteria-typecheck-chained-8
base: 8.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...in/groovy/org/apache/grails/data/testing/tck/support/StaticCompiledCriteriaQueries.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * 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.apache.grails.data.testing.tck.support | ||
|
|
||
| import grails.compiler.GrailsCompileStatic | ||
|
|
||
| import org.apache.grails.data.testing.tck.domains.TestEntity | ||
|
|
||
| /** | ||
| * Criteria queries compiled with {@code @GrailsCompileStatic} so that the bytecode produced by | ||
| * the {@code CriteriaTypeCheckingExtension} is executed by the TCK against every GORM | ||
| * implementation. Calls declared on the {@code Criteria} API (eq, like, or, order) compile to | ||
| * static delegate dispatch, while calls that are not on the API (projections, min, countDistinct, | ||
| * property, maxResults and the {@code count} terminal chained on {@code createCriteria()}) only | ||
| * compile because the extension falls back to dynamic dispatch — executing them here proves that | ||
| * dispatch resolves against the criteria builder of the implementation under test. | ||
| */ | ||
| @GrailsCompileStatic | ||
| class StaticCompiledCriteriaQueries { | ||
|
|
||
| static List listByNameLike(String pattern) { | ||
| (List) TestEntity.createCriteria().list { | ||
| like('name', pattern) | ||
| } | ||
| } | ||
|
|
||
| static List listByNameLikeLimited(String pattern, int limit) { | ||
| (List) TestEntity.createCriteria().list { | ||
| like('name', pattern) | ||
| maxResults(limit) | ||
| } | ||
| } | ||
|
|
||
| static List listPaginatedOrderedByAge(int max, int offset) { | ||
| (List) TestEntity.createCriteria().list(max: max, offset: offset) { | ||
| order('age') | ||
| } | ||
| } | ||
|
|
||
| static Number countByNameLike(String pattern) { | ||
| (Number) TestEntity.createCriteria().count { | ||
| like('name', pattern) | ||
| } | ||
| } | ||
|
|
||
| static TestEntity getByName(String name) { | ||
| (TestEntity) TestEntity.createCriteria().get { | ||
| eq('name', name) | ||
| } | ||
| } | ||
|
|
||
| static Number minAge() { | ||
| (Number) TestEntity.createCriteria().get { | ||
| projections { | ||
| min('age') | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static Number countDistinctAges() { | ||
| (Number) TestEntity.createCriteria().get { | ||
| projections { | ||
| countDistinct('age') | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static List namesMatching(String pattern) { | ||
| (List) TestEntity.withCriteria { | ||
| like('name', pattern) | ||
| projections { | ||
| property('name') | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static List listByNameLikeOrAge(String pattern, int age) { | ||
| (List) TestEntity.withCriteria { | ||
| or { | ||
| like('name', pattern) | ||
| eq('age', age) | ||
| } | ||
| order('age', 'asc') | ||
| } | ||
| } | ||
| } |
153 changes: 153 additions & 0 deletions
153
...rc/main/groovy/org/apache/grails/data/testing/tck/tests/StaticCompiledCriteriaSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /* | ||
| * 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.apache.grails.data.testing.tck.tests | ||
|
|
||
| import org.apache.grails.data.testing.tck.base.GrailsDataTckSpec | ||
| import org.apache.grails.data.testing.tck.domains.ChildEntity | ||
| import org.apache.grails.data.testing.tck.domains.TestEntity | ||
| import org.apache.grails.data.testing.tck.support.StaticCompiledCriteriaQueries | ||
|
|
||
| /** | ||
| * Executes the criteria queries that {@link StaticCompiledCriteriaQueries} compiled with | ||
| * {@code @GrailsCompileStatic} against the GORM implementation under test, verifying that the | ||
| * dispatch produced by the {@code CriteriaTypeCheckingExtension} behaves identically to the | ||
| * dynamically compiled queries covered by {@code CriteriaBuilderSpec}. | ||
| */ | ||
| class StaticCompiledCriteriaSpec extends GrailsDataTckSpec { | ||
|
|
||
| @Override | ||
| void setupSpec() { | ||
| manager.registerDomainClasses(TestEntity, ChildEntity) | ||
| } | ||
|
|
||
| private static void seedPeople() { | ||
| def age = 40 | ||
| ['Bob', 'Fred', 'Barney', 'Frank'].each { | ||
| new TestEntity(name: it, age: age++, child: new ChildEntity(name: "$it Child")).save() | ||
| } | ||
| } | ||
|
|
||
| void 'Test statically compiled list with a criteria closure on a chained terminal'() { | ||
| given: | ||
| seedPeople() | ||
|
|
||
| when: | ||
| def results = StaticCompiledCriteriaQueries.listByNameLike('B%') | ||
|
|
||
| then: | ||
| 2 == results.size() | ||
| results.every { it instanceof TestEntity } | ||
| } | ||
|
|
||
| void 'Test statically compiled list with a dynamically dispatched maxResults call'() { | ||
| given: | ||
| seedPeople() | ||
|
|
||
| when: | ||
| def results = StaticCompiledCriteriaQueries.listByNameLikeLimited('B%', 1) | ||
|
|
||
| then: | ||
| 1 == results.size() | ||
| } | ||
|
|
||
| void 'Test statically compiled paginated list with named arguments and a chained terminal'() { | ||
| given: | ||
| seedPeople() | ||
|
|
||
| when: | ||
| def results = StaticCompiledCriteriaQueries.listPaginatedOrderedByAge(2, 1) | ||
|
|
||
| then: | ||
| 2 == results.size() | ||
| 'Fred' == results[0].name | ||
| 'Barney' == results[1].name | ||
| } | ||
|
|
||
| void 'Test statically compiled count terminal chained on createCriteria()'() { | ||
| given: | ||
| seedPeople() | ||
|
|
||
| when: | ||
| def result = StaticCompiledCriteriaQueries.countByNameLike('B%') | ||
|
|
||
| then: | ||
| 2 == result | ||
| } | ||
|
|
||
| void 'Test statically compiled get with an equals criterion'() { | ||
| given: | ||
| seedPeople() | ||
|
|
||
| when: | ||
| TestEntity result = StaticCompiledCriteriaQueries.getByName('Bob') | ||
|
|
||
| then: | ||
| result != null | ||
| 'Bob' == result.name | ||
| 40 == result.age | ||
| } | ||
|
|
||
| void 'Test statically compiled min projection'() { | ||
| given: | ||
| seedPeople() | ||
|
|
||
| when: | ||
| def result = StaticCompiledCriteriaQueries.minAge() | ||
|
|
||
| then: | ||
| 40 == result | ||
| } | ||
|
|
||
| void 'Test statically compiled count distinct projection'() { | ||
| given: | ||
| seedPeople() | ||
| new TestEntity(name: 'Chuck', age: 43, child: new ChildEntity(name: 'Chuckie')).save() | ||
|
|
||
| when: | ||
| def result = StaticCompiledCriteriaQueries.countDistinctAges() | ||
|
|
||
| then: | ||
| 4 == result | ||
| } | ||
|
|
||
| void 'Test statically compiled property projection inside withCriteria'() { | ||
| given: | ||
| seedPeople() | ||
|
|
||
| when: | ||
| def results = StaticCompiledCriteriaQueries.namesMatching('B%') | ||
|
|
||
| then: | ||
| ['Barney', 'Bob'] == results.sort() | ||
| } | ||
|
|
||
| void 'Test statically compiled disjunction with ordering'() { | ||
| given: | ||
| seedPeople() | ||
|
|
||
| when: | ||
| def results = StaticCompiledCriteriaQueries.listByNameLikeOrAge('B%', 41) | ||
|
|
||
| then: | ||
| 3 == results.size() | ||
| 'Bob' == results[0].name | ||
| 'Fred' == results[1].name | ||
| 'Barney' == results[2].name | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To change the default, we need to expand the test coverage so that there are tests in the TCK - this way we know we haven't broken it in hibernate 7.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in b10f36f. The TCK now executes criteria queries that were compiled with
@GrailsCompileStatic, covering both dispatch paths the extension produces: statically-resolvedCriteriaAPI calls and the dynamic fallback (projections,min/countDistinct/property,maxResults, and the chainedcount { }terminal, which is not declared onBuildableCriteria). Verified green against the in-memory, Hibernate 5, Hibernate 7, and MongoDB implementations locally.