From 0761775db0e8f630bbd9b5a1eca77e272f14de4b Mon Sep 17 00:00:00 2001 From: Puneet Behl Date: Wed, 4 Oct 2023 18:00:34 +0530 Subject: [PATCH] Update HintQueryArgumentSpec.groovy B/c the internal MongoDB error message has changed for bad hint --- .../gorm/mongo/HintQueryArgumentSpec.groovy | 77 ++++++++++--------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/HintQueryArgumentSpec.groovy b/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/HintQueryArgumentSpec.groovy index 1e6c768f..8310d4f2 100644 --- a/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/HintQueryArgumentSpec.groovy +++ b/grails-datastore-gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/HintQueryArgumentSpec.groovy @@ -1,49 +1,56 @@ package org.grails.datastore.gorm.mongo + import com.mongodb.MongoException +import com.mongodb.MongoQueryException import grails.gorm.CriteriaBuilder import grails.gorm.DetachedCriteria import grails.gorm.tests.GormDatastoreSpec import grails.gorm.tests.Person +import org.bson.BsonString class HintQueryArgumentSpec extends GormDatastoreSpec { void "Test that hints work on criteria queries"() { - when:"A criteria query is created with a hint" - CriteriaBuilder c = Person.createCriteria() - c.list { - eq 'firstName', 'Bob' - arguments hint:["firstName":1] - } - - then:"The query contains the hint" - c.query.@queryArguments == [hint:['firstName':1]] - - when:"A dynamic finder uses a hint" - def results = Person.findAllByFirstName("Bob", [hint:"firstName"]) - for(e in results) {} // just to trigger the query - - then:"The hint is used" - MongoException exception = thrown() - exception.message.contains('bad hint') - - when:"A dynamic finder uses a hint" - results = Person.findAllByFirstName("Bob", [hint:["firstName":1]]) - - then:"The hint is used" - results.size() == 0 - } + when: "A criteria query is created with a hint" + CriteriaBuilder c = Person.createCriteria() + c.list { + eq 'firstName', 'Bob' + arguments hint: ["firstName": 1] + } + + then: "The query contains the hint" + c.query.@queryArguments == [hint: ['firstName': 1]] + + when: "A dynamic finder uses a hint" + def results = Person.findAllByFirstName("Bob", [hint: "firstName"]) + // just to trigger the query + for (e in results) { } + + then: "The hint is used" + MongoException exception = thrown() + exception instanceof MongoQueryException + ((MongoQueryException) exception).message.contains('BadValue') + + when: "A dynamic finder uses a hint" + results = Person.findAllByFirstName("Bob", [hint: ["firstName": 1]]) + + then: "The hint is used" + results.size() == 0 + } void "Test that hints work on detached criteria queries"() { - when:"A criteria query is created with a hint" - DetachedCriteria detachedCriteria = new DetachedCriteria<>(Person) - detachedCriteria = detachedCriteria.build { - eq 'firstName', 'Bob' - } - - def results = detachedCriteria.list(hint:["firstName":"blah"]) - for(e in results) {} // just to trigger the query - then:"The hint is used" - MongoException exception = thrown() - exception.message.contains('bad hint') + when: "A criteria query is created with a hint" + DetachedCriteria detachedCriteria = new DetachedCriteria<>(Person) + detachedCriteria = detachedCriteria.build { + eq 'firstName', 'Bob' + } + + def results = detachedCriteria.list(hint: ["firstName": "blah"]) + // just to trigger the query + for (e in results) { } + then: "The hint is used" + MongoException exception = thrown() + exception instanceof MongoQueryException + exception.message.contains('BadValue') } }