Open wureka opened 6 years ago
In TestController. createHeaderCase1
if you replace header = new Header(headerNo: 'HEADER_1')
with header = new Header(headerNo: 'HEADER_1').save()
, I expect the problem in that method may go away. It does go away with H2. I am not setup to test with postgres right now. If you can test that and provide feedback, that would be appreciated.
A significant detail that is not mentioned in the original description is the fact that failOnError
is set to true
in application.yml
, which affects the relevant behavior. If that isn't set, the controller action will appear to work because the code isn't checking the return value when calling .save()
.
The project at https://github.com/jeffbrown/issue11077 contains an app which demonstrates the behavior but eliminates a lot of unrelated stuff in the originally linked projects.
The relevant bits:
https://github.com/jeffbrown/issue11077/blob/master/grails-app/domain/issue11077/Header.groovy
package issue11077
class Header {
String headerNo
static hasMany = [items: Item]
}
https://github.com/jeffbrown/issue11077/blob/master/grails-app/domain/issue11077/Item.groovy
package issue11077
class Item {
int itemNo
static belongsTo = [header: Header]
static constraints = {
itemNo unique: 'header'
}
}
package issue11077
class DemoController {
def index() {
Header.withTransaction {
// Saving the Header before adding
// items allows the integration test to pass
Header h = new Header(headerNo: 'SOMETHING')//.save()
h.addToItems itemNo: 1
h.addToItems itemNo: 2
h.save(failOnError: true)
}
render 'Success'
}
}
package issue11077
import geb.spock.GebSpec
import grails.testing.mixin.integration.Integration
@Integration
class DemoControllerSpec extends GebSpec {
void 'test that the index action may be invoked multiple times'() {
when:
go '/demo/index'
then:
$().text() == 'Success'
when:
go '/demo/index'
then:
$().text() == 'Success'
}
}
That integration test will fail. If DemoController
is modified to save the Header
before adding the items, the test will pass.
@jeffbrown I have tried your suggestion as below:
header = new Header(headerNo: 'HEADER_1').save()
when using PostgreSQL V10, the below exception always appears since first trial:
o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: null value in column "date_created" violates not-null constraint
When using H2, it will pass no matter how many trials I did.
By the way, I have set failOnError to true in application.yml:
grails:
profile: web
codegen:
defaultPackage: web338
gorm:
failOnError: true
reactor:
# Whether to translate GORM events into Reactor events
# Disabled by default for performance reasons
events: false
By the way, I have set failOnError to true in application.yml
I noticed that and commented on that in an earlier comment.
Thanks again.
@wureka as @zyro23 mentioned, keep in mind that the null value in column "date_created" violates not-null constraint
error in postgres is because of #10964. I sent a PR to fix it and it will be included in the next GORM version.
Environment Information
Domains
application.groovy is below:
My database config in application.yml:
build.gradle:
My TestController:
Problems:
However, all above tests will pass when using Grails 3.2.8 (I didn't test 3.2.9~3.2.11). I think the behaviors of Grails 3.2,8 should be correct. If I am right, could Grails team fix that bug? That's very important.
Thanks
Example Application
example app with Grails 3.3.8 web338.zip
example app with Grails 3.2.8 web328.zip