-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
B/c the internal MongoDB error message has changed for bad hint
- Loading branch information
1 parent
e926000
commit 0761775
Showing
1 changed file
with
42 additions
and
35 deletions.
There are no files selected for viewing
77 changes: 42 additions & 35 deletions
77
...gorm-mongodb/src/test/groovy/org/grails/datastore/gorm/mongo/HintQueryArgumentSpec.groovy
This file contains 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 |
---|---|---|
@@ -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<Person> 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<Person> 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') | ||
} | ||
} |