This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
GMongoSpec.groovy
60 lines (46 loc) · 1.76 KB
/
GMongoSpec.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.grails.datastore.gorm.mongo
import grails.gorm.tests.Person
import org.grails.datastore.gorm.mongo.bean.factory.GMongoFactoryBean
import org.springframework.context.support.GenericApplicationContext
import org.grails.datastore.mapping.mongo.MongoDatastore
import org.grails.datastore.mapping.mongo.config.MongoMappingContext
import spock.lang.Specification
import com.mongodb.DB
class GMongoSpec extends Specification {
void setup() {
ExpandoMetaClass.enableGlobally()
}
void cleanup() {
ExpandoMetaClass.disableGlobally()
}
def "Test configure and use gmongo"() {
given:
def gfb = new GMongoFactoryBean()
gfb.afterPropertiesSet()
when:
def gmongo = gfb.getObject()
DB db = gmongo.getDB("test")
db.dropDatabase()
then:
gmongo != null
when:
def ctx = new GenericApplicationContext()
ctx.refresh()
def datastore = new MongoDatastore(new MongoMappingContext("test"), gmongo.mongo, ctx)
def session = datastore.connect()
def entity = datastore.mappingContext.addPersistentEntity(Person)
new MongoGormEnhancer(datastore).enhance entity
then:
Person.count() == 0
when:
def p = new Person(firstName:"Fred", lastName:"Flintstone").save(flush:true)
then:
Person.count() == 1
Person.withSession {
Person.collection.count() == 1
Person.collection.findOne(firstName:"Fred").lastName == "Flintstone"
db[Person.collectionName].count() == 1
db[Person.collectionName].findOne(firstName:"Fred").lastName == "Flintstone"
}
}
}