是否有"最佳实践"或事实标准,在单元/功能测试中应该测试多少GORM功能?
我的看法是,应该将大多数域测试作为功能测试,以便您获得完整的grails环境.但是你测试什么?插入,更新,删除?您是否测试了约束,即使它们可能已经通过grails发布进行了更彻底的测试?
或者你只是假设GORM做它应该做的事情并转移到应用程序的其他部分?
在我的grails应用程序中,我有:
keywords = Keyword
.findAll("from Keyword where locale = '$locale' order by rand() ", [max:20])
Run Code Online (Sandbox Code Playgroud)
假设表中有数千行符合上述条件.但似乎从表返回的行不是随机的,但是按顺序将行存储在Db中,尽管在返回的20行的上下文中它们是随机的.为了我的应用程序工作,我希望此查询从表中返回完全随机的行,如行ID 203,行ID 3789,行ID 9087,行ID 789,依此类推.怎么可能?
class Contact {
String name
String number
}
class Message {
String text
String number
Contact contactInfo //If any
}
Run Code Online (Sandbox Code Playgroud)
我需要加入Message.number = Contact.number.有关使用非主键列在Grails/GORM中创建关联的任何想法?
我正在使用Grails,并且我有一个具有多个hasMany属性的域模型到同一个域类,如下所示:
static hasMany = [ posts : Post, likes : Post, dislikes : Post ]
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我在帖子列表中添加内容时,它也会以某种方式将其变成喜欢和不喜欢的列表.至少,当我遍历每个列表时,它就是这样的样子.
我认为问题是我在Post域中也有以下关系:
static belongsTo = [ contributer : Contributer ]
Run Code Online (Sandbox Code Playgroud)
配置这些关系以使我的模型工作的最佳方法是什么?有什么建议?
@Wayne,
我也试过使用你的测试,它成功通过了.所以,我唯一能想到的是我的PostController中的save方法有问题.我已粘贴下面的相关代码(我正在使用Spring Security Core插件,而我的Contributer类扩展了使用该插件创建的User类):
@Secured(['IS_AUTHENTICATED_FULLY'])
def save = {
def props = [title:params.title, post:params.post, category:Category.get(params.category.id)]
def user = Contributer.get(springSecurityService.principal.id)
def postInstance = new Post(props)
postInstance.contributer = user
if (postInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'post.label', default: 'Post'), postInstance.id])}"
redirect(action: "show", id: postInstance.id)
}
else {
render(view: "create", model: [postInstance: postInstance])
} …Run Code Online (Sandbox Code Playgroud) 我正在写一个标准来进行查询,但我无法弄清楚如何否定inList标准......有没有办法做类似的事情:
def result = c {
not inList('id', ids)
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尝试使用grails mongo插件在mongodb中持久化spring-security-acl域对象.执行以下代码行时
aclUtilService.addPermission Phone.class, phoneInstance.id, new PrincipalSid(username), BasePermission.ADMINISTRATION
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
此实现的GORM目前不支持像[executeQuery]这样的基于字符串的查询.使用标准代替.. Stacktrace如下:消息:此实现的GORM目前不支持像[executeQuery]这样的基于字符串的查询.改用标准.
任何艰难的?
**Grails Configuration Details:**
app.grails.version=2.0.3
app.name=eateri
app.servlet.version=2.5
app.version=0.1
plugins.mongodb=1.0.0.RC5
plugins.spring-security-acl=1.1
plugins.spring-security-core=1.2.7.2
Run Code Online (Sandbox Code Playgroud) 我正在尝试对一个命名查询方法进行单元测试而且它没有工作,因为我使用grails.orm.HibernateCriteriaBuilder.createAlias这个方法似乎没有被Grails找到:"没有方法的签名:grails. gorm.CriteriaBuilder.createAlias()"
我想问题是,在进行单元测试和模拟类时,它试图找到围绕" grails.gorm.CriteriaBuilder "类而不是grails.orm.HibernateCriteriaBuilder类的方法,为什么?有什么想法可以解决吗?
class Book{
static namedQueries = {
testMethod()
{
createAlias('name', 'james')
}
}
}
@Mock([Book])
class BookTests{
@Test
void myTest() {
Book.testMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
错误:没有方法签名:grails.gorm.CriteriaBuilder.createAlias()
我有以下域对象:
class User {
String name
Transaction transaction
static constraints = {
transaction nullable: true
}
}
class Transaction {
boolean successful
User user
static belongsTo = User
}
Run Code Online (Sandbox Code Playgroud)
我想选择所有users没有成功交易的东西.这意味着我希望没有任何transaction(transaction == null)的用户和具有成功值false(transaction.successful == false)的事务的用户.我想使用Criteria API执行此操作(因为这可以与基于用户输入的其他条件结合使用).
我试过这个:
def c = User.createCriteria()
def results = c {
or {
isNull 'transaction'
transaction {
eq 'successful', false
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这只给了我有交易的用户(成功的价值false).但我没有得到交易所在的用户null
以下代码显示了我如何创建一些示例数据:
def createUserAndTransaction(String name, Boolean successful = null) {
User u …Run Code Online (Sandbox Code Playgroud) Grails上有一些ENUM的例子(这里也是SO),但我无法得到理想的结果.
解决方案包括1)将ENUM放在src/groovy 域类下的单独 类中
class Offer {
PaymentMethod acceptedPaymentMethod
..
}
Run Code Online (Sandbox Code Playgroud)
src/groovy PaymentMethod
public enum PaymentMethod {
BYBANKTRANSFERINADVANCE('BANKADVANCE'),
BYINVOICE('ByInvoice'),
CASH('Cash'),
CHECKINADVANCE('CheckInAdvance'),
PAYPAL('PayPal'),
String id
PaymentMethod(String id) {
this.id = id
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,在发出错误的域类中根本不识别Enum类.看起来这个曾经在版本2之前用于Grails.
我在这里错过了什么吗?如何在Grails的域中使用外部ENUM类?
2)将ENUM放在域类中.
在这种情况下,grails在编译时不会抱怨,但是脚手架不包含ENUM值的任何信息(就像在scaffolding过程中不包含属性acceptedPaymentMethod)示例:
class Offer {
PaymentMethod acceptedPaymentMethod
..
enum PaymentMethod {
BYBANKTRANSFERINADVANCE('BANKADVANCE'),
BYINVOICE('ByInvoice'),
CASH('Cash'),
CHECKINADVANCE('CheckInAdvance'),
PAYPAL('PayPal'),
String id
PaymentMethod(String id) {
this.id = id
}
}
}
Run Code Online (Sandbox Code Playgroud)
检查数据库表的结构,该字段不是ENUM而是简单的VarChar:
| accepted_payment_method | varchar(255) | YES | | NULL | |
Run Code Online (Sandbox Code Playgroud)
是否支持Grails Gorm上的ENUM?
我开发了一个小的Heroku + Grails + Postreg应用程序.部署后20分钟它会工作正常,之后我总是得到:
This connection has been closed.. Stacktrace follows: Heroku/myapp
- org.postgresql.util.PSQLException: This connection has been closed. Heroku/myapp
- at org.postgresql.jdbc2.AbstractJdbc2Connection.checkClosed(AbstractJdbc2Connection.java:837) Heroku/myapp
- at org.postgresql.jdbc2.AbstractJdbc2Connection.getAutoCommit(AbstractJdbc2Connection.java:798) Heroku/myapp
- at org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2.doCall(GormStaticApi.groovy:102) Heroku/myapp
- at com.myapp.WorkspaceController.list(WorkspaceController.groovy:18) Heroku/myapp
- at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198) Heroku/myapp
- at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63) Heroku/myapp
- at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53) Heroku/myapp
- at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:53) Heroku/myapp
- at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:62) Heroku/myapp
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) Heroku/myapp
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) Heroku/myapp
- at java.lang.Thread.run(Thread.java:745) Heroku/myapp
- 2015-08-07 15:11:10,685 [http-nio-20850-exec-5] ERROR spi.SqlExceptionHelper - This connection has been …Run Code Online (Sandbox Code Playgroud) grails ×10
grails-orm ×10
criteria ×1
enums ×1
has-many ×1
heroku ×1
mongodb ×1
postgresql ×1
security ×1
tdd ×1
testing ×1
unit-testing ×1