我的最终目标是拥有以下领域的领域类:
equals
和hashCode
方法生成的@EqualsAndHashCode
考虑hasMany
属性unique
域类中属性的约束将hasMany
属性考虑在内- 域类的实例被认为是唯一的,只要它的一个属性使它不同于已经存在的实例。
感谢@James Kleeh 和@dmahapatro,我想我已经很接近了,但是第 2 点和第 3 点给我带来了麻烦。
我第一次尝试测试我的需求是testFooWithMockedBar
我FooTests.groovy
文件中的单元测试。我正在尝试Bar.get()
在该测试中使用,但它不起作用。我认为调用也不起作用mockForConstraintsTests
。
我很确定我在下一次测试中纠正了这个问题testFooWithoutMockedBar
,但是我不确定测试是否在做我认为它正在做的事情,我将在接下来解释。
通过后,testFooWithoutMockedBar
我尝试在开发模式下运行应用程序,看看它是否按预期工作。不幸的是,文件bars
中的行中的属性阻止 Grails在数据库中创建表。这是我得到的错误:prop1(unique: ['prop2', 'prop3', 'bars'])
Foo.groovy
foo
| Error 2013-08-20 16:17:52,249 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - Unsuccessful: create table foo (id bigint not null auto_increment, version bigint not null, prop1 varchar(255) not null, prop2 varchar(255) not null, prop3 varchar(255) not null, primary key (id), unique (foo_bars_id, prop3, prop2, prop1)) ENGINE=InnoDB
| Error 2013-08-20 16:17:52,250 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - Key column 'foo_bars_id' doesn't exist in table
| Error 2013-08-20 16:17:52,309 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - Unsuccessful: alter table foo_bar add index FKD76E651A96EEE146 (foo_bars_id), add constraint FKD76E651A96EEE146 foreign key (foo_bars_id) references foo (id)
| Error 2013-08-20 16:17:52,310 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - Can't create table 'foobar.#sql-474_8c' (errno: 150)
我不确定是否有解决此问题的常规方法。我能想到解决它的唯一方法是使用自定义验证器Foo.groovy
:
class Foo {
...
boolean isUnique
static transients = ['isUnique']
static constraints = {
isUnique(
validator: { val, obj ->
def rslt = true
for(foo in Foo.getAll()) {
if (foo == obj) {
rslt = false
break
}
}
return rslt
}
)
bars(nullable: false)
}
有没有更好的方法来做我想做的事?
Foo.groovy
package foobar
@groovy.transform.EqualsAndHashCode
class Foo {
String prop1
String prop2
String prop3
Set<Bar> bars
static hasMany = [bars: Bar]
static constraints = {
prop1(unique: ['prop2', 'prop3', 'bars']) // The 'bars' in this line is preventing Grails from creating the foo table in the database.
bars(nullable: false)
}
}
酒吧.groovy
package foobar
@groovy.transform.EqualsAndHashCode
class Bar {
String prop1
}
FooTests.groovy
package foobar
import grails.test.mixin.*
import org.junit.*
@TestFor(Foo)
@Mock(Bar)
class FooTests {
void testFooWithMockedBar() {
// Create existing instances to validate against
mockForConstraintsTests(Bar, [
new Bar(prop1: "a"),
new Bar(prop1: "b"),
new Bar(prop1: "c"),
new Bar(prop1: "d")
]
)
mockForConstraintsTests(Foo, [
new Foo(prop1: "a", prop2: "b", prop3: "c", bars: [Bar.get(1), Bar.get(2)])
]
)
// Validation should fail if all properties are null
def foo = new Foo()
assert !foo.validate()
assert "nullable" == foo.errors["prop1"]
assert "nullable" == foo.errors["prop2"]
assert "nullable" == foo.errors["prop3"]
assert "nullable" == foo.errors["bars"]
// Test unique constraints
foo = new Foo(prop1: "a", prop2: "b", prop3: "c", bars: [Bar.get(1), Bar.get(2)])
assert !foo.validate()
assert "unique" == foo.errors["prop1"]
// Validation should pass with all unique, not null properties
foo = new Foo(prop1: "a", prop2: "b", prop3: "c", bars: [Bar.get(3), Bar.get(4)])
assert foo.validate()
// Test equals and hashCode
assert foo == new Foo(prop1: "a", prop2: "b", prop3: "c", bars: [Bar.get(3), Bar.get(4)])
}
void testFooWithoutMockedBar() {
// Create existing instances to validate against
def bars1 = [new Bar(prop1: "a"), new Bar(prop1: "b")]
def bars2 = [new Bar(prop1: "c"), new Bar(prop1: "d")]
mockForConstraintsTests(Foo, [
new Foo(prop1: "a", prop2: "b", prop3: "c", bars: bars1)
]
)
// Validation should fail if all properties are null
def foo = new Foo()
assert !foo.validate()
assert "nullable" == foo.errors["prop1"]
assert "nullable" == foo.errors["prop2"]
assert "nullable" == foo.errors["prop3"]
assert "nullable" == foo.errors["bars"]
// Test unique constraints
foo = new Foo(prop1: "a", prop2: "b", prop3: "c", bars: bars1)
assert !foo.validate()
assert "unique" == foo.errors["prop1"]
// Validation should pass
foo = new Foo(prop1: "a", prop2: "b", prop3: "c", bars: bars2)
assert foo.validate()
// Test equals and hashCode
assert foo != new Foo(prop1: "a", prop2: "b", prop3: "c", bars: bars1)
assert foo == new Foo(prop1: "a", prop2: "b", prop3: "c", bars: bars2)
}
}